#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*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, long *, int);
int /*<<< orphan*/ srand(long);
int seed_random(void) {
  int fp;
  long seed;
  fp = open("/dev/urandom", O_RDONLY);
  if (fp < 0) {
    perror("/dev/urandom");
    return 0;
  }
  if (read(fp, &seed, sizeof(seed)) != sizeof(seed)) {
    perror("read random seed");
    return 0;
  }
  close(fp);
  srand(seed);
  return 1;
}


x86 assembly (clang, -O0):
<seed_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1192 <seed_random+0x42>
lea    0xe7f(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11df <seed_random+0x8f>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1040 <read@plt>
cltq
cmp    $0x8,%rax
je     11c7 <seed_random+0x77>
lea    0xe57(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11df <seed_random+0x8f>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rdi
call   1050 <srand@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seed_random>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a2 <seed_random+0x52>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1040 <read@plt>
cmp    $0x8,%eax
jne    11ab <seed_random+0x5b>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x8(%rsp),%rdi
call   1050 <srand@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe57(%rip),%rdi
jmp    11b2 <seed_random+0x62>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
jmp    119c <seed_random+0x4c>

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

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