#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 *);
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);
  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    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe5e(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11d8 <getentropy_urandom+0x68>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%eax
jne    11d3 <getentropy_urandom+0x63>
jmp    118c <getentropy_urandom+0x1c>
jmp    1329 <getentropy_urandom+0x1b9>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    %eax,%edx
mov    0x2db4(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     123b <getentropy_urandom+0xcb>
mov    -0x20(%rbp),%edi
call   1070 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    1248 <getentropy_urandom+0xd8>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    1329 <getentropy_urandom+0x1b9>
mov    -0x2c(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1273 <getentropy_urandom+0x103>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    1329 <getentropy_urandom+0x1b9>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1309 <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    12f8 <getentropy_urandom+0x188>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    0x2cd2(%rip),%rcx
cmp    (%rcx),%eax
je     12e6 <getentropy_urandom+0x176>
mov    0x2ce3(%rip),%rax
mov    (%rax),%eax
mov    0x2cca(%rip),%rcx
cmp    (%rcx),%eax
jne    12eb <getentropy_urandom+0x17b>
jmp    127b <getentropy_urandom+0x10b>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    1329 <getentropy_urandom+0x1b9>
movslq -0x44(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    127b <getentropy_urandom+0x10b>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%ecx
mov    0x2c9d(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1342 <getentropy_urandom+0x1d2>
mov    0x2ca0(%rip),%rax
mov    (%rax),%ecx
mov    0x2c7f(%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    0x2e2b(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    0x2e4c(%rip),%r14
lea    0xe5d(%rip),%rbx
mov    0x2dfe(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    11d1 <getentropy_urandom+0x61>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     11b0 <getentropy_urandom+0x40>
jmp    128c <getentropy_urandom+0x11c>
mov    %eax,%ebp
mov    0x2dee(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2dcb(%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   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     1285 <getentropy_urandom+0x115>
mov    (%rsp),%edi
call   1070 <S_ISCHR@plt>
test   %eax,%eax
je     1285 <getentropy_urandom+0x115>
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1285 <getentropy_urandom+0x115>
test   %r15,%r15
mov    0x8(%rsp),%r14
je     12ad <getentropy_urandom+0x13d>
xor    %ebx,%ebx
jmp    125a <getentropy_urandom+0xea>
nopw   0x0(%rax,%rax,1)
cltq
add    %rax,%rbx
cmp    %r15,%rbx
jae    12ad <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    1250 <getentropy_urandom+0xe0>
mov    (%r12),%eax
mov    0x2d1d(%rip),%rcx
cmp    (%rcx),%eax
je     1255 <getentropy_urandom+0xe5>
cmp    0x0(%r13),%eax
je     1255 <getentropy_urandom+0xe5>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d3d(%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>
xor    %eax,%eax
mov    0x4(%rsp),%ecx
jmp    129a <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 %93
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 %93
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 %93
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 %93
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 i32, i32* %11
store i32 %92, i32* @errno
store i32 0, i32* %3
br label %95
93: ; preds = %81, %52, %44, %26
%94 = load i32, i32* @EIO
store i32 %94, i32* @errno
store i32 -1, i32* %3
br label %95
95: ; preds = %93, %89
%96 = load i32, i32* %3
ret i32 %96
}

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* nocapture %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 %63
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 %63
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 %63
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 %63
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)
br label %65
63: ; preds = %12, %53, %38, %30
%64 = load i32, i32* @EIO
br label %65
65: ; preds = %63, %61
%66 = phi i32 [ %7, %61 ], [ %64, %63 ]
%67 = phi i32 [ 0, %61 ], [ -1, %63 ]
store i32 %66, i32* @errno
ret i32 %67
}