//
// sub_8640D8 from 0x8640d8 to 0x8642ec (532 bytes)
//
// 2 xrefs:
// > sub_8644A4 @ 0x864a5a
// > sub_8644A4 @ 0x864c40
//
// This function is hookable on this platform (idp: arm, 32 bit: True, 64 bit: False!
//
// Using BromaIDA 8.3.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit 1c6ec395 at Thu Nov 26 21:29:22 2026 from https://github.com/geode-sdk/bindings
// Generated at Fri, 27 Nov 2026 12:02:45 +0000
//
unsigned int __fastcall sub_8640D8(_DWORD *a1)
{
int v1; // r6
int v2; // r3
unsigned int v3; // r11
int v5; // r9
unsigned int v6; // r1
int v7; // r5
int v8; // r12
int v9; // r7
int v10; // r3
int v11; // r9
bool v12; // zf
int v13; // r2
unsigned int v14; // r4
unsigned int v15; // r0
unsigned __int16 v16; // lr
int v17; // r4
unsigned int v18; // r2
unsigned int v19; // r2
int v20; // r1
unsigned int v22; // r2
int v23; // r3
unsigned int v24; // r1
int v25; // r1
int v26; // r3
unsigned __int16 v27; // [sp+0h] [bp-14h]
int v28; // [sp+4h] [bp-10h]
int v29; // [sp+8h] [bp-Ch]
int v30; // [sp+Ch] [bp-8h]
v2 = 0;
v3 = 0;
v28 = 0;
do
{
v5 = 0;
v30 = v2;
do
{
v6 = v3;
v7 = 0;
v8 = v5;
v29 = v5;
do
{
v9 = *a1;
++v3;
v10 = *(_DWORD *)(*a1 + 16);
if ( (v10 & 4) != 0 )
v11 = 2;
else
v11 = 1;
if ( (v10 & 2) != 0 )
goto LABEL_32;
v12 = v28 == v8;
if ( v28 == v8 )
v12 = v8 == v7;
if ( v12 )
{
LABEL_32:
if ( v6 > 0xFF )
LABEL_58:
png_error(**(_DWORD **)v9, "color-map index out of range");
if ( v11 != 2 )
{
LOBYTE(v16) = v7;
if ( (v10 & 0x10) != 0 )
v1 = 0;
v22 = __clz((*(_DWORD *)(v9 + 16) & 0x21) - 33);
LOBYTE(v27) = v28;
if ( (v10 & 0x10) == 0 )
v1 = 2;
LOBYTE(v15) = v8;
v19 = v22 >> 5;
if ( (v10 & 0x10) != 0 )
v17 = 2;
else
v17 = 0;
LABEL_23:
v20 = a1[3] + ((*(_DWORD *)(*a1 + 16) & 3) + 1) * v6;
switch ( *(_DWORD *)(*a1 + 16) & 3 )
{
case 2:
goto LABEL_46;
case 3:
if ( v19 )
v23 = 0;
else
v23 = 3;
*(_BYTE *)(v20 + v23) = -1;
LABEL_46:
v24 = v20 + v19;
*(_BYTE *)(v24 + v1) = v16;
*(_BYTE *)(v24 + 1) = v15;
*(_BYTE *)(v24 + v17) = v27;
goto LABEL_28;
case 1:
*(_BYTE *)(v20 + (v19 ^ 1)) = -1;
break;
}
*(_BYTE *)(v20 + v19) = v15;
goto LABEL_28;
}
v16 = png_sRGB_table[v7];
v27 = png_sRGB_table[v30];
LOWORD(v15) = png_sRGB_table[v29];
}
else
{
if ( v6 > 0xFF )
goto LABEL_58;
v1 = 23434;
v13 = 6968 * png_sRGB_table[v30] + 23434 * png_sRGB_table[v29] + 2366 * png_sRGB_table[v7];
if ( v11 == 2 )
{
v15 = (unsigned int)(v13 + 0x4000) >> 15;
}
else
{
v14 = 255 * ((unsigned int)(v13 + 128) >> 8) + 64;
v1 = (v14 >> 7) & 0x7FFF;
LOWORD(v15) = (unsigned __int8)((unsigned __int16)(png_sRGB_base[v14 >> 22]
+ ((v1 * (unsigned int)png_sRGB_delta[v14 >> 22]) >> 12)) >> 8);
}
v27 = v15;
v16 = v15;
}
v17 = *(_DWORD *)(*a1 + 16) & 0x10;
if ( (v10 & 0x10) != 0 )
v1 = 0;
v18 = __clz((*(_DWORD *)(v9 + 16) & 0x21) - 33);
if ( (v10 & 0x10) != 0 )
v17 = 2;
else
v1 = 2;
if ( (v10 & 0x10) == 0 )
v17 = 0;
v19 = v18 >> 5;
if ( v11 != 2 )
goto LABEL_23;
v25 = a1[3] + 2 * ((*(_DWORD *)(*a1 + 16) & 3) + 1) * v6;
switch ( *(_DWORD *)(*a1 + 16) & 3 )
{
case 2:
goto LABEL_56;
case 3:
if ( v19 )
v26 = 0;
else
v26 = 6;
*(_WORD *)(v25 + v26) = -1;
LABEL_56:
v1 += v19;
*(_WORD *)(v25 + 2 * v1) = v16;
*(_WORD *)(v25 + 2 * (v19 + 1)) = v15;
*(_WORD *)(v25 + 2 * (v17 + v19)) = v27;
goto LABEL_28;
case 1:
*(_WORD *)(v25 + 2 * (v19 ^ 1)) = -1;
break;
}
*(_WORD *)(v25 + 2 * v19) = v15;
LABEL_28:
v7 += 51;
v6 = v3;
}
while ( v7 != 306 );
v5 = v8 + 51;
}
while ( v8 != 255 );
v2 = v28 + 51;
v28 = v2;
}
while ( v2 != 306 );
return v3;
}