#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 dummy;
};
struct hostent {
  scalar_t__ h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ printf(char *, char *);
char *get_ip_from_hostname(char *hostname) {
  struct hostent *he = gethostbyname(hostname);
  if (he == NULL) {
    printf("Host \"%s\" not found\n", hostname);
    exit(1);
  }
  struct in_addr **addr_list = (struct in_addr **)he->h_addr_list;
  return inet_ntoa(*addr_list[0]);
}


x86 assembly (clang, -O0):
<get_ip_from_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117e <get_ip_from_hostname+0x3e>
mov    -0x8(%rbp),%rsi
lea    0xe91(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_from_hostname>:
push   %rbx
mov    %rdi,%rbx
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     115c <get_ip_from_hostname+0x1c>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%edi
pop    %rbx
jmp    1030 <inet_ntoa@plt>
lea    0xe9d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
@.str = [21 x i8] c"Host \22%s\22 not found\0A\00"
define i8* @get_ip_from_hostname(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.hostent*
%4 = alloca %struct.in_addr**
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call %struct.hostent* @gethostbyname(i8* %5)
store %struct.hostent* %6, %struct.hostent** %3
%7 = load %struct.hostent*, %struct.hostent** %3
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load %struct.hostent*, %struct.hostent** %3
%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*** %4
%18 = load %struct.in_addr**, %struct.in_addr*** %4
%19 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %18, i64 0
%20 = load %struct.in_addr*, %struct.in_addr** %19
%21 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i8* @inet_ntoa(i32 %22)
ret i8* %23
}

LLVM IR (-O2):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
@.str = [21 x i8] c"Host \22%s\22 not found\0A\00"
define i8* @get_ip_from_hostname(i8* %0) {
%2 = tail call %struct.hostent* @gethostbyname(i8* %0)
%3 = icmp eq %struct.hostent* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
%9 = load i64, i64* %8
%10 = inttoptr i64 %9 to %struct.in_addr**
%11 = load %struct.in_addr*, %struct.in_addr** %10
%12 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i8* @inet_ntoa(i32 %13)
ret i8* %14
}