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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
int rand_bytes(uint8_t *buf, int size) {
  int c, l, fd;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd == -1) {
    return 0;
  }
  l = 0;
  do {
    c = read(fd, buf + l, size - l);
    l += c;
    if (c == -1) {
      return l;
    }
  } while (l < size);
  close(fd);
  return l;
}


x86 assembly (clang, -O0):
<rand_bytes>:
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,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    116d <rand_bytes+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <rand_bytes+0xa1>
movl   $0x0,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b2 <rand_bytes+0x82>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <rand_bytes+0xa1>
jmp    11b7 <rand_bytes+0x87>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1174 <rand_bytes+0x44>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
lea    0xeb4(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1188 <rand_bytes+0x58>
mov    %eax,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rbx
lea    (%r14,%rbx,4),%rsi
mov    %ebp,%edx
sub    %ebx,%edx
mov    %r15d,%edi
call   1040 <read@plt>
add    %eax,%ebx
cmp    $0xffffffff,%eax
je     118a <rand_bytes+0x5a>
cmp    %ebp,%ebx
jl     1160 <rand_bytes+0x30>
mov    %r15d,%edi
call   1030 <close@plt>
jmp    118a <rand_bytes+0x5a>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @rand_bytes(i32* 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 eq i32 %4, -1
br i1 %5, label %18, label %6
6: ; preds = %2, %14
%7 = phi i32 [ %12, %14 ], [ 0, %2 ]
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = sub nsw i32 %1, %7
%11 = tail call i32 @read(i32 %4, i32* %9, i32 %10)
%12 = add nsw i32 %11, %7
%13 = icmp eq i32 %11, -1
br i1 %13, label %18, label %14
14: ; preds = %6
%15 = icmp slt i32 %12, %1
br i1 %15, label %6, label %16
16: ; preds = %14
%17 = tail call i32 @close(i32 %4)
br label %18
18: ; preds = %6, %2, %16
%19 = phi i32 [ %12, %16 ], [ 0, %2 ], [ %12, %6 ]
ret i32 %19
}