#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;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ id;

/* Variables and functions */
int /*<<< orphan*/ MAXHOSTNAMELEN;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
struct hostent *gethostbyname(char *);
scalar_t__ gethostname(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, void *, int);
long int gethostid(void) {
  char host[MAXHOSTNAMELEN + 1];
  int fd, id;
  if ((fd = open("/etc/hostid", O_RDONLY)) >= 0 &&
      read(fd, (void *)&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;
    if ((hp = gethostbyname(host)) == (struct hostent *)NULL)
      return 0;
    else {
      memcpy((char *)&in, (char *)hp->h_addr, hp->h_length);
      return (in.s_addr << 16 | in.s_addr >> 16);
    }
  } else
    return 0;
}


x86 assembly (clang, -O0):
<gethostid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e79(%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,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     11ef <gethostid+0x8f>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
je     11ef <gethostid+0x8f>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movslq -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    129e <gethostid+0x13e>
cmpl   $0x0,-0x1c(%rbp)
jl     1201 <gethostid+0xa1>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x40(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1080 <gethostname@plt>
cmp    $0x0,%rax
jl     128f <gethostid+0x12f>
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     128f <gethostid+0x12f>
mov    -0x40(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1258 <gethostid+0xf8>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    129e <gethostid+0x13e>
lea    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
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    129e <gethostid+0x13e>
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    $0x10,%rsp
mov    0x2e72(%rip),%r12
mov    (%r12),%eax
add    $0x1,%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ce <gethostid+0x6e>
mov    %eax,%ebx
lea    -0x2c(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1030 <close@plt>
test   %r15,%r15
je     11ce <gethostid+0x6e>
movslq -0x2c(%rbp),%rax
jmp    1214 <gethostid+0xb4>
mov    (%r12),%esi
mov    %r14,%rdi
call   1080 <gethostname@plt>
test   %rax,%rax
js     1212 <gethostid+0xb2>
cmpb   $0x0,(%r14)
je     1212 <gethostid+0xb2>
mov    %r14,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1212 <gethostid+0xb2>
mov    0x8(%rax),%rsi
mov    (%rax),%edx
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    1214 <gethostid+0xb4>
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, i64 }
%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 = load i32, i32* @MAXHOSTNAMELEN
%10 = add nsw i32 %9, 1
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %2
%13 = alloca i8, i64 %11
store i64 %11, i64* %3
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %4
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %0
%18 = load i32, i32* %4
%19 = bitcast i32* %5 to i8*
%20 = call i64 @read(i32 %18, i8* %19, i32 4)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
store i64 %26, i64* %1
store i32 1, i32* %6
br label %64
27: ; preds = %17, %0
%28 = load i32, i32* %4
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %30, %27
%34 = load i32, i32* @MAXHOSTNAMELEN
%35 = call i64 @gethostname(i8* %13, i32 %34)
%36 = icmp sge i64 %35, 0
br i1 %36, label %37, label %63
37: ; preds = %33
%38 = load i8, i8* %13
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %63
41: ; preds = %37
%42 = call %struct.hostent* @gethostbyname(i8* %13)
store %struct.hostent* %42, %struct.hostent** %7
%43 = icmp eq %struct.hostent* %42, null
br i1 %43, label %44, label %45
44: ; preds = %41
store i64 0, i64* %1
store i32 1, i32* %6
br label %64
45: ; preds = %41
%46 = bitcast %struct.in_addr* %8 to i8*
%47 = load %struct.hostent*, %struct.hostent** %7
%48 = getelementptr inbounds %struct.hostent, %struct.hostent* %47, i32 0, i32 1
%49 = load i64, i64* %48
%50 = inttoptr i64 %49 to i8*
%51 = load %struct.hostent*, %struct.hostent** %7
%52 = getelementptr inbounds %struct.hostent, %struct.hostent* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @memcpy(i8* %46, i8* %50, i32 %53)
%55 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %8, i32 0, i32 0
%56 = load i32, i32* %55
%57 = shl i32 %56, 16
%58 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %8, i32 0, i32 0
%59 = load i32, i32* %58
%60 = ashr i32 %59, 16
%61 = or i32 %57, %60
%62 = sext i32 %61 to i64
store i64 %62, i64* %1
store i32 1, i32* %6
br label %64
63: ; preds = %37, %33
store i64 0, i64* %1
store i32 1, i32* %6
br label %64
64: ; preds = %63, %45, %44, %22
%65 = load i8*, i8** %2
%66 = load i64, i64* %1
ret i64 %66
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32, i64 }
@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.in_addr
%3 = load i32, i32* @MAXHOSTNAMELEN
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32* %1 to i8*
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %18
11: ; preds = %0
%12 = call i64 @read(i32 %9, i8* %7, i32 4)
%13 = icmp eq i64 %12, 0
%14 = tail call i32 @close(i32 %9)
br i1 %13, label %18, label %15
15: ; preds = %11
%16 = load i32, i32* %1
%17 = sext i32 %16 to i64
br label %44
18: ; preds = %11, %0
%19 = load i32, i32* @MAXHOSTNAMELEN
%20 = call i64 @gethostname(i8* %6, i32 %19)
%21 = icmp sgt i64 %20, -1
br i1 %21, label %22, label %44
22: ; preds = %18
%23 = load i8, i8* %6
%24 = icmp eq i8 %23, 0
br i1 %24, label %44, label %25
25: ; preds = %22
%26 = bitcast %struct.in_addr* %2 to i8*
%27 = call %struct.hostent* @gethostbyname(i8* %6)
%28 = icmp eq %struct.hostent* %27, null
br i1 %28, label %42, label %29
29: ; preds = %25
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i8*
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @memcpy(i8* %26, i8* %32, i32 %34)
%36 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i64 0, i32 0
%37 = load i32, i32* %36
%38 = shl i32 %37, 16
%39 = ashr i32 %37, 16
%40 = or i32 %38, %39
%41 = sext i32 %40 to i64
br label %42
42: ; preds = %25, %29
%43 = phi i64 [ %41, %29 ], [ 0, %25 ]
br label %44
44: ; preds = %18, %22, %42, %15
%45 = phi i64 [ %17, %15 ], [ %43, %42 ], [ 0, %22 ], [ 0, %18 ]
ret i64 %45
}