//
// sub_BAC5C from 0xbac5c to 0xbaeba (606 bytes)
//
// 3 xrefs:
// > ??> @ 0xbac3c
// > sub_BB510 @ 0xbb686
// > sub_BB510 @ 0xbb71a
//
// 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_BAC5C(int a1, int a2)
{
int v3; // r5
int v4; // r0
__int64 v5; // kr00_8
int v6; // r2
int *v7; // r11
int v8; // r10
int v9; // r3
int v10; // r3
unsigned int v11; // r8
int v12; // r9
int v13; // r0
unsigned int v14; // r1
_DWORD *v15; // r1
bool v16; // zf
int v17; // r0
int v18; // r0
unsigned int v19; // r1
int v20; // r2
bool v21; // vf
int v22; // r2
int v23; // r0
void (__fastcall *v24)(_DWORD, int, unsigned int, _DWORD); // r6
unsigned int v25; // r0
unsigned int v26; // r1
int v27; // r1
int v28; // r1
int v29; // r2
int v30; // r0
unsigned __int64 *v31; // r0
unsigned __int64 v32; // kr08_8
__int64 v33; // kr10_8
int v34; // r6
unsigned int v35; // r0
int v36; // r3
unsigned int *v37; // r0
unsigned int v38; // r1
int v40; // [sp+8h] [bp-38h]
unsigned int *v42; // [sp+10h] [bp-30h]
unsigned int v43; // [sp+14h] [bp-2Ch] BYREF
if ( *(_DWORD *)(a1 + 408) == 46 )
return 46;
if ( !a2 )
{
v4 = *(_DWORD *)(a1 + 360);
__dmb(0xBu);
if ( !v4 )
return 46;
}
v5 = *(_QWORD *)(a1 + 320);
v6 = *(_DWORD *)(a1 + 328);
v7 = (int *)(a1 + 408);
v8 = 0;
*(_DWORD *)(a1 + 364) |= 8u;
v9 = 50;
if ( a2 )
v9 = 0;
v40 = v9;
v10 = *(_DWORD *)(a1 + 404);
v3 = 0;
v11 = HIDWORD(v5) - v5 - v10;
v12 = v5 + *(_DWORD *)(a1 + 412) + v6 + v10;
v42 = (unsigned int *)(a1 + 396);
while ( v11 )
{
v43 = 0;
v13 = *v7;
if ( !*v7 )
goto LABEL_12;
if ( v13 == 46 )
goto LABEL_16;
if ( v13 == 16 )
{
LABEL_12:
v14 = *v42;
if ( !*v42 )
{
if ( a2 )
v18 = *(_DWORD *)(a1 + 336) + v8;
else
v18 = *(_DWORD *)(a1 + 340);
v19 = *(_DWORD *)(a1 + 16);
if ( v19 != -1 )
{
v20 = *(_DWORD *)(a1 + 344);
v21 = __OFSUB__(v18, v20);
v22 = v18 - v20;
if ( !((v22 < 0) ^ v21 | (v22 == 0)) && v22 >= v19 )
{
v11 = 0;
v43 = 0;
LABEL_31:
v3 = 16;
goto LABEL_32;
}
}
*(_DWORD *)(a1 + 408) = 0;
v28 = *(_DWORD *)(a1 + 368);
v29 = v40;
if ( (*(_DWORD *)(a1 + 364) & 0x100) != 0 )
v29 = 100;
*(_DWORD *)(a1 + 372) = a1;
*(_DWORD *)(a1 + 376) = v18;
*(_DWORD *)(a1 + 380) = v11;
*(_DWORD *)(a1 + 384) = v29;
*(_DWORD *)(a1 + 388) = 0;
*(_DWORD *)(a1 + 392) = v12;
*(_DWORD *)(a1 + 396) = 0;
if ( v28 )
{
v30 = *(_DWORD *)(a1 + 40);
if ( v30 != 3 )
{
__dmb(0xBu);
v31 = (unsigned __int64 *)(v28 + 8 * v30 + 68648);
do
v32 = __ldrexd(v31);
while ( __strexd(v32 + (int)v11, v31) );
__dmb(0xBu);
}
}
v3 = (*(int (__fastcall **)(int, int, unsigned int *, int))(*(_DWORD *)a1 + 40))(a1, a1 + 372, &v43, a2);
if ( v3 == 46 )
{
v15 = (_DWORD *)(a1 + 396);
if ( !a2 )
return 46;
}
else
{
v15 = (_DWORD *)(a1 + 396);
}
goto LABEL_15;
}
*(_DWORD *)(a1 + 404) += v14;
v43 = v14;
*(_DWORD *)(a1 + 396) = 0;
}
v15 = (_DWORD *)(a1 + 408);
v3 = v13;
LABEL_15:
*v15 = 0;
LABEL_16:
if ( v3 )
{
v11 = v43;
v16 = v3 == 16;
if ( v3 == 16 )
v16 = *(_DWORD *)(a1 + 16) == -1;
if ( v16 )
{
v17 = *(_DWORD *)(a1 + 340);
if ( v17 )
{
*(_DWORD *)(a1 + 16) = v17;
*(_DWORD *)(a1 + 20) = v17;
}
goto LABEL_31;
}
}
else
{
v3 = 0;
}
LABEL_32:
v23 = *(_DWORD *)(a1 + 368);
if ( v23 )
{
v24 = *(void (__fastcall **)(_DWORD, int, unsigned int, _DWORD))(v23 + 69044);
if ( v24 )
v24(*(_DWORD *)(a1 + 28), v12, v43, 0);
}
v25 = v43;
if ( v43 - 1 >= v11 )
break;
v26 = *(_DWORD *)(a1 + 340) + v43;
*(_DWORD *)(a1 + 340) = v26;
v27 = (int)(float)((float)((float)((float)v26 - (float)*(unsigned int *)(a1 + 332)) * 100.0)
/ (float)*(unsigned int *)(a1 + 316));
*(_DWORD *)(a1 + 348) = v27 & ~(v27 >> 31);
if ( *(_BYTE *)(a1 + 36) )
{
*(_BYTE *)(a1 + 36) = 0;
break;
}
v11 -= v25;
v12 += v25;
v8 += v25;
}
v33 = *(_QWORD *)(a1 + 324);
v34 = 128;
v35 = *(_DWORD *)(a1 + 316);
if ( !HIDWORD(v33) )
v34 = 64;
*(_DWORD *)(a1 + 364) |= v34;
v36 = 0;
*(_DWORD *)(a1 + 404) = 0;
*(_DWORD *)(a1 + 320) = 0;
*(_DWORD *)(a1 + 352) = v3;
if ( (int)v33 + HIDWORD(v33) < v35 )
v36 = v33 + HIDWORD(v33);
*(_DWORD *)(a1 + 328) = v36;
*(_DWORD *)(a1 + 364) &= 0xFFFFFED7;
if ( !a2 )
{
v37 = (unsigned int *)(a1 + 360);
__dmb(0xBu);
do
v38 = __ldrex(v37);
while ( __strex(v38 - 1, v37) );
__dmb(0xBu);
}
return v3;
}