#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ exit(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                struct addrinfo **);
scalar_t__ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
main(int argc, char *argv[]) {
  struct addrinfo *res, *pt;
  struct sockaddr_in *sinp;
  const char *addr;
  char abuf[INET_ADDRSTRLEN];
  int succ = 0, i = 0;
  if (argc == 1) {
    printf(
        "USAGE: getip <server name>\nFor example:\n    getip www.google.com\n");
    exit(1);
  }
  succ = getaddrinfo(argv[1], NULL, NULL, &res);
  if (succ != 0) {
    printf("Can't get address info! error code = %d", succ);
    exit(succ);
  }
  for (pt = res, i = 0; pt != NULL; pt = pt->ai_next, i++) {
    sinp = (struct sockaddr_in *)pt->ai_addr;
    addr = (const char *)inet_ntop(AF_INET, &sinp->sin_addr, abuf,
                                   INET_ADDRSTRLEN);
    printf("%2d. IP=%s\n", i, addr);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11b2 <main+0x72>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
mov    %rdx,%rsi
call   1060 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     11f0 <main+0xb0>
mov    -0x44(%rbp),%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x44(%rbp),%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1267 <main+0x127>
mov    -0x50(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    0x2dab(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x30(%rbp)
mov    -0x48(%rbp),%esi
mov    -0x30(%rbp),%rdx
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11ff <main+0xbf>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%rcx
mov    0x2e6d(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
cmp    $0x1,%edi
je     120a <main+0xba>
mov    %rcx,-0x30(%rbp)
mov    0x8(%rsi),%rdi
lea    -0x38(%rbp),%rcx
xor    %esi,%esi
xor    %edx,%edx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
jne    1220 <main+0xd0>
mov    -0x38(%rbp),%r12
test   %r12,%r12
je     11f5 <main+0xa5>
mov    %r14,%r13
lea    0xe70(%rip),%r14
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rsi
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%edi
mov    0x0(%r13),%ecx
mov    %r15,%rdx
call   1030 <inet_ntop@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebx
mov    0x8(%r12),%r12
test   %r12,%r12
jne    11c0 <main+0x70>
mov    -0x30(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe23(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %eax,%ebx
lea    0xdd7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, %struct.addrinfo* }
%struct.sockaddr_in = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@.str = [66 x i8] c"USAGE: getip <server name>\0AFor example:\0A getip www.google.com\0A\00"
@.str.1 = [40 x i8] c"Can't get address info! error code = %d\00"
@AF_INET = global i32 0
@.str.2 = [12 x i8] c"%2d. IP=%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.sockaddr_in*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @INET_ADDRSTRLEN
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %10
%17 = alloca i8, i64 %15
store i64 %15, i64* %11
store i32 0, i32* %12
store i32 0, i32* %13
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @getaddrinfo(i8* %26, i32* null, i32* null, %struct.addrinfo** %6)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = load i32, i32* %12
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %12
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %23
%36 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %36, %struct.addrinfo** %7
store i32 0, i32* %13
br label %37
37: ; preds = %54, %35
%38 = load %struct.addrinfo*, %struct.addrinfo** %7
%39 = icmp ne %struct.addrinfo* %38, null
br i1 %39, label %40, label %60
40: ; preds = %37
%41 = load %struct.addrinfo*, %struct.addrinfo** %7
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = inttoptr i64 %43 to %struct.sockaddr_in*
store %struct.sockaddr_in* %44, %struct.sockaddr_in** %8
%45 = load i32, i32* @AF_INET
%46 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %46, i32 0, i32 0
%48 = load i32, i32* @INET_ADDRSTRLEN
%49 = call i64 @inet_ntop(i32 %45, i32* %47, i8* %17, i32 %48)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %9
%51 = load i32, i32* %13
%52 = load i8*, i8** %9
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %51, i8* %52)
br label %54
54: ; preds = %40
%55 = load %struct.addrinfo*, %struct.addrinfo** %7
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 1
%57 = load %struct.addrinfo*, %struct.addrinfo** %56
store %struct.addrinfo* %57, %struct.addrinfo** %7
%58 = load i32, i32* %13
%59 = add nsw i32 %58, 1
store i32 %59, i32* %13
br label %37
60: ; preds = %37
%61 = load i8*, i8** %10
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, %struct.addrinfo* }
%struct.sockaddr_in = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@.str.1 = [40 x i8] c"Can't get address info! error code = %d\00"
@AF_INET = global i32 0
@.str.2 = [12 x i8] c"%2d. IP=%s\0A\00"
@str = [65 x i8] c"USAGE: getip <server name>\0AFor example:\0A getip www.google.com\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo** %3 to i8*
%5 = load i32, i32* @INET_ADDRSTRLEN
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = icmp eq i32 %0, 1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @getaddrinfo(i8* %15, i32* null, i32* null, %struct.addrinfo** %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load %struct.addrinfo*, %struct.addrinfo** %3
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %40, label %24
21: ; preds = %13
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%23 = call i32 @exit(i32 %16)
unreachable
24: ; preds = %18, %24
%25 = phi %struct.addrinfo* [ %38, %24 ], [ %19, %18 ]
%26 = phi i32 [ %37, %24 ], [ 0, %18 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to %struct.sockaddr_in*
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %29, i64 0, i32 0
%32 = load i32, i32* @INET_ADDRSTRLEN
%33 = call i64 @inet_ntop(i32 %30, i32* %31, i8* %8, i32 %32)
%34 = inttoptr i64 %33 to i8*
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %26, i8* %34)
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%37 = add nuw nsw i32 %26, 1
%38 = load %struct.addrinfo*, %struct.addrinfo** %36
%39 = icmp eq %struct.addrinfo* %38, null
br i1 %39, label %40, label %24
40: ; preds = %24, %18
ret i32 0
}