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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EIO;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ fstat(int, struct stat *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ randombytes_block_on_dev_random();
int __randombytes_sysrandom_random_dev_open(void) {
  struct stat st;
  static const char *devices[] = {"/dev/urandom", "/dev/random", NULL};
  const char **device = devices;
  int fd;
  if (randombytes_block_on_dev_random() != 0) {
    return -1;
  }
  do {
    fd = open(*device, O_RDONLY);
    if (fd != -1) {
      if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) {
        return fd;
      }
      (void)close(fd);
    } else if (errno == EINTR) {
      continue;
    }
    device++;
  } while (*device != NULL);
  errno = EIO;
  return -1;
}


x86 assembly (clang, -O0):
<__randombytes_sysrandom_random_dev_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0x2ef1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <randombytes_block_on_dev_random@plt>
cmp    $0x0,%rax
je     1180 <__randombytes_sysrandom_random_dev_open+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <__randombytes_sysrandom_random_dev_open+0xf0>
jmp    1185 <__randombytes_sysrandom_random_dev_open+0x35>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     11e7 <__randombytes_sysrandom_random_dev_open+0x97>
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jne    11da <__randombytes_sysrandom_random_dev_open+0x8a>
mov    -0x8(%rbp),%edi
call   1050 <S_ISCHR@plt>
cmp    $0x0,%rax
je     11da <__randombytes_sysrandom_random_dev_open+0x8a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <__randombytes_sysrandom_random_dev_open+0xf0>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    120b <__randombytes_sysrandom_random_dev_open+0xbb>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <__randombytes_sysrandom_random_dev_open+0xb6>
jmp    1217 <__randombytes_sysrandom_random_dev_open+0xc7>
jmp    120b <__randombytes_sysrandom_random_dev_open+0xbb>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1185 <__randombytes_sysrandom_random_dev_open+0x35>
mov    0x2dac(%rip),%rax
mov    (%rax),%rcx
mov    0x2d9a(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__randombytes_sysrandom_random_dev_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <randombytes_block_on_dev_random@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    120c <__randombytes_sysrandom_random_dev_open+0xbc>
lea    0x2c69(%rip),%rbx
lea    0xe82(%rip),%rdi
mov    0x2e5b(%rip),%r13
mov    0x2e3c(%rip),%r15
mov    0x2e2d(%rip),%r14
mov    %rsp,%r12
jmp    11b3 <__randombytes_sysrandom_random_dev_open+0x63>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x8,%rbx
mov    (%rbx),%rdi
test   %rdi,%rdi
je     11fa <__randombytes_sysrandom_random_dev_open+0xaa>
mov    0x0(%r13),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11f0 <__randombytes_sysrandom_random_dev_open+0xa0>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
call   1070 <fstat@plt>
test   %rax,%rax
jne    11a0 <__randombytes_sysrandom_random_dev_open+0x50>
mov    (%rsp),%edi
call   1050 <S_ISCHR@plt>
test   %rax,%rax
je     11a0 <__randombytes_sysrandom_random_dev_open+0x50>
jmp    120c <__randombytes_sysrandom_random_dev_open+0xbc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r14),%rax
jne    11a7 <__randombytes_sysrandom_random_dev_open+0x57>
jmp    11ab <__randombytes_sysrandom_random_dev_open+0x5b>
mov    0x2dcf(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@__randombytes_sysrandom_random_dev_open.devices = internal unnamed_addr constant [3 x i8*] [i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EIO = global i64 0
define i32 @__randombytes_sysrandom_random_dev_open() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = tail call i64 (...) @randombytes_block_on_dev_random()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %34
5: ; preds = %0
%6 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
br label %7
7: ; preds = %5, %28
%8 = phi i8* [ %30, %28 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %5 ]
%9 = phi i8** [ %29, %28 ], [ getelementptr inbounds ([3 x i8*], [3 x i8*]* @__randombytes_sysrandom_random_dev_open.devices, i64 0, i64 0), %5 ]
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %8, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %22, label %13
13: ; preds = %7
%14 = call i64 @fstat(i32 %11, %struct.stat* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* %6
%18 = tail call i64 @S_ISCHR(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %16, %13
%21 = tail call i32 @close(i32 %11)
br label %26
22: ; preds = %7
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %28, label %26
26: ; preds = %22, %20
%27 = getelementptr inbounds i8*, i8** %9, i64 1
br label %28
28: ; preds = %22, %26
%29 = phi i8** [ %27, %26 ], [ %9, %22 ]
%30 = load i8*, i8** %29
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %7
32: ; preds = %28
%33 = load i64, i64* @EIO
store i64 %33, i64* @errno
br label %34
34: ; preds = %16, %0, %32
%35 = phi i32 [ -1, %32 ], [ -1, %0 ], [ %11, %16 ]
ret i32 %35
}