//
// sub_1800338B0 from 0x1800338b0 to 0x180033bbc (780 bytes)
//
// 6 xrefs:
// > sub_1800338A0 @ 0x1800338a0
// > ??> @ 0x1801801cc
// > ??> @ 0x1801801dc
// > ??> @ 0x1801801f0
// > ??> @ 0x180180200
// > ??> @ 0x1801f79ec
//
// 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_1800338B0(__int64 a1, unsigned __int64 a2, unsigned __int64 a3, unsigned int *a4)
{
__int64 v4; // r10
unsigned int *v5; // r12
unsigned __int64 v8; // rbp
__int64 v9; // rax
int v10; // eax
unsigned int v11; // r15d
int v13; // ecx
_WORD *v14; // r10
unsigned __int64 v15; // rdx
__int16 v16; // cx
__int16 v17; // r9
__int16 v18; // r8
_WORD *v19; // r9
unsigned __int64 v20; // r10
__int16 v21; // cx
__int16 v22; // r8
__int16 v23; // dx
__int64 v24; // rax
__int64 v25; // r14
int v26; // ecx
int v27; // ebp
__int64 v28; // r12
const char *v29; // rsi
const char *v30; // rdi
char v31; // al
const char *v32; // rcx
void (__fastcall **v33)(__int64, __int64, const char *, const char *, int, int, int); // rbx
int v34; // eax
__int64 v35; // rcx
unsigned int v36; // r8d
int v37; // ecx
int v38; // ecx
int v39; // ecx
v4 = *(_QWORD *)(a1 + 8);
v5 = a4;
v8 = 32;
switch ( *(_DWORD *)(v4 + 8) )
{
case 1:
v9 = 8;
break;
case 2:
v9 = 16;
break;
case 3:
v9 = 24;
break;
default:
if ( (unsigned int)(*(_DWORD *)(v4 + 8) - 4) > 1 )
goto LABEL_10;
v9 = 32;
break;
}
a3 = (v9 * (unsigned __int64)(unsigned int)a3) >> 3;
LABEL_10:
v10 = sub_180051FD0(a1, (int)a1 + 376, a2, (int)a3 * *(_DWORD *)(v4 + 12), 0, 2, 1, 0);
v11 = v10;
if ( v10 <= 0 )
{
if ( v10 == -131 )
return 31;
if ( v10 == -139 )
return 38;
if ( v10 != -3 )
return 16;
v11 = 0;
}
v13 = *(_DWORD *)(*(_QWORD *)(a1 + 8) + 12LL);
if ( v13 == 6 )
{
if ( a2 < v11 + a2 )
{
v14 = (_WORD *)(a2 + 4);
v15 = ((unsigned __int64)v11 - 1) / 0xC + 1;
do
{
v16 = *(v14 - 1);
v17 = v14[1];
v18 = v14[2];
*(v14 - 1) = *v14;
v14[1] = v14[3];
*v14 = v16;
v14[2] = v17;
v14[3] = v18;
v14 += 6;
--v15;
}
while ( v15 );
}
}
else if ( v13 == 8 && a2 < v11 + a2 )
{
v19 = (_WORD *)(a2 + 4);
v20 = (((unsigned __int64)v11 - 1) >> 4) + 1;
do
{
v21 = *(v19 - 1);
v22 = v19[1];
v23 = v19[2];
*(v19 - 1) = *v19;
v19[1] = v19[5];
v19[2] = v19[3];
v19[3] = v19[4];
*v19 = v21;
v19[4] = v22;
v19[5] = v23;
v19 += 8;
--v20;
}
while ( v20 );
}
v24 = sub_180050B80(a1 + 376, 0xFFFFFFFFLL);
v25 = v24;
if ( v24 )
{
v26 = *(_DWORD *)(v24 + 16);
if ( v26 )
{
if ( v26 > 0 )
{
v27 = 0;
v28 = 0;
do
{
v29 = *(const char **)(v28 + *(_QWORD *)v25);
v30 = v29;
v31 = *v29;
if ( *v29 )
{
while ( 1 )
{
v32 = v30++;
if ( v31 == 61 )
break;
v31 = *v30;
if ( !*v30 )
goto LABEL_34;
}
*v32 = 0;
}
else
{
LABEL_34:
v30 = *(const char **)(v28 + *(_QWORD *)v25);
v29 = "NONAME";
}
v33 = *(void (__fastcall ***)(__int64, __int64, const char *, const char *, int, int, int))(a1 + 16);
v34 = sub_1800B7F50(v30);
(*v33)(a1, 3, v29, v30, v34 + 1, 6, 1);
++v27;
v28 += 8;
}
while ( v27 < *(_DWORD *)(v25 + 16) );
v5 = a4;
v8 = 32;
}
sub_18004C4D0(a1, v25);
}
}
if ( v5 )
{
v35 = *(_QWORD *)(a1 + 8);
v36 = *(_DWORD *)(v35 + 12);
if ( v36 )
{
v37 = *(_DWORD *)(v35 + 8) - 1;
if ( v37 )
{
v38 = v37 - 1;
if ( v38 )
{
v39 = v38 - 1;
if ( v39 )
{
if ( (unsigned int)(v39 - 1) >= 2 )
{
*v5 = v11;
return 0;
}
}
else
{
v8 = 24;
}
}
else
{
v8 = 16;
}
}
else
{
v8 = 8;
}
*v5 = (unsigned int)(8 * (unsigned __int64)v11 / v8) / v36;
}
}
return 0;
}