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

/* Variables and functions */
scalar_t__ BN_cmp(int /*<<< orphan*/ *, int /*<<< orphan*/ const *);
scalar_t__ BN_is_one(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_exp_mont(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_mul(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ bn_check_top(int /*<<< orphan*/ *);
int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1, const BIGNUM *a1_odd,
            int k, BN_CTX *ctx, BN_MONT_CTX *mont) {
  if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont))
    return -1;
  if (BN_is_one(w))
    return 0;
  if (BN_cmp(w, a1) == 0)
    return 0;
  while (--k) {
    if (!BN_mod_mul(w, w, w, a, ctx))
      return -1;
    if (BN_is_one(w))
      return 1;
    if (BN_cmp(w, a1) == 0)
      return 0;
  }
  bn_check_top(w);
  return 1;
}


x86 assembly (clang, -O0):
<witness>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    0x10(%rbp),%r9
call   1070 <BN_mod_exp_mont@plt>
cmp    $0x0,%eax
jne    11a6 <witness+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <witness+0x134>
mov    -0x10(%rbp),%rdi
call   1060 <BN_is_one@plt>
cmp    $0x0,%rax
je     11c5 <witness+0x75>
movl   $0x0,-0x4(%rbp)
jmp    1284 <witness+0x134>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <BN_cmp@plt>
cmp    $0x0,%rax
jne    11e8 <witness+0x98>
movl   $0x0,-0x4(%rbp)
jmp    1284 <witness+0x134>
jmp    11ed <witness+0x9d>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     1274 <witness+0x124>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x38(%rbp),%r8
call   1040 <BN_mod_mul@plt>
cmp    $0x0,%eax
jne    122d <witness+0xdd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <witness+0x134>
mov    -0x10(%rbp),%rdi
call   1060 <BN_is_one@plt>
cmp    $0x0,%rax
je     124c <witness+0xfc>
movl   $0x1,-0x4(%rbp)
jmp    1284 <witness+0x134>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <BN_cmp@plt>
cmp    $0x0,%rax
jne    126f <witness+0x11f>
movl   $0x0,-0x4(%rbp)
jmp    1284 <witness+0x134>
jmp    11ed <witness+0x9d>
mov    -0x10(%rbp),%rdi
call   1030 <bn_check_top@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<witness>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r15
mov    %r8d,%ebp
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbx
mov    0x40(%rsp),%r9
mov    %rdi,%rsi
mov    %rcx,%rdx
mov    %r13,%rcx
mov    %r15,%r8
call   1070 <BN_mod_exp_mont@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
je     119a <witness+0x4a>
mov    %rbx,%rdi
call   1060 <BN_is_one@plt>
test   %rax,%rax
je     11ac <witness+0x5c>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <BN_cmp@plt>
test   %rax,%rax
je     1197 <witness+0x47>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
je     11fc <witness+0xac>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
mov    %r13,%rcx
mov    %r15,%r8
call   1040 <BN_mod_mul@plt>
test   %eax,%eax
je     119a <witness+0x4a>
mov    %rbx,%rdi
call   1060 <BN_is_one@plt>
test   %rax,%rax
jne    1204 <witness+0xb4>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <BN_cmp@plt>
test   %rax,%rax
jne    11c0 <witness+0x70>
jmp    1197 <witness+0x47>
mov    %rbx,%rdi
call   1030 <bn_check_top@plt>
mov    $0x1,%r14d
jmp    119a <witness+0x4a>

LLVM IR (-O0):
define i32 @witness(i32* %0, i32* %1, i32* %2, i32* %3, i32 %4, i32* %5, i32* %6) {
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32*
store i32* %0, i32** %9
store i32* %1, i32** %10
store i32* %2, i32** %11
store i32* %3, i32** %12
store i32 %4, i32* %13
store i32* %5, i32** %14
store i32* %6, i32** %15
%16 = load i32*, i32** %9
%17 = load i32*, i32** %9
%18 = load i32*, i32** %12
%19 = load i32*, i32** %10
%20 = load i32*, i32** %14
%21 = load i32*, i32** %15
%22 = call i32 @BN_mod_exp_mont(i32* %16, i32* %17, i32* %18, i32* %19, i32* %20, i32* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %7
store i32 -1, i32* %8
br label %65
25: ; preds = %7
%26 = load i32*, i32** %9
%27 = call i64 @BN_is_one(i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 0, i32* %8
br label %65
30: ; preds = %25
%31 = load i32*, i32** %9
%32 = load i32*, i32** %11
%33 = call i64 @BN_cmp(i32* %31, i32* %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 0, i32* %8
br label %65
36: ; preds = %30
br label %37
37: ; preds = %61, %36
%38 = load i32, i32* %13
%39 = add nsw i32 %38, -1
store i32 %39, i32* %13
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %62
41: ; preds = %37
%42 = load i32*, i32** %9
%43 = load i32*, i32** %9
%44 = load i32*, i32** %9
%45 = load i32*, i32** %10
%46 = load i32*, i32** %14
%47 = call i32 @BN_mod_mul(i32* %42, i32* %43, i32* %44, i32* %45, i32* %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %41
store i32 -1, i32* %8
br label %65
50: ; preds = %41
%51 = load i32*, i32** %9
%52 = call i64 @BN_is_one(i32* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
store i32 1, i32* %8
br label %65
55: ; preds = %50
%56 = load i32*, i32** %9
%57 = load i32*, i32** %11
%58 = call i64 @BN_cmp(i32* %56, i32* %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
store i32 0, i32* %8
br label %65
61: ; preds = %55
br label %37
62: ; preds = %37
%63 = load i32*, i32** %9
%64 = call i32 @bn_check_top(i32* %63)
store i32 1, i32* %8
br label %65
65: ; preds = %62, %60, %54, %49, %35, %29, %24
%66 = load i32, i32* %8
ret i32 %66
}

LLVM IR (-O2):
define i32 @witness(i32* %0, i32* %1, i32* %2, i32* %3, i32 %4, i32* %5, i32* %6) {
%8 = tail call i32 @BN_mod_exp_mont(i32* %0, i32* %0, i32* %3, i32* %1, i32* %5, i32* %6)
%9 = icmp eq i32 %8, 0
br i1 %9, label %31, label %10
10: ; preds = %7
%11 = tail call i64 @BN_is_one(i32* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = tail call i64 @BN_cmp(i32* %0, i32* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %31, label %16
16: ; preds = %13, %26
%17 = phi i32 [ %18, %26 ], [ %4, %13 ]
%18 = add nsw i32 %17, -1
%19 = icmp eq i32 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %16
%21 = tail call i32 @BN_mod_mul(i32* %0, i32* %0, i32* %0, i32* %1, i32* %5)
%22 = icmp eq i32 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %20
%24 = tail call i64 @BN_is_one(i32* %0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = tail call i64 @BN_cmp(i32* %0, i32* %2)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %16
29: ; preds = %16
%30 = tail call i32 @bn_check_top(i32* %0)
br label %31
31: ; preds = %26, %23, %20, %13, %10, %7, %29
%32 = phi i32 [ 1, %29 ], [ -1, %7 ], [ 0, %10 ], [ 0, %13 ], [ 0, %26 ], [ 1, %23 ], [ -1, %20 ]
ret i32 %32
}