//
// sub_765C04 from 0x765c04 to 0x7660b8 (1204 bytes)
//
// 2 xrefs:
// > sub_7665E4 @ 0x766698
// > sub_766740 @ 0x76676c
//
// 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
//
_DWORD *__fastcall sub_765C04(_DWORD *a1, int a2, int a3, int a4, _DWORD *a5, wchar_t c, char **a7)
{
void *v8; // r7
int v9; // r0
int v10; // r5
int v11; // r4
int v12; // r9
char *v13; // r5
int *v14; // r2
int v15; // r3
int v16; // r7
int v18; // r3
int v19; // r9
_DWORD *v20; // r0
char *v21; // r0
char *v22; // r3
char *v23; // r10
int v24; // r1
unsigned int v25; // r3
unsigned int v26; // r1
_DWORD *v27; // r3
int v28; // r2
int v29; // r10
int v30; // r7
unsigned int v31; // r7
unsigned int v32; // r10
int v33; // r5
void *v34; // r2
_BOOL4 v35; // r3
_DWORD *v36; // r1
unsigned int v37; // r3
int v38; // r0
_DWORD *v39; // r0
char *v40; // r0
unsigned int *v41; // r3
int v42; // r2
_DWORD *v43; // r3
int v44; // r2
int v45; // r7
int v46; // r2
int *v47; // r1
unsigned int *v48; // r0
unsigned int *v49; // r4
unsigned int *v50; // r1
int v51; // r3
int v52; // [sp+8h] [bp-4Ch]
unsigned int v54; // [sp+10h] [bp-44h]
int v55; // [sp+1Ch] [bp-38h]
int *v56; // [sp+24h] [bp-30h]
int v57; // [sp+28h] [bp-2Ch]
_BOOL4 v58; // [sp+2Ch] [bp-28h]
unsigned int v59; // [sp+30h] [bp-24h]
int v60; // [sp+34h] [bp-20h]
int v61; // [sp+3Ch] [bp-18h]
int v62; // [sp+44h] [bp-10h]
_DWORD *v63; // [sp+48h] [bp-Ch] BYREF
_DWORD *v64; // [sp+4Ch] [bp-8h] BYREF
v61 = a4;
v52 = (unsigned __int8)a4;
v8 = sub_764188((int)(a5 + 27));
v9 = sub_756B58(&dword_ACDD90);
v10 = *(_DWORD *)(a5[27] + 12);
v11 = *(_DWORD *)(v10 + 4 * v9);
v12 = v9;
if ( !v11 )
{
v48 = (unsigned int *)operator new(0x70u);
v48[1] = 0;
v48[2] = 0;
v48[3] = 0;
*v48 = (unsigned int)&off_A2E440;
v49 = v48;
*((_BYTE *)v48 + 16) = 0;
v48[5] = 0;
v48[6] = 0;
v48[7] = 0;
v48[8] = 0;
v48[9] = 0;
v48[10] = 0;
v48[11] = 0;
v48[12] = 0;
v48[13] = 0;
*((_BYTE *)v48 + 56) = 0;
*((_BYTE *)v48 + 57) = 0;
*((_BYTE *)v48 + 58) = 0;
*((_BYTE *)v48 + 59) = 0;
*((_BYTE *)v48 + 60) = 0;
*((_BYTE *)v48 + 61) = 0;
*((_BYTE *)v48 + 62) = 0;
*((_BYTE *)v48 + 63) = 0;
*((_BYTE *)v48 + 108) = 0;
sub_764B1C((int)v48, (int)(a5 + 27));
sub_756A4C(a5[27], v49, v12);
v11 = *(_DWORD *)(v10 + 4 * v12);
}
v13 = *a7;
if ( *(_DWORD *)*a7 == *(_DWORD *)(v11 + 64) )
{
v47 = *(int **)(v11 + 44);
v15 = *((_DWORD *)v13 - 3);
v62 = *(_DWORD *)(v11 + 60);
v56 = v47;
v54 = *(_DWORD *)(v11 + 48);
if ( v15 )
v13 += 4;
}
else
{
v14 = *(int **)(v11 + 36);
v62 = *(_DWORD *)(v11 + 56);
v56 = v14;
v15 = *((_DWORD *)v13 - 3);
v54 = *(_DWORD *)(v11 + 40);
}
v16 = ((*(int (__fastcall **)(void *, int, char *, char *))(*(_DWORD *)v8 + 20))(v8, 4, v13, &v13[4 * v15]) - (int)v13) >> 2;
if ( v16 )
{
v63 = &unk_ACDDB8;
sub_778308(&v63, 2 * v16);
v18 = *(_DWORD *)(v11 + 52);
v19 = v16 - v18;
if ( v16 - v18 > 0 )
{
v19 &= ~(v18 >> 31);
if ( v18 < 0 )
v19 = v16;
if ( *(_DWORD *)(v11 + 12) )
{
sub_777A0C((int)&v63, 0, *(v63 - 3), 2 * v19, 0);
v20 = v63;
if ( (int)*(v63 - 1) >= 0 )
{
sub_777910(&v63);
v20 = v63;
}
v21 = (char *)sub_765A94(
v20,
*(_DWORD *)(v11 + 24),
*(unsigned __int8 **)(v11 + 8),
*(_DWORD *)(v11 + 12),
v13,
&v13[4 * v19]);
v22 = (char *)v63;
v23 = v21;
if ( (int)*(v63 - 1) >= 0 )
{
sub_777910(&v63);
v22 = (char *)v63;
}
v24 = v23 - v22;
v25 = *((_DWORD *)v22 - 3);
v26 = v24 >> 2;
if ( v26 > v25 )
sub_7732C4("%s: __pos (which is %zu) > this->size() (which is %zu)", "basic_string::erase");
sub_7777D0(&v63, v26, v25 - v26, 0);
v18 = *(_DWORD *)(v11 + 52);
}
else
{
sub_777A98(&v63, v13, v19);
v18 = *(_DWORD *)(v11 + 52);
}
}
if ( v18 > 0 )
{
v27 = v63;
v55 = *(_DWORD *)(v11 + 20);
v28 = *(v63 - 3);
v29 = v28 + 1;
if ( (unsigned int)(v28 + 1) > *(v63 - 2) || (int)*(v63 - 1) > 0 )
{
sub_778308(&v63, v28 + 1);
v27 = v63;
v28 = *(v63 - 3);
}
v27[v28] = v55;
if ( v27 - 3 != (_DWORD *)&unk_ACDDAC )
{
*(v27 - 3) = v29;
*(v27 - 1) = 0;
v27[v29] = 0;
}
if ( v19 < 0 )
{
sub_7786CC(&v63, -v19, *(_DWORD *)(v11 + 68));
sub_7785C8(&v63, v13, v16);
}
else
{
sub_7785C8(&v63, &v13[4 * v19], *(_DWORD *)(v11 + 52));
}
}
v30 = a5[3] & 0x200;
v57 = a5[3] & 0xB0;
if ( (a5[3] & 0x200) != 0 )
v30 = *(_DWORD *)(v11 + 32);
v31 = v30 + *(v63 - 3) + v54;
v64 = &unk_ACDDB8;
sub_778308(&v64, 2 * v31);
v32 = a5[2];
v33 = 0;
v34 = (void *)v57;
v35 = v31 < v32;
if ( v57 != 16 )
v35 = 0;
v58 = v35;
v59 = v32 - v31;
do
{
switch ( *((_BYTE *)&v62 + v33) )
{
case 0:
if ( v58 )
goto LABEL_55;
break;
case 1:
if ( v58 )
{
LABEL_55:
sub_7786CC(&v64, v59, c);
}
else
{
v43 = v64;
v46 = *(v64 - 3);
v45 = v46 + 1;
if ( (unsigned int)(v46 + 1) > *(v64 - 2) || (int)*(v64 - 1) > 0 )
{
sub_778308(&v64, v46 + 1);
v43 = v64;
v46 = *(v64 - 3);
}
v43[v46] = c;
v34 = &unk_ACDDAC;
if ( v43 - 3 != (_DWORD *)&unk_ACDDAC )
goto LABEL_53;
}
break;
case 2:
if ( (a5[3] & 0x200) != 0 )
sub_7785C8(&v64, *(_DWORD *)(v11 + 28), *(_DWORD *)(v11 + 32));
break;
case 3:
if ( v54 )
{
v43 = v64;
v60 = *v56;
v44 = *(v64 - 3);
v45 = v44 + 1;
if ( (unsigned int)(v44 + 1) > *(v64 - 2) || (int)*(v64 - 1) > 0 )
{
sub_778308(&v64, v44 + 1);
v43 = v64;
v44 = *(v64 - 3);
}
v43[v44] = v60;
v34 = &unk_ACDDAC;
if ( v43 - 3 != (_DWORD *)&unk_ACDDAC )
{
LABEL_53:
v34 = nullptr;
*(v43 - 3) = v45;
*(v43 - 1) = 0;
v43[v45] = 0;
}
}
break;
case 4:
sub_7783F8(&v64, &v63, v34, *((unsigned __int8 *)&v62 + v33));
break;
default:
break;
}
++v33;
}
while ( v33 != 4 );
if ( v54 > 1 )
sub_7785C8(&v64, v56 + 1, v54 - 1);
v36 = v64;
v37 = *(v64 - 3);
if ( v32 > v37 )
{
if ( v57 == 32 )
sub_7786CC(&v64, v32 - v37, c);
else
sub_777A0C((int)&v64, 0, 0, v32 - v37, c);
v36 = v64;
}
else
{
v32 = *(v64 - 3);
}
if ( !v52 )
{
v38 = (*(int (__fastcall **)(int))(*(_DWORD *)a3 + 48))(a3);
v36 = v64;
v52 = v32 != v38;
}
v39 = v36 - 3;
if ( v36 - 3 != (_DWORD *)&unk_ACDDAC )
{
if ( &pthread_create )
{
v50 = v36 - 1;
__dmb(0xFu);
do
v51 = __ldrex(v50);
while ( __strex(v51 - 1, v50) );
__dmb(0xFu);
}
else
{
v51 = *(v36 - 1);
*(v36 - 1) = v51 - 1;
}
if ( v51 <= 0 )
operator delete(v39);
}
v40 = (char *)(v63 - 3);
if ( v63 - 3 != (_DWORD *)&unk_ACDDAC )
{
if ( &pthread_create )
{
v41 = v63 - 1;
__dmb(0xFu);
do
v42 = __ldrex(v41);
while ( __strex(v42 - 1, v41) );
__dmb(0xFu);
}
else
{
v42 = *(v63 - 1);
*(v63 - 1) = v42 - 1;
}
if ( v42 <= 0 )
operator delete(v40);
}
}
LOBYTE(v61) = v52;
a5[2] = 0;
*a1 = a3;
a1[1] = v61;
return a1;
}