#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 /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int genrand_dev(unsigned char *buffer, int buf_len) {
  int fd;
  fd = open("/dev/random", O_RDONLY);
  if (fd < 0)
    return 0;
  if (read(fd, buffer, buf_len) < buf_len) {
    close(fd);
    return 0;
  }
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<genrand_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    116d <genrand_dev+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    11a8 <genrand_dev+0x78>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
cmp    -0x14(%rbp),%eax
jge    1199 <genrand_dev+0x69>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a8 <genrand_dev+0x78>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genrand_dev>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
lea    0xeb3(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1176 <genrand_dev+0x46>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <read@plt>
xor    %ebx,%ebx
cmp    %r15d,%eax
setge  %bl
mov    %ebp,%edi
call   1030 <close@plt>
jmp    1178 <genrand_dev+0x48>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @genrand_dev(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %25
12: ; preds = %2
%13 = load i32, i32* %6
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = call i32 @read(i32 %13, i8* %14, i32 %15)
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
store i32 0, i32* %3
br label %25
22: ; preds = %12
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
store i32 1, i32* %3
br label %25
25: ; preds = %22, %19, %11
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @genrand_dev(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call i32 @read(i32 %4, i8* %0, i32 %1)
%8 = icmp sge i32 %7, %1
%9 = tail call i32 @close(i32 %4)
%10 = zext i1 %8 to i32
br label %11
11: ; preds = %6, %2
%12 = phi i32 [ 0, %2 ], [ %10, %6 ]
ret i32 %12
}