#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
};

/* Variables and functions */
int NI_MAXHOST;
int NI_MAXSERV;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, int, int,
                           int, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *, int, char *,
                int, int);
int /*<<< orphan*/ net_resolve(char *, char *, int, int, int,
                               struct addrinfo **);
int /*<<< orphan*/ stdout;
void resolve(char *ai_host, char *ai_serv, int family, int type, int flags) {
  struct addrinfo *ai;
  char ni_host[NI_MAXHOST], ni_serv[NI_MAXSERV];
  int e;
  net_resolve(ai_host, ai_serv, family, type, flags, &ai);
  if (ai) {
    e = getnameinfo(ai->ai_addr, ai->ai_addrlen, ni_host, NI_MAXHOST, ni_serv,
                    NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV);
    if (e != 0)
      err(1, "getnameinfo");
    (void)fprintf(stdout,
                  "%s:%s (family %d, type %d, flags 0x%04x) resolved to node "
                  "%s service %s\n",
                  ai_host, ai_serv, family, type, flags, ni_host, ni_serv);
  } else
    (void)fprintf(stdout,
                  "%s:%s (family %d, type %d, flags 0x%04x) failed to resolv\n",
                  ai_host, ai_serv, family, type, flags);
  freeaddrinfo(ai);
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
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)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    -0x28(%rbp),%r9
call   1030 <net_resolve@plt>
cmpq   $0x0,-0x28(%rbp)
je     1298 <resolve+0x148>
mov    -0x50(%rbp),%r8
mov    -0x58(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%ecx
mov    0x2dda(%rip),%rax
mov    (%rax),%r9d
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    0x2dcf(%rip),%r10
or     (%r10),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     124a <resolve+0xfa>
mov    $0x1,%edi
lea    0xdbb(%rip),%rsi
call   1060 <err@plt>
mov    -0x50(%rbp),%rax
mov    -0x58(%rbp),%r10
mov    0x2d6f(%rip),%rcx
mov    (%rcx),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
mov    -0x1c(%rbp),%r11d
lea    0xd96(%rip),%rsi
sub    $0x20,%rsp
mov    %r11d,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
mov    $0x0,%al
call   1050 <fprintf@plt>
add    $0x20,%rsp
jmp    12cd <resolve+0x17d>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
mov    -0x1c(%rbp),%eax
lea    0xd9a(%rip),%rsi
sub    $0x10,%rsp
mov    %eax,(%rsp)
mov    $0x0,%al
call   1050 <fprintf@plt>
add    $0x10,%rsp
mov    -0x28(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsp,-0x58(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x40(%rbp),%r9
mov    %rdi,%r12
mov    %rsi,%rbx
mov    %edx,%r13d
mov    %ecx,-0x2c(%rbp)
mov    %r8,-0x38(%rbp)
call   1030 <net_resolve@plt>
mov    -0x40(%rbp),%rax
test   %rax,%rax
je     1257 <resolve+0x107>
mov    %r12,-0x48(%rbp)
mov    %rbx,-0x50(%rbp)
mov    %r13d,-0x30(%rbp)
mov    (%rax),%esi
mov    0x4(%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%ecx
mov    0x2e00(%rip),%rax
mov    (%rax),%r9d
mov    0x2de6(%rip),%r8
mov    0x2df7(%rip),%rdx
mov    (%rdx),%eax
or     (%r8),%eax
sub    $0x8,%rsp
mov    %r14,%rdx
mov    %r15,%r8
push   %rax
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
je     121f <resolve+0xcf>
lea    0xdeb(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
sub    $0x8,%rsp
lea    0xdd9(%rip),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    -0x30(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
xor    %eax,%eax
push   %r15
push   %r14
push   -0x38(%rbp)
call   1050 <fprintf@plt>
add    $0x20,%rsp
jmp    1286 <resolve+0x136>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
sub    $0x8,%rsp
lea    0xdea(%rip),%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
mov    %r13d,%r8d
mov    -0x2c(%rbp),%r9d
xor    %eax,%eax
push   -0x38(%rbp)
call   1050 <fprintf@plt>
add    $0x10,%rsp
mov    -0x40(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x58(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [12 x i8] c"getnameinfo\00"
@stdout = global i32 0
@.str.1 = [73 x i8] c"%s:%s (family %d, type %d, flags 0x%04x) resolved to node %s service %s\0A\00"
@.str.2 = [59 x i8] c"%s:%s (family %d, type %d, flags 0x%04x) failed to resolv\0A\00"
define void @resolve(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.addrinfo*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = load i32, i32* @NI_MAXHOST
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %12
%19 = alloca i8, i64 %17
store i64 %17, i64* %13
%20 = load i32, i32* @NI_MAXSERV
%21 = zext i32 %20 to i64
%22 = alloca i8, i64 %21
store i64 %21, i64* %14
%23 = load i8*, i8** %6
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = load i32, i32* %9
%27 = load i32, i32* %10
%28 = call i32 @net_resolve(i8* %23, i8* %24, i32 %25, i32 %26, i32 %27, %struct.addrinfo** %11)
%29 = load %struct.addrinfo*, %struct.addrinfo** %11
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %56
31: ; preds = %5
%32 = load %struct.addrinfo*, %struct.addrinfo** %11
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %11
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @NI_MAXHOST
%39 = load i32, i32* @NI_MAXSERV
%40 = load i32, i32* @NI_NUMERICHOST
%41 = load i32, i32* @NI_NUMERICSERV
%42 = or i32 %40, %41
%43 = call i32 @getnameinfo(i32 %34, i32 %37, i8* %19, i32 %38, i8* %22, i32 %39, i32 %42)
store i32 %43, i32* %15
%44 = load i32, i32* %15
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %31
%47 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %48
48: ; preds = %46, %31
%49 = load i32, i32* @stdout
%50 = load i8*, i8** %6
%51 = load i8*, i8** %7
%52 = load i32, i32* %8
%53 = load i32, i32* %9
%54 = load i32, i32* %10
%55 = call i32 (i32, i8*, i8*, i8*, i32, i32, i32, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.1, i64 0, i64 0), i8* %50, i8* %51, i32 %52, i32 %53, i32 %54, i8* %19, i8* %22)
br label %64
56: ; preds = %5
%57 = load i32, i32* @stdout
%58 = load i8*, i8** %6
%59 = load i8*, i8** %7
%60 = load i32, i32* %8
%61 = load i32, i32* %9
%62 = load i32, i32* %10
%63 = call i32 (i32, i8*, i8*, i8*, i32, i32, i32, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* %58, i8* %59, i32 %60, i32 %61, i32 %62)
br label %64
64: ; preds = %56, %48
%65 = load %struct.addrinfo*, %struct.addrinfo** %11
%66 = call i32 @freeaddrinfo(%struct.addrinfo* %65)
%67 = load i8*, i8** %12
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [12 x i8] c"getnameinfo\00"
@stdout = global i32 0
@.str.1 = [73 x i8] c"%s:%s (family %d, type %d, flags 0x%04x) resolved to node %s service %s\0A\00"
@.str.2 = [59 x i8] c"%s:%s (family %d, type %d, flags 0x%04x) failed to resolv\0A\00"
define void @resolve(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.addrinfo*
%7 = bitcast %struct.addrinfo** %6 to i8*
%8 = load i32, i32* @NI_MAXHOST
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = load i32, i32* @NI_MAXSERV
%13 = zext i32 %12 to i64
%14 = alloca i8, i64 %13
%15 = call i32 @net_resolve(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4, %struct.addrinfo** %6)
%16 = load %struct.addrinfo*, %struct.addrinfo** %6
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %35, label %18
18: ; preds = %5
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @NI_MAXHOST
%24 = load i32, i32* @NI_MAXSERV
%25 = load i32, i32* @NI_NUMERICHOST
%26 = load i32, i32* @NI_NUMERICSERV
%27 = or i32 %26, %25
%28 = call i32 @getnameinfo(i32 %20, i32 %22, i8* %11, i32 %23, i8* %14, i32 %24, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %18
%31 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %18
%33 = load i32, i32* @stdout
%34 = call i32 (i32, i8*, i8*, i8*, i32, i32, i32, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1, i32 %2, i32 %3, i32 %4, i8* %11, i8* %14)
br label %38
35: ; preds = %5
%36 = load i32, i32* @stdout
%37 = call i32 (i32, i8*, i8*, i8*, i32, i32, i32, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1, i32 %2, i32 %3, i32 %4)
br label %38
38: ; preds = %35, %32
%39 = load %struct.addrinfo*, %struct.addrinfo** %6
%40 = call i32 @freeaddrinfo(%struct.addrinfo* %39)
ret void
}