#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;
typedef int /*<<< orphan*/ ASN1_INTEGER;

/* Variables and functions */
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_pseudo_rand(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                                  int /*<<< orphan*/);
int /*<<< orphan*/ BN_to_ASN1_INTEGER(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int rand_serial(BIGNUM *b, ASN1_INTEGER *ai) {
  BIGNUM *btmp;
  int ret = 0;
  if (b)
    btmp = b;
  else
    btmp = BN_new();
  if (!btmp)
    return 0;
  if (!BN_pseudo_rand(btmp, 64, 0, 0))
    goto error;
  if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
    goto error;
  ret = 1;
error:
  if (!b)
    BN_free(btmp);
  return ret;
}


x86 assembly (clang, -O0):
<rand_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116f <rand_serial+0x2f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    117a <rand_serial+0x3a>
mov    $0x0,%al
call   1030 <BN_new@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <rand_serial+0x51>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <rand_serial+0xb8>
mov    -0x20(%rbp),%rdi
mov    $0x40,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1050 <BN_pseudo_rand@plt>
cmp    $0x0,%eax
jne    11b1 <rand_serial+0x71>
jmp    11de <rand_serial+0x9e>
cmpq   $0x0,-0x18(%rbp)
je     11d7 <rand_serial+0x97>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <BN_to_ASN1_INTEGER@plt>
cmp    $0x0,%eax
jne    11d7 <rand_serial+0x97>
jmp    11de <rand_serial+0x9e>
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f2 <rand_serial+0xb2>
mov    -0x20(%rbp),%rdi
call   1040 <BN_free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_serial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rdi,%rbp
test   %rdi,%rdi
jne    1167 <rand_serial+0x27>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <BN_new@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11a6 <rand_serial+0x66>
xor    %r14d,%r14d
mov    %rbp,%rdi
mov    $0x40,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <BN_pseudo_rand@plt>
test   %eax,%eax
je     1199 <rand_serial+0x59>
test   %r15,%r15
je     1193 <rand_serial+0x53>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <BN_to_ASN1_INTEGER@plt>
test   %eax,%eax
je     1199 <rand_serial+0x59>
mov    $0x1,%r14d
test   %rbx,%rbx
jne    11a6 <rand_serial+0x66>
mov    %rbp,%rdi
call   1040 <BN_free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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