//
// sub_CB33A8 from 0xcb33a8 to 0xcb396c (1476 bytes)
//
// 2 xrefs:
// > sub_CB396C @ 0xcb3b20
// > sub_CB3F30 @ 0xcb4f64
//
// 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_CB33A8(__int64 a1, __int64 a2, unsigned __int8 *a3, __int64 a4)
{
int v6; // w24
__int64 v8; // x4
__int64 v9; // x0
int v10; // w19
int v11; // w24
unsigned __int8 *v12; // x2
int v13; // t1
__int64 v14; // x1
int v15; // w26
char *v16; // x19
char *v17; // x26
unsigned int v18; // t1
__int64 *v19; // x2
__int64 v20; // x0
int v21; // w4
__int64 v22; // x0
int v23; // w2
int v24; // w19
unsigned __int8 *v25; // x3
int v26; // w0
int v27; // w1
int v28; // w25
int v29; // w21
unsigned int v30; // w26
unsigned int v32; // w0
int v33; // w2
__int64 v34; // x1
unsigned int v35; // w21
unsigned __int8 *v36; // x24
unsigned __int8 *v37; // x23
int v38; // w19
unsigned int v39; // w21
int v40; // [xsp+60h] [xbp+60h]
int v41; // [xsp+64h] [xbp+64h]
int v42; // [xsp+68h] [xbp+68h]
__int64 *v43; // [xsp+70h] [xbp+70h]
_QWORD *v44; // [xsp+78h] [xbp+78h]
_QWORD v45[4]; // [xsp+80h] [xbp+80h] BYREF
int v46; // [xsp+A0h] [xbp+A0h]
__int16 v47; // [xsp+A4h] [xbp+A4h]
_QWORD v48[9]; // [xsp+A8h] [xbp+A8h] BYREF
int v49; // [xsp+F0h] [xbp+F0h]
__int64 v50; // [xsp+F8h] [xbp+F8h] BYREF
__int64 v51; // [xsp+100h] [xbp+100h]
__int16 v52; // [xsp+108h] [xbp+108h]
char v53; // [xsp+10Ah] [xbp+10Ah]
_QWORD v54[2]; // [xsp+110h] [xbp+110h] BYREF
__int16 v55; // [xsp+120h] [xbp+120h]
char v56; // [xsp+122h] [xbp+122h]
v40 = 0;
v6 = *(_DWORD *)a4;
v41 = 0;
if ( v6 <= 0 )
{
LABEL_73:
sub_CB76C8(a1, 1, 4);
return _stack_chk_guard;
}
v8 = *(_QWORD *)(a4 + 8);
v9 = 0;
v10 = 0;
while ( 1 )
{
if ( *(_BYTE *)(v8 + v9) )
{
if ( v10 != 2 )
*(&v40 + v10) = v9;
++v10;
}
if ( v9 == v6 - 1 )
break;
++v9;
if ( v10 > 2 )
goto LABEL_9;
}
if ( !v10 )
goto LABEL_73;
if ( v10 > 2 || (v29 = v40, v40 > 255) || (v30 = v41, v41 > 255) )
{
LABEL_9:
v42 = 19;
v44 = v45;
v50 = 0;
v51 = 0;
v52 = 0;
v53 = 0;
memset(v45, 0, sizeof(v45));
v46 = 0;
v47 = 0;
v43 = &v50;
sub_CB76C8(a1, 0, 1);
v11 = sub_CB879C(a4, a3, (unsigned int)v6);
v49 = 0;
memset(v48, 0, sizeof(v48));
v54[0] = 0;
v54[1] = 0;
v55 = 0;
v56 = 0;
if ( v11 > 0 )
{
v12 = a3;
do
{
v13 = *v12;
v12 += 2;
++*((_DWORD *)v48 + v13);
}
while ( v12 != &a3[2 * (v11 - 1) + 2] );
}
sub_CB8960((int)v48, 7, v54);
if ( HIBYTE(v51) )
{
v15 = 19;
v14 = 15;
goto LABEL_28;
}
if ( BYTE6(v51) )
{
v15 = 18;
v14 = 14;
goto LABEL_28;
}
if ( BYTE5(v51) )
{
v15 = 17;
v14 = 13;
goto LABEL_28;
}
if ( BYTE4(v51) )
{
v15 = 16;
v14 = 12;
goto LABEL_28;
}
if ( BYTE3(v51) )
{
v15 = 15;
v14 = 11;
goto LABEL_28;
}
if ( BYTE2(v51) )
{
v15 = 14;
v14 = 10;
goto LABEL_28;
}
if ( BYTE1(v51) )
{
v15 = 13;
v14 = 9;
goto LABEL_28;
}
if ( (_BYTE)v51 )
{
v15 = 12;
}
else
{
if ( HIBYTE(v50) )
{
v15 = 11;
v14 = 7;
goto LABEL_28;
}
if ( BYTE6(v50) )
{
v15 = 10;
v14 = 6;
goto LABEL_28;
}
if ( (_BYTE)v52 )
{
v15 = 9;
v14 = 5;
goto LABEL_28;
}
if ( BYTE5(v50) )
{
v15 = 8;
v14 = 4;
goto LABEL_28;
}
if ( BYTE4(v50) )
{
v15 = 7;
v14 = 3;
goto LABEL_28;
}
if ( BYTE3(v50) )
{
v15 = 6;
}
else
{
if ( !BYTE2(v50) )
{
v14 = 0;
v15 = 4;
LABEL_28:
v16 = (char *)&unk_F58760;
v17 = (char *)&unk_F58760 + (unsigned int)(v15 - 1) + 1;
sub_CB76C8(a1, v14, 4);
do
{
v18 = (unsigned __int8)*v16++;
sub_CB76C8(a1, *((unsigned __int8 *)&v50 + v18), 3);
}
while ( v16 != v17 );
if ( v42 > 0 )
{
v19 = v43;
v20 = 0;
v21 = 0;
do
{
if ( *((_BYTE *)v43 + v20) )
{
if ( v21 )
goto LABEL_39;
v21 = 1;
}
++v20;
}
while ( v42 > (int)v20 );
v22 = 0;
while ( 1 )
{
*((_BYTE *)v19 + v22) = 0;
v23 = v42;
*((_WORD *)v44 + v22++) = 0;
if ( v23 <= (int)v22 )
break;
v19 = v43;
}
}
LABEL_39:
v24 = v11;
v25 = &a3[2 * v11];
v26 = 0;
if ( v11 <= 0 )
goto LABEL_47;
while ( 1 )
{
v27 = *(v25 - 2);
if ( (unsigned __int8)(v27 - 17) > 1u )
{
if ( *(v25 - 2) )
break;
}
--v24;
v26 += *((unsigned __int8 *)&v50 + *(v25 - 2));
if ( v27 == 17 )
{
v26 += 3;
v25 -= 2;
}
else
{
if ( v27 == 18 )
v26 += 7;
v25 -= 2;
}
if ( v24 <= 0 )
goto LABEL_47;
}
v28 = v24;
if ( v26 <= 12 || v24 <= 1 )
{
LABEL_47:
v28 = v11;
sub_CB76C8(a1, 0, 1);
goto LABEL_64;
}
sub_CB76C8(a1, 1, 1);
v32 = __clz(v24 - 1) ^ 0x1F;
if ( v24 - 1 == ((1 - v24) & (v24 - 1)) )
{
v35 = 2;
v34 = 0;
if ( !v32 )
goto LABEL_63;
}
else
{
++v32;
}
v33 = (int)(v32 + 1) >> 1;
v34 = (unsigned int)(v33 - 1);
v35 = 2 * v33;
LABEL_63:
sub_CB76C8(a1, v34, 3);
sub_CB76C8(a1, (unsigned int)(v24 - 2), v35);
LABEL_64:
if ( v28 > 0 )
{
v36 = a3 + 1;
v37 = &a3[2 * (v28 - 1) + 3];
do
{
v38 = *(v36 - 1);
v39 = *v36;
sub_CB76C8(a1, *((unsigned __int16 *)v44 + *(v36 - 1)), *((unsigned __int8 *)v43 + (unsigned __int8)v38));
switch ( v38 )
{
case 17:
sub_CB76C8(a1, v39, 3);
break;
case 18:
sub_CB76C8(a1, v39, 7);
break;
case 16:
sub_CB76C8(a1, v39, 2);
break;
}
v36 += 2;
}
while ( v36 != v37 );
}
return _stack_chk_guard;
}
v15 = 5;
}
}
v14 = (unsigned int)(v15 - 4);
goto LABEL_28;
}
sub_CB76C8(a1, 1, 1);
sub_CB76C8(a1, (unsigned int)(v10 - 1), 1);
if ( v29 <= 1 )
{
sub_CB76C8(a1, 0, 1);
sub_CB76C8(a1, (unsigned int)v29, 1);
}
else
{
sub_CB76C8(a1, 1, 1);
sub_CB76C8(a1, (unsigned int)v29, 8);
}
if ( v10 == 2 )
sub_CB76C8(a1, v30, 8);
return _stack_chk_guard;
}