#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 uint64_t;
typedef int /*<<< orphan*/ RSA;
typedef int /*<<< orphan*/ const BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ const *BN_dup(int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ const *);
int BN_get_word(int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_mask_bits(int /*<<< orphan*/ const *, int);
int BN_num_bits(int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_rshift(int /*<<< orphan*/ const *,
                             int /*<<< orphan*/ const *, int);
int EINVAL;
int /*<<< orphan*/ RSA_get0_key(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ const **,
                                int /*<<< orphan*/ *);
int rsa_get_exponent(RSA *key, uint64_t *e) {
  int ret;
  BIGNUM *bn_te;
  const BIGNUM *key_e;
  uint64_t te;
  ret = -EINVAL;
  bn_te = NULL;
  if (!e)
    goto cleanup;
  RSA_get0_key(key, NULL, &key_e, NULL);
  if (BN_num_bits(key_e) > 64)
    goto cleanup;
  *e = BN_get_word(key_e);
  if (BN_num_bits(key_e) < 33) {
    ret = 0;
    goto cleanup;
  }
  bn_te = BN_dup(key_e);
  if (!bn_te)
    goto cleanup;
  if (!BN_rshift(bn_te, bn_te, 32))
    goto cleanup;
  if (!BN_mask_bits(bn_te, 32))
    goto cleanup;
  te = BN_get_word(bn_te);
  te <<= 32;
  *e |= te;
  ret = 0;
cleanup:
  if (bn_te)
    BN_free(bn_te);
  return ret;
}


x86 assembly (clang, -O0):
<rsa_get_exponent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    0x2e57(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <rsa_get_exponent+0x36>
jmp    1284 <rsa_get_exponent+0x114>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%rdx
mov    %rcx,%rsi
call   1080 <RSA_get0_key@plt>
mov    -0x28(%rbp),%rdi
call   1050 <BN_num_bits@plt>
cmp    $0x40,%eax
jle    11d1 <rsa_get_exponent+0x61>
jmp    1284 <rsa_get_exponent+0x114>
mov    -0x28(%rbp),%rdi
call   1040 <BN_get_word@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <BN_num_bits@plt>
cmp    $0x21,%eax
jge    1200 <rsa_get_exponent+0x90>
movl   $0x0,-0x14(%rbp)
jmp    1284 <rsa_get_exponent+0x114>
mov    -0x28(%rbp),%rdi
call   1060 <BN_dup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121d <rsa_get_exponent+0xad>
jmp    1284 <rsa_get_exponent+0x114>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x20,%edx
call   1090 <BN_rshift@plt>
cmp    $0x0,%eax
jne    123d <rsa_get_exponent+0xcd>
jmp    1284 <rsa_get_exponent+0x114>
mov    -0x20(%rbp),%rdi
mov    $0x20,%esi
call   1030 <BN_mask_bits@plt>
cmp    $0x0,%eax
jne    1259 <rsa_get_exponent+0xe9>
jmp    1284 <rsa_get_exponent+0x114>
mov    -0x20(%rbp),%rdi
call   1040 <BN_get_word@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1298 <rsa_get_exponent+0x128>
mov    -0x20(%rbp),%rdi
call   1070 <BN_free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_get_exponent>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e61(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
test   %rsi,%rsi
je     1213 <rsa_get_exponent+0xa3>
mov    %rsi,%rbx
xor    %r14d,%r14d
lea    0x8(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1080 <RSA_get0_key@plt>
mov    0x8(%rsp),%rdi
call   1050 <BN_num_bits@plt>
cmp    $0x40,%eax
jg     1213 <rsa_get_exponent+0xa3>
mov    0x8(%rsp),%rdi
call   1040 <BN_get_word@plt>
mov    %eax,(%rbx)
mov    0x8(%rsp),%rdi
call   1050 <BN_num_bits@plt>
cmp    $0x21,%eax
jl     1216 <rsa_get_exponent+0xa6>
mov    0x8(%rsp),%rdi
call   1060 <BN_dup@plt>
test   %rax,%rax
je     1213 <rsa_get_exponent+0xa3>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rax,%rsi
mov    $0x20,%edx
call   1090 <BN_rshift@plt>
test   %eax,%eax
je     120b <rsa_get_exponent+0x9b>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1030 <BN_mask_bits@plt>
test   %eax,%eax
je     120b <rsa_get_exponent+0x9b>
mov    %rbx,%rdi
call   1040 <BN_get_word@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1070 <BN_free@plt>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
define i32 @rsa_get_exponent(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
store i32 %10, i32* %5
store i32* null, i32** %6
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
br label %55
14: ; preds = %2
%15 = load i32*, i32** %3
%16 = call i32 @RSA_get0_key(i32* %15, i32* null, i32** %7, i32* null)
%17 = load i32*, i32** %7
%18 = call i32 @BN_num_bits(i32* %17)
%19 = icmp sgt i32 %18, 64
br i1 %19, label %20, label %21
20: ; preds = %14
br label %55
21: ; preds = %14
%22 = load i32*, i32** %7
%23 = call i32 @BN_get_word(i32* %22)
%24 = load i32*, i32** %4
store i32 %23, i32* %24
%25 = load i32*, i32** %7
%26 = call i32 @BN_num_bits(i32* %25)
%27 = icmp slt i32 %26, 33
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 0, i32* %5
br label %55
29: ; preds = %21
%30 = load i32*, i32** %7
%31 = call i32* @BN_dup(i32* %30)
store i32* %31, i32** %6
%32 = load i32*, i32** %6
%33 = icmp ne i32* %32, null
br i1 %33, label %35, label %34
34: ; preds = %29
br label %55
35: ; preds = %29
%36 = load i32*, i32** %6
%37 = load i32*, i32** %6
%38 = call i32 @BN_rshift(i32* %36, i32* %37, i32 32)
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %35
br label %55
41: ; preds = %35
%42 = load i32*, i32** %6
%43 = call i32 @BN_mask_bits(i32* %42, i32 32)
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %41
br label %55
46: ; preds = %41
%47 = load i32*, i32** %6
%48 = call i32 @BN_get_word(i32* %47)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = shl i32 %49, 32
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = load i32*, i32** %4
%53 = load i32, i32* %52
%54 = or i32 %53, %51
store i32 %54, i32* %52
store i32 0, i32* %5
br label %55
55: ; preds = %46, %45, %40, %34, %28, %20, %13
%56 = load i32*, i32** %6
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i32*, i32** %6
%60 = call i32 @BN_free(i32* %59)
br label %61
61: ; preds = %58, %55
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
@EINVAL = global i32 0
define i32 @rsa_get_exponent(i32* %0, i32* writeonly %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
%5 = load i32, i32* @EINVAL
%6 = sub nsw i32 0, %5
%7 = icmp eq i32* %1, null
br i1 %7, label %34, label %8
8: ; preds = %2
%9 = call i32 @RSA_get0_key(i32* %0, i32* null, i32** %3, i32* null)
%10 = load i32*, i32** %3
%11 = call i32 @BN_num_bits(i32* %10)
%12 = icmp sgt i32 %11, 64
br i1 %12, label %34, label %13
13: ; preds = %8
%14 = load i32*, i32** %3
%15 = call i32 @BN_get_word(i32* %14)
store i32 %15, i32* %1
%16 = load i32*, i32** %3
%17 = call i32 @BN_num_bits(i32* %16)
%18 = icmp slt i32 %17, 33
br i1 %18, label %34, label %19
19: ; preds = %13
%20 = load i32*, i32** %3
%21 = call i32* @BN_dup(i32* %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %34, label %23
23: ; preds = %19
%24 = call i32 @BN_rshift(i32* %21, i32* %21, i32 32)
%25 = icmp eq i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = call i32 @BN_mask_bits(i32* %21, i32 32)
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @BN_get_word(i32* %21)
br label %31
31: ; preds = %29, %23, %26
%32 = phi i32 [ 0, %29 ], [ %6, %26 ], [ %6, %23 ]
%33 = call i32 @BN_free(i32* %21)
br label %34
34: ; preds = %13, %2, %19, %8, %31
%35 = phi i32 [ %32, %31 ], [ 0, %13 ], [ %6, %2 ], [ %6, %19 ], [ %6, %8 ]
ret i32 %35
}