//
// sub_B7BE9C from 0xb7be9c to 0xb7c1f0 (852 bytes)
//
// 2 xrefs:
// > sub_B7C1F0 @ 0xb7c270
// > sub_B951E4 @ 0xb95248
//
// 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_B7BE9C(__int64 *a1, int a2, char *a3, size_t a4)
{
size_t v5; // x19
__int64 v8; // x22
__int64 v9; // x22
__int64 result; // x0
__int64 (__fastcall *v11)(char *, __int64, __int64, _QWORD); // x24
__int64 (__fastcall *v12)(char *, __int64, __int64, __int64); // x26
__int64 v13; // x23
__int64 v14; // x20
__int64 v15; // x2
__int64 v16; // x0
__int64 v17; // x0
__int64 v18; // x0
void *v19; // x0
void *v20; // x20
int v21; // w1
__int64 v22; // x2
__int64 v23; // x22
__int64 v24; // x3
__int64 v25; // x20
__int64 v26; // x0
void *v27; // x0
void *v28; // x23
int v29; // w1
__int64 v30; // x2
__int64 v31; // x22
size_t v32; // x20
char *v33; // x0
char *v34; // x23
v5 = a4;
v8 = *a1;
if ( !a4 )
return 0;
if ( (*(_DWORD *)(v8 + 484) & 0x10) != 0 )
{
v9 = v8 + 0x8000;
result = 56;
if ( *(_DWORD *)(v9 + 2896) != a2 )
return result;
v32 = a4 + *(_QWORD *)(v9 + 2888);
v33 = (char *)off_1231010(*(void **)(v9 + 2880), v32);
v34 = v33;
if ( v33 )
{
memcpy(&v33[*(_QWORD *)(v9 + 2888)], a3, v5);
*(_QWORD *)(v9 + 2880) = v34;
*(_QWORD *)(v9 + 2888) = v32;
return 0;
}
return 27;
}
v11 = nullptr;
if ( (a2 & 1) != 0 )
v11 = *(__int64 (__fastcall **)(char *, __int64, __int64, _QWORD))(v8 + 688);
if ( (a2 & 2) == 0 )
goto LABEL_19;
v12 = *(__int64 (__fastcall **)(char *, __int64, __int64, __int64))(v8 + 696);
if ( v12 )
{
if ( v11 )
goto LABEL_10;
v24 = *(_QWORD *)(v8 + 592);
LABEL_42:
while ( 1 )
{
v25 = v5 > 0x4000 ? 0x4000LL : v5;
v26 = v12(a3, 1, v25, v24);
if ( v26 == 268435457 )
break;
a3 += v26;
if ( v26 != v25 )
goto LABEL_38;
v5 -= v26;
if ( !v5 )
return 0;
v24 = *(_QWORD *)(v8 + 592);
}
LABEL_36:
v19 = off_1231000(v5);
v20 = v19;
if ( v19 )
{
memcpy(v19, a3, v5);
v21 = *(_DWORD *)(v8 + 484);
v22 = v8 + 120;
v23 = v8 + 0x8000;
*(_QWORD *)(v23 + 2880) = v20;
*(_QWORD *)(v23 + 2888) = v5;
*(_DWORD *)(v23 + 2896) = 2;
*(_DWORD *)(v22 + 364) = v21 | 0x10;
return 0;
}
return 27;
}
v24 = *(_QWORD *)(v8 + 592);
if ( v24 )
{
v12 = *(__int64 (__fastcall **)(char *, __int64, __int64, __int64))(v8 + 688);
if ( v11 )
{
if ( v12 )
{
LABEL_10:
while ( 1 )
{
v13 = v5 > 0x4000 ? 0x4000LL : v5;
v14 = v11(a3, 1, v13, *(_QWORD *)(v8 + 576));
v15 = v14;
if ( v14 == 268435457 )
break;
if ( v14 != v13 )
{
LABEL_35:
sub_B7B690(v8, "Failed writing body (%zu != %zu)", v14, v13);
return 23;
}
v16 = v12(a3, 1, v14, *(_QWORD *)(v8 + 592));
if ( v16 == 268435457 )
goto LABEL_36;
a3 += v16;
if ( v16 != v14 )
{
LABEL_38:
sub_B7B690(v8, "Failed writing header");
return 23;
}
v5 -= v16;
if ( !v5 )
return 0;
}
}
else
{
LABEL_20:
while ( 1 )
{
v13 = v5 > 0x4000 ? 0x4000LL : v5;
v17 = v11(a3, 1, v13, *(_QWORD *)(v8 + 576));
v14 = v17;
if ( v17 == 268435457 )
break;
a3 += v17;
if ( v17 != v13 )
goto LABEL_35;
v5 -= v17;
if ( !v5 )
return 0;
}
}
if ( (*(_DWORD *)(a1[128] + 124) & 0x10) != 0 )
{
sub_B7B690(v8, "Write callback asked for PAUSE when not supported!", v15);
return 23;
}
v27 = off_1231000(v5);
v28 = v27;
if ( v27 )
{
memcpy(v27, a3, v5);
v29 = *(_DWORD *)(v8 + 484);
v30 = v8 + 120;
v31 = v8 + 0x8000;
*(_QWORD *)(v31 + 2880) = v28;
*(_QWORD *)(v31 + 2888) = v5;
*(_DWORD *)(v31 + 2896) = a2;
*(_DWORD *)(v30 + 364) = v29 | 0x10;
return 0;
}
return 27;
}
if ( v12 )
goto LABEL_42;
}
else
{
LABEL_19:
if ( v11 )
goto LABEL_20;
}
do
{
if ( v5 > 0x4000 )
v18 = 0x4000;
else
v18 = v5;
v5 -= v18;
}
while ( v5 );
return 0;
}