#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 timeval {
  int dummy;
};

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int open(char *, int);
int get_random_fd() {
  struct timeval tv;
  static int fd = -2;
  if (fd == -2) {
    gettimeofday(&tv, 0);
    fd = open("/dev/urandom", O_RDONLY);
    if (fd == -1)
      fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
  }
  return fd;
}


x86 assembly (clang, -O0):
<get_random_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0xfffffffe,0x2f01(%rip)
jne    1191 <get_random_fd+0x71>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xeb0(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ed5(%rip)
cmpl   $0xffffffff,0x2ece(%rip)
jne    118c <get_random_fd+0x6c>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ea4(%rip)
jmp    1191 <get_random_fd+0x71>
mov    0x2e99(%rip),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_random_fd>:
push   %rbx
sub    $0x10,%rsp
mov    0x2f05(%rip),%eax
cmp    $0xfffffffe,%eax
jne    1179 <get_random_fd+0x59>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x2e9d(%rip),%rbx
mov    (%rbx),%esi
lea    0xeb4(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ed9(%rip)
cmp    $0xffffffff,%eax
jne    1179 <get_random_fd+0x59>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
lea    0xe9f(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2eb7(%rip)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@get_random_fd.fd = internal global i32 -2
@.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
define i32 @get_random_fd() {
%1 = alloca %struct.timeval
%2 = load i32, i32* @get_random_fd.fd
%3 = icmp eq i32 %2, -2
br i1 %3, label %4, label %16
4: ; preds = %0
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* @get_random_fd.fd
%8 = load i32, i32* @get_random_fd.fd
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 %14, i32* @get_random_fd.fd
br label %15
15: ; preds = %10, %4
br label %16
16: ; preds = %15, %0
%17 = load i32, i32* @get_random_fd.fd
ret i32 %17
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@get_random_fd.fd = internal unnamed_addr global i32 -2
@.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
define i32 @get_random_fd() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = load i32, i32* @get_random_fd.fd
%4 = icmp eq i32 %3, -2
br i1 %4, label %5, label %15
5: ; preds = %0
%6 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* @get_random_fd.fd
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %12, %11
%14 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 %14, i32* @get_random_fd.fd
br label %15
15: ; preds = %5, %10, %0
%16 = phi i32 [ %8, %5 ], [ %14, %10 ], [ %3, %0 ]
ret i32 %16
}