#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);
  if ((xinv = BN_CTX_get(ctx)) == 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)
cmp    $0x0,%rax
jne    11cb <BN_GF2m_mod_div+0x6b>
jmp    1225 <BN_GF2m_mod_div+0xc5>
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    11ee <BN_GF2m_mod_div+0x8e>
jmp    1225 <BN_GF2m_mod_div+0xc5>
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    1215 <BN_GF2m_mod_div+0xb5>
jmp    1225 <BN_GF2m_mod_div+0xc5>
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 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %5
br label %45
25: ; preds = %5
%26 = load i32*, i32** %11
%27 = load i32*, i32** %8
%28 = load i32*, i32** %9
%29 = load i32*, i32** %10
%30 = call i32 @BN_GF2m_mod_inv(i32* %26, i32* %27, i32* %28, i32* %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %25
br label %45
33: ; preds = %25
%34 = load i32*, i32** %6
%35 = load i32*, i32** %7
%36 = load i32*, i32** %11
%37 = load i32*, i32** %9
%38 = load i32*, i32** %10
%39 = call i32 @BN_GF2m_mod_mul(i32* %34, i32* %35, i32* %36, i32* %37, i32* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %33
br label %45
42: ; preds = %33
%43 = load i32*, i32** %6
%44 = call i32 @bn_check_top(i32* %43)
store i32 1, i32* %12
br label %45
45: ; preds = %42, %41, %32, %24
%46 = load i32*, i32** %10
%47 = call i32 @BN_CTX_end(i32* %46)
%48 = load i32, i32* %12
ret i32 %48
}

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
}