#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 pollfd {
  int fd;
  scalar_t__ revents;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EIO;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ POLLIN;
int close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int poll(struct pollfd *, int, int);
int hydro_random_block_on_dev_random(void) {
  struct pollfd pfd;
  int fd;
  int pret;
  fd = open("/dev/random", O_RDONLY);
  if (fd == -1) {
    return 0;
  }
  pfd.fd = fd;
  pfd.events = POLLIN;
  pfd.revents = 0;
  do {
    pret = poll(&pfd, 1, -1);
  } while (pret < 0 && (errno == EINTR || errno == EAGAIN));
  if (pret != 1) {
    (void)close(fd);
    errno = EIO;
    return -1;
  }
  return close(fd);
}


x86 assembly (clang, -O0):
<hydro_random_block_on_dev_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1166 <hydro_random_block_on_dev_random+0x36>
movl   $0x0,-0x4(%rbp)
jmp    122c <hydro_random_block_on_dev_random+0xfc>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1040 <poll@plt>
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jge    11e4 <hydro_random_block_on_dev_random+0xb4>
mov    0x2e1c(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e08(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x2a(%rbp)
je     11de <hydro_random_block_on_dev_random+0xae>
mov    0x2dfd(%rip),%rax
mov    (%rax),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1180 <hydro_random_block_on_dev_random+0x50>
cmpl   $0x1,-0x28(%rbp)
je     1221 <hydro_random_block_on_dev_random+0xf1>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%rcx
mov    0x2db6(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <hydro_random_block_on_dev_random+0xfc>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hydro_random_block_on_dev_random>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
lea    0xeb4(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b7 <hydro_random_block_on_dev_random+0x87>
mov    %eax,%r14d
mov    %eax,0x8(%rsp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rbx
mov    0x2e49(%rip),%r15
mov    0x2e3a(%rip),%rbp
mov    0x2e23(%rip),%r12
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1040 <poll@plt>
test   %eax,%eax
jns    11bb <hydro_random_block_on_dev_random+0x8b>
mov    (%r15),%rax
cmp    0x0(%rbp),%rax
je     1190 <hydro_random_block_on_dev_random+0x60>
cmp    (%r12),%rax
je     1190 <hydro_random_block_on_dev_random+0x60>
jmp    11ca <hydro_random_block_on_dev_random+0x9a>
xor    %eax,%eax
jmp    11e4 <hydro_random_block_on_dev_random+0xb4>
cmp    $0x1,%eax
jne    11ca <hydro_random_block_on_dev_random+0x9a>
mov    %r14d,%edi
call   1030 <close@plt>
jmp    11e4 <hydro_random_block_on_dev_random+0xb4>
mov    %r14d,%edi
call   1030 <close@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i32 }
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EIO = global i64 0
define i32 @hydro_random_block_on_dev_random() {
%1 = alloca i32
%2 = alloca %struct.pollfd
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 0, i32* %1
br label %43
10: ; preds = %0
%11 = load i32, i32* %3
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @POLLIN
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 1
store i64 0, i64* %15
br label %16
16: ; preds = %31, %10
%17 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 -1)
store i32 %17, i32* %4
br label %18
18: ; preds = %16
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
br label %29
29: ; preds = %25, %21
%30 = phi i1 [ true, %21 ], [ %28, %25 ]
br label %31
31: ; preds = %29, %18
%32 = phi i1 [ false, %18 ], [ %30, %29 ]
br i1 %32, label %16, label %33
33: ; preds = %31
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
%39 = load i64, i64* @EIO
store i64 %39, i64* @errno
store i32 -1, i32* %1
br label %43
40: ; preds = %33
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
store i32 %42, i32* %1
br label %43
43: ; preds = %40, %36, %9
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64, i32 }
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EIO = global i64 0
define i32 @hydro_random_block_on_dev_random() {
%1 = alloca %struct.pollfd
%2 = bitcast %struct.pollfd* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %28, label %6
6: ; preds = %0
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 0
store i32 %4, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 1
store i64 0, i64* %10
br label %11
11: ; preds = %14, %6
%12 = call i32 @poll(%struct.pollfd* %1, i32 1, i32 -1)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %15, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %11, label %23
21: ; preds = %11
%22 = icmp eq i32 %12, 1
br i1 %22, label %26, label %23
23: ; preds = %14, %21
%24 = call i32 @close(i32 %4)
%25 = load i64, i64* @EIO
store i64 %25, i64* @errno
br label %28
26: ; preds = %21
%27 = call i32 @close(i32 %4)
br label %28
28: ; preds = %0, %26, %23
%29 = phi i32 [ -1, %23 ], [ %27, %26 ], [ 0, %0 ]
ret i32 %29
}