#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 */
typedef int /*<<< orphan*/ foo;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned int *, int);
int /*<<< orphan*/ srand(unsigned int);
void init_rng(void) {
  int fd = open("/dev/urandom", O_RDONLY);
  unsigned int foo;
  if (fd > 0) {
    int ret = read(fd, &foo, sizeof(foo));
    if (ret < 0)
      srand(fd + foo);
    close(fd);
  }
  srand(foo);
}


x86 assembly (clang, -O0):
<init_rng>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    119b <init_rng+0x5b>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1193 <init_rng+0x53>
mov    -0x4(%rbp),%edi
add    -0x8(%rbp),%edi
call   1050 <srand@plt>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <srand@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_rng>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
mov    0x4(%rsp),%ebp
test   %eax,%eax
jns    117c <init_rng+0x3c>
mov    %ebx,%edi
add    %ebp,%edi
call   1050 <srand@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <srand@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @init_rng() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%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)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %21
8: ; preds = %0
%9 = load i32, i32* %1
%10 = call i32 @read(i32 %9, i32* %2, i32 4)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i32, i32* %1
%15 = load i32, i32* %2
%16 = add i32 %14, %15
%17 = call i32 @srand(i32 %16)
br label %18
18: ; preds = %13, %8
%19 = load i32, i32* %1
%20 = call i32 @close(i32 %19)
br label %21
21: ; preds = %18, %0
%22 = load i32, i32* %2
%23 = call i32 @srand(i32 %22)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @init_rng() {
%1 = alloca i32
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = bitcast i32* %1 to i8*
%5 = icmp sgt i32 %3, 0
%6 = call i32 @read(i32 %3, i32* %1, i32 4)
%7 = icmp slt i32 %6, 0
%8 = load i32, i32* %1
br i1 %7, label %9, label %12
9: ; preds = %0
%10 = add i32 %8, %3
%11 = tail call i32 @srand(i32 %10)
br label %12
12: ; preds = %0, %9
%13 = tail call i32 @close(i32 %3)
%14 = tail call i32 @srand(i32 %8)
ret void
}