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

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_end(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_get(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_CTX_start(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_mod_inverse(int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mul(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_sub(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ BN_value_one();
BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, const BIGNUM *q,
                           BN_CTX *ctx) {
  BIGNUM *ret = NULL, *r0, *r1, *r2;
  if (d == NULL || p == NULL || q == NULL)
    return NULL;
  BN_CTX_start(ctx);
  if ((r0 = BN_CTX_get(ctx)) == NULL)
    goto err;
  if ((r1 = BN_CTX_get(ctx)) == NULL)
    goto err;
  if ((r2 = BN_CTX_get(ctx)) == NULL)
    goto err;
  if (!BN_sub(r1, p, BN_value_one()))
    goto err;
  if (!BN_sub(r2, q, BN_value_one()))
    goto err;
  if (!BN_mul(r0, r1, r2, ctx))
    goto err;
  ret = BN_mod_inverse(NULL, d, r0, ctx);
err:
  BN_CTX_end(ctx);
  return ret;
}


x86 assembly (clang, -O0):
<rsa_get_public_exp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b1 <rsa_get_public_exp+0x41>
cmpq   $0x0,-0x18(%rbp)
je     11b1 <rsa_get_public_exp+0x41>
cmpq   $0x0,-0x20(%rbp)
jne    11be <rsa_get_public_exp+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <rsa_get_public_exp+0x160>
mov    -0x28(%rbp),%rdi
call   1090 <BN_CTX_start@plt>
mov    -0x28(%rbp),%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11e3 <rsa_get_public_exp+0x73>
jmp    12bf <rsa_get_public_exp+0x14f>
mov    -0x28(%rbp),%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11ff <rsa_get_public_exp+0x8f>
jmp    12bf <rsa_get_public_exp+0x14f>
mov    -0x28(%rbp),%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    121b <rsa_get_public_exp+0xab>
jmp    12bf <rsa_get_public_exp+0x14f>
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <BN_value_one@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1060 <BN_sub@plt>
cmp    $0x0,%eax
jne    124f <rsa_get_public_exp+0xdf>
jmp    12bf <rsa_get_public_exp+0x14f>
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1030 <BN_value_one@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1060 <BN_sub@plt>
cmp    $0x0,%eax
jne    1283 <rsa_get_public_exp+0x113>
jmp    12bf <rsa_get_public_exp+0x14f>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1070 <BN_mul@plt>
cmp    $0x0,%eax
jne    12a6 <rsa_get_public_exp+0x136>
jmp    12bf <rsa_get_public_exp+0x14f>
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
call   1050 <BN_mod_inverse@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <BN_CTX_end@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_get_public_exp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %ebx,%ebx
test   %rdi,%rdi
je     1265 <rsa_get_public_exp+0xf5>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1265 <rsa_get_public_exp+0xf5>
mov    %rdx,%r13
test   %rdx,%rdx
je     1265 <rsa_get_public_exp+0xf5>
mov    %rcx,%r15
mov    %rdi,%r14
mov    %rcx,%rdi
call   1090 <BN_CTX_start@plt>
mov    %r15,%rdi
call   1040 <BN_CTX_get@plt>
test   %rax,%rax
je     125b <rsa_get_public_exp+0xeb>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <BN_CTX_get@plt>
test   %rax,%rax
je     125b <rsa_get_public_exp+0xeb>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     125b <rsa_get_public_exp+0xeb>
mov    %rbx,(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <BN_value_one@plt>
mov    %r12,0x8(%rsp)
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1060 <BN_sub@plt>
test   %eax,%eax
je     125d <rsa_get_public_exp+0xed>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <BN_value_one@plt>
mov    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1060 <BN_sub@plt>
test   %eax,%eax
je     125d <rsa_get_public_exp+0xed>
mov    (%rsp),%rbx
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1070 <BN_mul@plt>
test   %eax,%eax
je     125b <rsa_get_public_exp+0xeb>
xor    %edi,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1050 <BN_mod_inverse@plt>
mov    %rax,%rbx
jmp    125d <rsa_get_public_exp+0xed>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1080 <BN_CTX_end@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @rsa_get_public_exp(i32* %0, i32* %1, i32* %2, i32* %3) {
%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*
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* null, i32** %10
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %4
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32*, i32** %8
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19, %16, %4
store i32* null, i32** %5
br label %71
23: ; preds = %19
%24 = load i32*, i32** %9
%25 = call i32 @BN_CTX_start(i32* %24)
%26 = load i32*, i32** %9
%27 = call i32* @BN_CTX_get(i32* %26)
store i32* %27, i32** %11
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %23
br label %67
30: ; preds = %23
%31 = load i32*, i32** %9
%32 = call i32* @BN_CTX_get(i32* %31)
store i32* %32, i32** %12
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %30
br label %67
35: ; preds = %30
%36 = load i32*, i32** %9
%37 = call i32* @BN_CTX_get(i32* %36)
store i32* %37, i32** %13
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %40
39: ; preds = %35
br label %67
40: ; preds = %35
%41 = load i32*, i32** %12
%42 = load i32*, i32** %7
%43 = call i32 (...) @BN_value_one()
%44 = call i32 @BN_sub(i32* %41, i32* %42, i32 %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %40
br label %67
47: ; preds = %40
%48 = load i32*, i32** %13
%49 = load i32*, i32** %8
%50 = call i32 (...) @BN_value_one()
%51 = call i32 @BN_sub(i32* %48, i32* %49, i32 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %47
br label %67
54: ; preds = %47
%55 = load i32*, i32** %11
%56 = load i32*, i32** %12
%57 = load i32*, i32** %13
%58 = load i32*, i32** %9
%59 = call i32 @BN_mul(i32* %55, i32* %56, i32* %57, i32* %58)
%60 = icmp ne i32 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %54
br label %67
62: ; preds = %54
%63 = load i32*, i32** %6
%64 = load i32*, i32** %11
%65 = load i32*, i32** %9
%66 = call i32* @BN_mod_inverse(i32* null, i32* %63, i32* %64, i32* %65)
store i32* %66, i32** %10
br label %67
67: ; preds = %62, %61, %53, %46, %39, %34, %29
%68 = load i32*, i32** %9
%69 = call i32 @BN_CTX_end(i32* %68)
%70 = load i32*, i32** %10
store i32* %70, i32** %5
br label %71
71: ; preds = %67, %22
%72 = load i32*, i32** %5
ret i32* %72
}

LLVM IR (-O2):
define i32* @rsa_get_public_exp(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = icmp eq i32* %0, null
%6 = icmp eq i32* %1, null
%7 = or i1 %5, %6
%8 = icmp eq i32* %2, null
%9 = or i1 %7, %8
br i1 %9, label %36, label %10
10: ; preds = %4
%11 = tail call i32 @BN_CTX_start(i32* %3)
%12 = tail call i32* @BN_CTX_get(i32* %3)
%13 = icmp eq i32* %12, null
br i1 %13, label %33, label %14
14: ; preds = %10
%15 = tail call i32* @BN_CTX_get(i32* %3)
%16 = icmp eq i32* %15, null
br i1 %16, label %33, label %17
17: ; preds = %14
%18 = tail call i32* @BN_CTX_get(i32* %3)
%19 = icmp eq i32* %18, null
br i1 %19, label %33, label %20
20: ; preds = %17
%21 = tail call i32 (...) @BN_value_one()
%22 = tail call i32 @BN_sub(i32* %15, i32* %1, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %20
%25 = tail call i32 (...) @BN_value_one()
%26 = tail call i32 @BN_sub(i32* %18, i32* %2, i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = tail call i32 @BN_mul(i32* %12, i32* %15, i32* %18, i32* %3)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = tail call i32* @BN_mod_inverse(i32* null, i32* %0, i32* %12, i32* %3)
br label %33
33: ; preds = %28, %24, %20, %17, %14, %10, %31
%34 = phi i32* [ null, %10 ], [ null, %14 ], [ null, %17 ], [ %32, %31 ], [ null, %28 ], [ null, %24 ], [ null, %20 ]
%35 = tail call i32 @BN_CTX_end(i32* %3)
br label %36
36: ; preds = %4, %33
%37 = phi i32* [ %34, %33 ], [ null, %4 ]
ret i32* %37
}