//
// int_bn_mod_inverse from 0x86f0e8 to 0x86fa44 (2396 bytes)
//
// 3 xrefs:
// > BN_BLINDING_create_param @ 0x86bbd0
// > BN_mod_inverse @ 0x86fa5c
// > ??> @ 0x69cbc
//
// 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 int_bn_mod_inverse(int a1, int a2, int *a3, int ***a4, int *a5)
{
int *v7; // r4
int *v8; // r5
int **v9; // r7
int **v10; // r9
int **v11; // r6
int v12; // r10
int v13; // r0
int *v14; // r3
int **v15; // r1
int *v17; // r4
int *v18; // r5
int **v19; // r6
int **v20; // r9
int **v21; // r10
int *v22; // r8
int v23; // r7
int *v24; // r3
int v25; // r11
int v26; // r11
int v27; // r7
int *v28; // r3
int v29; // r7
int *v30; // r3
int *v31; // r3
int v32; // [sp+0h] [bp-3Ch]
int **v33; // [sp+Ch] [bp-30h]
int **v34; // [sp+Ch] [bp-30h]
int ***v35; // [sp+Ch] [bp-30h]
int v37; // [sp+14h] [bp-28h]
int *v38; // [sp+18h] [bp-24h]
int **v39; // [sp+1Ch] [bp-20h]
int v40[6]; // [sp+24h] [bp-18h] BYREF
v38 = a3;
if ( a5 )
a3 = a5;
if ( a5 )
*a3 = 0;
if ( BN_get_flags(a2, 4) || BN_get_flags((int)v38, 4) )
{
BN_CTX_start((int)a4);
v7 = (int *)BN_CTX_get(a4);
v8 = (int *)BN_CTX_get(a4);
v9 = BN_CTX_get(a4);
v33 = BN_CTX_get(a4);
v10 = BN_CTX_get(a4);
v11 = BN_CTX_get(a4);
if ( !BN_CTX_get(a4) )
{
v37 = 0;
goto LABEL_25;
}
if ( a1 )
{
BN_set_word(v9, 1);
BN_set_word(v11, 0);
if ( !BN_copy((int)v8, a2) || !BN_copy((int)v7, (int)v38) )
goto LABEL_26;
v37 = a1;
}
else
{
v37 = BN_new();
if ( !v37 )
goto LABEL_31;
BN_set_word(v9, 1);
BN_set_word(v11, 0);
if ( !BN_copy((int)v8, a2) || !BN_copy((int)v7, (int)v38) )
goto LABEL_31;
}
v7[3] = 0;
if ( !v8[3] && BN_ucmp(v8, v7) < 0 || (bn_init(v40), BN_with_flags(v40, v8, 4), BN_nnmod(v8, v40, v7, a4, v32)) )
{
v12 = -1;
while ( !BN_is_zero((int)v8) )
{
bn_init(v40);
BN_with_flags(v40, v7, 4);
v12 = -v12;
if ( BN_div(v33, (int)v10, v40, v8, a4) )
{
if ( BN_mul((int **)v7, v33, v9, (int)a4) )
{
v13 = BN_add(v7, v7, (int *)v11);
v14 = v7;
v7 = v8;
v8 = (int *)v10;
v10 = v11;
v11 = v9;
v9 = (int **)v14;
if ( v13 )
continue;
}
}
goto LABEL_25;
}
if ( v12 != -1 || BN_sub((int *)v11, v38, (int *)v11) )
{
if ( !BN_is_one((int)v7) )
{
ERR_put_error(3, 139, 108, (int)"crypto/bn/bn_gcd.c", 610);
goto LABEL_25;
}
if ( v11[3] || BN_ucmp((int *)v11, v38) >= 0 )
{
v15 = v11;
goto LABEL_24;
}
if ( BN_copy(v37, (int)v11) )
goto LABEL_27;
}
}
LABEL_25:
if ( a1 )
{
LABEL_26:
v37 = 0;
goto LABEL_27;
}
LABEL_31:
BN_free(v37);
goto LABEL_26;
}
BN_CTX_start((int)a4);
v17 = (int *)BN_CTX_get(a4);
v18 = (int *)BN_CTX_get(a4);
v19 = BN_CTX_get(a4);
v20 = BN_CTX_get(a4);
v34 = BN_CTX_get(a4);
v21 = BN_CTX_get(a4);
v39 = BN_CTX_get(a4);
if ( !v39 )
{
v37 = 0;
goto LABEL_25;
}
if ( a1 )
{
BN_set_word(v19, 1);
BN_set_word(v21, 0);
if ( !BN_copy((int)v18, a2) || !BN_copy((int)v17, (int)v38) )
goto LABEL_26;
v37 = a1;
}
else
{
v37 = BN_new();
if ( !v37 )
goto LABEL_31;
BN_set_word(v19, 1);
BN_set_word(v21, 0);
if ( !BN_copy((int)v18, a2) || !BN_copy((int)v17, (int)v38) )
goto LABEL_31;
}
v17[3] = 0;
if ( (v18[3] || BN_ucmp(v18, v17) >= 0) && !BN_nnmod(v18, v18, v17, a4, v32) )
goto LABEL_25;
if ( BN_is_odd((int)v38) && BN_num_bits(v38) <= 2048 )
{
while ( !BN_is_zero((int)v18) )
{
v27 = 0;
while ( !BN_is_bit_set(v18, v27) )
{
++v27;
if ( BN_is_odd((int)v19) && !BN_uadd((int *)v19, (int *)v19, v38, v28) || !BN_rshift1((int)v19, (int *)v19) )
goto LABEL_25;
}
if ( v27 && !BN_rshift((int)v18, v18, v27) )
goto LABEL_25;
v29 = 0;
while ( !BN_is_bit_set(v17, v29) )
{
++v29;
if ( BN_is_odd((int)v21) && !BN_uadd((int *)v21, (int *)v21, v38, v30) || !BN_rshift1((int)v21, (int *)v21) )
goto LABEL_25;
}
if ( v29 && !BN_rshift((int)v17, v17, v29) )
goto LABEL_25;
if ( BN_ucmp(v18, v17) < 0 )
{
if ( !BN_uadd((int *)v21, (int *)v21, (int *)v19, v31) || !BN_usub(v17, v17, v18) )
goto LABEL_25;
}
else if ( !BN_uadd((int *)v19, (int *)v19, (int *)v21, v31) || !BN_usub(v18, v18, v17) )
{
goto LABEL_25;
}
}
goto LABEL_118;
}
v22 = (int *)v34;
v23 = -1;
v35 = a4;
while ( !BN_is_zero((int)v18) )
{
v25 = BN_num_bits(v17);
if ( v25 == BN_num_bits(v18) )
goto LABEL_127;
v26 = BN_num_bits(v17);
if ( v26 != BN_num_bits(v18) + 1 )
{
if ( !BN_div(v20, (int)v22, v17, v18, v35) )
goto LABEL_51;
goto LABEL_47;
}
if ( !BN_lshift1(v39, (int **)v18) )
goto LABEL_51;
if ( BN_ucmp(v17, (int *)v39) < 0 )
{
LABEL_127:
if ( !BN_set_word(v20, 1) || !BN_sub(v22, v17, v18) )
{
LABEL_51:
a4 = v35;
goto LABEL_25;
}
}
else
{
if ( !BN_sub(v22, v17, (int *)v39) || !BN_add((int *)v20, (int *)v39, v18) )
goto LABEL_51;
if ( BN_ucmp(v17, (int *)v20) < 0 )
{
if ( !BN_set_word(v20, 2) )
goto LABEL_51;
}
else if ( !BN_set_word(v20, 3) || !BN_sub(v22, v22, v18) )
{
goto LABEL_51;
}
}
LABEL_47:
if ( BN_is_one((int)v20) )
{
if ( !BN_add(v17, (int *)v19, (int *)v21) )
goto LABEL_51;
}
else
{
if ( BN_is_word((int)v20, 2) )
{
if ( !BN_lshift1((int **)v17, v19) )
goto LABEL_51;
}
else if ( BN_is_word((int)v20, 4) )
{
if ( !BN_lshift(v17, (int *)v19, 2) )
goto LABEL_51;
}
else if ( v20[1] == (int *)((char *)&dword_0 + 1) )
{
if ( !BN_copy((int)v17, (int)v19) || !BN_mul_word((int **)v17, **v20) )
goto LABEL_51;
}
else if ( !BN_mul((int **)v17, v20, v19, (int)v35) )
{
goto LABEL_51;
}
if ( !BN_add(v17, v17, (int *)v21) )
goto LABEL_51;
}
v24 = v17;
v23 = -v23;
v17 = v18;
v18 = v22;
v22 = (int *)v21;
v21 = v19;
v19 = (int **)v24;
}
a4 = v35;
if ( v23 != -1 )
goto LABEL_110;
LABEL_118:
if ( !BN_sub((int *)v21, v38, (int *)v21) )
goto LABEL_25;
LABEL_110:
if ( !BN_is_one((int)v17) )
{
if ( a5 )
*a5 = 1;
goto LABEL_25;
}
if ( v21[3] || BN_ucmp((int *)v21, v38) >= 0 )
{
v15 = v21;
LABEL_24:
if ( !BN_nnmod(v37, v15, v38, a4, v32) )
goto LABEL_25;
}
else if ( !BN_copy(v37, (int)v21) )
{
goto LABEL_25;
}
LABEL_27:
BN_CTX_end(a4);
return v37;
}