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

/* Variables and functions */
int /*<<< orphan*/ BN_add(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ const *);
scalar_t__ BN_cmp(int /*<<< orphan*/ const *, int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_sub(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *);
int /*<<< orphan*/ bn_rand_range(int /*<<< orphan*/, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/ *);
int bn_rand_interval(BIGNUM *rnd, const BIGNUM *lower_inc,
                     const BIGNUM *upper_exc) {
  BIGNUM *len = NULL;
  int ret = 0;
  if (BN_cmp(lower_inc, upper_exc) >= 0)
    goto err;
  if ((len = BN_new()) == NULL)
    goto err;
  if (!BN_sub(len, upper_exc, lower_inc))
    goto err;
  if (!bn_rand_range(0, rnd, len))
    goto err;
  if (!BN_add(rnd, rnd, lower_inc))
    goto err;
  ret = 1;
err:
  BN_free(len);
  return ret;
}


x86 assembly (clang, -O0):
<bn_rand_interval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <BN_cmp@plt>
cmp    $0x0,%rax
jl     119f <bn_rand_interval+0x3f>
jmp    121b <bn_rand_interval+0xbb>
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b9 <bn_rand_interval+0x59>
jmp    121b <bn_rand_interval+0xbb>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1060 <BN_sub@plt>
cmp    $0x0,%eax
jne    11d8 <bn_rand_interval+0x78>
jmp    121b <bn_rand_interval+0xbb>
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %edi,%edi
call   1050 <bn_rand_range@plt>
cmp    $0x0,%eax
jne    11f5 <bn_rand_interval+0x95>
jmp    121b <bn_rand_interval+0xbb>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <BN_add@plt>
cmp    $0x0,%eax
jne    1214 <bn_rand_interval+0xb4>
jmp    121b <bn_rand_interval+0xbb>
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <BN_free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bn_rand_interval>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1080 <BN_cmp@plt>
test   %rax,%rax
js     119d <bn_rand_interval+0x3d>
xor    %r13d,%r13d
xor    %r14d,%r14d
mov    %r13,%rdi
call   1070 <BN_free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <BN_new@plt>
test   %rax,%rax
je     11ee <bn_rand_interval+0x8e>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <BN_sub@plt>
test   %eax,%eax
je     1188 <bn_rand_interval+0x28>
xor    %r14d,%r14d
xor    %edi,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <bn_rand_range@plt>
test   %eax,%eax
je     1188 <bn_rand_interval+0x28>
mov    %r12,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <BN_add@plt>
xor    %r14d,%r14d
test   %eax,%eax
setne  %r14b
jmp    1188 <bn_rand_interval+0x28>
xor    %r13d,%r13d
jmp    1188 <bn_rand_interval+0x28>

LLVM IR (-O0):
define i32 @bn_rand_interval(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i32* null, i32** %7
store i32 0, i32* %8
%9 = load i32*, i32** %5
%10 = load i32*, i32** %6
%11 = call i64 @BN_cmp(i32* %9, i32* %10)
%12 = icmp sge i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
br label %39
14: ; preds = %3
%15 = call i32* (...) @BN_new()
store i32* %15, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %14
br label %39
18: ; preds = %14
%19 = load i32*, i32** %7
%20 = load i32*, i32** %6
%21 = load i32*, i32** %5
%22 = call i32 @BN_sub(i32* %19, i32* %20, i32* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %18
br label %39
25: ; preds = %18
%26 = load i32*, i32** %4
%27 = load i32*, i32** %7
%28 = call i32 @bn_rand_range(i32 0, i32* %26, i32* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %25
br label %39
31: ; preds = %25
%32 = load i32*, i32** %4
%33 = load i32*, i32** %4
%34 = load i32*, i32** %5
%35 = call i32 @BN_add(i32* %32, i32* %33, i32* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %31
br label %39
38: ; preds = %31
store i32 1, i32* %8
br label %39
39: ; preds = %38, %37, %30, %24, %17, %13
%40 = load i32*, i32** %7
%41 = call i32 @BN_free(i32* %40)
%42 = load i32, i32* %8
ret i32 %42
}

LLVM IR (-O2):
define i32 @bn_rand_interval(i32* %0, i32* %1, i32* %2) {
%4 = tail call i64 @BN_cmp(i32* %1, i32* %2)
%5 = icmp sgt i64 %4, -1
br i1 %5, label %19, label %6
6: ; preds = %3
%7 = tail call i32* (...) @BN_new()
%8 = icmp eq i32* %7, null
br i1 %8, label %19, label %9
9: ; preds = %6
%10 = tail call i32 @BN_sub(i32* %7, i32* %2, i32* %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %9
%13 = tail call i32 @bn_rand_range(i32 0, i32* %0, i32* %7)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = tail call i32 @BN_add(i32* %0, i32* %0, i32* %1)
%17 = icmp ne i32 %16, 0
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %15, %12, %9, %6, %3
%20 = phi i32* [ null, %3 ], [ null, %6 ], [ %7, %12 ], [ %7, %9 ], [ %7, %15 ]
%21 = phi i32 [ 0, %3 ], [ 0, %6 ], [ 0, %12 ], [ 0, %9 ], [ %18, %15 ]
%22 = tail call i32 @BN_free(i32* %20)
ret i32 %21
}