#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 tv_sec;
  int tv_usec;
};

/* Variables and functions */
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int O_NONBLOCK;
int O_RDONLY;
int fcntl(int, int /*<<< orphan*/, ...);
int getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int getuid();
int open(char *, int);
int /*<<< orphan*/ rand();
int /*<<< orphan*/ srand(int);
int random_get_fd(void) {
  int i, fd;
  struct timeval tv;
  gettimeofday(&tv, 0);
  fd = open("/dev/urandom", O_RDONLY);
  if (fd == -1)
    fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
  if (fd >= 0) {
    i = fcntl(fd, F_GETFD);
    if (i >= 0)
      fcntl(fd, F_SETFD, i | FD_CLOEXEC);
  }
  srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
  gettimeofday(&tv, 0);
  for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
    rand();
  return fd;
}


x86 assembly (clang, -O0):
<random_get_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c6 <random_get_fd+0x56>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1214 <random_get_fd+0xa4>
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     120f <random_get_fd+0x9f>
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    -0x4(%rbp),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
jmp    1214 <random_get_fd+0xa4>
mov    $0x0,%al
call   1040 <getpid@plt>
shl    $0x10,%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%edi
xor    -0x10(%rbp),%edi
xor    -0xc(%rbp),%edi
call   1070 <srand@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    -0x10(%rbp),%eax
xor    -0xc(%rbp),%eax
and    $0x1f,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    126e <random_get_fd+0xfe>
mov    $0x0,%al
call   1090 <rand@plt>
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    124f <random_get_fd+0xdf>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_get_fd>:
push   %rbp
push   %rbx
push   %rax
mov    %rsp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x2e5c(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe72(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11a0 <random_get_fd+0x30>
test   %ebx,%ebx
jns    11be <random_get_fd+0x4e>
jmp    11f1 <random_get_fd+0x81>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %ebx,%ebx
js     11f1 <random_get_fd+0x81>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     11f1 <random_get_fd+0x81>
mov    0x2df5(%rip),%rcx
mov    (%rcx),%esi
mov    0x2de4(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
shl    $0x10,%ebp
xor    %eax,%eax
call   1050 <getuid@plt>
xor    %eax,%ebp
xor    (%rsp),%ebp
xor    0x4(%rsp),%ebp
mov    %ebp,%edi
call   1070 <srand@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x4(%rsp),%ebp
xor    (%rsp),%ebp
and    $0x1f,%ebp
je     123f <random_get_fd+0xcf>
add    $0x1,%ebp
nopl   (%rax)
xor    %eax,%eax
call   1090 <rand@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1230 <random_get_fd+0xc0>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.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 = alloca %struct.timeval
%4 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %10, %11
%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 = %9, %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 (...) @getpid()
%33 = shl i32 %32, 16
%34 = call i32 (...) @getuid()
%35 = xor i32 %33, %34
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%37 = load i32, i32* %36
%38 = xor i32 %35, %37
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%40 = load i32, i32* %39
%41 = xor i32 %38, %40
%42 = call i32 @srand(i32 %41)
%43 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%47 = load i32, i32* %46
%48 = xor i32 %45, %47
%49 = and i32 %48, 31
store i32 %49, i32* %1
br label %50
50: ; preds = %55, %31
%51 = load i32, i32* %1
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = call i32 (...) @rand()
br label %55
55: ; preds = %53
%56 = load i32, i32* %1
%57 = add nsw i32 %56, -1
store i32 %57, i32* %1
br label %50
58: ; preds = %50
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.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 %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %8
%11 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %7, %0
%13 = phi i32 [ %11, %7 ], [ %5, %0 ]
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i32, i32* @F_GETFD
%17 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %16)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* @F_SETFD
%21 = load i32, i32* @FD_CLOEXEC
%22 = or i32 %21, %17
%23 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %20, i32 %22)
br label %24
24: ; preds = %15, %19, %12
%25 = call i32 (...) @getpid()
%26 = shl i32 %25, 16
%27 = call i32 (...) @getuid()
%28 = xor i32 %26, %27
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%30 = load i32, i32* %29
%31 = xor i32 %28, %30
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%33 = load i32, i32* %32
%34 = xor i32 %31, %33
%35 = call i32 @srand(i32 %34)
%36 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%37 = load i32, i32* %29
%38 = load i32, i32* %32
%39 = xor i32 %38, %37
%40 = and i32 %39, 31
%41 = icmp eq i32 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %24, %42
%43 = phi i32 [ %45, %42 ], [ %40, %24 ]
%44 = call i32 (...) @rand()
%45 = add nsw i32 %43, -1
%46 = icmp ugt i32 %43, 1
br i1 %46, label %42, label %47
47: ; preds = %42, %24
ret i32 %13
}