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

/* Variables and functions */
int /*<<< orphan*/ *BN_CTX_new();
int /*<<< orphan*/ BN_exp(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_get_word(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_rshift(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ BN_set_word(int /*<<< orphan*/ *, long);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int get_key_bignum(BIGNUM *num, int num_bits, uint32_t *key_mod) {
  BIGNUM *tmp, *big2, *big32, *big2_32;
  BN_CTX *ctx;
  int ret;
  tmp = BN_new();
  big2 = BN_new();
  big32 = BN_new();
  big2_32 = BN_new();
  if (!tmp || !big2 || !big32 || !big2_32) {
    fprintf(stderr, "Out of memory (bignum)\n");
    return -1;
  }
  ctx = BN_CTX_new();
  if (!tmp) {
    fprintf(stderr, "Out of memory (bignum context)\n");
    return -1;
  }
  BN_set_word(big2, 2L);
  BN_set_word(big32, 32L);
  BN_exp(big2_32, big2, big32, ctx);
  for (ret = 0; ret <= 63; ret++) {
    BN_mod(tmp, num, big2_32, ctx);
    key_mod[ret] = htonl(BN_get_word(tmp));
    BN_rshift(num, num, 32);
  }
  BN_free(tmp);
  BN_free(big2);
  BN_free(big32);
  BN_free(big2_32);
  return 0;
}


x86 assembly (clang, -O0):
<get_key_bignum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     120b <get_key_bignum+0x6b>
cmpq   $0x0,-0x30(%rbp)
je     120b <get_key_bignum+0x6b>
cmpq   $0x0,-0x38(%rbp)
je     120b <get_key_bignum+0x6b>
cmpq   $0x0,-0x40(%rbp)
jne    122c <get_key_bignum+0x8c>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1322 <get_key_bignum+0x182>
mov    $0x0,%al
call   10a0 <BN_CTX_new@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1263 <get_key_bignum+0xc3>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1322 <get_key_bignum+0x182>
mov    -0x30(%rbp),%rdi
mov    $0x2,%esi
call   1030 <BN_set_word@plt>
mov    -0x38(%rbp),%rdi
mov    $0x20,%esi
call   1030 <BN_set_word@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x48(%rbp),%rcx
call   1060 <BN_exp@plt>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x3f,-0x4c(%rbp)
jg     12f7 <get_key_bignum+0x157>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rcx
call   10b0 <BN_mod@plt>
mov    -0x28(%rbp),%rdi
call   1050 <BN_get_word@plt>
mov    %eax,%edi
call   1070 <htonl@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x20,%edx
call   10c0 <BN_rshift@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    129b <get_key_bignum+0xfb>
mov    -0x28(%rbp),%rdi
call   1080 <BN_free@plt>
mov    -0x30(%rbp),%rdi
call   1080 <BN_free@plt>
mov    -0x38(%rbp),%rdi
call   1080 <BN_free@plt>
mov    -0x40(%rbp),%rdi
call   1080 <BN_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_key_bignum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r13
xor    %eax,%eax
call   1040 <BN_new@plt>
test   %r15,%r15
je     12b6 <get_key_bignum+0x116>
test   %r12,%r12
je     12b6 <get_key_bignum+0x116>
test   %r13,%r13
je     12b6 <get_key_bignum+0x116>
mov    %rax,%rbp
test   %rax,%rax
je     12b6 <get_key_bignum+0x116>
mov    %r12,0x10(%rsp)
xor    %r12d,%r12d
xor    %eax,%eax
call   10a0 <BN_CTX_new@plt>
mov    %r13,0x8(%rsp)
mov    %rax,%r13
mov    $0x2,%esi
mov    0x10(%rsp),%rdi
call   1030 <BN_set_word@plt>
mov    $0x20,%esi
mov    0x8(%rsp),%rdi
call   1030 <BN_set_word@plt>
mov    %rbp,%rdi
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    %r13,%rcx
call   1060 <BN_exp@plt>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %r13,%rcx
call   10b0 <BN_mod@plt>
mov    %r15,%rdi
call   1050 <BN_get_word@plt>
mov    %eax,%edi
call   1070 <htonl@plt>
mov    %eax,(%r14,%r12,4)
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    $0x20,%edx
call   10c0 <BN_rshift@plt>
add    $0x1,%r12
cmp    $0x40,%r12
jne    1250 <get_key_bignum+0xb0>
mov    %r15,%rdi
call   1080 <BN_free@plt>
mov    0x10(%rsp),%rdi
call   1080 <BN_free@plt>
mov    0x8(%rsp),%rdi
call   1080 <BN_free@plt>
mov    %rbp,%rdi
call   1080 <BN_free@plt>
xor    %eax,%eax
jmp    12d0 <get_key_bignum+0x130>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Out of memory (bignum)\0A\00"
@.str.1 = [32 x i8] c"Out of memory (bignum context)\0A\00"
define i32 @get_key_bignum(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
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%14 = call i32* (...) @BN_new()
store i32* %14, i32** %8
%15 = call i32* (...) @BN_new()
store i32* %15, i32** %9
%16 = call i32* (...) @BN_new()
store i32* %16, i32** %10
%17 = call i32* (...) @BN_new()
store i32* %17, i32** %11
%18 = load i32*, i32** %8
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %29
20: ; preds = %3
%21 = load i32*, i32** %9
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i32*, i32** %10
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32*, i32** %11
%28 = icmp ne i32* %27, null
br i1 %28, label %32, label %29
29: ; preds = %26, %23, %20, %3
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %80
32: ; preds = %26
%33 = call i32* (...) @BN_CTX_new()
store i32* %33, i32** %12
%34 = load i32*, i32** %8
%35 = icmp ne i32* %34, null
br i1 %35, label %39, label %36
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %80
39: ; preds = %32
%40 = load i32*, i32** %9
%41 = call i32 @BN_set_word(i32* %40, i64 2)
%42 = load i32*, i32** %10
%43 = call i32 @BN_set_word(i32* %42, i64 32)
%44 = load i32*, i32** %11
%45 = load i32*, i32** %9
%46 = load i32*, i32** %10
%47 = load i32*, i32** %12
%48 = call i32 @BN_exp(i32* %44, i32* %45, i32* %46, i32* %47)
store i32 0, i32* %13
br label %49
49: ; preds = %68, %39
%50 = load i32, i32* %13
%51 = icmp sle i32 %50, 63
br i1 %51, label %52, label %71
52: ; preds = %49
%53 = load i32*, i32** %8
%54 = load i32*, i32** %5
%55 = load i32*, i32** %11
%56 = load i32*, i32** %12
%57 = call i32 @BN_mod(i32* %53, i32* %54, i32* %55, i32* %56)
%58 = load i32*, i32** %8
%59 = call i32 @BN_get_word(i32* %58)
%60 = call i32 @htonl(i32 %59)
%61 = load i32*, i32** %7
%62 = load i32, i32* %13
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %61, i64 %63
store i32 %60, i32* %64
%65 = load i32*, i32** %5
%66 = load i32*, i32** %5
%67 = call i32 @BN_rshift(i32* %65, i32* %66, i32 32)
br label %68
68: ; preds = %52
%69 = load i32, i32* %13
%70 = add nsw i32 %69, 1
store i32 %70, i32* %13
br label %49
71: ; preds = %49
%72 = load i32*, i32** %8
%73 = call i32 @BN_free(i32* %72)
%74 = load i32*, i32** %9
%75 = call i32 @BN_free(i32* %74)
%76 = load i32*, i32** %10
%77 = call i32 @BN_free(i32* %76)
%78 = load i32*, i32** %11
%79 = call i32 @BN_free(i32* %78)
store i32 0, i32* %4
br label %80
80: ; preds = %71, %36, %29
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Out of memory (bignum)\0A\00"
define i32 @get_key_bignum(i32* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = tail call i32* (...) @BN_new()
%5 = tail call i32* (...) @BN_new()
%6 = tail call i32* (...) @BN_new()
%7 = tail call i32* (...) @BN_new()
%8 = icmp ne i32* %4, null
%9 = icmp ne i32* %5, null
%10 = select i1 %8, i1 %9, i1 false
%11 = icmp ne i32* %6, null
%12 = select i1 %10, i1 %11, i1 false
%13 = icmp ne i32* %7, null
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %37
18: ; preds = %3
%19 = tail call i32* (...) @BN_CTX_new()
%20 = tail call i32 @BN_set_word(i32* %5, i64 2)
%21 = tail call i32 @BN_set_word(i32* %6, i64 32)
%22 = tail call i32 @BN_exp(i32* %7, i32* %5, i32* %6, i32* %19)
br label %23
23: ; preds = %18, %23
%24 = phi i64 [ 0, %18 ], [ %30, %23 ]
%25 = tail call i32 @BN_mod(i32* %4, i32* %0, i32* %7, i32* %19)
%26 = tail call i32 @BN_get_word(i32* %4)
%27 = tail call i32 @htonl(i32 %26)
%28 = getelementptr inbounds i32, i32* %2, i64 %24
store i32 %27, i32* %28
%29 = tail call i32 @BN_rshift(i32* %0, i32* %0, i32 32)
%30 = add nuw nsw i64 %24, 1
%31 = icmp eq i64 %30, 64
br i1 %31, label %32, label %23
32: ; preds = %23
%33 = tail call i32 @BN_free(i32* %4)
%34 = tail call i32 @BN_free(i32* %5)
%35 = tail call i32 @BN_free(i32* %6)
%36 = tail call i32 @BN_free(i32* %7)
br label %37
37: ; preds = %32, %15
%38 = phi i32 [ 0, %32 ], [ -1, %15 ]
ret i32 %38
}