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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
unsigned long rand();
int /*<<< orphan*/ sanei_debug_sanei_auth_call(int, char *);
unsigned long randombits(void) {
  FILE *dev_urandom;
  unsigned long result = 0;
  char buffer[4];
  if ((dev_urandom = fopen("/dev/urandom", "r")) == NULL) {
    sanei_debug_sanei_auth_call(2,
                                "randombits: could not open /dev/urandom...\n");
    return rand();
  }
  fread(buffer, 1, 4, dev_urandom);
  fclose(dev_urandom);
  result = buffer[0];
  result <<= 8;
  result += buffer[1];
  result <<= 8;
  result += buffer[2];
  result <<= 8;
  result += buffer[3];
  return result;
}


x86 assembly (clang, -O0):
<randombits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x18(%rbp)
lea    0xe99(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a2 <randombits+0x52>
mov    $0x2,%edi
lea    0xe82(%rip),%rsi
call   1050 <sanei_debug_sanei_auth_call@plt>
mov    $0x0,%al
call   1070 <rand@plt>
mov    %rax,-0x8(%rbp)
jmp    121e <randombits+0xce>
lea    -0x1c(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movsbq -0x1c(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x8,%rax
mov    %rax,-0x18(%rbp)
movsbq -0x1b(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x8,%rax
mov    %rax,-0x18(%rbp)
movsbq -0x1a(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x8,%rax
mov    %rax,-0x18(%rbp)
movsbq -0x19(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randombits>:
push   %rbx
sub    $0x10,%rsp
lea    0xea4(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c2 <randombits+0x72>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movsbq 0xc(%rsp),%rax
shl    $0x8,%rax
movsbq 0xd(%rsp),%rcx
add    %rax,%rcx
shl    $0x8,%rcx
movsbq 0xe(%rsp),%rdx
add    %rcx,%rdx
shl    $0x8,%rdx
movsbq 0xf(%rsp),%rax
add    %rdx,%rax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe46(%rip),%rsi
mov    $0x2,%edi
call   1050 <sanei_debug_sanei_auth_call@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
jmp    1070 <rand@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [44 x i8] c"randombits: could not open /dev/urandom...\0A\00"
define i64 @randombits() {
%1 = alloca i64
%2 = alloca i32*
%3 = alloca i64
%4 = alloca [4 x i8]
store i64 0, i64* %3
%5 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @sanei_debug_sanei_auth_call(i32 2, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%9 = call i64 (...) @rand()
store i64 %9, i64* %1
br label %41
10: ; preds = %0
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%12 = load i32*, i32** %2
%13 = call i32 @fread(i8* %11, i32 1, i32 4, i32* %12)
%14 = load i32*, i32** %2
%15 = call i32 @fclose(i32* %14)
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i64
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = shl i64 %19, 8
store i64 %20, i64* %3
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%22 = load i8, i8* %21
%23 = sext i8 %22 to i64
%24 = load i64, i64* %3
%25 = add i64 %24, %23
store i64 %25, i64* %3
%26 = load i64, i64* %3
%27 = shl i64 %26, 8
store i64 %27, i64* %3
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%29 = load i8, i8* %28
%30 = sext i8 %29 to i64
%31 = load i64, i64* %3
%32 = add i64 %31, %30
store i64 %32, i64* %3
%33 = load i64, i64* %3
%34 = shl i64 %33, 8
store i64 %34, i64* %3
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
%36 = load i8, i8* %35
%37 = sext i8 %36 to i64
%38 = load i64, i64* %3
%39 = add i64 %38, %37
store i64 %39, i64* %3
%40 = load i64, i64* %3
store i64 %40, i64* %1
br label %41
41: ; preds = %10, %7
%42 = load i64, i64* %1
ret i64 %42
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [44 x i8] c"randombits: could not open /dev/urandom...\0A\00"
define i64 @randombits() {
%1 = alloca [4 x i8]
%2 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @sanei_debug_sanei_auth_call(i32 2, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i64 (...) @rand()
br label %28
8: ; preds = %0
%9 = call i32 @fread(i8* %2, i32 1, i32 4, i32* %3)
%10 = tail call i32 @fclose(i32* %3)
%11 = load i8, i8* %2
%12 = sext i8 %11 to i64
%13 = shl nsw i64 %12, 8
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 1
%15 = load i8, i8* %14
%16 = sext i8 %15 to i64
%17 = add nsw i64 %13, %16
%18 = shl nsw i64 %17, 8
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 2
%20 = load i8, i8* %19
%21 = sext i8 %20 to i64
%22 = add nsw i64 %18, %21
%23 = shl nsw i64 %22, 8
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 3
%25 = load i8, i8* %24
%26 = sext i8 %25 to i64
%27 = add nsw i64 %23, %26
br label %28
28: ; preds = %8, %5
%29 = phi i64 [ %7, %5 ], [ %27, %8 ]
ret i64 %29
}