//
// sub_1800216F0 from 0x1800216f0 to 0x1800219c6 (726 bytes)
//
// 2 xrefs:
// > ??> @ 0x1800e7048
// > ??> @ 0x1801aee3c
//
// 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
//
// Hidden C++ exception states: #wind=1
__int64 __fastcall sub_1800216F0(__int64 a1)
{
unsigned __int64 v2; // rbx
FILE *v4; // rcx
int v5; // eax
unsigned int v6; // ebx
int v7; // eax
char v8; // r8
unsigned __int64 v9; // rcx
void **v10; // rax
void **v11; // r9
void **v12; // r8
int v13; // eax
int v14; // eax
void **v15; // rcx
unsigned __int64 v16; // rax
void **v17; // rcx
unsigned __int64 v18; // rbx
int v19; // eax
void *v20; // rcx
void **v21; // rax
void **v22; // rax
__int64 v23; // rcx
__int64 v24; // rbx
unsigned __int8 v25; // [rsp+40h] [rbp-40h] BYREF
char v26[7]; // [rsp+41h] [rbp-3Fh] BYREF
__int64 v27; // [rsp+48h] [rbp-38h] BYREF
unsigned __int8 *v28; // [rsp+50h] [rbp-30h] BYREF
void *Block[2]; // [rsp+58h] [rbp-28h] BYREF
unsigned __int64 v30; // [rsp+68h] [rbp-18h]
unsigned __int64 v31; // [rsp+70h] [rbp-10h]
if ( std::streambuf::gptr(a1) )
{
v2 = std::streambuf::egptr(a1);
if ( std::streambuf::gptr(a1) < v2 )
return *(unsigned __int8 *)std::streambuf::_Gninc(a1);
}
if ( !*(_QWORD *)(a1 + 128) )
return 0xFFFFFFFFLL;
if ( std::streambuf::eback(a1) == a1 + 112 )
std::streambuf::setg(a1, *(_QWORD *)(a1 + 136), *(_QWORD *)(a1 + 136), *(_QWORD *)(a1 + 144));
v4 = *(FILE **)(a1 + 128);
if ( *(_QWORD *)(a1 + 104) )
{
*(_OWORD *)Block = 0;
v30 = 0;
v31 = 15;
LOBYTE(Block[0]) = 0;
v7 = fgetc(v4);
v8 = v7;
if ( v7 == -1 )
{
LABEL_31:
v6 = -1;
}
else
{
while ( 1 )
{
v9 = v30;
if ( v30 >= v31 )
{
sub_1800162D0(Block, 1u, 0, v8);
}
else
{
++v30;
v10 = Block;
if ( v31 > 0xF )
v10 = (void **)Block[0];
*((_BYTE *)v10 + v9) = v8;
*((_BYTE *)v10 + v9 + 1) = 0;
}
v11 = Block;
if ( v31 > 0xF )
v11 = (void **)Block[0];
v12 = Block;
if ( v31 > 0xF )
v12 = (void **)Block[0];
v13 = std::codecvt::in(
*(_QWORD *)(a1 + 104),
a1 + 116,
v12,
(char *)v11 + v30,
&v27,
&v25,
v26,
&v28);
if ( v13 )
{
v14 = v13 - 1;
if ( v14 )
break;
}
if ( v28 != &v25 )
{
v22 = Block;
if ( v31 > 0xF )
v22 = (void **)Block[0];
v23 = v27;
v24 = (__int64)v22 + v30 - v27;
if ( v24 > 0 )
{
while ( 1 )
{
ungetc(*(char *)(--v24 + v23), *(FILE **)(a1 + 128));
if ( v24 <= 0 )
break;
v23 = v27;
}
}
v6 = v25;
goto LABEL_32;
}
v15 = Block;
if ( v31 > 0xF )
v15 = (void **)Block[0];
v16 = v27 - (_QWORD)v15;
if ( v30 < v27 - (__int64)v15 )
v16 = v30;
v17 = Block;
if ( v31 > 0xF )
v17 = (void **)Block[0];
v18 = v30 - v16;
memmove(v17, (char *)v17 + v16, v30 - v16 + 1);
v30 = v18;
v19 = fgetc(*(FILE **)(a1 + 128));
v8 = v19;
if ( v19 == -1 )
goto LABEL_31;
}
if ( v14 != 2 )
goto LABEL_31;
v21 = Block;
if ( v31 > 0xF )
v21 = (void **)Block[0];
v6 = *(char *)v21;
}
LABEL_32:
if ( v31 > 0xF )
{
v20 = Block[0];
if ( v31 + 1 >= 0x1000 )
{
v20 = *((void **)Block[0] - 1);
if ( (unsigned __int64)((char *)Block[0] - (char *)v20 - 8) > 0x1F )
invalid_parameter_noinfo_noreturn();
}
j_j_free_0(v20);
}
}
else
{
v5 = fgetc(v4);
v6 = -1;
if ( v5 != -1 )
return (unsigned __int8)v5;
}
return v6;
}