#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 in_addr {
  int s_addr;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ id;
typedef int /*<<< orphan*/ ghbn_buf;

/* Variables and functions */
int /*<<< orphan*/ MAXHOSTNAMELEN;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ gethostbyname_r(char *, struct hostent *, char *, int,
                                   struct hostent **, int *);
scalar_t__ gethostname(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(struct in_addr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, int *, int);
long int gethostid(void) {
  char host[MAXHOSTNAMELEN + 1];
  int fd, id;
  fd = open("/etc/hostid", O_RDONLY);
  if (fd >= 0 && read(fd, &id, sizeof(id))) {
    close(fd);
    return id;
  }
  if (fd >= 0)
    close(fd);
  if (gethostname(host, MAXHOSTNAMELEN) >= 0 && *host) {
    struct hostent *hp;
    struct in_addr in;
    struct hostent ghbn_h;
    char ghbn_buf[sizeof(struct in_addr) + sizeof(struct in_addr *) * 2 +
                  sizeof(char *) * ((2 + 5 + 1)) + 256 + 32];
    int ghbn_errno;
    gethostbyname_r(host, &ghbn_h, ghbn_buf, sizeof(ghbn_buf), &hp,
                    &ghbn_errno);
    if (hp == NULL) {
      return 0;
    }
    memcpy(&in, hp->h_addr, hp->h_length);
    return (in.s_addr << 16 | in.s_addr >> 16);
  }
  return 0;
}


x86 assembly (clang, -O0):
<gethostid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1d0,%rsp
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x1d0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11f6 <gethostid+0x96>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     11f6 <gethostid+0x96>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movslq -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12c5 <gethostid+0x165>
cmpl   $0x0,-0x1c(%rbp)
jl     1208 <gethostid+0xa8>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x1d0(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
call   1080 <gethostname@plt>
cmp    $0x0,%rax
jl     12b6 <gethostid+0x156>
mov    -0x1d0(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12b6 <gethostid+0x156>
mov    -0x1d0(%rbp),%rdi
lea    -0x1c0(%rbp),%rdx
lea    -0x40(%rbp),%rsi
mov    $0x174,%ecx
lea    -0x30(%rbp),%r8
lea    -0x1c4(%rbp),%r9
call   1030 <gethostbyname_r@plt>
cmpq   $0x0,-0x30(%rbp)
jne    1280 <gethostid+0x120>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12c5 <gethostid+0x165>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rdi
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%eax
shl    $0x10,%eax
mov    -0x38(%rbp),%ecx
sar    $0x10,%ecx
or     %ecx,%eax
cltq
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    12c5 <gethostid+0x165>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostid>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1a0,%rsp
mov    0x2e6f(%rip),%r12
mov    (%r12),%eax
add    $0x1,%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11d1 <gethostid+0x71>
mov    %eax,%ebx
lea    -0x2c(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1040 <close@plt>
test   %r15,%r15
je     11d1 <gethostid+0x71>
movslq -0x2c(%rbp),%rax
jmp    1232 <gethostid+0xd2>
mov    (%r12),%esi
mov    %r14,%rdi
call   1080 <gethostname@plt>
test   %rax,%rax
js     1230 <gethostid+0xd0>
cmpb   $0x0,(%r14)
je     1230 <gethostid+0xd0>
lea    -0x48(%rbp),%rsi
lea    -0x1c0(%rbp),%rdx
lea    -0x38(%rbp),%r8
lea    -0x3c(%rbp),%r9
mov    %r14,%rdi
mov    $0x174,%ecx
call   1030 <gethostbyname_r@plt>
mov    -0x38(%rbp),%rax
test   %rax,%rax
je     1230 <gethostid+0xd0>
mov    (%rax),%edx
mov    0x4(%rax),%esi
lea    -0x28(%rbp),%rdi
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
shl    $0x10,%ecx
sar    $0x10,%eax
or     %ecx,%eax
cltq
jmp    1232 <gethostid+0xd2>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.in_addr = type { i32 }
@MAXHOSTNAMELEN = global i32 0
@.str = [12 x i8] c"/etc/hostid\00"
@O_RDONLY = global i32 0
define i64 @gethostid() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca %struct.in_addr
%9 = alloca %struct.hostent
%10 = alloca [372 x i8]
%11 = alloca i32
%12 = load i32, i32* @MAXHOSTNAMELEN
%13 = add nsw i32 %12, 1
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %2
%16 = alloca i8, i64 %14
store i64 %14, i64* %3
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %0
%22 = load i32, i32* %4
%23 = call i64 @read(i32 %22, i32* %5, i32 4)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
store i64 %29, i64* %1
store i32 1, i32* %6
br label %67
30: ; preds = %21, %0
%31 = load i32, i32* %4
%32 = icmp sge i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %30
%37 = load i32, i32* @MAXHOSTNAMELEN
%38 = call i64 @gethostname(i8* %16, i32 %37)
%39 = icmp sge i64 %38, 0
br i1 %39, label %40, label %66
40: ; preds = %36
%41 = load i8, i8* %16
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %66
44: ; preds = %40
%45 = getelementptr inbounds [372 x i8], [372 x i8]* %10, i64 0, i64 0
%46 = call i32 @gethostbyname_r(i8* %16, %struct.hostent* %9, i8* %45, i32 372, %struct.hostent** %7, i32* %11)
%47 = load %struct.hostent*, %struct.hostent** %7
%48 = icmp eq %struct.hostent* %47, null
br i1 %48, label %49, label %50
49: ; preds = %44
store i64 0, i64* %1
store i32 1, i32* %6
br label %67
50: ; preds = %44
%51 = load %struct.hostent*, %struct.hostent** %7
%52 = getelementptr inbounds %struct.hostent, %struct.hostent* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load %struct.hostent*, %struct.hostent** %7
%55 = getelementptr inbounds %struct.hostent, %struct.hostent* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @memcpy(%struct.in_addr* %8, i32 %53, i32 %56)
%58 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %8, i32 0, i32 0
%59 = load i32, i32* %58
%60 = shl i32 %59, 16
%61 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %8, i32 0, i32 0
%62 = load i32, i32* %61
%63 = ashr i32 %62, 16
%64 = or i32 %60, %63
%65 = sext i32 %64 to i64
store i64 %65, i64* %1
store i32 1, i32* %6
br label %67
66: ; preds = %40, %36
store i64 0, i64* %1
store i32 1, i32* %6
br label %67
67: ; preds = %66, %50, %49, %25
%68 = load i8*, i8** %2
%69 = load i64, i64* %1
ret i64 %69
}

LLVM IR (-O2):
%struct.hostent = type { i32, i32 }
%struct.in_addr = type { i32 }
@MAXHOSTNAMELEN = global i32 0
@.str = [12 x i8] c"/etc/hostid\00"
@O_RDONLY = global i32 0
define i64 @gethostid() {
%1 = alloca i32
%2 = alloca %struct.hostent*
%3 = alloca %struct.in_addr
%4 = alloca %struct.hostent
%5 = alloca [372 x i8]
%6 = alloca i32
%7 = load i32, i32* @MAXHOSTNAMELEN
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = bitcast i32* %1 to i8*
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %22
15: ; preds = %0
%16 = call i64 @read(i32 %13, i32* %1, i32 4)
%17 = icmp eq i64 %16, 0
%18 = tail call i32 @close(i32 %13)
br i1 %17, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* %1
%21 = sext i32 %20 to i64
br label %52
22: ; preds = %15, %0
%23 = load i32, i32* @MAXHOSTNAMELEN
%24 = call i64 @gethostname(i8* %10, i32 %23)
%25 = icmp sgt i64 %24, -1
br i1 %25, label %26, label %52
26: ; preds = %22
%27 = load i8, i8* %10
%28 = icmp eq i8 %27, 0
br i1 %28, label %52, label %29
29: ; preds = %26
%30 = bitcast %struct.hostent** %2 to i8*
%31 = bitcast %struct.in_addr* %3 to i8*
%32 = bitcast %struct.hostent* %4 to i8*
%33 = getelementptr inbounds [372 x i8], [372 x i8]* %5, i64 0, i64 0
%34 = bitcast i32* %6 to i8*
%35 = call i32 @gethostbyname_r(i8* %10, %struct.hostent* %4, i8* %33, i32 372, %struct.hostent** %2, i32* %6)
%36 = load %struct.hostent*, %struct.hostent** %2
%37 = icmp eq %struct.hostent* %36, null
br i1 %37, label %50, label %38
38: ; preds = %29
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i64 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(%struct.in_addr* %3, i32 %40, i32 %42)
%44 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%45 = load i32, i32* %44
%46 = shl i32 %45, 16
%47 = ashr i32 %45, 16
%48 = or i32 %46, %47
%49 = sext i32 %48 to i64
br label %50
50: ; preds = %29, %38
%51 = phi i64 [ %49, %38 ], [ 0, %29 ]
br label %52
52: ; preds = %22, %26, %50, %19
%53 = phi i64 [ %21, %19 ], [ %51, %50 ], [ 0, %26 ], [ 0, %22 ]
ret i64 %53
}