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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
size_t rand_gather(char *bytes, size_t len) {
  int fd = open("/dev/urandom", 0);
  if (fd == -1) {
    return -1;
  }
  size_t bread = 0;
  while (len - bread > 0) {
    ssize_t r = read(fd, &bytes[bread], len - bread);
    if (r == -1) {
      close(fd);
      return bread;
    }
    bread += r;
  }
  close(fd);
  return bread;
}


x86 assembly (clang, -O0):
<rand_gather>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xeb9(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1168 <rand_gather+0x38>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11dd <rand_gather+0xad>
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    $0x0,%rax
jbe    11cd <rand_gather+0x9d>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11bc <rand_gather+0x8c>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11dd <rand_gather+0xad>
movslq -0x2c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1170 <rand_gather+0x40>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_gather>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xebc(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1186 <rand_gather+0x56>
mov    %eax,%ebp
xor    %ebx,%ebx
test   %r14,%r14
je     117d <rand_gather+0x4d>
mov    %r14,%rdx
nopl   0x0(%rax)
lea    (%r15,%rbx,1),%rsi
mov    %ebp,%edi
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     117d <rand_gather+0x4d>
cltq
add    %rax,%rbx
mov    %r14,%rdx
sub    %rbx,%rdx
jne    1160 <rand_gather+0x30>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    118d <rand_gather+0x5d>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
define i64 @rand_gather(i8* nocapture %0, i64 %1) {
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = icmp eq i64 %1, 0
br i1 %6, label %18, label %10
7: ; preds = %10
%8 = sub i64 %1, %17
%9 = icmp eq i64 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %5, %7
%11 = phi i64 [ %8, %7 ], [ %1, %5 ]
%12 = phi i64 [ %17, %7 ], [ 0, %5 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = tail call i32 @read(i32 %3, i8* %13, i64 %11)
%15 = icmp eq i32 %14, -1
%16 = sext i32 %14 to i64
%17 = add i64 %12, %16
br i1 %15, label %18, label %7
18: ; preds = %7, %10, %5
%19 = phi i64 [ 0, %5 ], [ %12, %10 ], [ %17, %7 ]
%20 = tail call i32 @close(i32 %3)
br label %21
21: ; preds = %18, %2
%22 = phi i64 [ -1, %2 ], [ %19, %18 ]
ret i64 %22
}