#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*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
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 *);
int /*<<< orphan*/ stderr;
scalar_t__ time(int /*<<< orphan*/ *);
int main(void) {
  struct stat st;
  long int n;
  fprintf(stderr, "warning: genhostid is now deprecated, and will be removed "
                  "in the near future!\n"
                  "warning: use systemd-machine-id-setup (1) instead! More "
                  "info: 'man 5 machine-id'\n");
  if (stat("/etc/hostid", &st) == 0 && S_ISREG(st.st_mode) && st.st_size == 4) {
    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)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xed5(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    1206 <main+0x66>
mov    -0xc(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     1206 <main+0x66>
cmpl   $0x4,-0x10(%rbp)
jne    1206 <main+0x66>
movl   $0x0,-0x4(%rbp)
jmp    127e <main+0xde>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1245 <main+0xa5>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cltq
cmp    $0x8,%rax
je     1270 <main+0xd0>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%rdi
xor    %rax,%rdi
call   10b0 <srand48@plt>
mov    $0x0,%al
call   10a0 <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
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xedf(%rip),%rdi
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     1236 <main+0x96>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    0xece(%rip),%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1205 <main+0x65>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0x8,%eax
jne    1205 <main+0x65>
mov    0x8(%rsp),%rdi
jmp    122b <main+0x8b>
xor    %edi,%edi
call   1080 <time@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
xor    %rax,%rbx
mov    %rbx,%rdi
call   10b0 <srand48@plt>
xor    %eax,%eax
call   10a0 <lrand48@plt>
mov    %rax,%rdi
call   1040 <sethostid@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x4(%rsp),%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     11ce <main+0x2e>
xor    %eax,%eax
cmpl   $0x4,(%rsp)
je     1230 <main+0x90>
jmp    11ce <main+0x2e>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@stderr = global i32 0
@.str = [160 x i8] c"warning: genhostid is now deprecated, and will be removed in the near future!\0Awarning: use systemd-machine-id-setup (1) instead! More info: 'man 5 machine-id'\0A\00"
@.str.1 = [12 x i8] c"/etc/hostid\00"
@.str.2 = [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 = load i32, i32* @stderr
%6 = call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str, i64 0, i64 0))
%7 = call i64 @stat(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %0
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 1
%11 = load i32, i32* %10
%12 = call i64 @S_ISREG(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, 4
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 0, i32* %1
br label %39
19: ; preds = %14, %9, %0
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, -1
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i32, i32* %4
%26 = call i32 @read(i32 %25, i64* %3, i32 8)
%27 = sext i32 %26 to i64
%28 = icmp ne i64 %27, 8
br i1 %28, label %29, label %35
29: ; preds = %24, %19
%30 = call i64 @time(i32* null)
%31 = call i64 (...) @getpid()
%32 = xor i64 %30, %31
%33 = call i32 @srand48(i64 %32)
%34 = call i64 (...) @lrand48()
store i64 %34, i64* %3
br label %35
35: ; preds = %29, %24
%36 = load i64, i64* %3
%37 = trunc i64 %36 to i32
%38 = call i32 @sethostid(i32 %37)
store i32 %38, i32* %1
br label %39
39: ; preds = %35, %18
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@stderr = global i32 0
@.str = [160 x i8] c"warning: genhostid is now deprecated, and will be removed in the near future!\0Awarning: use systemd-machine-id-setup (1) instead! More info: 'man 5 machine-id'\0A\00"
@.str.1 = [12 x i8] c"/etc/hostid\00"
@.str.2 = [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 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str, i64 0, i64 0))
%7 = call i64 @stat(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %0
%10 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%11 = load i32, i32* %10
%12 = call i64 @S_ISREG(i32 %11)
%13 = icmp ne i64 %12, 0
%14 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = icmp eq i32 %15, 4
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %37, label %18
18: ; preds = %9, %0
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %27, label %22
22: ; preds = %18
%23 = call i32 @read(i32 %20, i64* %2, i32 8)
%24 = icmp eq i32 %23, 8
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i64, i64* %2
br label %33
27: ; preds = %22, %18
%28 = call i64 @time(i32* null)
%29 = call i64 (...) @getpid()
%30 = xor i64 %29, %28
%31 = call i32 @srand48(i64 %30)
%32 = call i64 (...) @lrand48()
br label %33
33: ; preds = %25, %27
%34 = phi i64 [ %26, %25 ], [ %32, %27 ]
%35 = trunc i64 %34 to i32
%36 = call i32 @sethostid(i32 %35)
br label %37
37: ; preds = %9, %33
%38 = phi i32 [ %36, %33 ], [ 0, %9 ]
ret i32 %38
}