#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 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*/ BN_rand(int /*<<< orphan*/ *, int, int, int);
scalar_t__ malloc(int);
int dh_gen_private_public(int dh_group, BIGNUM *bn_prime, BIGNUM *bn_base,
                          BIGNUM **pbn_private, unsigned char **ppublic,
                          int *ppublic_len) {
  int err, len;
  BN_CTX *ctx = NULL;
  BIGNUM *bn_public = NULL;
  *pbn_private = NULL;
  *ppublic = NULL;
  *pbn_private = BN_new();
  if (!*pbn_private) {
    err = 5;
    goto ret_err;
  }
  bn_public = BN_new();
  if (!bn_public) {
    err = 5;
    goto ret_err;
  }
  ctx = BN_CTX_new();
  if (!ctx) {
    err = 5;
    goto ret_err;
  }
  if (!BN_rand(*pbn_private, dh_group, 1, 1)) {
    err = 5;
    goto ret_err;
  }
  if (!BN_mod_exp(bn_public, bn_base, *pbn_private, bn_prime, ctx)) {
    err = 5;
    goto ret_err;
  }
  *ppublic_len = BN_num_bytes(bn_public);
  *ppublic = (unsigned char *)malloc(*ppublic_len);
  if (!*ppublic) {
    err = 5;
    goto ret_err;
  }
  len = BN_bn2bin(bn_public, *ppublic);
  if (len != *ppublic_len) {
    err = 5;
    goto ret_err;
  }
  BN_free(bn_public);
  BN_CTX_free(ctx);
  return 0;
ret_err:;
  if (*pbn_private)
    BN_free(*pbn_private);
  if (bn_public)
    BN_free(bn_public);
  if (ctx)
    BN_CTX_free(ctx);
  *pbn_private = NULL;
  *ppublic = NULL;
  return err;
}


x86 assembly (clang, -O0):
<dh_gen_private_public>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1200 <dh_gen_private_public+0x70>
movl   $0x5,-0x34(%rbp)
jmp    132c <dh_gen_private_public+0x19c>
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1222 <dh_gen_private_public+0x92>
movl   $0x5,-0x34(%rbp)
jmp    132c <dh_gen_private_public+0x19c>
mov    $0x0,%al
call   1080 <BN_CTX_new@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1244 <dh_gen_private_public+0xb4>
movl   $0x5,-0x34(%rbp)
jmp    132c <dh_gen_private_public+0x19c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%esi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1030 <BN_rand@plt>
cmp    $0x0,%eax
jne    126f <dh_gen_private_public+0xdf>
movl   $0x5,-0x34(%rbp)
jmp    132c <dh_gen_private_public+0x19c>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1070 <BN_mod_exp@plt>
cmp    $0x0,%eax
jne    12a0 <dh_gen_private_public+0x110>
movl   $0x5,-0x34(%rbp)
jmp    132c <dh_gen_private_public+0x19c>
mov    -0x48(%rbp),%rdi
call   10a0 <BN_num_bytes@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12e0 <dh_gen_private_public+0x150>
movl   $0x5,-0x34(%rbp)
jmp    132c <dh_gen_private_public+0x19c>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   10b0 <BN_bn2bin@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    -0x30(%rbp),%rcx
cmp    (%rcx),%eax
je     130e <dh_gen_private_public+0x17e>
movl   $0x5,-0x34(%rbp)
jmp    132c <dh_gen_private_public+0x19c>
mov    -0x48(%rbp),%rdi
call   1060 <BN_free@plt>
mov    -0x40(%rbp),%rdi
call   1050 <BN_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
jmp    138a <dh_gen_private_public+0x1fa>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1346 <dh_gen_private_public+0x1b6>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1060 <BN_free@plt>
cmpq   $0x0,-0x48(%rbp)
je     135a <dh_gen_private_public+0x1ca>
mov    -0x48(%rbp),%rdi
call   1060 <BN_free@plt>
cmpq   $0x0,-0x40(%rbp)
je     136e <dh_gen_private_public+0x1de>
mov    -0x40(%rbp),%rdi
call   1050 <BN_CTX_free@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dh_gen_private_public>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r15
mov    %r8,%rbp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %edi,%r13d
movq   $0x0,(%rcx)
movq   $0x0,(%r8)
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     129f <dh_gen_private_public+0x10f>
mov    %r12,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r15,%rbx
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,%r12
test   %rax,%rax
je     1278 <dh_gen_private_public+0xe8>
xor    %eax,%eax
call   1080 <BN_CTX_new@plt>
test   %rax,%rax
je     1275 <dh_gen_private_public+0xe5>
mov    %rax,%r15
mov    (%r14),%rdi
mov    %r13d,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1030 <BN_rand@plt>
test   %eax,%eax
je     1278 <dh_gen_private_public+0xe8>
mov    (%r14),%rdx
mov    %r12,%rdi
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rcx
mov    %r15,%r8
call   1070 <BN_mod_exp@plt>
test   %eax,%eax
je     1278 <dh_gen_private_public+0xe8>
mov    %r12,%rdi
call   10a0 <BN_num_bytes@plt>
mov    %eax,(%rbx)
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
je     1278 <dh_gen_private_public+0xe8>
mov    %r12,%rdi
mov    %rax,%rsi
call   10b0 <BN_bn2bin@plt>
cmp    (%rbx),%eax
jne    1278 <dh_gen_private_public+0xe8>
mov    %r12,%rdi
call   1060 <BN_free@plt>
mov    %r15,%rdi
call   1050 <BN_CTX_free@plt>
xor    %eax,%eax
jmp    12b3 <dh_gen_private_public+0x123>
xor    %r15d,%r15d
mov    (%r14),%rdi
test   %rdi,%rdi
je     1285 <dh_gen_private_public+0xf5>
call   1060 <BN_free@plt>
test   %r12,%r12
je     1292 <dh_gen_private_public+0x102>
mov    %r12,%rdi
call   1060 <BN_free@plt>
test   %r15,%r15
je     129f <dh_gen_private_public+0x10f>
mov    %r15,%rdi
call   1050 <BN_CTX_free@plt>
movq   $0x0,(%r14)
movq   $0x0,0x0(%rbp)
mov    $0x5,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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