#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_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_new();
int /*<<< orphan*/ *BN_bin2bn(unsigned char const *, short,
                              int /*<<< orphan*/ *);
int BN_bn2bin(int /*<<< orphan*/ *, unsigned char *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_exp(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int BN_num_bytes(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int dh_get_secret(const unsigned char *public, short public_len,
                  BIGNUM *bn_private, BIGNUM *bn_prime,
                  unsigned char **psecret_key, int *psecret_key_len) {
  int err, len;
  BIGNUM *bn_public = NULL;
  BIGNUM *bn_secret = NULL;
  BN_CTX *ctx = NULL;
  *psecret_key = NULL;
  bn_public = BN_bin2bn(public, public_len, NULL);
  if (!bn_public) {
    err = 5;
    goto ret_err;
  }
  bn_secret = BN_new();
  if (!bn_secret) {
    err = 5;
    goto ret_err;
  }
  ctx = BN_CTX_new();
  if (!ctx) {
    err = 5;
    goto ret_err;
  }
  if (!BN_mod_exp(bn_secret, bn_public, bn_private, bn_prime, ctx)) {
    err = 5;
    goto ret_err;
  }
  *psecret_key_len = BN_num_bytes(bn_secret);
  *psecret_key = (unsigned char *)malloc(*psecret_key_len);
  if (!*psecret_key) {
    err = 5;
    goto ret_err;
  }
  len = BN_bn2bin(bn_secret, *psecret_key);
  if (len != *psecret_key_len) {
    err = 5;
    goto ret_err;
  }
  BN_free(bn_public);
  BN_free(bn_secret);
  BN_CTX_free(ctx);
  return 0;
ret_err:;
  if (*psecret_key)
    free(*psecret_key);
  if (bn_public)
    BN_free(bn_public);
  if (bn_secret)
    BN_free(bn_secret);
  if (ctx)
    BN_CTX_free(ctx);
  *psecret_key = NULL;
  return err;
}


x86 assembly (clang, -O0):
<dh_get_secret>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
movswl -0x12(%rbp),%esi
call   1050 <BN_bin2bn@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1212 <dh_get_secret+0x72>
movl   $0x5,-0x3c(%rbp)
jmp    1319 <dh_get_secret+0x179>
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1234 <dh_get_secret+0x94>
movl   $0x5,-0x3c(%rbp)
jmp    1319 <dh_get_secret+0x179>
mov    $0x0,%al
call   1090 <BN_CTX_new@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1256 <dh_get_secret+0xb6>
movl   $0x5,-0x3c(%rbp)
jmp    1319 <dh_get_secret+0x179>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x58(%rbp),%r8
call   1080 <BN_mod_exp@plt>
cmp    $0x0,%eax
jne    1284 <dh_get_secret+0xe4>
movl   $0x5,-0x3c(%rbp)
jmp    1319 <dh_get_secret+0x179>
mov    -0x50(%rbp),%rdi
call   10b0 <BN_num_bytes@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12c4 <dh_get_secret+0x124>
movl   $0x5,-0x3c(%rbp)
jmp    1319 <dh_get_secret+0x179>
mov    -0x50(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   10c0 <BN_bn2bin@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    -0x38(%rbp),%rcx
cmp    (%rcx),%eax
je     12f2 <dh_get_secret+0x152>
movl   $0x5,-0x3c(%rbp)
jmp    1319 <dh_get_secret+0x179>
mov    -0x48(%rbp),%rdi
call   1070 <BN_free@plt>
mov    -0x50(%rbp),%rdi
call   1070 <BN_free@plt>
mov    -0x58(%rbp),%rdi
call   1060 <BN_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1380 <dh_get_secret+0x1e0>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     1333 <dh_get_secret+0x193>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x48(%rbp)
je     1347 <dh_get_secret+0x1a7>
mov    -0x48(%rbp),%rdi
call   1070 <BN_free@plt>
cmpq   $0x0,-0x50(%rbp)
je     135b <dh_get_secret+0x1bb>
mov    -0x50(%rbp),%rdi
call   1070 <BN_free@plt>
cmpq   $0x0,-0x58(%rbp)
je     136f <dh_get_secret+0x1cf>
mov    -0x58(%rbp),%rdi
call   1060 <BN_CTX_free@plt>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dh_get_secret>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,(%rsp)
mov    %r8,%r14
mov    %rcx,%rbp
mov    %rdx,%r13
movq   $0x0,(%r8)
xor    %ebx,%ebx
xor    %edx,%edx
call   1050 <BN_bin2bn@plt>
mov    %rax,%r12
mov    $0x0,%r15d
test   %rax,%rax
je     1264 <dh_get_secret+0xc4>
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <BN_new@plt>
test   %rax,%rax
je     125d <dh_get_secret+0xbd>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <BN_CTX_new@plt>
test   %rax,%rax
je     1261 <dh_get_secret+0xc1>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
mov    %rax,%r8
call   1080 <BN_mod_exp@plt>
test   %eax,%eax
je     1264 <dh_get_secret+0xc4>
mov    %rbx,%rdi
call   10b0 <BN_num_bytes@plt>
mov    (%rsp),%rbp
mov    %eax,0x0(%rbp)
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1267 <dh_get_secret+0xc7>
mov    %rbx,%rdi
mov    %rax,%rsi
call   10c0 <BN_bn2bin@plt>
cmp    0x0(%rbp),%eax
jne    1264 <dh_get_secret+0xc4>
mov    %r12,%rdi
call   1070 <BN_free@plt>
mov    %rbx,%rdi
call   1070 <BN_free@plt>
mov    %r15,%rdi
call   1060 <BN_CTX_free@plt>
xor    %eax,%eax
jmp    12a7 <dh_get_secret+0x107>
xor    %ebx,%ebx
jmp    1264 <dh_get_secret+0xc4>
xor    %r15d,%r15d
mov    (%r14),%rax
test   %rax,%rax
je     1274 <dh_get_secret+0xd4>
mov    %rax,%rdi
call   1030 <free@plt>
test   %r12,%r12
je     1281 <dh_get_secret+0xe1>
mov    %r12,%rdi
call   1070 <BN_free@plt>
test   %rbx,%rbx
je     128e <dh_get_secret+0xee>
mov    %rbx,%rdi
call   1070 <BN_free@plt>
test   %r15,%r15
je     129b <dh_get_secret+0xfb>
mov    %r15,%rdi
call   1060 <BN_CTX_free@plt>
movq   $0x0,(%r14)
mov    $0x5,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dh_get_secret(i8* %0, i16 signext %1, i32* %2, i32* %3, i8** %4, i32* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i16
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8**
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32*
%17 = alloca i32*
%18 = alloca i32*
store i8* %0, i8** %8
store i16 %1, i16* %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i8** %4, i8*** %12
store i32* %5, i32** %13
store i32* null, i32** %16
store i32* null, i32** %17
store i32* null, i32** %18
%19 = load i8**, i8*** %12
store i8* null, i8** %19
%20 = load i8*, i8** %8
%21 = load i16, i16* %9
%22 = call i32* @BN_bin2bn(i8* %20, i16 signext %21, i32* null)
store i32* %22, i32** %16
%23 = load i32*, i32** %16
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %6
store i32 5, i32* %14
br label %75
26: ; preds = %6
%27 = call i32* (...) @BN_new()
store i32* %27, i32** %17
%28 = load i32*, i32** %17
%29 = icmp ne i32* %28, null
br i1 %29, label %31, label %30
30: ; preds = %26
store i32 5, i32* %14
br label %75
31: ; preds = %26
%32 = call i32* (...) @BN_CTX_new()
store i32* %32, i32** %18
%33 = load i32*, i32** %18
%34 = icmp ne i32* %33, null
br i1 %34, label %36, label %35
35: ; preds = %31
store i32 5, i32* %14
br label %75
36: ; preds = %31
%37 = load i32*, i32** %17
%38 = load i32*, i32** %16
%39 = load i32*, i32** %10
%40 = load i32*, i32** %11
%41 = load i32*, i32** %18
%42 = call i32 @BN_mod_exp(i32* %37, i32* %38, i32* %39, i32* %40, i32* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %36
store i32 5, i32* %14
br label %75
45: ; preds = %36
%46 = load i32*, i32** %17
%47 = call i32 @BN_num_bytes(i32* %46)
%48 = load i32*, i32** %13
store i32 %47, i32* %48
%49 = load i32*, i32** %13
%50 = load i32, i32* %49
%51 = call i64 @malloc(i32 %50)
%52 = inttoptr i64 %51 to i8*
%53 = load i8**, i8*** %12
store i8* %52, i8** %53
%54 = load i8**, i8*** %12
%55 = load i8*, i8** %54
%56 = icmp ne i8* %55, null
br i1 %56, label %58, label %57
57: ; preds = %45
store i32 5, i32* %14
br label %75
58: ; preds = %45
%59 = load i32*, i32** %17
%60 = load i8**, i8*** %12
%61 = load i8*, i8** %60
%62 = call i32 @BN_bn2bin(i32* %59, i8* %61)
store i32 %62, i32* %15
%63 = load i32, i32* %15
%64 = load i32*, i32** %13
%65 = load i32, i32* %64
%66 = icmp ne i32 %63, %65
br i1 %66, label %67, label %68
67: ; preds = %58
store i32 5, i32* %14
br label %75
68: ; preds = %58
%69 = load i32*, i32** %16
%70 = call i32 @BN_free(i32* %69)
%71 = load i32*, i32** %17
%72 = call i32 @BN_free(i32* %71)
%73 = load i32*, i32** %18
%74 = call i32 @BN_CTX_free(i32* %73)
store i32 0, i32* %7
br label %104
75: ; preds = %67, %57, %44, %35, %30, %25
%76 = load i8**, i8*** %12
%77 = load i8*, i8** %76
%78 = icmp ne i8* %77, null
br i1 %78, label %79, label %83
79: ; preds = %75
%80 = load i8**, i8*** %12
%81 = load i8*, i8** %80
%82 = call i32 @free(i8* %81)
br label %83
83: ; preds = %79, %75
%84 = load i32*, i32** %16
%85 = icmp ne i32* %84, null
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = load i32*, i32** %16
%88 = call i32 @BN_free(i32* %87)
br label %89
89: ; preds = %86, %83
%90 = load i32*, i32** %17
%91 = icmp ne i32* %90, null
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32*, i32** %17
%94 = call i32 @BN_free(i32* %93)
br label %95
95: ; preds = %92, %89
%96 = load i32*, i32** %18
%97 = icmp ne i32* %96, null
br i1 %97, label %98, label %101
98: ; preds = %95
%99 = load i32*, i32** %18
%100 = call i32 @BN_CTX_free(i32* %99)
br label %101
101: ; preds = %98, %95
%102 = load i8**, i8*** %12
store i8* null, i8** %102
%103 = load i32, i32* %14
store i32 %103, i32* %7
br label %104
104: ; preds = %101, %68
%105 = load i32, i32* %7
ret i32 %105
}

LLVM IR (-O2):
define i32 @dh_get_secret(i8* %0, i16 signext %1, i32* %2, i32* %3, i8** nocapture %4, i32* nocapture %5) {
store i8* null, i8** %4
%7 = tail call i32* @BN_bin2bn(i8* %0, i16 signext %1, i32* null)
%8 = icmp eq i32* %7, null
br i1 %8, label %31, label %9
9: ; preds = %6
%10 = tail call i32* (...) @BN_new()
%11 = icmp eq i32* %10, null
br i1 %11, label %31, label %12
12: ; preds = %9
%13 = tail call i32* (...) @BN_CTX_new()
%14 = icmp eq i32* %13, null
br i1 %14, label %31, label %15
15: ; preds = %12
%16 = tail call i32 @BN_mod_exp(i32* %10, i32* %7, i32* %2, i32* %3, i32* %13)
%17 = icmp eq i32 %16, 0
br i1 %17, label %31, label %18
18: ; preds = %15
%19 = tail call i32 @BN_num_bytes(i32* %10)
store i32 %19, i32* %5
%20 = tail call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %4
%22 = icmp eq i64 %20, 0
br i1 %22, label %35, label %23
23: ; preds = %18
%24 = tail call i32 @BN_bn2bin(i32* %10, i8* %21)
%25 = load i32, i32* %5
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = tail call i32 @BN_free(i32* %7)
%29 = tail call i32 @BN_free(i32* %10)
%30 = tail call i32 @BN_CTX_free(i32* %13)
br label %54
31: ; preds = %6, %9, %12, %15, %23
%32 = phi i32* [ %10, %23 ], [ %10, %15 ], [ %10, %12 ], [ null, %9 ], [ null, %6 ]
%33 = phi i32* [ %13, %23 ], [ %13, %15 ], [ null, %12 ], [ null, %9 ], [ null, %6 ]
%34 = load i8*, i8** %4
br label %35
35: ; preds = %31, %18
%36 = phi i8* [ %34, %31 ], [ %21, %18 ]
%37 = phi i32* [ %32, %31 ], [ %10, %18 ]
%38 = phi i32* [ %33, %31 ], [ %13, %18 ]
%39 = icmp eq i8* %36, null
br i1 %39, label %42, label %40
40: ; preds = %35
%41 = tail call i32 @free(i8* %36)
br label %42
42: ; preds = %40, %35
br i1 %8, label %45, label %43
43: ; preds = %42
%44 = tail call i32 @BN_free(i32* %7)
br label %45
45: ; preds = %43, %42
%46 = icmp eq i32* %37, null
br i1 %46, label %49, label %47
47: ; preds = %45
%48 = tail call i32 @BN_free(i32* %37)
br label %49
49: ; preds = %47, %45
%50 = icmp eq i32* %38, null
br i1 %50, label %53, label %51
51: ; preds = %49
%52 = tail call i32 @BN_CTX_free(i32* %38)
br label %53
53: ; preds = %51, %49
store i8* null, i8** %4
br label %54
54: ; preds = %53, %27
%55 = phi i32 [ 5, %53 ], [ 0, %27 ]
ret i32 %55
}