#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ ipstr;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  struct addrinfo hints, *res, *p;
  int status;
  char ipstr[INET6_ADDRSTRLEN];
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((status = getaddrinfo("www.google.se", "telnet", &hints, &res)) != 0) {
    printf("getaddrinfo() error: %s\n", gai_strerror(status));
    return 1;
  }
  printf("IP address:\n\n");
  for (p = res; p != NULL; p = p->ai_next) {
    void *addr;
    char *ipver;
    if (p->ai_family == AF_INET) {
      struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
      addr = &(ipv4->sin_addr);
      ipver = "IPv4";
    } else {
      struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
      addr = &(ipv6->sin6_addr);
      ipver = "IPv6";
    }
    inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));
    printf("  %s: %s\n", ipver, ipstr);
  }
  freeaddrinfo(res);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x90(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe14(%rip),%rdi
lea    0xe1b(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1238 <main+0xd8>
mov    -0x44(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    1314 <main+0x1b4>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12fd <main+0x19d>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
jne    1294 <main+0x134>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    0xdb1(%rip),%rax
mov    %rax,-0x70(%rbp)
jmp    12b3 <main+0x153>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    0xd92(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    -0x88(%rbp),%rdx
mov    -0x90(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rcx),%rdi
mov    -0x68(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x88(%rbp),%rdx
mov    -0x70(%rbp),%rsi
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    124e <main+0xee>
mov    -0x38(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x50(%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    $0x38,%rsp
mov    %rsp,%r12
mov    0x2e5d(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x58(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    0xe21(%rip),%rdi
lea    0xe28(%rip),%rsi
lea    -0x30(%rbp),%rcx
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     1218 <main+0xa8>
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xe11(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
jmp    129a <main+0x12a>
lea    0xe23(%rip),%rdi
call   1040 <puts@plt>
mov    -0x30(%rbp),%r15
xor    %ebx,%ebx
test   %r15,%r15
je     1293 <main+0x123>
mov    %r12,-0x38(%rbp)
lea    0xdfe(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rdi
mov    0x8(%r15),%rsi
mov    0x2d8a(%rip),%rax
cmp    (%rax),%rdi
lea    0xddb(%rip),%rbx
lea    0xdcf(%rip),%rax
cmove  %rax,%rbx
mov    %r13,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%r15),%r15
test   %r15,%r15
jne    1240 <main+0xd0>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%r12
xor    %ebx,%ebx
jmp    1295 <main+0x125>
xor    %edi,%edi
call   1090 <freeaddrinfo@plt>
mov    %r12,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i64, %struct.addrinfo*, i32, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [14 x i8] c"www.google.se\00"
@.str.1 = [7 x i8] c"telnet\00"
@.str.2 = [25 x i8] c"getaddrinfo() error: %s\0A\00"
@.str.3 = [14 x i8] c"IP address:\0A\0A\00"
@AF_INET = global i64 0
@.str.4 = [5 x i8] c"IPv4\00"
@.str.5 = [5 x i8] c"IPv6\00"
@.str.6 = [10 x i8] c" %s: %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.addrinfo*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca %struct.sockaddr_in*
%16 = alloca %struct.sockaddr_in6*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* @INET6_ADDRSTRLEN
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %10
%20 = alloca i8, i64 %18
store i64 %18, i64* %11
%21 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%22 = load i64, i64* @AF_UNSPEC
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i64 %22, i64* %23
%24 = load i32, i32* @SOCK_STREAM
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %24, i32* %25
%26 = load i32, i32* @AI_PASSIVE
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %26, i32* %27
%28 = call i32 @getaddrinfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %28, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %2
%31 = load i32, i32* %9
%32 = call i8* @gai_strerror(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 1, i32* %3
store i32 1, i32* %12
br label %78
34: ; preds = %2
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%36 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %36, %struct.addrinfo** %8
br label %37
37: ; preds = %71, %34
%38 = load %struct.addrinfo*, %struct.addrinfo** %8
%39 = icmp ne %struct.addrinfo* %38, null
br i1 %39, label %40, label %75
40: ; preds = %37
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* @AF_INET
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %54
46: ; preds = %40
%47 = load %struct.addrinfo*, %struct.addrinfo** %8
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i64, i64* %48
%50 = inttoptr i64 %49 to %struct.sockaddr_in*
store %struct.sockaddr_in* %50, %struct.sockaddr_in** %15
%51 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %51, i32 0, i32 0
%53 = bitcast i32* %52 to i8*
store i8* %53, i8** %13
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8** %14
br label %62
54: ; preds = %40
%55 = load %struct.addrinfo*, %struct.addrinfo** %8
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 1
%57 = load i64, i64* %56
%58 = inttoptr i64 %57 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %58, %struct.sockaddr_in6** %16
%59 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %16
%60 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %59, i32 0, i32 0
%61 = bitcast i32* %60 to i8*
store i8* %61, i8** %13
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8** %14
br label %62
62: ; preds = %54, %46
%63 = load %struct.addrinfo*, %struct.addrinfo** %8
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 0
%65 = load i64, i64* %64
%66 = load i8*, i8** %13
%67 = trunc i64 %18 to i32
%68 = call i32 @inet_ntop(i64 %65, i8* %66, i8* %20, i32 %67)
%69 = load i8*, i8** %14
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %69, i8* %20)
br label %71
71: ; preds = %62
%72 = load %struct.addrinfo*, %struct.addrinfo** %8
%73 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %72, i32 0, i32 2
%74 = load %struct.addrinfo*, %struct.addrinfo** %73
store %struct.addrinfo* %74, %struct.addrinfo** %8
br label %37
75: ; preds = %37
%76 = load %struct.addrinfo*, %struct.addrinfo** %7
%77 = call i32 @freeaddrinfo(%struct.addrinfo* %76)
store i32 0, i32* %3
store i32 1, i32* %12
br label %78
78: ; preds = %75, %30
%79 = load i8*, i8** %10
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, %struct.addrinfo*, i32, i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [14 x i8] c"www.google.se\00"
@.str.1 = [7 x i8] c"telnet\00"
@.str.2 = [25 x i8] c"getaddrinfo() error: %s\0A\00"
@AF_INET = global i64 0
@.str.4 = [5 x i8] c"IPv4\00"
@.str.5 = [5 x i8] c"IPv6\00"
@.str.6 = [10 x i8] c" %s: %s\0A\00"
@str = [13 x i8] c"IP address:\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = load i32, i32* @INET6_ADDRSTRLEN
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%12 = load i64, i64* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i64 %12, i64* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @AI_PASSIVE
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %16, i32* %17
%18 = call i32 @getaddrinfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %3, %struct.addrinfo** %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %2
%21 = call i8* @gai_strerror(i32 %18)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %47
23: ; preds = %2
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %44, label %27
27: ; preds = %23, %27
%28 = phi %struct.addrinfo* [ %40, %27 ], [ %25, %23 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* @AF_INET
%32 = icmp eq i64 %30, %31
%33 = select i1 %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0)
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to i8*
%37 = call i32 @inet_ntop(i64 %30, i8* %36, i8* %10, i32 %7)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %33, i8* %10)
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%40 = load %struct.addrinfo*, %struct.addrinfo** %39
%41 = icmp eq %struct.addrinfo* %40, null
br i1 %41, label %42, label %27
42: ; preds = %27
%43 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %44
44: ; preds = %42, %23
%45 = phi %struct.addrinfo* [ %43, %42 ], [ null, %23 ]
%46 = call i32 @freeaddrinfo(%struct.addrinfo* %45)
br label %47
47: ; preds = %44, %20
%48 = phi i32 [ 1, %20 ], [ 0, %44 ]
ret i32 %48
}