#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ crank_random();
int fcntl(int, int /*<<< orphan*/, ...);
int open(char *, int);
int random_get_fd(void) {
  int i, fd;
  fd = open("/dev/urandom", O_RDONLY | 0);
  if (fd == -1)
    fd = open("/dev/random", O_RDONLY | O_NONBLOCK | 0);
  if (fd >= 0) {
    i = fcntl(fd, F_GETFD);
    if (i >= 0)
      fcntl(fd, F_SETFD, i | FD_CLOEXEC);
  }
  crank_random();
  return fd;
}


x86 assembly (clang, -O0):
<random_get_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
lea    0xeb5(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1181 <random_get_fd+0x51>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e7b(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
lea    0xe94(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11cf <random_get_fd+0x9f>
mov    -0x8(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     11ca <random_get_fd+0x9a>
mov    -0x8(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    -0x4(%rbp),%edx
mov    0x2e07(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
jmp    11cf <random_get_fd+0x9f>
mov    $0x0,%al
call   1040 <crank_random@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_get_fd>:
push   %r14
push   %rbx
push   %rax
mov    0x2ea5(%rip),%r14
mov    (%r14),%esi
lea    0xebb(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1157 <random_get_fd+0x27>
test   %ebx,%ebx
jns    1175 <random_get_fd+0x45>
jmp    11a8 <random_get_fd+0x78>
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
lea    0xea3(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %ebx,%ebx
js     11a8 <random_get_fd+0x78>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     11a8 <random_get_fd+0x78>
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e2d(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
xor    %eax,%eax
call   1040 <crank_random@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
@O_NONBLOCK = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @random_get_fd() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = or i32 %3, 0
%5 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %9, %10
%12 = or i32 %11, 0
%13 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %2
br label %14
14: ; preds = %8, %0
%15 = load i32, i32* %2
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %14
%18 = load i32, i32* %2
%19 = load i32, i32* @F_GETFD
%20 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19)
store i32 %20, i32* %1
%21 = load i32, i32* %1
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load i32, i32* %2
%25 = load i32, i32* @F_SETFD
%26 = load i32, i32* %1
%27 = load i32, i32* @FD_CLOEXEC
%28 = or i32 %26, %27
%29 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25, i32 %28)
br label %30
30: ; preds = %23, %17
br label %31
31: ; preds = %30, %14
%32 = call i32 (...) @crank_random()
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
@O_NONBLOCK = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @random_get_fd() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %9
4: ; preds = %0
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %4, %0
%10 = phi i32 [ %8, %4 ], [ %2, %0 ]
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = load i32, i32* @F_GETFD
%14 = tail call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %13)
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @F_SETFD
%18 = load i32, i32* @FD_CLOEXEC
%19 = or i32 %18, %14
%20 = tail call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %17, i32 %19)
br label %21
21: ; preds = %12, %16, %9
%22 = tail call i32 (...) @crank_random()
ret i32 %10
}