#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*/ seed;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ srand(unsigned int);
void seed_rng(void) {
  int fp = open("/dev/random", O_RDONLY);
  unsigned seed;
  unsigned pos = 0;
  if (fp == -1) {
    abort();
  }
  while (pos < sizeof(seed)) {
    int amt = read(fp, (char *)&seed + pos, sizeof(seed) - pos);
    if (amt <= 0) {
      abort();
    }
    pos += amt;
  }
  srand(seed);
  close(fp);
}


x86 assembly (clang, -O0):
<seed_rng>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1188 <seed_rng+0x38>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    118d <seed_rng+0x3d>
mov    -0xc(%rbp),%eax
cmp    $0x4,%rax
jae    11dd <seed_rng+0x8d>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
add    %rax,%rsi
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
mov    $0x4,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     11cf <seed_rng+0x7f>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    118d <seed_rng+0x3d>
mov    -0x8(%rbp),%edi
call   1060 <srand@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seed_rng>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11b9 <seed_rng+0x69>
mov    %eax,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%eax
lea    (%rsp,%rax,1),%rsi
add    $0x4,%rsi
mov    $0x4,%edx
sub    %ebp,%edx
mov    %ebx,%edi
call   1050 <read@plt>
test   %eax,%eax
jle    11b9 <seed_rng+0x69>
add    %eax,%ebp
cmp    $0x3,%ebp
jbe    1180 <seed_rng+0x30>
mov    0x4(%rsp),%edi
call   1060 <srand@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define void @seed_rng() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
store i32 0, i32* %3
%7 = load i32, i32* %1
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (...) @abort()
unreachable
11: ; preds = %0
br label %12
12: ; preds = %31, %11
%13 = load i32, i32* %3
%14 = zext i32 %13 to i64
%15 = icmp ult i64 %14, 4
br i1 %15, label %16, label %35
16: ; preds = %12
%17 = load i32, i32* %1
%18 = bitcast i32* %2 to i8*
%19 = load i32, i32* %3
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %3
%23 = zext i32 %22 to i64
%24 = sub i64 4, %23
%25 = trunc i64 %24 to i32
%26 = call i32 @read(i32 %17, i8* %21, i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %16
%32 = load i32, i32* %4
%33 = load i32, i32* %3
%34 = add i32 %33, %32
store i32 %34, i32* %3
br label %12
35: ; preds = %12
%36 = load i32, i32* %2
%37 = call i32 @srand(i32 %36)
%38 = load i32, i32* %1
%39 = call i32 @close(i32 %38)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define void @seed_rng() {
%1 = alloca i32
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = bitcast i32* %1 to i8*
%5 = icmp eq i32 %3, -1
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 (...) @abort()
unreachable
8: ; preds = %10
%9 = icmp ult i32 %17, 4
br i1 %9, label %10, label %20
10: ; preds = %0, %8
%11 = phi i32 [ %17, %8 ], [ 0, %0 ]
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %4, i64 %12
%14 = sub nuw nsw i32 4, %11
%15 = call i32 @read(i32 %3, i8* %13, i32 %14)
%16 = icmp slt i32 %15, 1
%17 = add nuw i32 %15, %11
br i1 %16, label %18, label %8
18: ; preds = %10
%19 = tail call i32 (...) @abort()
unreachable
20: ; preds = %8
%21 = load i32, i32* %1
%22 = tail call i32 @srand(i32 %21)
%23 = tail call i32 @close(i32 %3)
ret void
}