#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_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_get(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_new();
int /*<<< orphan*/ BN_CTX_start(int /*<<< orphan*/ *);
int BN_prime_checks_for_size(int);
int /*<<< orphan*/ probable_prime(int /*<<< orphan*/ *, int);
int xBN_is_prime_fasttest_ex(int /*<<< orphan*/ *, int, int /*<<< orphan*/ *,
                             int /*<<< orphan*/);
int xBN_generate_prime_ex(BIGNUM *ret, int bits) {
  BIGNUM *t;
  int found = 0;
  int i, j, c1 = 0;
  BN_CTX *ctx;
  int checks = BN_prime_checks_for_size(bits);
  ctx = BN_CTX_new();
  if (ctx == NULL)
    goto err;
  BN_CTX_start(ctx);
  t = BN_CTX_get(ctx);
  if (!t)
    goto err;
loop:
  if (!probable_prime(ret, bits))
    goto err;
  i = xBN_is_prime_fasttest_ex(ret, checks, ctx, 0);
  if (i == -1)
    goto err;
  if (i == 0)
    goto loop;
  found = 1;
err:
  if (ctx != NULL) {
    BN_CTX_end(ctx);
    BN_CTX_free(ctx);
  }
  return found;
}


x86 assembly (clang, -O0):
<xBN_generate_prime_ex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   10a0 <BN_prime_checks_for_size@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1090 <BN_CTX_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c3 <xBN_generate_prime_ex+0x43>
jmp    1242 <xBN_generate_prime_ex+0xc2>
mov    -0x30(%rbp),%rdi
call   1080 <BN_CTX_start@plt>
mov    -0x30(%rbp),%rdi
call   1030 <BN_CTX_get@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e9 <xBN_generate_prime_ex+0x69>
jmp    1242 <xBN_generate_prime_ex+0xc2>
jmp    11ee <xBN_generate_prime_ex+0x6e>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <probable_prime@plt>
cmp    $0x0,%eax
jne    1208 <xBN_generate_prime_ex+0x88>
jmp    1242 <xBN_generate_prime_ex+0xc2>
mov    -0x8(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <xBN_is_prime_fasttest_ex@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    122c <xBN_generate_prime_ex+0xac>
jmp    1242 <xBN_generate_prime_ex+0xc2>
cmpl   $0x0,-0x20(%rbp)
jne    123b <xBN_generate_prime_ex+0xbb>
jmp    11ee <xBN_generate_prime_ex+0x6e>
movl   $0x1,-0x1c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     125f <xBN_generate_prime_ex+0xdf>
mov    -0x30(%rbp),%rdi
call   1070 <BN_CTX_end@plt>
mov    -0x30(%rbp),%rdi
call   1060 <BN_CTX_free@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xBN_generate_prime_ex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    %esi,%edi
call   10a0 <BN_prime_checks_for_size@plt>
mov    %eax,%r15d
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <BN_CTX_new@plt>
test   %rax,%rax
je     1201 <xBN_generate_prime_ex+0x81>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <BN_CTX_start@plt>
mov    %r12,%rdi
call   1030 <BN_CTX_get@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11f1 <xBN_generate_prime_ex+0x71>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <probable_prime@plt>
test   %eax,%eax
je     11ef <xBN_generate_prime_ex+0x6f>
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1050 <xBN_is_prime_fasttest_ex@plt>
test   %eax,%eax
je     11c0 <xBN_generate_prime_ex+0x40>
cmp    $0xffffffff,%eax
je     11f1 <xBN_generate_prime_ex+0x71>
mov    $0x1,%ebx
jmp    11f1 <xBN_generate_prime_ex+0x71>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1070 <BN_CTX_end@plt>
mov    %r12,%rdi
call   1060 <BN_CTX_free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @xBN_generate_prime_ex(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %6
store i32 0, i32* %9
%12 = load i32, i32* %4
%13 = call i32 @BN_prime_checks_for_size(i32 %12)
store i32 %13, i32* %11
%14 = call i32* (...) @BN_CTX_new()
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %2
br label %46
18: ; preds = %2
%19 = load i32*, i32** %10
%20 = call i32 @BN_CTX_start(i32* %19)
%21 = load i32*, i32** %10
%22 = call i32* @BN_CTX_get(i32* %21)
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %18
br label %46
26: ; preds = %18
br label %27
27: ; preds = %44, %26
%28 = load i32*, i32** %3
%29 = load i32, i32* %4
%30 = call i32 @probable_prime(i32* %28, i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %27
br label %46
33: ; preds = %27
%34 = load i32*, i32** %3
%35 = load i32, i32* %11
%36 = load i32*, i32** %10
%37 = call i32 @xBN_is_prime_fasttest_ex(i32* %34, i32 %35, i32* %36, i32 0)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %33
br label %46
41: ; preds = %33
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
br label %27
45: ; preds = %41
store i32 1, i32* %6
br label %46
46: ; preds = %45, %40, %32, %25, %17
%47 = load i32*, i32** %10
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = load i32*, i32** %10
%51 = call i32 @BN_CTX_end(i32* %50)
%52 = load i32*, i32** %10
%53 = call i32 @BN_CTX_free(i32* %52)
br label %54
54: ; preds = %49, %46
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
define i32 @xBN_generate_prime_ex(i32* %0, i32 %1) {
%3 = tail call i32 @BN_prime_checks_for_size(i32 %1)
%4 = tail call i32* (...) @BN_CTX_new()
%5 = icmp eq i32* %4, null
br i1 %5, label %20, label %6
6: ; preds = %2
%7 = tail call i32 @BN_CTX_start(i32* %4)
%8 = tail call i32* @BN_CTX_get(i32* %4)
%9 = icmp eq i32* %8, null
br i1 %9, label %16, label %10
10: ; preds = %6, %13
%11 = tail call i32 @probable_prime(i32* %0, i32 %1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @xBN_is_prime_fasttest_ex(i32* %0, i32 %3, i32* %4, i32 0)
switch i32 %14, label %16 [
i32 -1, label %15
i32 0, label %10
]
15: ; preds = %13, %10
br label %16
16: ; preds = %13, %15, %6
%17 = phi i32 [ 0, %6 ], [ 0, %15 ], [ 1, %13 ]
%18 = tail call i32 @BN_CTX_end(i32* %4)
%19 = tail call i32 @BN_CTX_free(i32* %4)
br label %20
20: ; preds = %2, %16
%21 = phi i32 [ %17, %16 ], [ 0, %2 ]
ret i32 %21
}