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

/* Variables and functions */
int /*<<< orphan*/ BN_GENCB_call(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ BN_add_word(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ BN_copy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_is_odd(int /*<<< orphan*/ *);
scalar_t__ BN_is_prime_fasttest_ex(int /*<<< orphan*/ *, int,
                                   int /*<<< orphan*/ *, int,
                                   int /*<<< orphan*/ *);
int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx,
                      BN_GENCB *cb) {
  int i = 0;
  if (!BN_copy(pi, Xpi))
    return 0;
  if (!BN_is_odd(pi) && !BN_add_word(pi, 1))
    return 0;
  for (;;) {
    i++;
    BN_GENCB_call(cb, 0, i);
    if (BN_is_prime_fasttest_ex(pi, 27, ctx, 1, cb))
      break;
    if (!BN_add_word(pi, 2))
      return 0;
  }
  BN_GENCB_call(cb, 2, i);
  return 1;
}


x86 assembly (clang, -O0):
<bn_x931_derive_pi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <BN_copy@plt>
cmp    $0x0,%eax
jne    1191 <bn_x931_derive_pi+0x41>
movl   $0x0,-0x4(%rbp)
jmp    124c <bn_x931_derive_pi+0xfc>
mov    -0x10(%rbp),%rdi
call   1070 <BN_is_odd@plt>
cmp    $0x0,%eax
jne    11c6 <bn_x931_derive_pi+0x76>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <BN_add_word@plt>
cmp    $0x0,%eax
jne    11c6 <bn_x931_derive_pi+0x76>
movl   $0x0,-0x4(%rbp)
jmp    124c <bn_x931_derive_pi+0xfc>
jmp    11cb <bn_x931_derive_pi+0x7b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
xor    %esi,%esi
call   1060 <BN_GENCB_call@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%r8
mov    $0x1b,%esi
mov    $0x1,%ecx
call   1050 <BN_is_prime_fasttest_ex@plt>
cmp    $0x0,%rax
je     120c <bn_x931_derive_pi+0xbc>
jmp    1234 <bn_x931_derive_pi+0xe4>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1030 <BN_add_word@plt>
cmp    $0x0,%eax
jne    122f <bn_x931_derive_pi+0xdf>
movl   $0x0,-0x4(%rbp)
jmp    124c <bn_x931_derive_pi+0xfc>
jmp    11cb <bn_x931_derive_pi+0x7b>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    $0x2,%esi
call   1060 <BN_GENCB_call@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bn_x931_derive_pi>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rdi,%rbx
call   1040 <BN_copy@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     11e5 <bn_x931_derive_pi+0x95>
mov    %rbx,%rdi
call   1070 <BN_is_odd@plt>
test   %eax,%eax
jne    1188 <bn_x931_derive_pi+0x38>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <BN_add_word@plt>
test   %eax,%eax
je     11e5 <bn_x931_derive_pi+0x95>
mov    $0x1,%ebp
nopl   (%rax)
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <BN_GENCB_call@plt>
mov    %rbx,%rdi
mov    $0x1b,%esi
mov    %r14,%rdx
mov    $0x1,%ecx
mov    %r15,%r8
call   1050 <BN_is_prime_fasttest_ex@plt>
test   %rax,%rax
jne    11d1 <bn_x931_derive_pi+0x81>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <BN_add_word@plt>
add    $0x1,%ebp
test   %eax,%eax
jne    1190 <bn_x931_derive_pi+0x40>
xor    %ebp,%ebp
jmp    11e5 <bn_x931_derive_pi+0x95>
mov    %r15,%rdi
mov    $0x2,%esi
mov    %ebp,%edx
call   1060 <BN_GENCB_call@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bn_x931_derive_pi(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
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 0, i32* %10
%11 = load i32*, i32** %6
%12 = load i32*, i32** %7
%13 = call i32 @BN_copy(i32* %11, i32* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %4
store i32 0, i32* %5
br label %48
16: ; preds = %4
%17 = load i32*, i32** %6
%18 = call i32 @BN_is_odd(i32* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = load i32*, i32** %6
%22 = call i32 @BN_add_word(i32* %21, i32 1)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %20
store i32 0, i32* %5
br label %48
25: ; preds = %20, %16
br label %26
26: ; preds = %43, %25
%27 = load i32, i32* %10
%28 = add nsw i32 %27, 1
store i32 %28, i32* %10
%29 = load i32*, i32** %9
%30 = load i32, i32* %10
%31 = call i32 @BN_GENCB_call(i32* %29, i32 0, i32 %30)
%32 = load i32*, i32** %6
%33 = load i32*, i32** %8
%34 = load i32*, i32** %9
%35 = call i64 @BN_is_prime_fasttest_ex(i32* %32, i32 27, i32* %33, i32 1, i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %26
br label %44
38: ; preds = %26
%39 = load i32*, i32** %6
%40 = call i32 @BN_add_word(i32* %39, i32 2)
%41 = icmp ne i32 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %38
store i32 0, i32* %5
br label %48
43: ; preds = %38
br label %26
44: ; preds = %37
%45 = load i32*, i32** %9
%46 = load i32, i32* %10
%47 = call i32 @BN_GENCB_call(i32* %45, i32 2, i32 %46)
store i32 1, i32* %5
br label %48
48: ; preds = %44, %42, %24, %15
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
define i32 @bn_x931_derive_pi(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = tail call i32 @BN_copy(i32* %0, i32* %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %4
%8 = tail call i32 @BN_is_odd(i32* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @BN_add_word(i32* %0, i32 1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %10, %7
br label %14
14: ; preds = %13, %20
%15 = phi i32 [ %16, %20 ], [ 0, %13 ]
%16 = add nuw nsw i32 %15, 1
%17 = tail call i32 @BN_GENCB_call(i32* %3, i32 0, i32 %16)
%18 = tail call i64 @BN_is_prime_fasttest_ex(i32* %0, i32 27, i32* %2, i32 1, i32* %3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = tail call i32 @BN_add_word(i32* %0, i32 2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %14
23: ; preds = %14
%24 = tail call i32 @BN_GENCB_call(i32* %3, i32 2, i32 %16)
br label %25
25: ; preds = %20, %10, %4, %23
%26 = phi i32 [ 1, %23 ], [ 0, %4 ], [ 0, %10 ], [ 0, %20 ]
ret i32 %26
}