#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*/ uint16_t;
struct in_addr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr_list;
};
typedef int int32_t;

/* Variables and functions */
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int32_t hostname_to_ip(char *hostname, char *ip, uint16_t ip_len) {
  struct hostent *he;
  struct in_addr **addr_list;
  int32_t i;
  if ((he = gethostbyname(hostname)) == NULL) {
    herror("gethostbyname");
    return 1;
  }
  addr_list = (struct in_addr **)he->h_addr_list;
  for (i = 0; addr_list[i] != NULL; i++) {
    strncpy(ip, inet_ntoa(*addr_list[i]), ip_len);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<hostname_to_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1182 <hostname_to_ip+0x42>
lea    0xe8f(%rip),%rdi
call   1060 <herror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e3 <hostname_to_ip+0xa3>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11dc <hostname_to_ip+0x9c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e3 <hostname_to_ip+0xa3>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostname_to_ip>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1174 <hostname_to_ip+0x34>
mov    (%rax),%rax
mov    (%rax),%rax
test   %rax,%rax
je     1180 <hostname_to_ip+0x40>
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
jmp    1185 <hostname_to_ip+0x45>
lea    0xe85(%rip),%rdi
call   1060 <herror@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
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, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.hostent*
%9 = alloca %struct.in_addr**
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %8
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %44
16: ; preds = %3
%17 = load %struct.hostent*, %struct.hostent** %8
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to %struct.in_addr**
store %struct.in_addr** %20, %struct.in_addr*** %9
store i32 0, i32* %10
br label %21
21: ; preds = %40, %16
%22 = load %struct.in_addr**, %struct.in_addr*** %9
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %22, i64 %24
%26 = load %struct.in_addr*, %struct.in_addr** %25
%27 = icmp ne %struct.in_addr* %26, null
br i1 %27, label %28, label %43
28: ; preds = %21
%29 = load i8*, i8** %6
%30 = load %struct.in_addr**, %struct.in_addr*** %9
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %30, i64 %32
%34 = load %struct.in_addr*, %struct.in_addr** %33
%35 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @inet_ntoa(i32 %36)
%38 = load i32, i32* %7
%39 = call i32 @strncpy(i8* %29, i32 %37, i32 %38)
store i32 0, i32* %4
br label %44
40: ; No predecessors!
%41 = load i32, i32* %10
%42 = add nsw i32 %41, 1
store i32 %42, i32* %10
br label %21
43: ; preds = %21
store i32 1, i32* %4
br label %44
44: ; preds = %43, %28, %14
%45 = load i32, i32* %4
ret i32 %45
}

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, i32 %2) {
%4 = tail call %struct.hostent* @gethostbyname(i8* %0)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %19
8: ; preds = %3
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%10 = load i64, i64* %9
%11 = inttoptr i64 %10 to %struct.in_addr**
%12 = load %struct.in_addr*, %struct.in_addr** %11
%13 = icmp eq %struct.in_addr* %12, null
br i1 %13, label %19, label %14
14: ; preds = %8
%15 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @inet_ntoa(i32 %16)
%18 = tail call i32 @strncpy(i8* %1, i32 %17, i32 %2)
br label %19
19: ; preds = %8, %14, %6
%20 = phi i32 [ 1, %6 ], [ 0, %14 ], [ 1, %8 ]
ret i32 %20
}