#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*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char *hostname = "www.amazon.com";
  char ip[100];
  struct hostent *he;
  struct in_addr **addr_list;
  if ((he = gethostbyname(hostname)) == NULL) {
    printf("gethostbyname error");
    return 1;
  }
  addr_list = (struct in_addr **)he->h_addr_list;
  for (int i = 0; addr_list[i] != NULL; i++) {
    strcpy(ip, inet_ntoa(*addr_list[i]));
  }
  printf("ip is: %s", ip);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea0(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    1198 <main+0x58>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    122b <main+0xeb>
mov    -0x88(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x90(%rbp)
movl   $0x0,-0x94(%rbp)
mov    -0x90(%rbp),%rax
movslq -0x94(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1212 <main+0xd2>
lea    -0x80(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x90(%rbp),%rax
movslq -0x94(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0xa0(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
jmp    11b3 <main+0x73>
lea    -0x80(%rbp),%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0xeb2(%rip),%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     11a2 <main+0x62>
mov    (%rax),%rbx
mov    (%rbx),%rax
test   %rax,%rax
je     118d <main+0x4d>
add    $0x8,%rbx
mov    %rsp,%r14
nopw   0x0(%rax,%rax,1)
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    (%rbx),%rax
add    $0x8,%rbx
test   %rax,%rax
jne    1170 <main+0x30>
lea    0xe8f(%rip),%rdi
xor    %ebx,%ebx
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11b5 <main+0x75>
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
@.str = [15 x i8] c"www.amazon.com\00"
@.str.1 = [20 x i8] c"gethostbyname error\00"
@.str.2 = [10 x i8] c"ip is: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [100 x i8]
%8 = alloca %struct.hostent*
%9 = alloca %struct.in_addr**
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %6
%11 = load i8*, i8** %6
%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 = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %45
16: ; preds = %2
%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 = %39, %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 %42
28: ; preds = %21
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%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 = call i32 @strcpy(i8* %29, i32 %37)
br label %39
39: ; preds = %28
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
store i32 %41, i32* %10
br label %21
42: ; preds = %21
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %14
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
@.str = [15 x i8] c"www.amazon.com\00"
@.str.1 = [20 x i8] c"gethostbyname error\00"
@.str.2 = [10 x i8] c"ip is: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %28
9: ; preds = %2
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%11 = load i64, i64* %10
%12 = inttoptr i64 %11 to %struct.in_addr**
%13 = load %struct.in_addr*, %struct.in_addr** %12
%14 = icmp eq %struct.in_addr* %13, null
br i1 %14, label %15, label %17
15: ; preds = %17, %9
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %28
17: ; preds = %9, %17
%18 = phi i64 [ %24, %17 ], [ 0, %9 ]
%19 = phi %struct.in_addr* [ %26, %17 ], [ %13, %9 ]
%20 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %19, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @inet_ntoa(i32 %21)
%23 = call i32 @strcpy(i8* %4, i32 %22)
%24 = add nuw i64 %18, 1
%25 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %12, i64 %24
%26 = load %struct.in_addr*, %struct.in_addr** %25
%27 = icmp eq %struct.in_addr* %26, null
br i1 %27, label %15, label %17
28: ; preds = %15, %7
%29 = phi i32 [ 1, %7 ], [ 0, %15 ]
ret i32 %29
}