#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 */
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int hostname_to_ip(char *hostname, char *ip) {
  struct hostent *he;
  struct in_addr **addr_list;
  int i;
  if ((he = gethostbyname(hostname)) == NULL) {
    herror("gethostbyname");
    return 0;
  }
  addr_list = (struct in_addr **)he->h_addr_list;
  for (i = 0; addr_list[i] != NULL; i++) {
    strcpy(ip, inet_ntoa(*addr_list[i]));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<hostname_to_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    117f <hostname_to_ip+0x3f>
lea    0xe92(%rip),%rdi
call   1060 <herror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11dd <hostname_to_ip+0x9d>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11d6 <hostname_to_ip+0x96>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
jmp    11dd <hostname_to_ip+0x9d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostname_to_ip>:
push   %rbx
mov    %rsi,%rbx
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1171 <hostname_to_ip+0x31>
mov    (%rax),%rax
mov    (%rax),%rax
test   %rax,%rax
je     117d <hostname_to_ip+0x3d>
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    $0x1,%eax
pop    %rbx
ret
lea    0xe88(%rip),%rdi
call   1060 <herror@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
@.str = [14 x i8] c"gethostbyname\00"
define i32 @hostname_to_ip(i8* %0, i8* %1) {
%3 = tail call %struct.hostent* @gethostbyname(i8* %0)
%4 = icmp eq %struct.hostent* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %18
7: ; preds = %2
%8 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, 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 = icmp eq %struct.in_addr* %11, null
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %11, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @inet_ntoa(i32 %15)
%17 = tail call i32 @strcpy(i8* %1, i32 %16)
br label %18
18: ; preds = %7, %13, %5
%19 = phi i32 [ 0, %5 ], [ 1, %13 ], [ 0, %7 ]
ret i32 %19
}