//
// sub_1005F0540 from 0x1005f0540 to 0x1005f0854 (788 bytes)
//
// 3 xrefs:
// > sub_1005F04C8 @ 0x1005f04f0
// > sub_1005F0854 @ 0x1005f0880
// > sub_1005F08D4 @ 0x1005f0924
//
// 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
//
__int64 __fastcall sub_1005F0540(__int64 a1, unsigned __int64 **a2)
{
unsigned __int64 *v4; // x19
int v5; // w27
unsigned __int64 v6; // x22
__int64 v7; // x28
__int64 v8; // x8
__int64 v9; // x26
int v10; // w0
bool v11; // cf
unsigned __int64 v12; // x20
int v13; // w0
unsigned __int64 v14; // x20
unsigned __int64 v15; // x26
int v16; // w0
__int16 v18; // w2
int v19; // w4
unsigned __int64 **v20; // [xsp+8h] [xbp-A8h]
unsigned __int64 v21; // [xsp+18h] [xbp-98h] BYREF
int v22; // [xsp+28h] [xbp-88h]
int v23; // [xsp+2Ch] [xbp-84h] BYREF
__int64 v24; // [xsp+30h] [xbp-80h] BYREF
unsigned __int64 v25[5]; // [xsp+38h] [xbp-78h] BYREF
v4 = sub_1005D9DA0();
if ( v4 )
{
v20 = a2;
sub_1005E1158();
v5 = 0;
v6 = 0;
v7 = 0;
do
{
while ( 1 )
{
while ( 1 )
{
v8 = v7 - v6;
if ( v7 - v6 <= 8 )
{
v9 = 8 - v8;
if ( __CFADD__(v7, 8 - v8) || !(unsigned int)sub_1005D9F48(v4, v6 + 8) )
{
v18 = 65;
v19 = 167;
goto LABEL_44;
}
v10 = sub_1005DA62C(a1, v4[1] + v7, v9);
if ( v7 == v6 && v10 < 0 )
{
v18 = 142;
v19 = 172;
goto LABEL_44;
}
if ( v10 >= 1 )
{
v11 = __CFADD__(v7, v10);
v7 += v10;
if ( v11 )
{
v18 = 155;
v19 = 177;
goto LABEL_44;
}
}
}
v12 = v4[1] + v6;
v21 = v12;
v13 = sub_1005FB380(&v21, v25, &v23, &v24, v7 - v6);
v22 = v13;
if ( (v13 & 0x80) != 0 )
{
if ( (sub_1005E141C() & 0xFFF) != 0x9B )
goto LABEL_45;
sub_1005E1158();
LOBYTE(v13) = v22;
}
v6 += (int)v21 - (int)v12;
if ( (v13 & 1) == 0 )
break;
if ( v5 <= -2 )
{
v18 = 123;
v19 = 205;
goto LABEL_44;
}
++v5;
}
v14 = v25[0];
if ( !v5 || v25[0] || v23 )
break;
if ( v5 < 2 )
goto LABEL_37;
--v5;
}
v15 = v25[0] - (v7 - v6);
if ( v25[0] > v7 - v6 )
{
if ( v15 >> 31 || __CFADD__(v7, v15) )
{
v18 = 155;
v19 = 223;
goto LABEL_44;
}
if ( !(unsigned int)sub_1005D9F48(v4, v25[0] + v6) )
{
v18 = 65;
v19 = 227;
goto LABEL_44;
}
if ( v14 != v7 - v6 )
{
while ( 1 )
{
v16 = sub_1005DA62C(a1, v4[1] + v7, v15);
if ( v16 <= 0 )
break;
v7 += v16;
v15 -= v16;
if ( !v15 )
goto LABEL_26;
}
v18 = 142;
v19 = 234;
goto LABEL_44;
}
LABEL_26:
v14 = v25[0];
}
v11 = __CFADD__(v6, v14);
v6 += v14;
if ( v11 )
{
v18 = 155;
v19 = 246;
goto LABEL_44;
}
}
while ( v5 > 0 );
LABEL_37:
if ( !(v6 >> 31) )
{
*v20 = v4;
return v6;
}
v18 = 155;
v19 = 258;
LABEL_44:
sub_1005E0E48(13, 107, v18, (__int64)"a_d2i_fp.c", v19);
LABEL_45:
sub_1005D9DF8(v4);
}
else
{
sub_1005E0E48(13, 107, 65, (__int64)"a_d2i_fp.c", 157);
}
return 0xFFFFFFFFLL;
}