#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 {
  scalar_t__ s_addr;
};
struct hostent {
  int h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ res_init();
int /*<<< orphan*/ stderr;
int check_hostname(char *hostname, struct in_addr *ip) {
  struct hostent *he;
  res_init();
  he = gethostbyname(hostname);
  if (he == NULL) {
    herror("lookup failed");
    return 0;
  }
  if (he->h_length != sizeof(struct in_addr)) {
    fprintf(stderr, "Unsupported h_length (%d)\n", he->h_length);
    return 0;
  }
  struct in_addr *addr = (struct in_addr *)he->h_addr;
  if (ip->s_addr != 0 && ip->s_addr != addr->s_addr) {
    *ip = *addr;
    return 1;
  }
  *ip = *addr;
  return 0;
}


x86 assembly (clang, -O0):
<check_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1030 <res_init@plt>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1187 <check_hostname+0x47>
lea    0xe8a(%rip),%rdi
call   1060 <herror@plt>
movl   $0x0,-0x4(%rbp)
jmp    121c <check_hostname+0xdc>
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
cmp    $0x8,%rax
je     11bf <check_hostname+0x7f>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    121c <check_hostname+0xdc>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1207 <check_hostname+0xc7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
je     1207 <check_hostname+0xc7>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    121c <check_hostname+0xdc>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_hostname>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <res_init@plt>
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1180 <check_hostname+0x40>
mov    (%rax),%edx
cmp    $0x8,%edx
jne    118e <check_hostname+0x4e>
mov    0x8(%rax),%rax
mov    (%r14),%rcx
test   %rcx,%rcx
je     1178 <check_hostname+0x38>
cmp    (%rax),%rcx
jne    11ac <check_hostname+0x6c>
mov    (%rax),%rax
mov    %rax,(%r14)
jmp    11a3 <check_hostname+0x63>
lea    0xe79(%rip),%rdi
call   1060 <herror@plt>
jmp    11a5 <check_hostname+0x65>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1050 <fprintf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rax),%rax
mov    %rax,(%r14)
mov    $0x1,%ebp
jmp    11a5 <check_hostname+0x65>

LLVM IR (-O0):
%struct.in_addr = type { i64 }
%struct.hostent = type { i32, i64 }
@.str = [14 x i8] c"lookup failed\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unsupported h_length (%d)\0A\00"
define i32 @check_hostname(i8* %0, %struct.in_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca %struct.hostent*
%7 = alloca %struct.in_addr*
store i8* %0, i8** %4
store %struct.in_addr* %1, %struct.in_addr** %5
%8 = call i32 (...) @res_init()
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %6
%11 = load %struct.hostent*, %struct.hostent** %6
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %54
15: ; preds = %2
%16 = load %struct.hostent*, %struct.hostent** %6
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = sext i32 %18 to i64
%20 = icmp ne i64 %19, 8
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = load %struct.hostent*, %struct.hostent** %6
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 0, i32* %3
br label %54
27: ; preds = %15
%28 = load %struct.hostent*, %struct.hostent** %6
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 1
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.in_addr*
store %struct.in_addr* %31, %struct.in_addr** %7
%32 = load %struct.in_addr*, %struct.in_addr** %5
%33 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %49
36: ; preds = %27
%37 = load %struct.in_addr*, %struct.in_addr** %5
%38 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = load %struct.in_addr*, %struct.in_addr** %7
%41 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = icmp ne i64 %39, %42
br i1 %43, label %44, label %49
44: ; preds = %36
%45 = load %struct.in_addr*, %struct.in_addr** %5
%46 = load %struct.in_addr*, %struct.in_addr** %7
%47 = bitcast %struct.in_addr* %45 to i8*
%48 = bitcast %struct.in_addr* %46 to i8*
store i32 1, i32* %3
br label %54
49: ; preds = %36, %27
%50 = load %struct.in_addr*, %struct.in_addr** %5
%51 = load %struct.in_addr*, %struct.in_addr** %7
%52 = bitcast %struct.in_addr* %50 to i8*
%53 = bitcast %struct.in_addr* %51 to i8*
store i32 0, i32* %3
br label %54
54: ; preds = %49, %44, %21, %13
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.in_addr = type { i64 }
%struct.hostent = type { i32, i64 }
@.str = [14 x i8] c"lookup failed\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unsupported h_length (%d)\0A\00"
define i32 @check_hostname(i8* %0, %struct.in_addr* nocapture %1) {
%3 = tail call i32 (...) @res_init()
%4 = tail call %struct.hostent* @gethostbyname(i8* %0)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %32
8: ; preds = %2
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp eq i32 %10, 8
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %32
15: ; preds = %8
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 1
%17 = load i64, i64* %16
%18 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
%19 = load i64, i64* %18
%20 = icmp eq i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %15
%22 = inttoptr i64 %17 to %struct.in_addr*
%23 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %22, i64 0, i32 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %19, %24
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = inttoptr i64 %17 to i64*
%28 = load i64, i64* %27
store i64 %28, i64* %18
br label %32
29: ; preds = %21, %15
%30 = inttoptr i64 %17 to i64*
%31 = load i64, i64* %30
store i64 %31, i64* %18
br label %32
32: ; preds = %26, %29, %12, %6
%33 = phi i32 [ 0, %6 ], [ 0, %12 ], [ 1, %26 ], [ 0, %29 ]
ret i32 %33
}