#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 /*<<< orphan*/ BN_GF2m_mod_inv(int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_GF2m_mod_mul(int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ bn_check_top(int /*<<< orphan*/ const *);
int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
                    const BIGNUM *p, BN_CTX *ctx) {
  BIGNUM *xinv = NULL;
  int ret = 0;
  bn_check_top(y);
  bn_check_top(x);
  bn_check_top(p);
  BN_CTX_start(ctx);
  xinv = BN_CTX_get(ctx);
  if (xinv == NULL)
    goto err;
  if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
    goto err;
  if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
    goto err;
  bn_check_top(r);
  ret = 1;
err:
  BN_CTX_end(ctx);
  return ret;
}


x86 assembly (clang, -O0):
<BN_GF2m_mod_div>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <bn_check_top@plt>
mov    -0x18(%rbp),%rdi
call   1060 <bn_check_top@plt>
mov    -0x20(%rbp),%rdi
call   1060 <bn_check_top@plt>
mov    -0x28(%rbp),%rdi
call   1080 <BN_CTX_start@plt>
mov    -0x28(%rbp),%rdi
call   1030 <BN_CTX_get@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11cc <BN_GF2m_mod_div+0x6c>
jmp    1226 <BN_GF2m_mod_div+0xc6>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <BN_GF2m_mod_inv@plt>
cmp    $0x0,%eax
jne    11ef <BN_GF2m_mod_div+0x8f>
jmp    1226 <BN_GF2m_mod_div+0xc6>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <BN_GF2m_mod_mul@plt>
cmp    $0x0,%eax
jne    1216 <BN_GF2m_mod_div+0xb6>
jmp    1226 <BN_GF2m_mod_div+0xc6>
mov    -0x8(%rbp),%rdi
call   1060 <bn_check_top@plt>
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <BN_CTX_end@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BN_GF2m_mod_div>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %rdi,(%rsp)
mov    %rsi,%rdi
call   1060 <bn_check_top@plt>
mov    %rbp,%rdi
call   1060 <bn_check_top@plt>
mov    %r15,%rdi
call   1060 <bn_check_top@plt>
mov    %rbx,%rdi
call   1080 <BN_CTX_start@plt>
mov    %rbx,%rdi
call   1030 <BN_CTX_get@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     11ed <BN_GF2m_mod_div+0x8d>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1050 <BN_GF2m_mod_inv@plt>
test   %eax,%eax
je     11ed <BN_GF2m_mod_div+0x8d>
mov    (%rsp),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rbx,%r8
call   1040 <BN_GF2m_mod_mul@plt>
test   %eax,%eax
je     11ed <BN_GF2m_mod_div+0x8d>
mov    %rbp,%rdi
call   1060 <bn_check_top@plt>
mov    $0x1,%r12d
mov    %rbx,%rdi
call   1070 <BN_CTX_end@plt>
mov    %r12d,%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_div(i32* %0, i32* %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
store i32* null, i32** %11
store i32 0, i32* %12
%13 = load i32*, i32** %7
%14 = call i32 @bn_check_top(i32* %13)
%15 = load i32*, i32** %8
%16 = call i32 @bn_check_top(i32* %15)
%17 = load i32*, i32** %9
%18 = call i32 @bn_check_top(i32* %17)
%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** %11
%23 = load i32*, i32** %11
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %5
br label %46
26: ; preds = %5
%27 = load i32*, i32** %11
%28 = load i32*, i32** %8
%29 = load i32*, i32** %9
%30 = load i32*, i32** %10
%31 = call i32 @BN_GF2m_mod_inv(i32* %27, i32* %28, i32* %29, i32* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %26
br label %46
34: ; preds = %26
%35 = load i32*, i32** %6
%36 = load i32*, i32** %7
%37 = load i32*, i32** %11
%38 = load i32*, i32** %9
%39 = load i32*, i32** %10
%40 = call i32 @BN_GF2m_mod_mul(i32* %35, i32* %36, i32* %37, i32* %38, i32* %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %34
br label %46
43: ; preds = %34
%44 = load i32*, i32** %6
%45 = call i32 @bn_check_top(i32* %44)
store i32 1, i32* %12
br label %46
46: ; preds = %43, %42, %33, %25
%47 = load i32*, i32** %10
%48 = call i32 @BN_CTX_end(i32* %47)
%49 = load i32, i32* %12
ret i32 %49
}

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