#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;
};

/* Variables and functions */
struct hostent *gethostbyname(char *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ printf(char *);
char *hostname_to_ip(char *hostname) {
  struct hostent *h;
  if ((h = gethostbyname(hostname)) == NULL) {
    printf("gethostbyname");
    return NULL;
  }
  return inet_ntoa(*((struct in_addr *)h->h_addr));
}


x86 assembly (clang, -O0):
<hostname_to_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    116c <hostname_to_ip+0x3c>
lea    0xea6(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    117e <hostname_to_ip+0x4e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostname_to_ip>:
push   %rax
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1146 <hostname_to_ip+0x16>
mov    (%rax),%rax
mov    (%rax),%edi
pop    %rax
jmp    1030 <inet_ntoa@plt>
lea    0xeb3(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
@.str = [14 x i8] c"gethostbyname\00"
define i8* @hostname_to_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.hostent*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call %struct.hostent* @gethostbyname(i8* %5)
store %struct.hostent* %6, %struct.hostent** %4
%7 = icmp eq %struct.hostent* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %18
10: ; preds = %1
%11 = load %struct.hostent*, %struct.hostent** %4
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i32 0, i32 0
%13 = load i64, i64* %12
%14 = inttoptr i64 %13 to %struct.in_addr*
%15 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i8* @inet_ntoa(i32 %16)
store i8* %17, i8** %2
br label %18
18: ; preds = %10, %8
%19 = load i8*, i8** %2
ret i8* %19
}

LLVM IR (-O2):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
@.str = [14 x i8] c"gethostbyname\00"
define i8* @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 @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %13
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 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %9, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i8* @inet_ntoa(i32 %11)
br label %13
13: ; preds = %6, %4
%14 = phi i8* [ null, %4 ], [ %12, %6 ]
ret i8* %14
}