//
// sub_631978 from 0x631978 to 0x631be4 (620 bytes)
//
// 2 xrefs:
// > sub_62931C @ 0x629e82
// > sub_62C878 @ 0x62cb48
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
int __fastcall sub_631978(int *a1, int a2, int byte_count)
{
int v4; // r1
int v7; // r2
int v8; // r3
int result; // r0
int v10; // r0
int v11; // r1
int v12; // r3
int v13; // r1
_BYTE *v14; // r0
void *v15; // r0
const char *v16; // r0
bool v17; // nf
int v18; // r0
int v19; // r3
_BYTE *v20; // r0
void *v21; // r0
const char *v22; // r2
int v23; // r0
const char *v24; // r2
int v25; // r0
int v26; // r0
int v27; // r3
int v28; // r0
int v29; // r1
int v30; // r2
int v31; // r0
int v32; // r2
int v33; // r1
int v34; // [sp+Ch] [bp-4h] BYREF
v4 = *(_DWORD *)(a2 + 144);
if ( !v4 )
{
v15 = memset((void *)(a2 + 148), 0, 0x38u);
*(_DWORD *)(a2 + 180) = sub_631650;
*(_DWORD *)(a2 + 184) = sub_631664;
v16 = (const char *)zlibVersion(v15);
v17 = strcmp(v16, "1.2.0.4") < 0;
v18 = a2 + 148;
if ( v17 )
{
if ( !inflateInit2_(v18, -15, "1.2.8", 56) )
{
*(_DWORD *)(a2 + 144) = 1;
goto LABEL_18;
}
v24 = *(const char **)(a2 + 172);
v25 = *a1;
if ( v24 )
goto LABEL_29;
}
else
{
if ( !inflateInit2_(v18, 47, "1.2.8", 56) )
{
*(_DWORD *)(a2 + 144) = 4;
goto LABEL_15;
}
v24 = *(const char **)(a2 + 172);
v25 = *a1;
if ( v24 )
{
LABEL_29:
sub_6180F8(v25, "Error while processing content unencoding: %s", v24);
return 61;
}
}
sub_6180F8(v25, "Error while processing content unencoding: Unknown failure within decompression software.");
return 61;
}
if ( v4 == 4 )
{
LABEL_15:
v19 = *(_DWORD *)(a2 + 92);
*(_DWORD *)(a2 + 152) = byte_count;
*(_DWORD *)(a2 + 148) = v19;
return sub_631710(a1, a2);
}
if ( v4 != 1 )
{
if ( v4 != 2 )
{
v7 = *(_DWORD *)(a2 + 92);
v8 = byte_count;
*(_DWORD *)(a2 + 152) = byte_count;
*(_DWORD *)(a2 + 148) = v7;
goto LABEL_6;
}
v10 = *(_DWORD *)(a2 + 148);
v11 = *(_DWORD *)(a2 + 152) + byte_count;
*(_DWORD *)(a2 + 152) = v11;
v12 = sub_634ED0(v10, v11);
*(_DWORD *)(a2 + 148) = v12;
if ( !v12 )
{
inflateEnd(a2 + 148);
*(_DWORD *)(a2 + 144) = 0;
return 27;
}
memcpy((void *)(*(_DWORD *)(a2 + 152) - byte_count + v12), *(const void **)(a2 + 92), byte_count);
v13 = *(_DWORD *)(a2 + 152);
v14 = *(_BYTE **)(a2 + 148);
if ( v13 <= 9 )
return 0;
if ( *v14 == 31 && (unsigned __int8)v14[1] == 139 )
{
v26 = sub_631674(v14, v13, &v34);
if ( !v26 )
{
off_ABAEB0(*(void **)(a2 + 148));
v27 = *(_DWORD *)(a2 + 152);
v28 = v34;
v29 = *(_DWORD *)(a2 + 92);
v30 = v34 - v27;
*(_DWORD *)(a2 + 144) = 3;
v8 = v27 - v28;
*(_DWORD *)(a2 + 152) = v8;
*(_DWORD *)(a2 + 148) = byte_count + v30 + v29;
goto LABEL_6;
}
if ( v26 == 2 )
return 0;
v14 = *(_BYTE **)(a2 + 148);
}
off_ABAEB0(v14);
v22 = *(const char **)(a2 + 172);
v23 = *a1;
if ( v22 )
goto LABEL_23;
goto LABEL_37;
}
LABEL_18:
v20 = *(_BYTE **)(a2 + 92);
if ( byte_count > 9 )
{
if ( *v20 != 31 || (unsigned __int8)v20[1] != 139 )
goto LABEL_22;
v31 = sub_631674(v20, byte_count, &v34);
if ( !v31 )
{
v32 = v34;
v33 = *(_DWORD *)(a2 + 92);
v8 = byte_count - v34;
*(_DWORD *)(a2 + 144) = 3;
*(_DWORD *)(a2 + 152) = v8;
*(_DWORD *)(a2 + 148) = v32 + v33;
LABEL_6:
result = v8;
if ( v8 )
return sub_631710(a1, a2);
return result;
}
if ( v31 != 2 )
{
LABEL_22:
v22 = *(const char **)(a2 + 172);
v23 = *a1;
if ( v22 )
{
LABEL_23:
sub_6180F8(v23, "Error while processing content unencoding: %s", v22);
LABEL_24:
inflateEnd(a2 + 148);
*(_DWORD *)(a2 + 144) = 0;
return 61;
}
LABEL_37:
sub_6180F8(v23, "Error while processing content unencoding: Unknown failure within decompression software.");
goto LABEL_24;
}
}
*(_DWORD *)(a2 + 152) = byte_count;
v21 = off_ABAEAC(byte_count);
*(_DWORD *)(a2 + 148) = v21;
if ( v21 )
{
memcpy(v21, *(const void **)(a2 + 92), *(_DWORD *)(a2 + 152));
*(_DWORD *)(a2 + 144) = 2;
return 0;
}
else
{
inflateEnd(a2 + 148);
*(_DWORD *)(a2 + 144) = 0;
return 27;
}
}