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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int /*<<< orphan*/ exit_if(int, char *);
int open(char *, int /*<<< orphan*/);
int read(int, void *, size_t);
void arc4random_buf(void *buffer, size_t nbytes) {
  int bytes_read, file_descriptor;
  file_descriptor = open("/dev/urandom", O_RDONLY);
  exit_if(file_descriptor == -1, "Error opening file");
  bytes_read = read(file_descriptor, buffer, nbytes);
  exit_if(bytes_read == -1, "Error reading file");
  exit_if(close(file_descriptor) == -1, "Error closing file");
}


x86 assembly (clang, -O0):
<arc4random_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe92(%rip),%rsi
call   1030 <exit_if@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe7a(%rip),%rsi
call   1030 <exit_if@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe6e(%rip),%rsi
call   1030 <exit_if@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arc4random_buf>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
lea    0xe9b(%rip),%rsi
call   1030 <exit_if@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
lea    0xe8c(%rip),%rsi
call   1030 <exit_if@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
lea    0xe83(%rip),%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <exit_if@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [19 x i8] c"Error opening file\00"
@.str.2 = [19 x i8] c"Error reading file\00"
@.str.3 = [19 x i8] c"Error closing file\00"
define void @arc4random_buf(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, -1
%11 = zext i1 %10 to i32
%12 = call i32 @exit_if(i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* %6
%14 = load i8*, i8** %3
%15 = load i64, i64* %4
%16 = call i32 @read(i32 %13, i8* %14, i64 %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, -1
%19 = zext i1 %18 to i32
%20 = call i32 @exit_if(i32 %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
%23 = icmp eq i32 %22, -1
%24 = zext i1 %23 to i32
%25 = call i32 @exit_if(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [19 x i8] c"Error opening file\00"
@.str.2 = [19 x i8] c"Error reading file\00"
@.str.3 = [19 x i8] c"Error closing file\00"
define void @arc4random_buf(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
%6 = zext i1 %5 to i32
%7 = tail call i32 @exit_if(i32 %6, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @read(i32 %4, i8* %0, i64 %1)
%9 = icmp eq i32 %8, -1
%10 = zext i1 %9 to i32
%11 = tail call i32 @exit_if(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @close(i32 %4)
%13 = icmp eq i32 %12, -1
%14 = zext i1 %13 to i32
%15 = tail call i32 @exit_if(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
ret void
}