#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*/ const BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_end(int /*<<< orphan*/ *);
int /*<<< orphan*/ const *BN_CTX_get(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_CTX_start(int /*<<< orphan*/ *);
int BN_GF2m_mod_exp_arr(int /*<<< orphan*/ const *, int /*<<< orphan*/ const *,
                        int /*<<< orphan*/ const *, int const *,
                        int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_set_bit(int /*<<< orphan*/ const *, int const);
int /*<<< orphan*/ BN_zero(int /*<<< orphan*/ const *);
int /*<<< orphan*/ bn_check_top(int /*<<< orphan*/ const *);
int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
                         BN_CTX *ctx) {
  int ret = 0;
  BIGNUM *u;
  bn_check_top(a);
  if (!p[0]) {
    BN_zero(r);
    return 1;
  }
  BN_CTX_start(ctx);
  if ((u = BN_CTX_get(ctx)) == NULL)
    goto err;
  if (!BN_set_bit(u, p[0] - 1))
    goto err;
  ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
  bn_check_top(r);
err:
  BN_CTX_end(ctx);
  return ret;
}


x86 assembly (clang, -O0):
<BN_GF2m_mod_sqrt_arr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <bn_check_top@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11ba <BN_GF2m_mod_sqrt_arr+0x4a>
mov    -0x10(%rbp),%rdi
call   1090 <BN_zero@plt>
movl   $0x1,-0x4(%rbp)
jmp    1233 <BN_GF2m_mod_sqrt_arr+0xc3>
mov    -0x28(%rbp),%rdi
call   1060 <BN_CTX_start@plt>
mov    -0x28(%rbp),%rdi
call   1030 <BN_CTX_get@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11df <BN_GF2m_mod_sqrt_arr+0x6f>
jmp    1224 <BN_GF2m_mod_sqrt_arr+0xb4>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
sub    $0x1,%esi
call   1080 <BN_set_bit@plt>
cmp    $0x0,%eax
jne    11ff <BN_GF2m_mod_sqrt_arr+0x8f>
jmp    1224 <BN_GF2m_mod_sqrt_arr+0xb4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1070 <BN_GF2m_mod_exp_arr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <bn_check_top@plt>
mov    -0x28(%rbp),%rdi
call   1050 <BN_CTX_end@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BN_GF2m_mod_sqrt_arr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <bn_check_top@plt>
cmpl   $0x0,(%rbx)
je     11e9 <BN_GF2m_mod_sqrt_arr+0x79>
mov    %r15,%rdi
call   1060 <BN_CTX_start@plt>
mov    %r15,%rdi
call   1030 <BN_CTX_get@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     11df <BN_GF2m_mod_sqrt_arr+0x6f>
mov    %rax,%rbp
mov    (%rbx),%esi
add    $0xffffffff,%esi
mov    %rax,%rdi
call   1080 <BN_set_bit@plt>
test   %eax,%eax
je     11df <BN_GF2m_mod_sqrt_arr+0x6f>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r15,%r8
call   1070 <BN_GF2m_mod_exp_arr@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1040 <bn_check_top@plt>
mov    %r15,%rdi
call   1050 <BN_CTX_end@plt>
jmp    11f7 <BN_GF2m_mod_sqrt_arr+0x87>
mov    %r14,%rdi
call   1090 <BN_zero@plt>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @BN_GF2m_mod_sqrt_arr(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
%11 = 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
%12 = load i32*, i32** %7
%13 = call i32 @bn_check_top(i32* %12)
%14 = load i32*, i32** %8
%15 = getelementptr inbounds i32, i32* %14, i64 0
%16 = load i32, i32* %15
%17 = icmp ne i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %4
%19 = load i32*, i32** %6
%20 = call i32 @BN_zero(i32* %19)
store i32 1, i32* %5
br label %50
21: ; preds = %4
%22 = load i32*, i32** %9
%23 = call i32 @BN_CTX_start(i32* %22)
%24 = load i32*, i32** %9
%25 = call i32* @BN_CTX_get(i32* %24)
store i32* %25, i32** %11
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %28
27: ; preds = %21
br label %46
28: ; preds = %21
%29 = load i32*, i32** %11
%30 = load i32*, i32** %8
%31 = getelementptr inbounds i32, i32* %30, i64 0
%32 = load i32, i32* %31
%33 = sub nsw i32 %32, 1
%34 = call i32 @BN_set_bit(i32* %29, i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %28
br label %46
37: ; preds = %28
%38 = load i32*, i32** %6
%39 = load i32*, i32** %7
%40 = load i32*, i32** %11
%41 = load i32*, i32** %8
%42 = load i32*, i32** %9
%43 = call i32 @BN_GF2m_mod_exp_arr(i32* %38, i32* %39, i32* %40, i32* %41, i32* %42)
store i32 %43, i32* %10
%44 = load i32*, i32** %6
%45 = call i32 @bn_check_top(i32* %44)
br label %46
46: ; preds = %37, %36, %27
%47 = load i32*, i32** %9
%48 = call i32 @BN_CTX_end(i32* %47)
%49 = load i32, i32* %10
store i32 %49, i32* %5
br label %50
50: ; preds = %46, %18
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
define i32 @BN_GF2m_mod_sqrt_arr(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = tail call i32 @bn_check_top(i32* %1)
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @BN_zero(i32* %0)
br label %25
10: ; preds = %4
%11 = tail call i32 @BN_CTX_start(i32* %3)
%12 = tail call i32* @BN_CTX_get(i32* %3)
%13 = icmp eq i32* %12, null
br i1 %13, label %22, label %14
14: ; preds = %10
%15 = load i32, i32* %2
%16 = add nsw i32 %15, -1
%17 = tail call i32 @BN_set_bit(i32* %12, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = tail call i32 @BN_GF2m_mod_exp_arr(i32* %0, i32* %1, i32* %12, i32* %2, i32* %3)
%21 = tail call i32 @bn_check_top(i32* %0)
br label %22
22: ; preds = %14, %10, %19
%23 = phi i32 [ 0, %10 ], [ %20, %19 ], [ 0, %14 ]
%24 = tail call i32 @BN_CTX_end(i32* %3)
br label %25
25: ; preds = %22, %8
%26 = phi i32 [ %23, %22 ], [ 1, %8 ]
ret i32 %26
}