#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 hostent {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int gethostname(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  int i;
  char host[1024];
  struct hostent *hostent;
  if (argc > 1)
    strcpy(host, argv[1]);
  else {
    fprintf(stderr, "calling gethostname() ...\n");
    if (gethostname(host, 1024) == -1) {
      perror("gethostname");
      exit(1);
    } else {
      fprintf(stderr, "host name is %s\n", host);
    }
  }
  fprintf(stderr, "calling gethostbyname(host) ...\n");
  if (!(hostent = gethostbyname(host))) {
    perror("gethostbyname");
    exit(1);
  } else {
    fprintf(stderr, "gethostbyname() succeeded\n");
  }
  fprintf(stderr, "printing ip address(es) for this host ...\n");
  if (hostent) {
    for (i = 0; hostent->h_addr_list[i]; i++) {
      fprintf(stderr, "%x\n", *(int *)hostent->h_addr_list[i]);
    }
  } else {
    fprintf(stderr, "ERROR: can't look up: %s\n", host);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    119c <main+0x3c>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
jmp    1206 <main+0xa6>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   1070 <gethostname@plt>
cmp    $0xffffffff,%eax
jne    11e3 <main+0x83>
lea    0xe47(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1206 <main+0xa6>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x420(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jne    1250 <main+0xf0>
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpq   $0x0,-0x428(%rbp)
je     12ea <main+0x18a>
movl   $0x0,-0x14(%rbp)
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     12e5 <main+0x185>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edx
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1293 <main+0x133>
jmp    1308 <main+0x1a8>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
cmp    $0x2,%edi
jl     1181 <main+0x21>
mov    0x8(%rsi),%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
jmp    11c1 <main+0x61>
mov    0x2e58(%rip),%rbx
mov    (%rbx),%edi
lea    0xe6f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rsp,%rdi
mov    $0x400,%esi
call   1070 <gethostname@plt>
cmp    $0xffffffff,%eax
je     126a <main+0x10a>
mov    (%rbx),%edi
lea    0xe70(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e18(%rip),%r15
mov    (%r15),%edi
lea    0xe66(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rsp,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1261 <main+0x101>
mov    %rax,%r12
mov    (%r15),%edi
lea    0xe74(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
lea    0xe7e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r12),%rax
mov    (%rax),%rax
test   %rax,%rax
je     1250 <main+0xf0>
mov    $0x8,%ebx
lea    0xe8a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15),%edi
mov    (%rax),%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r12),%rax
mov    (%rax,%rbx,1),%rax
add    $0x8,%rbx
test   %rax,%rax
jne    1230 <main+0xd0>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf1(%rip),%rdi
jmp    1271 <main+0x111>
lea    0xdaa(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64* }
@stderr = global i32 0
@.str = [27 x i8] c"calling gethostname() ...\0A\00"
@.str.1 = [12 x i8] c"gethostname\00"
@.str.2 = [17 x i8] c"host name is %s\0A\00"
@.str.3 = [33 x i8] c"calling gethostbyname(host) ...\0A\00"
@.str.4 = [14 x i8] c"gethostbyname\00"
@.str.5 = [27 x i8] c"gethostbyname() succeeded\0A\00"
@.str.6 = [43 x i8] c"printing ip address(es) for this host ...\0A\00"
@.str.7 = [4 x i8] c"%x\0A\00"
@.str.8 = [26 x i8] c"ERROR: can't look up: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @strcpy(i8* %12, i8* %15)
br label %31
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%21 = call i32 @gethostname(i8* %20, i32 1024)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %26
br label %31
31: ; preds = %30, %11
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = call %struct.hostent* @gethostbyname(i8* %34)
store %struct.hostent* %35, %struct.hostent** %8
%36 = icmp ne %struct.hostent* %35, null
br i1 %36, label %40, label %37
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %31
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
%46 = load %struct.hostent*, %struct.hostent** %8
%47 = icmp ne %struct.hostent* %46, null
br i1 %47, label %48, label %74
48: ; preds = %43
store i32 0, i32* %6
br label %49
49: ; preds = %70, %48
%50 = load %struct.hostent*, %struct.hostent** %8
%51 = getelementptr inbounds %struct.hostent, %struct.hostent* %50, i32 0, i32 0
%52 = load i64*, i64** %51
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i64, i64* %52, i64 %54
%56 = load i64, i64* %55
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %73
58: ; preds = %49
%59 = load i32, i32* @stderr
%60 = load %struct.hostent*, %struct.hostent** %8
%61 = getelementptr inbounds %struct.hostent, %struct.hostent* %60, i32 0, i32 0
%62 = load i64*, i64** %61
%63 = load i32, i32* %6
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i64, i64* %62, i64 %64
%66 = load i64, i64* %65
%67 = inttoptr i64 %66 to i32*
%68 = load i32, i32* %67
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %68)
br label %70
70: ; preds = %58
%71 = load i32, i32* %6
%72 = add nsw i32 %71, 1
store i32 %72, i32* %6
br label %49
73: ; preds = %49
br label %78
74: ; preds = %43
%75 = load i32, i32* @stderr
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i8* %76)
br label %78
78: ; preds = %74, %73
ret i32 0
}

LLVM IR (-O2):
%struct.hostent = type { i64* }
@stderr = global i32 0
@.str = [27 x i8] c"calling gethostname() ...\0A\00"
@.str.1 = [12 x i8] c"gethostname\00"
@.str.2 = [17 x i8] c"host name is %s\0A\00"
@.str.3 = [33 x i8] c"calling gethostbyname(host) ...\0A\00"
@.str.4 = [14 x i8] c"gethostbyname\00"
@.str.5 = [27 x i8] c"gethostbyname() succeeded\0A\00"
@.str.6 = [43 x i8] c"printing ip address(es) for this host ...\0A\00"
@.str.7 = [4 x i8] c"%x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i32 @strcpy(i8* %4, i8* %8)
br label %21
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @gethostname(i8* %4, i32 1024)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %21
21: ; preds = %18, %6
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%24 = call %struct.hostent* @gethostbyname(i8* %4)
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 0
%35 = load i64*, i64** %34
%36 = load i64, i64* %35
%37 = icmp eq i64 %36, 0
br i1 %37, label %50, label %38
38: ; preds = %29, %38
%39 = phi i64 [ %45, %38 ], [ 0, %29 ]
%40 = phi i64 [ %48, %38 ], [ %36, %29 ]
%41 = load i32, i32* @stderr
%42 = inttoptr i64 %40 to i32*
%43 = load i32, i32* %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %43)
%45 = add nuw i64 %39, 1
%46 = load i64*, i64** %34
%47 = getelementptr inbounds i64, i64* %46, i64 %45
%48 = load i64, i64* %47
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %38
50: ; preds = %38, %29
ret i32 0
}