#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, unsigned char *, int);
int random_bytes(unsigned char *buf, int len) {
  ssize_t n;
  int fd;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0) {
    do {
    } while (0);
    return 0;
  }
  while (len > 0) {
    n = read(fd, buf, len);
    if (n < 0) {
      if (errno == EINTR)
        continue;
      do {
      } while (0);
      close(fd);
      return 0;
    }
    if (n == 0) {
      do {
      } while (0);
      close(fd);
      return 0;
    }
    buf += n;
    len -= n;
  }
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1177 <random_bytes+0x47>
jmp    1166 <random_bytes+0x36>
jmp    116b <random_bytes+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    1238 <random_bytes+0x108>
jmp    117c <random_bytes+0x4c>
cmpl   $0x0,-0x14(%rbp)
jle    1229 <random_bytes+0xf9>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11e1 <random_bytes+0xb1>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    11c3 <random_bytes+0x93>
jmp    117c <random_bytes+0x4c>
jmp    11c8 <random_bytes+0x98>
jmp    11cd <random_bytes+0x9d>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1238 <random_bytes+0x108>
cmpq   $0x0,-0x20(%rbp)
jne    120a <random_bytes+0xda>
jmp    11f1 <random_bytes+0xc1>
jmp    11f6 <random_bytes+0xc6>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1238 <random_bytes+0x108>
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
jmp    117c <random_bytes+0x4c>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%rbp
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
lea    0xeb0(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11b8 <random_bytes+0x88>
mov    %eax,%r15d
mov    $0x1,%r14d
test   %ebx,%ebx
jle    11ae <random_bytes+0x7e>
mov    0x2e6b(%rip),%r12
mov    0x2e5c(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
test   %rax,%rax
jns    119e <random_bytes+0x6e>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1180 <random_bytes+0x50>
jmp    11ab <random_bytes+0x7b>
je     11ab <random_bytes+0x7b>
add    %rax,%rbp
sub    %eax,%ebx
test   %ebx,%ebx
jg     1180 <random_bytes+0x50>
jmp    11ae <random_bytes+0x7e>
xor    %r14d,%r14d
mov    %r15d,%edi
call   1030 <close@plt>
jmp    11bb <random_bytes+0x8b>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @random_bytes(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
br label %13
13: ; preds = %12
br label %14
14: ; preds = %13
store i32 0, i32* %3
br label %56
15: ; preds = %2
br label %16
16: ; preds = %44, %30, %15
%17 = load i32, i32* %5
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %53
19: ; preds = %16
%20 = load i32, i32* %7
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = call i64 @read(i32 %20, i8* %21, i32 %22)
store i64 %23, i64* %6
%24 = load i64, i64* %6
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %19
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %16
31: ; preds = %26
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
store i32 0, i32* %3
br label %56
36: ; preds = %19
%37 = load i64, i64* %6
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %36
br label %40
40: ; preds = %39
br label %41
41: ; preds = %40
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
store i32 0, i32* %3
br label %56
44: ; preds = %36
%45 = load i64, i64* %6
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i64 %45
store i8* %47, i8** %4
%48 = load i64, i64* %6
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = sub nsw i64 %50, %48
%52 = trunc i64 %51 to i32
store i32 %52, i32* %5
br label %16
53: ; preds = %16
%54 = load i32, i32* %7
%55 = call i32 @close(i32 %54)
store i32 1, i32* %3
br label %56
56: ; preds = %53, %41, %33, %14
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @random_bytes(i8* nocapture %0, i32 %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 slt i32 %4, 0
br i1 %5, label %28, label %6
6: ; preds = %2
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %25
8: ; preds = %6, %20
%9 = phi i8* [ %21, %20 ], [ %0, %6 ]
%10 = phi i32 [ %23, %20 ], [ %1, %6 ]
br label %11
11: ; preds = %8, %14
%12 = tail call i64 @read(i32 %4, i8* %9, i32 %10)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %11, label %25
18: ; preds = %11
%19 = icmp eq i64 %12, 0
br i1 %19, label %25, label %20
20: ; preds = %18
%21 = getelementptr inbounds i8, i8* %9, i64 %12
%22 = trunc i64 %12 to i32
%23 = sub i32 %10, %22
%24 = icmp sgt i32 %23, 0
br i1 %24, label %8, label %25
25: ; preds = %20, %18, %14, %6
%26 = phi i32 [ 1, %6 ], [ 0, %14 ], [ 0, %18 ], [ 1, %20 ]
%27 = tail call i32 @close(i32 %4)
br label %28
28: ; preds = %25, %2
%29 = phi i32 [ 0, %2 ], [ %26, %25 ]
ret i32 %29
}