//
// gzclose_w from 0x741fbc to 0x742222 (614 bytes)
//
// 1 xrefs:
// > gzclose @ 0x740594
//
// 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 gzclose_w(int a1)
{
_DWORD *v2; // r6
_DWORD *v3; // r9
int v4; // r6
int v5; // r0
int v6; // r3
int v7; // r5
bool v8; // zf
_BYTE *v9; // r1
_BYTE *v10; // r2
int v11; // r5
int v12; // r3
ssize_t v14; // r0
ssize_t v15; // r0
int *v16; // r0
char *v17; // r0
int v18; // r3
signed int v19; // r8
int v20; // r2
signed int v21; // r3
int v22; // r7
size_t v23; // r7
int v24; // r1
int v25; // r2
int v26; // r3
_BYTE *v27; // r1
_BYTE *v28; // r2
int v29; // r5
int v30; // r5
int v31; // r3
int v32; // r0
ssize_t v33; // r0
ssize_t v34; // r0
int *v35; // r0
char *v36; // r0
if ( !a1 )
return -2;
if ( *(_DWORD *)(a1 + 12) != 31153 )
return -2;
if ( !*(_DWORD *)(a1 + 72) )
goto LABEL_4;
v18 = *(_DWORD *)(a1 + 88);
v19 = *(_DWORD *)(a1 + 68);
*(_DWORD *)(a1 + 72) = 0;
if ( v18 && sub_741674(a1) == -1 )
{
v3 = (_DWORD *)(a1 + 84);
LABEL_60:
v4 = *(_DWORD *)(a1 + 76);
goto LABEL_6;
}
v2 = (_DWORD *)(a1 + 84);
v20 = 1;
v3 = (_DWORD *)(a1 + 84);
if ( !v19 )
{
LABEL_4:
v2 = (_DWORD *)(a1 + 84);
goto LABEL_5;
}
while ( 1 )
{
LABEL_41:
v21 = *(_DWORD *)(a1 + 24);
v22 = (unsigned int)v21 >> 31;
if ( v21 > v19 )
v22 = 1;
if ( v22 )
v23 = v19;
else
v23 = *(_DWORD *)(a1 + 24);
if ( v20 )
{
memset(*(void **)(a1 + 32), 0, v23);
v21 = *(_DWORD *)(a1 + 24);
}
v24 = *(_DWORD *)(a1 + 32);
v25 = *(_DWORD *)(a1 + 8);
*(_DWORD *)(a1 + 88) = v23;
*(_DWORD *)(a1 + 84) = v24;
*(_DWORD *)(a1 + 8) = v25 + v23;
if ( !v21 )
break;
if ( !*(_DWORD *)(a1 + 40) )
goto LABEL_50;
LABEL_67:
v34 = write(*(_DWORD *)(a1 + 16), *(const void **)(a1 + 84), *(_DWORD *)(a1 + 88));
if ( v34 < 0 || v34 != *(_DWORD *)(a1 + 88) )
{
LABEL_71:
v35 = (int *)_errno();
v36 = strerror(*v35);
sub_740B68((_DWORD *)a1, -1, v36);
goto LABEL_60;
}
v19 -= v23;
*(_DWORD *)(a1 + 88) = 0;
v20 = 0;
if ( !v19 )
goto LABEL_5;
}
if ( sub_741450(a1) == -1 )
goto LABEL_60;
if ( *(_DWORD *)(a1 + 40) )
goto LABEL_67;
LABEL_50:
v26 = *(_DWORD *)(a1 + 100);
do
{
if ( v26 )
{
v30 = v26;
v32 = deflate(a1 + 84, 0);
if ( v32 == -2 )
goto LABEL_59;
}
else
{
v27 = *(_BYTE **)(a1 + 4);
v28 = *(_BYTE **)(a1 + 96);
v29 = v28 - v27;
if ( v28 == v27 )
goto LABEL_53;
v33 = write(*(_DWORD *)(a1 + 16), v27, v28 - v27);
if ( v33 < 0 || v29 != v33 )
goto LABEL_71;
v30 = *(_DWORD *)(a1 + 100);
if ( !v30 )
{
LABEL_53:
v30 = *(_DWORD *)(a1 + 24);
v31 = *(_DWORD *)(a1 + 36);
*(_DWORD *)(a1 + 100) = v30;
*(_DWORD *)(a1 + 96) = v31;
}
else
{
v31 = *(_DWORD *)(a1 + 96);
}
*(_DWORD *)(a1 + 4) = v31;
v32 = deflate(a1 + 84, 0);
if ( v32 == -2 )
{
LABEL_59:
sub_740B68((_DWORD *)a1, v32, "internal error: deflate stream corrupt");
goto LABEL_60;
}
}
v26 = *(_DWORD *)(a1 + 100);
}
while ( v26 != v30 );
v19 -= v23;
v20 = 0;
if ( v19 )
goto LABEL_41;
LABEL_5:
v3 = v2;
v4 = 0;
LABEL_6:
if ( !*(_DWORD *)(a1 + 24) && sub_741450(a1) == -1 )
{
LABEL_20:
v4 = *(_DWORD *)(a1 + 76);
goto LABEL_21;
}
v5 = *(_DWORD *)(a1 + 40);
if ( v5 )
{
v15 = write(*(_DWORD *)(a1 + 16), *(const void **)(a1 + 84), *(_DWORD *)(a1 + 88));
if ( v15 >= 0 && v15 == *(_DWORD *)(a1 + 88) )
{
*(_DWORD *)(a1 + 88) = 0;
goto LABEL_21;
}
LABEL_35:
v16 = (int *)_errno();
v17 = strerror(*v16);
sub_740B68((_DWORD *)a1, -1, v17);
goto LABEL_20;
}
v6 = *(_DWORD *)(a1 + 100);
while ( 2 )
{
v8 = v5 == 1;
if ( v5 != 1 )
v8 = v6 == 0;
if ( !v8 )
{
v7 = v6;
v5 = deflate((int)v3, 4u);
if ( v5 == -2 )
goto LABEL_19;
goto LABEL_10;
}
v9 = *(_BYTE **)(a1 + 4);
v10 = *(_BYTE **)(a1 + 96);
v11 = v10 - v9;
if ( v10 == v9 )
{
if ( v6 )
goto LABEL_17;
LABEL_31:
v7 = *(_DWORD *)(a1 + 24);
v12 = *(_DWORD *)(a1 + 36);
*(_DWORD *)(a1 + 100) = v7;
*(_DWORD *)(a1 + 96) = v12;
}
else
{
v14 = write(*(_DWORD *)(a1 + 16), v9, v10 - v9);
if ( v14 < 0 || v11 != v14 )
goto LABEL_35;
v6 = *(_DWORD *)(a1 + 100);
if ( !v6 )
goto LABEL_31;
LABEL_17:
v7 = v6;
v12 = *(_DWORD *)(a1 + 96);
}
*(_DWORD *)(a1 + 4) = v12;
v5 = deflate((int)v3, 4u);
if ( v5 == -2 )
{
LABEL_19:
sub_740B68((_DWORD *)a1, v5, "internal error: deflate stream corrupt");
goto LABEL_20;
}
LABEL_10:
v6 = *(_DWORD *)(a1 + 100);
if ( v6 != v7 )
continue;
break;
}
deflateReset(v3);
LABEL_21:
if ( *(_DWORD *)(a1 + 24) )
{
if ( !*(_DWORD *)(a1 + 40) )
{
deflateEnd((int)v3);
free(*(void **)(a1 + 36));
}
free(*(void **)(a1 + 32));
}
sub_740B68((_DWORD *)a1, 0, nullptr);
free(*(void **)(a1 + 20));
if ( close(*(_DWORD *)(a1 + 16)) == -1 )
v4 = -1;
free((void *)a1);
return v4;
}