#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;
  int ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ AI_CANONNAME;
int /*<<< orphan*/ NI_MAXHOST;
int /*<<< orphan*/ NI_NAMEREQD;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ gethostname(char *, int);
int getnameinfo(scalar_t__, int, char *, int /*<<< orphan*/,
                int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  char hostname[64];
  gethostname(hostname, 64);
  printf("gethostname: %s\n", hostname);
  struct addrinfo hints, *res;
  int error;
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_CANONNAME;
  hints.ai_family = AF_UNSPEC;
  error = getaddrinfo(hostname, NULL, &hints, &res);
  if (error == 0) {
    char addr_str[res->ai_addrlen];
    if (res->ai_family == AF_INET) {
      struct sockaddr_in *addr4;
      addr4 = (struct sockaddr_in *)res->ai_addr;
      inet_ntop(res->ai_family, &addr4->sin_addr, addr_str, res->ai_addrlen);
    } else if (res->ai_family == AF_INET6) {
      struct sockaddr_in6 *addr6;
      addr6 = (struct sockaddr_in6 *)res->ai_addr;
      inet_ntop(res->ai_family, &addr6->sin6_addr, addr_str, res->ai_addrlen);
    }
    printf("ip address: %s\n", addr_str);
    error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
                        NULL, 0, NI_NAMEREQD);
    freeaddrinfo(res);
    printf("resolved hostname: %s\n", hostname);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x40,%esi
call   1070 <gethostname@plt>
lea    -0x50(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x70(%rbp)
lea    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x70(%rbp),%rdx
lea    -0x78(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
jne    132b <main+0x1bb>
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x90(%rbp)
mov    -0x78(%rbp),%rax
mov    (%rax),%rax
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
jne    126b <main+0xfb>
mov    -0xa8(%rbp),%rdx
mov    -0x78(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x98(%rbp)
mov    -0x78(%rbp),%rax
mov    (%rax),%rdi
mov    -0x98(%rbp),%rsi
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%ecx
call   1030 <inet_ntop@plt>
jmp    12b7 <main+0x147>
mov    -0x78(%rbp),%rax
mov    (%rax),%rax
mov    0x2d4f(%rip),%rcx
cmp    (%rcx),%rax
jne    12b2 <main+0x142>
mov    -0xa8(%rbp),%rdx
mov    -0x78(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x78(%rbp),%rax
mov    (%rax),%rdi
mov    -0xa0(%rbp),%rsi
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%ecx
call   1030 <inet_ntop@plt>
jmp    12b7 <main+0x147>
mov    -0xa8(%rbp),%rsi
lea    0xd4c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x78(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%esi
lea    -0x50(%rbp),%rdx
mov    0x2cd4(%rip),%rax
mov    (%rax),%ecx
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x7c(%rbp)
mov    -0x78(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
lea    -0x50(%rbp),%rsi
lea    0xd05(%rip),%rdi
call   1040 <printf@plt>
mov    -0x88(%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   %r14
push   %rbx
sub    $0x70,%rsp
lea    -0x80(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1070 <gethostname@plt>
lea    0xe6d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    -0x38(%rbp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
lea    -0x18(%rbp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
jne    128c <main+0x11c>
mov    %rsp,%r14
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rdi
je     121b <main+0xab>
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rdi
jne    122a <main+0xba>
mov    0x10(%rax),%rsi
mov    0x8(%rax),%ecx
mov    %rbx,%rdx
call   1030 <inet_ntop@plt>
lea    0xde0(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x8(%rax),%esi
mov    0x2d6d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
lea    -0x80(%rbp),%rbx
mov    %rbx,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    -0x18(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
lea    0xda0(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %r14,%rsp
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i64, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [17 x i8] c"gethostname: %s\0A\00"
@AI_CANONNAME = global i32 0
@AF_UNSPEC = global i64 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str.1 = [16 x i8] c"ip address: %s\0A\00"
@NI_MAXHOST = global i32 0
@NI_NAMEREQD = global i32 0
@.str.2 = [23 x i8] c"resolved hostname: %s\0A\00"
define i32 @main() {
%1 = alloca [64 x i8]
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%5 = call i32 @gethostname(i8* %4, i32 64)
%6 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %4)
%7 = bitcast %struct.addrinfo* %2 to i8*
%8 = bitcast %struct.addrinfo** %3 to i8*
%9 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 32)
%10 = load i32, i32* @AI_CANONNAME
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i64, i64* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 0
store i64 %12, i64* %13
%14 = call i32 @getaddrinfo(i8* %4, i32* null, %struct.addrinfo* %2, %struct.addrinfo** %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %59
16: ; preds = %0
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 1
%19 = load i32, i32* %18
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
%22 = alloca i8, i64 %20
%23 = load %struct.addrinfo*, %struct.addrinfo** %3
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @AF_INET
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %16
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.sockaddr_in*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i64 0, i32 0
br label %41
33: ; preds = %16
%34 = load i64, i64* @AF_INET6
%35 = icmp eq i64 %25, %34
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to %struct.sockaddr_in6*
%40 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %39, i64 0, i32 0
br label %41
41: ; preds = %28, %36
%42 = phi i32* [ %40, %36 ], [ %32, %28 ]
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%44 = load i32, i32* %43
%45 = call i32 @inet_ntop(i64 %25, i32* %42, i8* %22, i32 %44)
br label %46
46: ; preds = %41, %33
%47 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%48 = load %struct.addrinfo*, %struct.addrinfo** %3
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i64 0, i32 2
%50 = load i64, i64* %49
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i64 0, i32 1
%52 = load i32, i32* %51
%53 = load i32, i32* @NI_MAXHOST
%54 = load i32, i32* @NI_NAMEREQD
%55 = call i32 @getnameinfo(i64 %50, i32 %52, i8* %4, i32 %53, i32* null, i32 0, i32 %54)
%56 = load %struct.addrinfo*, %struct.addrinfo** %3
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
%58 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %59
59: ; preds = %46, %0
ret i32 0
}