#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 {
  int h_length;
  int /*<<< orphan*/ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
int get_ip_from_hostname(char *hostname, char *ip, int ip_len) {
  struct hostent *host = gethostbyname(hostname);
  if (NULL == host) {
    fprintf(stderr, "get ip from host error: %s\n", hostname);
    return -1;
  }
  if (host->h_length < 1 || NULL == host->h_addr_list) {
    fprintf(stderr, "the hostname don't have ip list: %s\n", hostname);
    return -2;
  }
  if (strlen(host->h_addr_list[0]) > ip_len) {
    perror("ip array too short!\n");
    return -3;
  }
  struct in_addr **addr_list = (struct in_addr **)host->h_addr_list;
  strcpy(ip, inet_ntoa(*addr_list[0]));
  int i = 0;
  return 0;
}


x86 assembly (clang, -O0):
<get_ip_from_hostname>:
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   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11b1 <get_ip_from_hostname+0x51>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe60(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <get_ip_from_hostname+0xfe>
mov    -0x28(%rbp),%rax
cmpl   $0x1,(%rax)
jl     11ce <get_ip_from_hostname+0x6e>
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    0x8(%rcx),%rax
jne    11f3 <get_ip_from_hostname+0x93>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe3a(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    125e <get_ip_from_hostname+0xfe>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
cmp    -0x1c(%rbp),%eax
jle    1223 <get_ip_from_hostname+0xc3>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    125e <get_ip_from_hostname+0xfe>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_from_hostname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     11d2 <get_ip_from_hostname+0x72>
mov    %rax,%rbp
cmpl   $0x0,(%rax)
jle    11b3 <get_ip_from_hostname+0x53>
mov    0x8(%rbp),%rax
test   %rax,%rax
je     11b3 <get_ip_from_hostname+0x53>
mov    (%rax),%edi
call   1050 <strlen@plt>
cmp    %r15d,%eax
jg     11fa <get_ip_from_hostname+0x9a>
mov    0x8(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
xor    %eax,%eax
jmp    11ef <get_ip_from_hostname+0x8f>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    $0xfffffffe,%eax
jmp    11ef <get_ip_from_hostname+0x8f>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe40(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffd,%eax
jmp    11ef <get_ip_from_hostname+0x8f>

LLVM IR (-O0):
%struct.hostent = type { i32, i32* }
%struct.in_addr = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"get ip from host error: %s\0A\00"
@.str.1 = [37 x i8] c"the hostname don't have ip list: %s\0A\00"
@.str.2 = [21 x i8] c"ip array too short!\0A\00"
define i32 @get_ip_from_hostname(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 = load %struct.hostent*, %struct.hostent** %8
%14 = icmp eq %struct.hostent* null, %13
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %5
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %4
br label %57
19: ; preds = %3
%20 = load %struct.hostent*, %struct.hostent** %8
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = icmp slt i32 %22, 1
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load %struct.hostent*, %struct.hostent** %8
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 1
%27 = load i32*, i32** %26
%28 = icmp eq i32* null, %27
br i1 %28, label %29, label %33
29: ; preds = %24, %19
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %5
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %31)
store i32 -2, i32* %4
br label %57
33: ; preds = %24
%34 = load %struct.hostent*, %struct.hostent** %8
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i32 0, i32 1
%36 = load i32*, i32** %35
%37 = getelementptr inbounds i32, i32* %36, i64 0
%38 = load i32, i32* %37
%39 = call i32 @strlen(i32 %38)
%40 = load i32, i32* %7
%41 = icmp sgt i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %33
%43 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %4
br label %57
44: ; preds = %33
%45 = load %struct.hostent*, %struct.hostent** %8
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 1
%47 = load i32*, i32** %46
%48 = bitcast i32* %47 to %struct.in_addr**
store %struct.in_addr** %48, %struct.in_addr*** %9
%49 = load i8*, i8** %6
%50 = load %struct.in_addr**, %struct.in_addr*** %9
%51 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %50, i64 0
%52 = load %struct.in_addr*, %struct.in_addr** %51
%53 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @inet_ntoa(i32 %54)
%56 = call i32 @strcpy(i8* %49, i32 %55)
store i32 0, i32* %10
store i32 0, i32* %4
br label %57
57: ; preds = %44, %42, %29, %15
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.hostent = type { i32, i32* }
%struct.in_addr = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"get ip from host error: %s\0A\00"
@.str.1 = [37 x i8] c"the hostname don't have ip list: %s\0A\00"
@.str.2 = [21 x i8] c"ip array too short!\0A\00"
define i32 @get_ip_from_hostname(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 %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %34
9: ; preds = %3
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp slt i32 %11, 1
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 1
%15 = load i32*, i32** %14
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %13, %9
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %34
20: ; preds = %13
%21 = load i32, i32* %15
%22 = tail call i32 @strlen(i32 %21)
%23 = icmp sgt i32 %22, %2
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %34
26: ; preds = %20
%27 = bitcast i32** %14 to %struct.in_addr***
%28 = load %struct.in_addr**, %struct.in_addr*** %27
%29 = load %struct.in_addr*, %struct.in_addr** %28
%30 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %29, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i32 @inet_ntoa(i32 %31)
%33 = tail call i32 @strcpy(i8* %1, i32 %32)
br label %34
34: ; preds = %26, %24, %17, %6
%35 = phi i32 [ -1, %6 ], [ -2, %17 ], [ -3, %24 ], [ 0, %26 ]
ret i32 %35
}