#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*/ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int getRandom(char *buf, int len) {
  int randomfd = 0;
  randomfd = open("/dev/urandom", O_RDONLY);
  if (-1 == randomfd) {
    perror("generate_LUKS_masterkey:");
    return -1;
  }
  while (len) {
    int r;
    r = read(randomfd, buf, len);
    if (-1 == r && errno != -EINTR) {
      perror("read: ");
      return -1;
    }
    len -= r;
    buf += r;
  }
  close(randomfd);
  return 0;
}


x86 assembly (clang, -O0):
<getRandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    1194 <getRandom+0x54>
lea    0xe8a(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <getRandom+0xe7>
jmp    1199 <getRandom+0x59>
cmpl   $0x0,-0x14(%rbp)
je     1218 <getRandom+0xd8>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11f7 <getRandom+0xb7>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2dfb(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
je     11f7 <getRandom+0xb7>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <getRandom+0xe7>
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1199 <getRandom+0x59>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRandom>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11c1 <getRandom+0x81>
mov    %eax,%r14d
test   %ebp,%ebp
je     11ae <getRandom+0x6e>
mov    0x2e63(%rip),%r15
mov    0x2e54(%rip),%r12
jmp    118a <getRandom+0x4a>
xchg   %ax,%ax
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
je     11ae <getRandom+0x6e>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1180 <getRandom+0x40>
mov    (%r15),%ecx
add    (%r12),%ecx
je     1180 <getRandom+0x40>
lea    0xe7a(%rip),%rdi
jmp    11c8 <getRandom+0x88>
mov    %r14d,%edi
call   1030 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11b8 <getRandom+0x78>

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

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