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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, int /*<<< orphan*/ *,
                struct addrinfo **);
int getnameinfo(scalar_t__, int /*<<< orphan*/, char *, int,
                int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ntohs(int);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ stderr;
int doit(const char *host, const char *service) {
  struct addrinfo *result;
  struct addrinfo *res;
  int error;
  error = getaddrinfo(host, service, NULL, &result);
  if (error != 0) {
    fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
    return EXIT_FAILURE;
  }
  for (res = result; res != NULL; res = res->ai_next) {
    char hostname[1025] = "";
    error =
        getnameinfo(res->ai_addr, res->ai_addrlen, hostname, 1025, NULL, 0, 0);
    if (error != 0) {
      fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
      continue;
    }
    int port = 0;
    if (res->ai_family == AF_INET)
      port = ((struct sockaddr_in *)res->ai_addr)->sin_port;
    else if (res->ai_family == AF_INET6)
      port = ((struct sockaddr_in6 *)res->ai_addr)->sin6_port;
    port = ntohs(port);
    printf("hostname: %s, port: %d\n", hostname, port);
  }
  freeaddrinfo(result);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<doit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11f2 <doit+0x72>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x438(%rbp),%edi
mov    %rax,%rdx
lea    0xe24(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1337 <doit+0x1b7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1322 <doit+0x1a2>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x401,%edx
call   1050 <memset@plt>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%esi
lea    -0x430(%rbp),%rdx
mov    $0x401,%ecx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
movl   $0x0,(%rsp)
call   1060 <getnameinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1285 <doit+0x105>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x43c(%rbp),%edi
mov    %rax,%rdx
lea    0xd9f(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1311 <doit+0x191>
movl   $0x0,-0x434(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
jne    12bb <doit+0x13b>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
jmp    12e7 <doit+0x167>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2d17(%rip),%rcx
cmp    (%rcx),%rax
jne    12e2 <doit+0x162>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
jmp    12e7 <doit+0x167>
mov    -0x434(%rbp),%edi
call   1080 <ntohs@plt>
mov    %eax,-0x434(%rbp)
lea    -0x430(%rbp),%rsi
mov    -0x434(%rbp),%edx
lea    0xd28(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11fa <doit+0x7a>
mov    -0x20(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    0x2c8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     11ce <doit+0x4e>
mov    0x2e28(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe48(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2dff(%rip),%rax
jmp    12a1 <doit+0x121>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1293 <doit+0x113>
lea    0x10(%rsp),%r12
lea    0xe4c(%rip),%r14
mov    0x2de1(%rip),%r13
lea    0xe24(%rip),%r15
jmp    1221 <doit+0xa1>
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%ebp
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     128c <doit+0x10c>
mov    $0x401,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%esi
movl   $0x0,(%rsp)
mov    %r12,%rdx
mov    $0x401,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <getnameinfo@plt>
test   %eax,%eax
jne    1200 <doit+0x80>
mov    (%rbx),%rax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
je     1272 <doit+0xf2>
xor    %edi,%edi
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
jne    1278 <doit+0xf8>
mov    0x8(%rbx),%rax
mov    (%rax),%edi
call   1080 <ntohs@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <printf@plt>
jmp    1218 <doit+0x98>
mov    0x8(%rsp),%rdi
jmp    1295 <doit+0x115>
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, i32, %struct.addrinfo* }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"error in getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"error in getnameinfo: %s\0A\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str.2 = [24 x i8] c"hostname: %s, port: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @doit(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca [1025 x i8]
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = call i32 @getaddrinfo(i8* %0, i8* %1, i32* null, %struct.addrinfo** %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load %struct.addrinfo*, %struct.addrinfo** %3
%10 = icmp eq %struct.addrinfo* %9, null
br i1 %10, label %58, label %11
11: ; preds = %8
%12 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
br label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i8* @gai_strerror(i32 %6)
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %61
17: ; preds = %11, %52
%18 = phi %struct.addrinfo* [ %9, %11 ], [ %54, %52 ]
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%22 = load i32, i32* %21
%23 = call i32 @getnameinfo(i64 %20, i32 %22, i8* %12, i32 1025, i32* null, i32 0, i32 0)
%24 = icmp eq i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = call i8* @gai_strerror(i32 %23)
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %52
29: ; preds = %17
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 0
%31 = load i64, i64* %30
%32 = load i64, i64* @AF_INET
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i64, i64* %19
%36 = inttoptr i64 %35 to %struct.sockaddr_in*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i64 0, i32 0
br label %45
38: ; preds = %29
%39 = load i64, i64* @AF_INET6
%40 = icmp eq i64 %31, %39
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i64, i64* %19
%43 = inttoptr i64 %42 to %struct.sockaddr_in6*
%44 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %43, i64 0, i32 0
br label %45
45: ; preds = %34, %41
%46 = phi i32* [ %44, %41 ], [ %37, %34 ]
%47 = load i32, i32* %46
br label %48
48: ; preds = %45, %38
%49 = phi i32 [ 0, %38 ], [ %47, %45 ]
%50 = call i32 @ntohs(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %12, i32 %50)
br label %52
52: ; preds = %48, %25
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 3
%54 = load %struct.addrinfo*, %struct.addrinfo** %53
%55 = icmp eq %struct.addrinfo* %54, null
br i1 %55, label %56, label %17
56: ; preds = %52
%57 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %58
58: ; preds = %56, %8
%59 = phi %struct.addrinfo* [ %57, %56 ], [ null, %8 ]
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
br label %61
61: ; preds = %58, %13
%62 = phi i32* [ @EXIT_FAILURE, %13 ], [ @EXIT_SUCCESS, %58 ]
%63 = load i32, i32* %62
ret i32 %63
}