#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 {
  unsigned int tv_sec;
  unsigned int tv_usec;
};
typedef int /*<<< orphan*/ r;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned int *, int);
int /*<<< orphan*/ srand(unsigned int);
void init_random() {
  unsigned int ticks;
  struct timeval tv;
  int fd;
  gettimeofday(&tv, NULL);
  ticks = tv.tv_sec + tv.tv_usec;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd > 0) {
    unsigned int r;
    int i;
    for (i = 0; i < 512; i++) {
      if (read(fd, &r, sizeof(r)) == sizeof(r))
        ticks += r;
    }
    close(fd);
  }
  srand(ticks);
}


x86 assembly (clang, -O0):
<init_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11e5 <init_random+0x95>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x200,-0x1c(%rbp)
jge    11dd <init_random+0x8d>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cltq
cmp    $0x4,%rax
jne    11ca <init_random+0x7a>
mov    -0x18(%rbp),%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cf <init_random+0x7f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1197 <init_random+0x47>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%edi
call   1060 <srand@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_random>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x14(%rsp),%ebx
add    0x10(%rsp),%ebx
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jle    11cc <init_random+0x7c>
mov    %eax,%r15d
mov    $0x200,%ebp
lea    0xc(%rsp),%r14
jmp    11a7 <init_random+0x57>
cs nopw 0x0(%rax,%rax,1)
add    %ecx,%ebx
add    $0xffffffff,%ebp
je     11c4 <init_random+0x74>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
xor    %ecx,%ecx
cmp    $0x4,%eax
jne    11a0 <init_random+0x50>
mov    0xc(%rsp),%ecx
jmp    11a0 <init_random+0x50>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebx,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <srand@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @init_random() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%10 = load i32, i32* %9
%11 = add i32 %8, %10
store i32 %11, i32* %1
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %36
16: ; preds = %0
store i32 0, i32* %5
br label %17
17: ; preds = %30, %16
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 512
br i1 %19, label %20, label %33
20: ; preds = %17
%21 = load i32, i32* %3
%22 = call i32 @read(i32 %21, i32* %4, i32 4)
%23 = sext i32 %22 to i64
%24 = icmp eq i64 %23, 4
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* %4
%27 = load i32, i32* %1
%28 = add i32 %27, %26
store i32 %28, i32* %1
br label %29
29: ; preds = %25, %20
br label %30
30: ; preds = %29
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
br label %17
33: ; preds = %17
%34 = load i32, i32* %3
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %0
%37 = load i32, i32* %1
%38 = call i32 @srand(i32 %37)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @init_random() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = bitcast %struct.timeval* %1 to i8*
%4 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%8 = load i32, i32* %7
%9 = add i32 %8, %6
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %0
%14 = bitcast i32* %2 to i8*
br label %15
15: ; preds = %13, %15
%16 = phi i32 [ 0, %13 ], [ %23, %15 ]
%17 = phi i32 [ %9, %13 ], [ %22, %15 ]
%18 = call i32 @read(i32 %11, i32* %2, i32 4)
%19 = icmp eq i32 %18, 4
%20 = load i32, i32* %2
%21 = select i1 %19, i32 %20, i32 0
%22 = add i32 %21, %17
%23 = add nuw nsw i32 %16, 1
%24 = icmp eq i32 %23, 512
br i1 %24, label %25, label %15
25: ; preds = %15
%26 = tail call i32 @close(i32 %11)
br label %27
27: ; preds = %25, %0
%28 = phi i32 [ %22, %25 ], [ %9, %0 ]
%29 = tail call i32 @srand(i32 %28)
ret void
}