#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  int ai_family;
  char *ai_canonname;
  struct addrinfo *ai_next;
  TYPE_1__ *ai_addr;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ logmsg;
typedef int /*<<< orphan*/ hints;
struct TYPE_2__ {
  void *sa_data;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ AI_CANONNAME;
int PF_INET6;
int PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ inet_ntop(int, void *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int getaddr(char *hostname, char *addrstr) {
  struct addrinfo hints, *res;
  int errcode;
  void *ptr;
  hostname[1023] = '\0';
  gethostname(hostname, 1023);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_CANONNAME;
  errcode = getaddrinfo(hostname, NULL, &hints, &res);
  if (errcode != 0) {
    perror("getaddrinfo");
    return -1;
  }
  char logmsg[128];
  snprintf(logmsg, sizeof(logmsg), "Host: %s\n", hostname);
  while (res) {
    inet_ntop(res->ai_family, res->ai_addr->sa_data, addrstr, 100);
    switch (res->ai_family) {
    case AF_INET:
      ptr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
      break;
    case AF_INET6:
      ptr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
      break;
    }
    inet_ntop(res->ai_family, ptr, addrstr, 100);
    char logmsg[128];
    snprintf(logmsg, sizeof(logmsg), "IPv%d address: %s (%s)\n",
             res->ai_family == PF_INET6 ? 6 : 4, addrstr, res->ai_canonname);
    res = res->ai_next;
  }
}


x86 assembly (clang, -O0):
<getaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,0x3ff(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x3ff,%esi
call   1070 <gethostname@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     11fa <getaddr+0x9a>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <getaddr+0x19a>
lea    -0xe0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x80,%esi
lea    0xdfb(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
cmpq   $0x0,-0x48(%rbp)
je     12fa <getaddr+0x19a>
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
mov    0x18(%rax),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x164(%rbp)
sub    $0x80,%eax
je     1281 <getaddr+0x121>
jmp    125a <getaddr+0xfa>
mov    -0x164(%rbp),%eax
sub    $0x81,%eax
jne    128d <getaddr+0x12d>
jmp    1270 <getaddr+0x110>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    128d <getaddr+0x12d>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
lea    -0x160(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%ecx
mov    $0x6,%eax
cmp    %esi,%edx
cmove  %eax,%ecx
mov    -0x18(%rbp),%r8
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%r9
mov    $0x80,%esi
lea    0xd34(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1218 <getaddr+0xb8>
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaddr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x130,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
movb   $0x0,0x3ff(%rdi)
mov    $0x3ff,%esi
call   1070 <gethostname@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
jne    129e <getaddr+0x13e>
lea    0xe2b(%rip),%rdx
lea    0xb0(%rsp),%rdi
mov    $0x80,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    (%rsp),%rax
test   %rax,%rax
je     12aa <getaddr+0x14a>
mov    0x2dd4(%rip),%r13
lea    0xe03(%rip),%r14
lea    0x30(%rsp),%r15
jmp    126f <getaddr+0x10f>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rsi
mov    %rbx,%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    (%rsp),%rax
mov    (%rax),%ecx
xor    %edx,%edx
cmp    0x0(%r13),%ecx
sete   %dl
lea    (%rdx,%rdx,1),%ecx
add    $0x4,%ecx
mov    0x8(%rax),%r9
mov    %r15,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
mov    %rbx,%r8
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     12aa <getaddr+0x14a>
mov    (%rax),%edi
mov    0x18(%rax),%rax
mov    (%rax),%rsi
mov    %rbx,%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    (%rsp),%rax
mov    (%rax),%edi
mov    %edi,%ecx
and    $0xfffffffe,%ecx
cmp    $0x80,%ecx
jne    1220 <getaddr+0xc0>
mov    0x18(%rax),%r12
jmp    1220 <getaddr+0xc0>
lea    0xd5b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i8*, %struct.addrinfo*, %struct.TYPE_2__*, i32, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_CANONNAME = global i32 0
@.str = [12 x i8] c"getaddrinfo\00"
@.str.1 = [10 x i8] c"Host: %s\0A\00"
@.str.2 = [24 x i8] c"IPv%d address: %s (%s)\0A\00"
@PF_INET6 = global i32 0
define i32 @getaddr(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [128 x i8]
%11 = alloca [128 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 1023
store i8 0, i8* %13
%14 = load i8*, i8** %4
%15 = call i32 @gethostname(i8* %14, i32 1023)
%16 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%17 = load i32, i32* @PF_UNSPEC
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_STREAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 5
store i32 %19, i32* %20
%21 = load i32, i32* @AI_CANONNAME
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %21, i32* %22
%23 = load i8*, i8** %4
%24 = call i32 @getaddrinfo(i8* %23, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %87
29: ; preds = %2
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%31 = load i8*, i8** %4
%32 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %30, i32 128, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %64, %29
%34 = load %struct.addrinfo*, %struct.addrinfo** %7
%35 = icmp ne %struct.addrinfo* %34, null
br i1 %35, label %36, label %87
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %7
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %7
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 3
%42 = load %struct.TYPE_2__*, %struct.TYPE_2__** %41
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = load i8*, i8** %5
%46 = call i32 @inet_ntop(i32 %39, i8* %44, i8* %45, i32 100)
%47 = load %struct.addrinfo*, %struct.addrinfo** %7
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 0
%49 = load i32, i32* %48
switch i32 %49, label %64 [
i32 129, label %50
i32 128, label %57
]
50: ; preds = %36
%51 = load %struct.addrinfo*, %struct.addrinfo** %7
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 3
%53 = load %struct.TYPE_2__*, %struct.TYPE_2__** %52
%54 = bitcast %struct.TYPE_2__* %53 to %struct.sockaddr_in*
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i32 0, i32 0
%56 = bitcast i32* %55 to i8*
store i8* %56, i8** %9
br label %64
57: ; preds = %36
%58 = load %struct.addrinfo*, %struct.addrinfo** %7
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 3
%60 = load %struct.TYPE_2__*, %struct.TYPE_2__** %59
%61 = bitcast %struct.TYPE_2__* %60 to %struct.sockaddr_in6*
%62 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %61, i32 0, i32 0
%63 = bitcast i32* %62 to i8*
store i8* %63, i8** %9
br label %64
64: ; preds = %36, %57, %50
%65 = load %struct.addrinfo*, %struct.addrinfo** %7
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i8*, i8** %9
%69 = load i8*, i8** %5
%70 = call i32 @inet_ntop(i32 %67, i8* %68, i8* %69, i32 100)
%71 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%72 = load %struct.addrinfo*, %struct.addrinfo** %7
%73 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = load i32, i32* @PF_INET6
%76 = icmp eq i32 %74, %75
%77 = zext i1 %76 to i64
%78 = select i1 %76, i32 6, i32 4
%79 = load i8*, i8** %5
%80 = load %struct.addrinfo*, %struct.addrinfo** %7
%81 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %80, i32 0, i32 1
%82 = load i8*, i8** %81
%83 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %71, i32 128, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %78, i8* %79, i8* %82)
%84 = load %struct.addrinfo*, %struct.addrinfo** %7
%85 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %84, i32 0, i32 2
%86 = load %struct.addrinfo*, %struct.addrinfo** %85
store %struct.addrinfo* %86, %struct.addrinfo** %7
br label %33
87: ; preds = %27, %33
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i8*, %struct.addrinfo*, %struct.TYPE_2__*, i32, i32 }
%struct.TYPE_2__ = type { i8* }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_CANONNAME = global i32 0
@.str = [12 x i8] c"getaddrinfo\00"
@.str.1 = [10 x i8] c"Host: %s\0A\00"
@.str.2 = [24 x i8] c"IPv%d address: %s (%s)\0A\00"
@PF_INET6 = global i32 0
define i32 @getaddr(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca [128 x i8]
%6 = alloca [128 x i8]
%7 = bitcast %struct.addrinfo* %3 to i8*
%8 = bitcast %struct.addrinfo** %4 to i8*
%9 = getelementptr inbounds i8, i8* %0, i64 1023
store i8 0, i8* %9
%10 = tail call i32 @gethostname(i8* %0, i32 1023)
%11 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%12 = load i32, i32* @PF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %14, i32* %15
%16 = load i32, i32* @AI_CANONNAME
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %16, i32* %17
%18 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %65
22: ; preds = %2
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%24 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %23, i32 128, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %64, label %27
27: ; preds = %22
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
br label %29
29: ; preds = %27, %48
%30 = phi i8* [ undef, %27 ], [ %49, %48 ]
%31 = phi %struct.addrinfo* [ %25, %27 ], [ %62, %48 ]
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 0
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 3
%35 = load %struct.TYPE_2__*, %struct.TYPE_2__** %34
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i64 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @inet_ntop(i32 %33, i8* %37, i8* %1, i32 100)
%39 = load %struct.addrinfo*, %struct.addrinfo** %4
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, -2
%43 = icmp eq i32 %42, 128
br i1 %43, label %44, label %48
44: ; preds = %29
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 3
%46 = bitcast %struct.TYPE_2__** %45 to i8**
%47 = load i8*, i8** %46
br label %48
48: ; preds = %29, %44
%49 = phi i8* [ %30, %29 ], [ %47, %44 ]
%50 = call i32 @inet_ntop(i32 %41, i8* %49, i8* %1, i32 100)
%51 = load %struct.addrinfo*, %struct.addrinfo** %4
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i64 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* @PF_INET6
%55 = icmp eq i32 %53, %54
%56 = select i1 %55, i32 6, i32 4
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i64 0, i32 1
%58 = load i8*, i8** %57
%59 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %28, i32 128, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %56, i8* %1, i8* %58)
%60 = load %struct.addrinfo*, %struct.addrinfo** %4
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i64 0, i32 2
%62 = load %struct.addrinfo*, %struct.addrinfo** %61
store %struct.addrinfo* %62, %struct.addrinfo** %4
%63 = icmp eq %struct.addrinfo* %62, null
br i1 %63, label %64, label %29
64: ; preds = %48, %22
br label %65
65: ; preds = %64, %20
ret i32 -1
}