//
// BN_bntest_rand from 0xe1a028 to 0xe1a2c0 (664 bytes)
//
// 1 xrefs:
// > ??> @ 0x79cb0
//
// 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
//
__int64 __fastcall BN_bntest_rand(__int64 a1, int a2, int a3, __int64 a4)
{
char v4; // w20
int v8; // w23
char *v9; // x22
long double v10; // q0
__int64 v11; // x3
__int64 v12; // x19
_BOOL4 v13; // w19
int v14; // w20
char v15; // w1
__int64 v17; // [xsp+68h] [xbp+68h]
unsigned __int8 v18; // [xsp+77h] [xbp+77h] BYREF
time_t timer; // [xsp+78h] [xbp+78h] BYREF
v4 = a2;
if ( a2 )
{
if ( a2 < 0 || a2 == 1 && a3 > 0 )
goto LABEL_4;
v8 = (a2 + 7) >> 3;
v9 = (char *)CRYPTO_malloc((void *)v8, (__int64)"crypto/bn/bn_rand.c", 36);
if ( v9 )
{
time(&timer);
*(_QWORD *)&v10 = 0;
RAND_add((__int64)&timer, 8, v10);
if ( (int)RAND_bytes((__int64)v9, (unsigned int)v8) > 0 )
{
v11 = a4;
if ( v8 <= 0 )
{
LABEL_24:
v14 = (v4 - 1) & 7;
if ( a3 < 0 )
{
v15 = *v9;
}
else if ( a3 )
{
if ( v14 )
{
v15 = (3 << (v14 - 1)) | *v9;
}
else
{
v15 = 1;
v9[1] |= 0x80u;
}
}
else
{
v15 = (1 << v14) | *v9;
}
*v9 = v15 & ~(-1 << (v14 + 1));
if ( (_DWORD)v11 )
v9[v8 - 1] |= 1u;
v13 = BN_bin2bn(v9, v8, a1) != nullptr;
goto LABEL_23;
}
v12 = 0;
while ( 1 )
{
v17 = v11;
if ( (int)RAND_bytes((__int64)&v18, 1) <= 0 )
break;
v11 = v17;
if ( (v18 & 0x80) != 0 )
{
if ( (_DWORD)v12 )
v9[v12] = v9[v12 - 1];
}
else if ( v18 > 0x29u )
{
if ( v18 <= 0x53u )
v9[v12] = -1;
}
else
{
v9[v12] = 0;
}
if ( v8 <= (int)++v12 )
goto LABEL_24;
}
}
v13 = 0;
}
else
{
v13 = 0;
ERR_put_error(3, 127, 65, (__int64)"crypto/bn/bn_rand.c", 38);
}
LABEL_23:
CRYPTO_clear_free(v9, v8, (__int64)"crypto/bn/bn_rand.c", 0x57u);
return v13;
}
if ( a3 != -1 || (_DWORD)a4 )
{
LABEL_4:
ERR_put_error(3, 127, 118, (__int64)"crypto/bn/bn_rand.c", 92);
return 0;
}
BN_set_word(a1, 0);
return 1;
}