#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ RSA;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int BN_num_bits(int /*<<< orphan*/ const *);
int BN_num_bytes(int /*<<< orphan*/ const *);
int /*<<< orphan*/ PEM_F_CHECK_BITLEN_RSA;
int /*<<< orphan*/ PEM_R_UNSUPPORTED_KEY_COMPONENTS;
int /*<<< orphan*/ PEMerr(int /*<<< orphan*/, int /*<<< orphan*/);
int RSA_bits(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_get0_crt_params(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ const **,
                                       int /*<<< orphan*/ const **,
                                       int /*<<< orphan*/ const **);
int /*<<< orphan*/ RSA_get0_factors(int /*<<< orphan*/ *,
                                    int /*<<< orphan*/ const **,
                                    int /*<<< orphan*/ const **);
int /*<<< orphan*/ RSA_get0_key(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ const **,
                                int /*<<< orphan*/ const **);
int RSA_size(int /*<<< orphan*/ *);
int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic) {
  int nbyte, hnbyte, bitlen;
  const BIGNUM *e;
  RSA_get0_key(rsa, NULL, &e, NULL);
  if (BN_num_bits(e) > 32)
    goto badkey;
  bitlen = RSA_bits(rsa);
  nbyte = RSA_size(rsa);
  hnbyte = (bitlen + 15) >> 4;
  if (ispub) {
    *pmagic = 0x31415352L;
    return bitlen;
  } else {
    const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
    *pmagic = 0x32415352L;
    RSA_get0_key(rsa, NULL, NULL, &d);
    if (BN_num_bytes(d) > nbyte)
      goto badkey;
    RSA_get0_factors(rsa, &p, &q);
    RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
    if ((BN_num_bytes(iqmp) > hnbyte) || (BN_num_bytes(p) > hnbyte) ||
        (BN_num_bytes(q) > hnbyte) || (BN_num_bytes(dmp1) > hnbyte) ||
        (BN_num_bytes(dmq1) > hnbyte))
      goto badkey;
  }
  return bitlen;
badkey:
  PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
  return 0;
}


x86 assembly (clang, -O0):
<check_bitlen_rsa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%rdx
mov    %rcx,%rsi
call   1090 <RSA_get0_key@plt>
mov    -0x38(%rbp),%rdi
call   1050 <BN_num_bits@plt>
cmp    $0x20,%eax
jle    11be <check_bitlen_rsa+0x3e>
jmp    12cb <check_bitlen_rsa+0x14b>
mov    -0x10(%rbp),%rdi
call   1080 <RSA_bits@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <RSA_size@plt>
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xf,%eax
sar    $0x4,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1201 <check_bitlen_rsa+0x81>
mov    -0x20(%rbp),%rax
movl   $0x31415352,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e9 <check_bitlen_rsa+0x169>
mov    -0x20(%rbp),%rax
movl   $0x32415352,(%rax)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x40(%rbp),%rcx
mov    %rdx,%rsi
call   1090 <RSA_get0_key@plt>
mov    -0x40(%rbp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    -0x24(%rbp),%eax
jle    1236 <check_bitlen_rsa+0xb6>
jmp    12cb <check_bitlen_rsa+0x14b>
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   10a0 <RSA_get0_factors@plt>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0x68(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1030 <RSA_get0_crt_params@plt>
mov    -0x58(%rbp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    -0x28(%rbp),%eax
jg     12b6 <check_bitlen_rsa+0x136>
mov    -0x48(%rbp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    -0x28(%rbp),%eax
jg     12b6 <check_bitlen_rsa+0x136>
mov    -0x50(%rbp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    -0x28(%rbp),%eax
jg     12b6 <check_bitlen_rsa+0x136>
mov    -0x60(%rbp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    -0x28(%rbp),%eax
jg     12b6 <check_bitlen_rsa+0x136>
mov    -0x68(%rbp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    -0x28(%rbp),%eax
jle    12bb <check_bitlen_rsa+0x13b>
jmp    12cb <check_bitlen_rsa+0x14b>
jmp    12c0 <check_bitlen_rsa+0x140>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e9 <check_bitlen_rsa+0x169>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
call   1060 <PEMerr@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_bitlen_rsa>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r12
mov    %esi,%ebx
mov    %rdi,%rbp
xor    %r15d,%r15d
lea    0x30(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1090 <RSA_get0_key@plt>
mov    0x30(%rsp),%rdi
call   1050 <BN_num_bits@plt>
cmp    $0x20,%eax
jle    11cf <check_bitlen_rsa+0x4f>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1060 <PEMerr@plt>
jmp    11f1 <check_bitlen_rsa+0x71>
mov    %rbp,%rdi
call   1080 <RSA_bits@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1040 <RSA_size@plt>
test   %ebx,%ebx
je     1203 <check_bitlen_rsa+0x83>
movl   $0x31415352,(%r12)
31
mov    %r14d,%r15d
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r13d
movl   $0x32415352,(%r12)
32
lea    0x28(%rsp),%rcx
mov    %rbp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <RSA_get0_key@plt>
mov    0x28(%rsp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    %r13d,%eax
jg     11b6 <check_bitlen_rsa+0x36>
mov    %r14d,%ebx
add    $0xf,%ebx
sar    $0x4,%ebx
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %rbp,%rdi
call   10a0 <RSA_get0_factors@plt>
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
lea    0x10(%rsp),%rcx
mov    %rbp,%rdi
call   1030 <RSA_get0_crt_params@plt>
mov    0x10(%rsp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    %ebx,%eax
jg     11b6 <check_bitlen_rsa+0x36>
mov    0x20(%rsp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    %ebx,%eax
jg     11b6 <check_bitlen_rsa+0x36>
mov    0x18(%rsp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    %ebx,%eax
jg     11b6 <check_bitlen_rsa+0x36>
mov    0x8(%rsp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    %ebx,%eax
jg     11b6 <check_bitlen_rsa+0x36>
mov    (%rsp),%rdi
call   1070 <BN_num_bytes@plt>
cmp    %ebx,%eax
jg     11b6 <check_bitlen_rsa+0x36>
jmp    11ee <check_bitlen_rsa+0x6e>

LLVM IR (-O0):
@PEM_F_CHECK_BITLEN_RSA = global i32 0
@PEM_R_UNSUPPORTED_KEY_COMPONENTS = global i32 0
define i32 @check_bitlen_rsa(i32* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32*
%17 = alloca i32*
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%18 = load i32*, i32** %5
%19 = call i32 @RSA_get0_key(i32* %18, i32* null, i32** %11, i32** null)
%20 = load i32*, i32** %11
%21 = call i32 @BN_num_bits(i32* %20)
%22 = icmp sgt i32 %21, 32
br i1 %22, label %23, label %24
23: ; preds = %3
br label %79
24: ; preds = %3
%25 = load i32*, i32** %5
%26 = call i32 @RSA_bits(i32* %25)
store i32 %26, i32* %10
%27 = load i32*, i32** %5
%28 = call i32 @RSA_size(i32* %27)
store i32 %28, i32* %8
%29 = load i32, i32* %10
%30 = add nsw i32 %29, 15
%31 = ashr i32 %30, 4
store i32 %31, i32* %9
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = load i32*, i32** %7
store i32 826364754, i32* %35
%36 = load i32, i32* %10
store i32 %36, i32* %4
br label %83
37: ; preds = %24
%38 = load i32*, i32** %7
store i32 843141970, i32* %38
%39 = load i32*, i32** %5
%40 = call i32 @RSA_get0_key(i32* %39, i32* null, i32** null, i32** %12)
%41 = load i32*, i32** %12
%42 = call i32 @BN_num_bytes(i32* %41)
%43 = load i32, i32* %8
%44 = icmp sgt i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %37
br label %79
46: ; preds = %37
%47 = load i32*, i32** %5
%48 = call i32 @RSA_get0_factors(i32* %47, i32** %13, i32** %14)
%49 = load i32*, i32** %5
%50 = call i32 @RSA_get0_crt_params(i32* %49, i32** %16, i32** %17, i32** %15)
%51 = load i32*, i32** %15
%52 = call i32 @BN_num_bytes(i32* %51)
%53 = load i32, i32* %9
%54 = icmp sgt i32 %52, %53
br i1 %54, label %75, label %55
55: ; preds = %46
%56 = load i32*, i32** %13
%57 = call i32 @BN_num_bytes(i32* %56)
%58 = load i32, i32* %9
%59 = icmp sgt i32 %57, %58
br i1 %59, label %75, label %60
60: ; preds = %55
%61 = load i32*, i32** %14
%62 = call i32 @BN_num_bytes(i32* %61)
%63 = load i32, i32* %9
%64 = icmp sgt i32 %62, %63
br i1 %64, label %75, label %65
65: ; preds = %60
%66 = load i32*, i32** %16
%67 = call i32 @BN_num_bytes(i32* %66)
%68 = load i32, i32* %9
%69 = icmp sgt i32 %67, %68
br i1 %69, label %75, label %70
70: ; preds = %65
%71 = load i32*, i32** %17
%72 = call i32 @BN_num_bytes(i32* %71)
%73 = load i32, i32* %9
%74 = icmp sgt i32 %72, %73
br i1 %74, label %75, label %76
75: ; preds = %70, %65, %60, %55, %46
br label %79
76: ; preds = %70
br label %77
77: ; preds = %76
%78 = load i32, i32* %10
store i32 %78, i32* %4
br label %83
79: ; preds = %75, %45, %23
%80 = load i32, i32* @PEM_F_CHECK_BITLEN_RSA
%81 = load i32, i32* @PEM_R_UNSUPPORTED_KEY_COMPONENTS
%82 = call i32 @PEMerr(i32 %80, i32 %81)
store i32 0, i32* %4
br label %83
83: ; preds = %79, %77, %34
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
@PEM_F_CHECK_BITLEN_RSA = global i32 0
@PEM_R_UNSUPPORTED_KEY_COMPONENTS = global i32 0
define i32 @check_bitlen_rsa(i32* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = bitcast i32** %4 to i8*
%12 = call i32 @RSA_get0_key(i32* %0, i32* null, i32** %4, i32** null)
%13 = load i32*, i32** %4
%14 = call i32 @BN_num_bits(i32* %13)
%15 = icmp sgt i32 %14, 32
br i1 %15, label %58, label %16
16: ; preds = %3
%17 = call i32 @RSA_bits(i32* %0)
%18 = call i32 @RSA_size(i32* %0)
%19 = add nsw i32 %17, 15
%20 = ashr i32 %19, 4
%21 = icmp eq i32 %1, 0
br i1 %21, label %23, label %22
22: ; preds = %16
store i32 826364754, i32* %2
br label %62
23: ; preds = %16
%24 = bitcast i32** %5 to i8*
%25 = bitcast i32** %6 to i8*
%26 = bitcast i32** %7 to i8*
%27 = bitcast i32** %8 to i8*
%28 = bitcast i32** %9 to i8*
%29 = bitcast i32** %10 to i8*
store i32 843141970, i32* %2
%30 = call i32 @RSA_get0_key(i32* %0, i32* null, i32** null, i32** %5)
%31 = load i32*, i32** %5
%32 = call i32 @BN_num_bytes(i32* %31)
%33 = icmp sgt i32 %32, %18
br i1 %33, label %56, label %34
34: ; preds = %23
%35 = call i32 @RSA_get0_factors(i32* %0, i32** %6, i32** %7)
%36 = call i32 @RSA_get0_crt_params(i32* %0, i32** %9, i32** %10, i32** %8)
%37 = load i32*, i32** %8
%38 = call i32 @BN_num_bytes(i32* %37)
%39 = icmp sgt i32 %38, %20
br i1 %39, label %56, label %40
40: ; preds = %34
%41 = load i32*, i32** %6
%42 = call i32 @BN_num_bytes(i32* %41)
%43 = icmp sgt i32 %42, %20
br i1 %43, label %56, label %44
44: ; preds = %40
%45 = load i32*, i32** %7
%46 = call i32 @BN_num_bytes(i32* %45)
%47 = icmp sgt i32 %46, %20
br i1 %47, label %56, label %48
48: ; preds = %44
%49 = load i32*, i32** %9
%50 = call i32 @BN_num_bytes(i32* %49)
%51 = icmp sgt i32 %50, %20
br i1 %51, label %56, label %52
52: ; preds = %48
%53 = load i32*, i32** %10
%54 = call i32 @BN_num_bytes(i32* %53)
%55 = icmp sgt i32 %54, %20
br i1 %55, label %56, label %57
56: ; preds = %23, %48, %44, %40, %34, %52
br label %58
57: ; preds = %52
br label %62
58: ; preds = %56, %3
%59 = load i32, i32* @PEM_F_CHECK_BITLEN_RSA
%60 = load i32, i32* @PEM_R_UNSUPPORTED_KEY_COMPONENTS
%61 = call i32 @PEMerr(i32 %59, i32 %60)
br label %62
62: ; preds = %57, %58, %22
%63 = phi i32 [ 0, %58 ], [ %17, %22 ], [ %17, %57 ]
ret i32 %63
}