//
// sub_8165B4 from 0x8165b4 to 0x8167da (550 bytes)
//
// 4 xrefs:
// > sub_8165B4 @ 0x81675c
// > sub_8165B4 @ 0x816784
// > pugi::xpath_node_set::sort @ 0x816bec
// > sub_816CF0 @ 0x816db4
//
// This function is hookable on this platform (idp: arm, 32 bit: True, 64 bit: False!
//
// Using BromaIDA 8.3.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit 1c6ec395 at Thu Nov 26 21:29:22 2026 from https://github.com/geode-sdk/bindings
// Generated at Fri, 27 Nov 2026 12:01:42 +0000
//
void (*__fastcall sub_8165B4(void (*result)(), int *a2, int a3))()
{
int *v3; // r6
int v5; // r11
int *v6; // r7
int v7; // r5
int *v8; // r4
int *v9; // r0
int *v10; // r1
int *v11; // r2
int v12; // r5
int v13; // r2
int *v14; // r5
int *i; // r5
int v16; // r1
void (**k)(); // r7
int *j; // r3
int v19; // r1
int v20; // r1
void (*v21)(); // r1
void (*v22)(); // r1
int *v23; // r9
int *v24; // r7
int *v25; // r3
int *v26; // r1
int *v27; // r0
void (*v28)(); // [sp+Ch] [bp-10h] BYREF
int v29; // [sp+10h] [bp-Ch]
v3 = (int *)result;
v5 = a3;
while ( (char *)a2 - (char *)v3 >= 264 )
{
v6 = a2 - 2;
v7 = (char *)(a2 - 2) - (char *)v3;
v8 = &v3[2 * (((char *)a2 - (char *)v3) >> 4)];
if ( v7 >= 328 )
{
v12 = ((v7 >> 3) + 1) >> 3;
sub_8163B4(v3, &v3[2 * v12], &v3[2 * v12 + 2 * v12]);
sub_8163B4(&v8[-2 * v12], &v3[2 * (((char *)a2 - (char *)v3) >> 4)], &v8[2 * v12]);
sub_8163B4(&v6[-4 * v12], &v6[-2 * v12], a2 - 2);
v10 = &v3[2 * (((char *)a2 - (char *)v3) >> 4)];
v11 = &v6[-2 * v12];
v9 = &v3[2 * v12];
}
else
{
v9 = v3;
v10 = &v3[2 * (((char *)a2 - (char *)v3) >> 4)];
v11 = a2 - 2;
}
sub_8163B4(v9, v10, v11);
v14 = v8 + 2;
while ( v8 != v3 && pugi::xpath_node::operator==(v8 - 2, v8) )
v8 -= 2;
while ( v14 != a2 && pugi::xpath_node::operator==(v14, v8) )
v14 += 2;
v23 = v8;
v24 = v14;
while ( 1 )
{
LABEL_28:
if ( v14 == a2 )
goto LABEL_34;
if ( !sub_816170((void (**)())v8, (void (**)())v14, v13) )
break;
LABEL_32:
v14 += 2;
}
if ( pugi::xpath_node::operator==(v14, v8) )
{
sub_80FC3C(v14, v24);
v24 += 2;
goto LABEL_32;
}
LABEL_34:
while ( v23 != v3 )
{
v23 -= 2;
if ( !sub_816170((void (**)())v23, (void (**)())v8, v13) )
{
if ( !pugi::xpath_node::operator==(v8, v23) )
{
if ( v14 == a2 )
{
v8 -= 2;
if ( v23 != v8 )
sub_80FC3C(v23, v8);
v24 -= 2;
sub_80FC3C(v8, v24);
}
else
{
sub_80FC3C(v14, v23);
v14 += 2;
}
goto LABEL_28;
}
v8 -= 2;
sub_80FC3C(v23, v8);
}
}
v25 = v8;
if ( v14 != a2 )
{
if ( v24 != v14 )
{
sub_80FC3C(v8, v24);
v25 = v8;
}
sub_80FC3C(v14, v25);
v13 = (int)(v14 + 2);
v24 += 2;
v8 += 2;
v14 += 2;
goto LABEL_28;
}
if ( ((char *)a2 - (char *)v24) >> 3 >= ((char *)v8 - (char *)v3) >> 3 )
{
v27 = v3;
v3 = v24;
result = (void (*)())sub_8165B4(v27, v8, v5);
}
else
{
v26 = a2;
a2 = v8;
result = (void (*)())sub_8165B4(v24, v26, v5);
}
}
if ( v3 != a2 )
{
for ( i = v3 + 2; i != a2; i += 2 )
{
v16 = i[1];
v28 = (void (*)())*i;
v29 = v16;
if ( sub_816170(&v28, (void (**)())v3, a3) )
{
for ( j = i; v3 != j; j[3] = v19 )
{
j -= 2;
a3 = (int)(j + 2);
v19 = j[1];
j[2] = *j;
}
result = v28;
v20 = v29;
*v3 = (int)v28;
v3[1] = v20;
}
else
{
for ( k = (void (**)())i; sub_816170(&v28, k - 2, a3); k -= 2 )
{
v21 = *(k - 1);
*k = *(k - 2);
k[1] = v21;
}
result = v28;
v22 = (void (*)())v29;
*k = v28;
k[1] = v22;
}
}
}
return result;
}