#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;

/* Variables and functions */
int EAGAIN;
int EINTR;
int EIO;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int O_RDONLY;
int /*<<< orphan*/ RNDGETENTCNT;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int fstat(int, struct stat *);
scalar_t__ gotdata(void *, size_t);
int ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int, int /*<<< orphan*/);
int read(int, char *, size_t);
int getentropy_urandom(void *buf, size_t len) {
  struct stat st;
  size_t i;
  int fd, cnt, flags;
  int save_errno = errno;
start:
  flags = O_RDONLY;
  fd = open("/dev/urandom", flags, 0);
  if (fd == -1) {
    if (errno == EINTR)
      goto start;
    goto nodevrandom;
  }
  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
  if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) {
    close(fd);
    goto nodevrandom;
  }
  if (ioctl(fd, RNDGETENTCNT, &cnt) == -1) {
    close(fd);
    goto nodevrandom;
  }
  for (i = 0; i < len;) {
    size_t wanted = len - i;
    ssize_t ret = read(fd, (char *)buf + i, wanted);
    if (ret == -1) {
      if (errno == EAGAIN || errno == EINTR)
        continue;
      close(fd);
      goto nodevrandom;
    }
    i += ret;
  }
  close(fd);
  if (gotdata(buf, len) == 0) {
    errno = save_errno;
    return 0;
  }
nodevrandom:
  errno = EIO;
  return -1;
}


x86 assembly (clang, -O0):
<getentropy_urandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe4e(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e8 <getentropy_urandom+0x68>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%eax
jne    11e3 <getentropy_urandom+0x63>
jmp    119c <getentropy_urandom+0x1c>
jmp    1355 <getentropy_urandom+0x1d5>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    %eax,%edx
mov    0x2da4(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
je     124b <getentropy_urandom+0xcb>
mov    -0x20(%rbp),%edi
call   1070 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    1258 <getentropy_urandom+0xd8>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    1355 <getentropy_urandom+0x1d5>
mov    -0x2c(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1283 <getentropy_urandom+0x103>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    1355 <getentropy_urandom+0x1d5>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1319 <getentropy_urandom+0x199>
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1308 <getentropy_urandom+0x188>
mov    0x2ceb(%rip),%rax
mov    (%rax),%eax
mov    0x2cc2(%rip),%rcx
cmp    (%rcx),%eax
je     12f6 <getentropy_urandom+0x176>
mov    0x2cd3(%rip),%rax
mov    (%rax),%eax
mov    0x2cba(%rip),%rcx
cmp    (%rcx),%eax
jne    12fb <getentropy_urandom+0x17b>
jmp    128b <getentropy_urandom+0x10b>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    1355 <getentropy_urandom+0x1d5>
movslq -0x44(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    128b <getentropy_urandom+0x10b>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <gotdata@plt>
cmp    $0x0,%rax
jne    1350 <getentropy_urandom+0x1d0>
mov    -0x38(%rbp),%ecx
mov    0x2c76(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    136e <getentropy_urandom+0x1ee>
jmp    1355 <getentropy_urandom+0x1d5>
mov    0x2c74(%rip),%rax
mov    (%rax),%ecx
mov    0x2c53(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getentropy_urandom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,0x8(%rsp)
mov    0x2e1b(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    0x2e3c(%rip),%r14
lea    0xe4d(%rip),%rbx
mov    0x2dee(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    11e1 <getentropy_urandom+0x61>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     11c0 <getentropy_urandom+0x40>
jmp    129c <getentropy_urandom+0x11c>
mov    %eax,%ebp
mov    0x2dde(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2dbb(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1295 <getentropy_urandom+0x115>
mov    (%rsp),%edi
call   1070 <S_ISCHR@plt>
test   %eax,%eax
je     1295 <getentropy_urandom+0x115>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1295 <getentropy_urandom+0x115>
test   %r15,%r15
mov    0x8(%rsp),%r14
je     12bd <getentropy_urandom+0x13d>
xor    %ebx,%ebx
jmp    126a <getentropy_urandom+0xea>
nopw   0x0(%rax,%rax,1)
cltq
add    %rax,%rbx
cmp    %r15,%rbx
jae    12bd <getentropy_urandom+0x13d>
mov    %r15,%rdx
sub    %rbx,%rdx
lea    (%r14,%rbx,1),%rsi
mov    %ebp,%edi
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    1260 <getentropy_urandom+0xe0>
mov    (%r12),%eax
mov    0x2d0d(%rip),%rcx
cmp    (%rcx),%eax
je     1265 <getentropy_urandom+0xe5>
cmp    0x0(%r13),%eax
je     1265 <getentropy_urandom+0xe5>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%ecx
mov    $0xffffffff,%eax
mov    %ecx,(%r12)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <gotdata@plt>
test   %rax,%rax
jne    129c <getentropy_urandom+0x11c>
xor    %eax,%eax
mov    0x4(%rsp),%ecx
jmp    12aa <getentropy_urandom+0x12a>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@O_RDONLY = global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@EINTR = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@RNDGETENTCNT = global i32 0
@EAGAIN = global i32 0
@EIO = global i32 0
define i32 @getentropy_urandom(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%14 = load i32, i32* @errno
store i32 %14, i32* %11
br label %15
15: ; preds = %25, %2
%16 = load i32, i32* @O_RDONLY
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %17, i32 0)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = load i32, i32* @errno
%23 = load i32, i32* @EINTR
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
br label %15
26: ; preds = %21
br label %99
27: ; preds = %15
%28 = load i32, i32* %8
%29 = load i32, i32* @F_SETFD
%30 = load i32, i32* %8
%31 = load i32, i32* @F_GETFD
%32 = call i32 (i32, i32, ...) @fcntl(i32 %30, i32 %31)
%33 = load i32, i32* @FD_CLOEXEC
%34 = or i32 %32, %33
%35 = call i32 (i32, i32, ...) @fcntl(i32 %28, i32 %29, i32 %34)
%36 = load i32, i32* %8
%37 = call i32 @fstat(i32 %36, %struct.stat* %6)
%38 = icmp eq i32 %37, -1
br i1 %38, label %44, label %39
39: ; preds = %27
%40 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @S_ISCHR(i32 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %39, %27
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
br label %99
47: ; preds = %39
%48 = load i32, i32* %8
%49 = load i32, i32* @RNDGETENTCNT
%50 = call i32 @ioctl(i32 %48, i32 %49, i32* %9)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
br label %99
55: ; preds = %47
store i64 0, i64* %7
br label %56
56: ; preds = %84, %80, %55
%57 = load i64, i64* %7
%58 = load i64, i64* %5
%59 = icmp ult i64 %57, %58
br i1 %59, label %60, label %89
60: ; preds = %56
%61 = load i64, i64* %5
%62 = load i64, i64* %7
%63 = sub i64 %61, %62
store i64 %63, i64* %12
%64 = load i32, i32* %8
%65 = load i8*, i8** %4
%66 = load i64, i64* %7
%67 = getelementptr inbounds i8, i8* %65, i64 %66
%68 = load i64, i64* %12
%69 = call i32 @read(i32 %64, i8* %67, i64 %68)
store i32 %69, i32* %13
%70 = load i32, i32* %13
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %84
72: ; preds = %60
%73 = load i32, i32* @errno
%74 = load i32, i32* @EAGAIN
%75 = icmp eq i32 %73, %74
br i1 %75, label %80, label %76
76: ; preds = %72
%77 = load i32, i32* @errno
%78 = load i32, i32* @EINTR
%79 = icmp eq i32 %77, %78
br i1 %79, label %80, label %81
80: ; preds = %76, %72
br label %56
81: ; preds = %76
%82 = load i32, i32* %8
%83 = call i32 @close(i32 %82)
br label %99
84: ; preds = %60
%85 = load i32, i32* %13
%86 = sext i32 %85 to i64
%87 = load i64, i64* %7
%88 = add i64 %87, %86
store i64 %88, i64* %7
br label %56
89: ; preds = %56
%90 = load i32, i32* %8
%91 = call i32 @close(i32 %90)
%92 = load i8*, i8** %4
%93 = load i64, i64* %5
%94 = call i64 @gotdata(i8* %92, i64 %93)
%95 = icmp eq i64 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %89
%97 = load i32, i32* %11
store i32 %97, i32* @errno
store i32 0, i32* %3
br label %101
98: ; preds = %89
br label %99
99: ; preds = %98, %81, %52, %44, %26
%100 = load i32, i32* @EIO
store i32 %100, i32* @errno
store i32 -1, i32* %3
br label %101
101: ; preds = %99, %96
%102 = load i32, i32* %3
ret i32 %102
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@O_RDONLY = global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@EINTR = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@RNDGETENTCNT = global i32 0
@EAGAIN = global i32 0
@EIO = global i32 0
define i32 @getentropy_urandom(i8* %0, i64 %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @errno
br label %8
8: ; preds = %12, %2
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i32, i32* @errno
%14 = load i32, i32* @EINTR
%15 = icmp eq i32 %13, %14
br i1 %15, label %8, label %65
16: ; preds = %8
%17 = load i32, i32* @F_SETFD
%18 = load i32, i32* @F_GETFD
%19 = tail call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %18)
%20 = load i32, i32* @FD_CLOEXEC
%21 = or i32 %20, %19
%22 = tail call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %17, i32 %21)
%23 = call i32 @fstat(i32 %10, %struct.stat* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %30, label %25
25: ; preds = %16
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%27 = load i32, i32* %26
%28 = tail call i32 @S_ISCHR(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25, %16
%31 = tail call i32 @close(i32 %10)
br label %65
32: ; preds = %25
%33 = load i32, i32* @RNDGETENTCNT
%34 = call i32 @ioctl(i32 %10, i32 %33, i32* %4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = icmp eq i64 %1, 0
br i1 %37, label %61, label %40
38: ; preds = %32
%39 = call i32 @close(i32 %10)
br label %65
40: ; preds = %36, %58
%41 = phi i64 [ %59, %58 ], [ 0, %36 ]
%42 = sub i64 %1, %41
%43 = getelementptr inbounds i8, i8* %0, i64 %41
%44 = call i32 @read(i32 %10, i8* %43, i64 %42)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %55
46: ; preds = %40
%47 = load i32, i32* @errno
%48 = load i32, i32* @EAGAIN
%49 = icmp eq i32 %47, %48
%50 = load i32, i32* @EINTR
%51 = icmp eq i32 %47, %50
%52 = select i1 %49, i1 true, i1 %51
br i1 %52, label %58, label %53
53: ; preds = %46
%54 = call i32 @close(i32 %10)
br label %65
55: ; preds = %40
%56 = sext i32 %44 to i64
%57 = add i64 %41, %56
br label %58
58: ; preds = %46, %55
%59 = phi i64 [ %57, %55 ], [ %41, %46 ]
%60 = icmp ult i64 %59, %1
br i1 %60, label %40, label %61
61: ; preds = %58, %36
%62 = call i32 @close(i32 %10)
%63 = call i64 @gotdata(i8* %0, i64 %1)
%64 = icmp eq i64 %63, 0
br i1 %64, label %67, label %65
65: ; preds = %12, %53, %61, %38, %30
%66 = load i32, i32* @EIO
br label %67
67: ; preds = %61, %65
%68 = phi i32 [ %66, %65 ], [ %7, %61 ]
%69 = phi i32 [ -1, %65 ], [ 0, %61 ]
store i32 %68, i32* @errno
ret i32 %69
}