#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 stat {
  int st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ n;
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ getpid();
long lrand48();
int open(char *, int /*<<< orphan*/);
int read(int, long *, int);
int sethostid(int /*<<< orphan*/);
int /*<<< orphan*/ srand48(long);
scalar_t__ stat(char *, struct stat *);
scalar_t__ time(int /*<<< orphan*/ *);
int main(void) {
  struct stat st;
  long int n;
  if (stat("/etc/hostid", &st) == 0 && S_ISREG(st.st_mode) &&
      st.st_size >= sizeof(n))
    return 0;
  int fd = open("/dev/random", O_RDONLY);
  if (fd == -1 || read(fd, &n, sizeof(n)) != sizeof(n)) {
    srand48((long int)time(NULL) ^ (long int)getpid());
    n = lrand48();
  }
  return sethostid((int32_t)n);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    11e5 <main+0x55>
mov    -0xc(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%rax
je     11e5 <main+0x55>
movslq -0x10(%rbp),%rax
cmp    $0x8,%rax
jb     11e5 <main+0x55>
movl   $0x0,-0x4(%rbp)
jmp    125d <main+0xcd>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1224 <main+0x94>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cltq
cmp    $0x8,%rax
je     124f <main+0xbf>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%rdi
xor    %rax,%rdi
call   10a0 <srand48@plt>
mov    $0x0,%al
call   1090 <lrand48@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
call   1040 <sethostid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe64(%rip),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     1211 <main+0x81>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x50>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0x8,%eax
jne    11e0 <main+0x50>
mov    0x8(%rsp),%rdi
jmp    1206 <main+0x76>
xor    %edi,%edi
call   1070 <time@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
xor    %rax,%rbx
mov    %rbx,%rdi
call   10a0 <srand48@plt>
xor    %eax,%eax
call   1090 <lrand48@plt>
mov    %rax,%rdi
call   1040 <sethostid@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x4(%rsp),%edi
call   1080 <S_ISREG@plt>
test   %rax,%rax
je     11a9 <main+0x19>
xor    %eax,%eax
cmpl   $0x7,(%rsp)
ja     120b <main+0x7b>
jmp    11a9 <main+0x19>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [12 x i8] c"/etc/hostid\00"
@.str.1 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 @stat(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %0
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 1
%9 = load i32, i32* %8
%10 = call i64 @S_ISREG(i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%14 = load i32, i32* %13
%15 = sext i32 %14 to i64
%16 = icmp uge i64 %15, 8
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 0, i32* %1
br label %38
18: ; preds = %12, %7, %0
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load i32, i32* %4
%25 = call i32 @read(i32 %24, i64* %3, i32 8)
%26 = sext i32 %25 to i64
%27 = icmp ne i64 %26, 8
br i1 %27, label %28, label %34
28: ; preds = %23, %18
%29 = call i64 @time(i32* null)
%30 = call i64 (...) @getpid()
%31 = xor i64 %29, %30
%32 = call i32 @srand48(i64 %31)
%33 = call i64 (...) @lrand48()
store i64 %33, i64* %3
br label %34
34: ; preds = %28, %23
%35 = load i64, i64* %3
%36 = trunc i64 %35 to i32
%37 = call i32 @sethostid(i32 %36)
store i32 %37, i32* %1
br label %38
38: ; preds = %34, %17
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [12 x i8] c"/etc/hostid\00"
@.str.1 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @main() {
%1 = alloca %struct.stat
%2 = alloca i64
%3 = bitcast %struct.stat* %1 to i8*
%4 = bitcast i64* %2 to i8*
%5 = call i64 @stat(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %0
%8 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%9 = load i32, i32* %8
%10 = call i64 @S_ISREG(i32 %9)
%11 = icmp ne i64 %10, 0
%12 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = icmp ugt i32 %13, 7
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %35, label %16
16: ; preds = %7, %0
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = call i32 @read(i32 %18, i64* %2, i32 8)
%22 = icmp eq i32 %21, 8
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i64, i64* %2
br label %31
25: ; preds = %20, %16
%26 = call i64 @time(i32* null)
%27 = call i64 (...) @getpid()
%28 = xor i64 %27, %26
%29 = call i32 @srand48(i64 %28)
%30 = call i64 (...) @lrand48()
br label %31
31: ; preds = %23, %25
%32 = phi i64 [ %24, %23 ], [ %30, %25 ]
%33 = trunc i64 %32 to i32
%34 = call i32 @sethostid(i32 %33)
br label %35
35: ; preds = %7, %31
%36 = phi i32 [ %34, %31 ], [ 0, %7 ]
ret i32 %36
}