//
// sub_785EB0 from 0x785eb0 to 0x7865a4 (1780 bytes)
//
// 1 xrefs:
// > ??> @ 0xa2ee88
//
// 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 sub_785EB0(int *a1, int a2, int a3, int *a4)
{
int **v8; // r7
int v9; // r0
int *v10; // r5
_BOOL4 v11; // r3
int v12; // r10
int v13; // r2
int *v15; // r0
int *v16; // r0
int *data; // r4
int v18; // r0
int v19; // lr
int v20; // r12
int v21; // r5
int v22; // r1
int v23; // r4
int v24; // r0
int v25; // r1
int *v26; // r3
int v27; // r2
int v28; // r3
int v29; // r4
int v30; // r4
time_t v31; // r0
_DWORD *v32; // r4
int v33; // r4
int rbio; // r4
int info_callback; // r0
int retry_reason; // r0
v8 = (int **)((int (*)(void))BIO_get_data)();
v9 = BIO_next(a1);
v10 = *v8;
v11 = a2 != 109;
if ( *v8 )
v11 = 0;
if ( v11 )
return 0;
v12 = v9;
switch ( a2 )
{
case 1:
SSL_shutdown(*v8);
v27 = v10[1];
v28 = v10[6];
if ( v28 == *(_DWORD *)(v27 + 28) )
{
SSL_set_connect_state(v10);
}
else if ( v28 == *(_DWORD *)(v27 + 24) )
{
SSL_set_accept_state(v10);
}
if ( !SSL_clear(v10) )
return 0;
if ( v12 )
{
v24 = v12;
v13 = a3;
v26 = a4;
v25 = 1;
return BIO_ctrl(v24, v25, v13, v26);
}
v24 = v10[2];
if ( v24 )
{
v13 = a3;
v26 = a4;
v25 = 1;
return BIO_ctrl(v24, v25, v13, v26);
}
LABEL_13:
v13 = 1;
break;
case 3:
case 14:
return 0;
case 6:
if ( !v9 || v10[2] == v9 )
goto LABEL_13;
BIO_up_ref(v9);
SSL_set_bio(v10, v12, v12);
return 1;
case 7:
if ( a1 != a4 )
goto LABEL_13;
SSL_set_bio(*v8, 0, 0);
return 1;
case 8:
return BIO_get_shutdown(a1);
case 9:
BIO_set_shutdown(a1, a3);
return 1;
case 10:
v13 = SSL_pending(*v8);
if ( v13 )
return v13;
v24 = v10[2];
v25 = 10;
v26 = nullptr;
return BIO_ctrl(v24, v25, v13, v26);
case 11:
BIO_clear_flags(a1, 15);
v23 = BIO_ctrl(v10[3], 11, a3, a4);
BIO_copy_next_retry(a1);
return v23;
case 12:
data = (int *)BIO_get_data(a4);
SSL_free(*data);
v18 = SSL_dup(v10);
v19 = (int)v8[1];
v20 = (int)v8[2];
v21 = (int)v8[3];
v22 = (int)v8[4];
data[5] = (int)v8[5];
data[1] = v19;
data[2] = v20;
data[3] = v21;
data[4] = v22;
v13 = v18;
*data = v18;
if ( v18 )
return 1;
return v13;
case 13:
v24 = v10[3];
v13 = a3;
v26 = a4;
v25 = 13;
return BIO_ctrl(v24, v25, v13, v26);
case 15:
info_callback = SSL_get_info_callback(*v8);
v13 = 1;
*a4 = info_callback;
return v13;
case 101:
BIO_clear_flags(a1, 15);
BIO_set_retry_reason(a1, 0);
v29 = SSL_do_handshake(v10);
switch ( SSL_get_error(v10, v29) )
{
case 2:
BIO_set_flags(a1, 9);
v13 = v29;
break;
case 3:
BIO_set_flags(a1, 10);
v13 = v29;
break;
case 4:
BIO_set_flags(a1, 12);
BIO_set_retry_reason(a1, 1);
v13 = v29;
break;
case 7:
BIO_set_flags(a1, 12);
retry_reason = BIO_get_retry_reason(v12);
BIO_set_retry_reason(a1, retry_reason);
v13 = v29;
break;
default:
v13 = v29;
break;
}
return v13;
case 105:
v24 = v10[2];
v13 = a3;
v26 = a4;
v25 = 105;
return BIO_ctrl(v24, v25, v13, v26);
case 109:
if ( !v10 )
goto LABEL_49;
if ( a1 )
{
v32 = (_DWORD *)BIO_get_data(a1);
if ( *v32 )
SSL_shutdown(*v32);
if ( BIO_get_shutdown(a1) )
{
if ( BIO_get_init(a1) )
SSL_free(*v32);
BIO_clear_flags(a1, -1);
BIO_set_init(a1, 0);
}
CRYPTO_free(v32, "ssl/bio_ssl.c", 85);
}
v33 = CRYPTO_zalloc(24, "ssl/bio_ssl.c", 55);
if ( v33 )
{
BIO_set_init(a1, 0);
BIO_set_data(a1, v33);
BIO_clear_flags(a1, -1);
LABEL_49:
BIO_set_shutdown(a1, a3);
*v8 = a4;
rbio = SSL_get_rbio(a4);
if ( rbio )
{
if ( v12 )
BIO_push();
BIO_set_next(a1, rbio);
BIO_up_ref(rbio);
}
BIO_set_init(a1, 1);
return 1;
}
else
{
ERR_put_error(32, 118, 65, "ssl/bio_ssl.c", 58);
return 0;
}
case 110:
if ( !a4 )
return 0;
*a4 = (int)v10;
return 1;
case 119:
v16 = *v8;
if ( a3 )
{
SSL_set_connect_state(v16);
goto LABEL_13;
}
SSL_set_accept_state(v16);
return 1;
case 125:
v15 = v8[2];
if ( a3 >= 512 )
v8[2] = (int *)a3;
return (int)v15;
case 126:
return (int)v8[1];
case 127:
if ( a3 <= 59 )
a3 = 5;
v30 = (int)v8[4];
v8[4] = (int *)a3;
v31 = time(nullptr);
v13 = v30;
v8[5] = (int *)v31;
return v13;
default:
v24 = v10[2];
v25 = a2;
v13 = a3;
v26 = a4;
return BIO_ctrl(v24, v25, v13, v26);
}
return v13;
}