#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, char *, int);
void fill_random(char *buf, int size) {
  int fd, chunk, done = 0;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd == -1)
    return;
  while (done < size) {
    chunk = read(fd, buf + done, size - done);
    if (chunk <= 0)
      break;
    done += chunk;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<fill_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0xeaa(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    116d <fill_random+0x3d>
jmp    11bf <fill_random+0x8f>
jmp    1172 <fill_random+0x42>
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b7 <fill_random+0x87>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11a9 <fill_random+0x79>
jmp    11b7 <fill_random+0x87>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1172 <fill_random+0x42>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_random>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
lea    0xeb4(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     118f <fill_random+0x5f>
mov    %eax,%r15d
test   %ebx,%ebx
jle    117d <fill_random+0x4d>
xor    %ebp,%ebp
nop
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1040 <read@plt>
test   %eax,%eax
jle    117d <fill_random+0x4d>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1160 <fill_random+0x30>
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>
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 void @fill_random(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %7
%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* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
br label %38
13: ; preds = %2
br label %14
14: ; preds = %31, %13
%15 = load i32, i32* %7
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %35
18: ; preds = %14
%19 = load i32, i32* %5
%20 = load i8*, i8** %3
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i32, i32* %4
%25 = load i32, i32* %7
%26 = sub nsw i32 %24, %25
%27 = call i32 @read(i32 %19, i8* %23, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %18
br label %35
31: ; preds = %18
%32 = load i32, i32* %6
%33 = load i32, i32* %7
%34 = add nsw i32 %33, %32
store i32 %34, i32* %7
br label %14
35: ; preds = %30, %14
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %12
ret void
}

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