#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 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;
  }
  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   1060 <BN_mod_exp_mont@plt>
cmp    $0x0,%eax
jne    1196 <witness+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <witness+0x12b>
mov    -0x10(%rbp),%rdi
call   1050 <BN_is_one@plt>
cmp    $0x0,%rax
je     11b5 <witness+0x75>
movl   $0x0,-0x4(%rbp)
jmp    126b <witness+0x12b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <BN_cmp@plt>
cmp    $0x0,%rax
jne    11d8 <witness+0x98>
movl   $0x0,-0x4(%rbp)
jmp    126b <witness+0x12b>
jmp    11dd <witness+0x9d>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     1264 <witness+0x124>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x38(%rbp),%r8
call   1030 <BN_mod_mul@plt>
cmp    $0x0,%eax
jne    121d <witness+0xdd>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <witness+0x12b>
mov    -0x10(%rbp),%rdi
call   1050 <BN_is_one@plt>
cmp    $0x0,%rax
je     123c <witness+0xfc>
movl   $0x1,-0x4(%rbp)
jmp    126b <witness+0x12b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <BN_cmp@plt>
cmp    $0x0,%rax
jne    125f <witness+0x11f>
movl   $0x0,-0x4(%rbp)
jmp    126b <witness+0x12b>
jmp    11dd <witness+0x9d>
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,%r13d
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x40(%rsp),%r9
mov    %rdi,%rsi
mov    %rcx,%rdx
mov    %r12,%rcx
mov    %r15,%r8
call   1060 <BN_mod_exp_mont@plt>
test   %eax,%eax
je     11dd <witness+0x9d>
mov    %rbx,%rdi
call   1050 <BN_is_one@plt>
test   %rax,%rax
je     1185 <witness+0x45>
xor    %ebp,%ebp
jmp    11e2 <witness+0xa2>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <BN_cmp@plt>
test   %rax,%rax
je     1181 <witness+0x41>
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%r13d
je     11e2 <witness+0xa2>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
mov    %r15,%r8
call   1030 <BN_mod_mul@plt>
test   %eax,%eax
je     11dd <witness+0x9d>
mov    %rbx,%rdi
call   1050 <BN_is_one@plt>
test   %rax,%rax
jne    11e2 <witness+0xa2>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <BN_cmp@plt>
test   %rax,%rax
jne    11a0 <witness+0x60>
jmp    1181 <witness+0x41>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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 %63
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 %63
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 %63
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 %63
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 %63
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 %63
61: ; preds = %55
br label %37
62: ; preds = %37
store i32 1, i32* %8
br label %63
63: ; preds = %62, %60, %54, %49, %35, %29, %24
%64 = load i32, i32* %8
ret i32 %64
}

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 %29, 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 %29
13: ; preds = %10
%14 = tail call i64 @BN_cmp(i32* %0, i32* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %29, 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 %29, 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 %29
26: ; preds = %23
%27 = tail call i64 @BN_cmp(i32* %0, i32* %2)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %16
29: ; preds = %16, %26, %23, %20, %13, %10, %7
%30 = phi i32 [ -1, %7 ], [ 0, %10 ], [ 0, %13 ], [ 1, %16 ], [ 0, %26 ], [ 1, %23 ], [ -1, %20 ]
ret i32 %30
}