//
// BN_div_recp from 0x871354 to 0x8715b4 (608 bytes)
//
// 2 xrefs:
// > BN_mod_mul_reciprocal @ 0x871628
// > ??> @ 0x69e3c
//
// 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
//
int __fastcall BN_div_recp(int **a1, int **a2, int *a3, int a4, int ***a5)
{
int **v9; // r4
int **v10; // r7
bool v11; // zf
bool v12; // zf
int v13; // r4
int ***v14; // r0
int v16; // r0
int v17; // r1
int v18; // r10
int *v19; // r3
BN_CTX_start((int)a5);
v9 = BN_CTX_get(a5);
v10 = BN_CTX_get(a5);
if ( !a1 )
a1 = BN_CTX_get(a5);
if ( !a2 )
a2 = BN_CTX_get(a5);
v11 = v10 == nullptr;
if ( v10 )
v11 = v9 == nullptr;
if ( v11 )
goto LABEL_12;
v12 = a2 == nullptr;
if ( a2 )
v12 = a1 == nullptr;
if ( v12 )
goto LABEL_12;
if ( BN_ucmp(a3, (int *)a4) >= 0 )
{
v16 = BN_num_bits(a3);
v17 = *(_DWORD *)(a4 + 44);
if ( v16 < 2 * *(_DWORD *)(a4 + 40) )
v18 = 2 * *(_DWORD *)(a4 + 40);
else
v18 = v16;
if ( v17 != v18 )
{
v17 = BN_reciprocal((int **)(a4 + 20), (int *)a4, v18, a5);
*(_DWORD *)(a4 + 44) = v17;
}
if ( v17 != -1 )
{
if ( BN_rshift((int)v9, a3, *(_DWORD *)(a4 + 40)) )
{
if ( BN_mul(v10, v9, (int **)(a4 + 20), (int)a5) )
{
if ( BN_rshift((int)a1, (int *)v10, v18 - *(_DWORD *)(a4 + 40)) )
{
a1[3] = nullptr;
if ( BN_mul(v10, (int **)a4, a1, (int)a5) )
{
if ( BN_usub(a2, a3, v10) )
{
a2[3] = nullptr;
v13 = 4;
while ( BN_ucmp((int *)a2, (int *)a4) >= 0 )
{
if ( !--v13 )
{
ERR_put_error(3, 130, 101, (int)"crypto/bn/bn_recp.c", 154);
goto LABEL_13;
}
if ( !BN_usub(a2, (int *)a2, (_DWORD *)a4) || !BN_add_word((int)a1, 1u) )
goto LABEL_12;
}
v13 = 1;
if ( BN_is_zero((int)a2) )
v19 = nullptr;
else
v19 = (int *)a3[3];
a2[3] = v19;
a1[3] = (int *)(a3[3] ^ *(_DWORD *)(a4 + 12));
goto LABEL_13;
}
}
}
}
}
}
LABEL_12:
v13 = 0;
LABEL_13:
v14 = a5;
LABEL_14:
BN_CTX_end(v14);
return v13;
}
BN_set_word(a1, 0);
v13 = BN_copy((int)a2, (int)a3);
v14 = a5;
if ( !v13 )
goto LABEL_14;
BN_CTX_end(a5);
return 1;
}