#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 */
typedef int /*<<< orphan*/ uint32_t;
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ h_addr_list;
};

/* Variables and functions */
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ inet_addr(char *);
uint32_t hostname_to_ip(char *hostname) {
  struct hostent *he;
  struct in_addr **addr_list;
  int i;
  if ((he = gethostbyname(hostname)) == NULL) {
    return inet_addr(hostname);
  }
  addr_list = (struct in_addr **)he->h_addr_list;
  for (i = 0; addr_list[i] != NULL; i++) {
    return (*addr_list[i]).s_addr;
  }
  return 0;
}


x86 assembly (clang, -O0):
<hostname_to_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1154 <hostname_to_ip+0x34>
mov    -0x10(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %eax,-0x4(%rbp)
jmp    1196 <hostname_to_ip+0x76>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     118f <hostname_to_ip+0x6f>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <hostname_to_ip+0x76>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostname_to_ip>:
push   %rbx
mov    %rdi,%rbx
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     113d <hostname_to_ip+0x1d>
mov    (%rax),%rax
mov    (%rax),%rax
test   %rax,%rax
je     1146 <hostname_to_ip+0x26>
mov    (%rax),%eax
pop    %rbx
ret
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <inet_addr@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
define i32 @hostname_to_ip(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.hostent*
%5 = alloca %struct.in_addr**
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call %struct.hostent* @gethostbyname(i8* %7)
store %struct.hostent* %8, %struct.hostent** %4
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @inet_addr(i8* %11)
store i32 %12, i32* %2
br label %37
13: ; preds = %1
%14 = load %struct.hostent*, %struct.hostent** %4
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 0
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to %struct.in_addr**
store %struct.in_addr** %17, %struct.in_addr*** %5
store i32 0, i32* %6
br label %18
18: ; preds = %33, %13
%19 = load %struct.in_addr**, %struct.in_addr*** %5
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %19, i64 %21
%23 = load %struct.in_addr*, %struct.in_addr** %22
%24 = icmp ne %struct.in_addr* %23, null
br i1 %24, label %25, label %36
25: ; preds = %18
%26 = load %struct.in_addr**, %struct.in_addr*** %5
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %26, i64 %28
%30 = load %struct.in_addr*, %struct.in_addr** %29
%31 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %30, i32 0, i32 0
%32 = load i32, i32* %31
store i32 %32, i32* %2
br label %37
33: ; No predecessors!
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %18
36: ; preds = %18
store i32 0, i32* %2
br label %37
37: ; preds = %36, %25, %10
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
define i32 @hostname_to_ip(i8* %0) {
%2 = tail call %struct.hostent* @gethostbyname(i8* %0)
%3 = icmp eq %struct.hostent* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @inet_addr(i8* %0)
br label %15
6: ; preds = %1
%7 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = inttoptr i64 %8 to %struct.in_addr**
%10 = load %struct.in_addr*, %struct.in_addr** %9
%11 = icmp eq %struct.in_addr* %10, null
br i1 %11, label %15, label %12
12: ; preds = %6
%13 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %10, i64 0, i32 0
%14 = load i32, i32* %13
br label %15
15: ; preds = %6, %12, %4
%16 = phi i32 [ %5, %4 ], [ %14, %12 ], [ 0, %6 ]
ret i32 %16
}