#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_NUMERICHOST;
int NI_NUMERICSERV;
int getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *, size_t, char *,
                size_t, int);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int lookup_addrinfo(const struct addrinfo *ai, char *host, size_t hostlen,
                    char *port, size_t portlen) {
  int error = getnameinfo(ai->ai_addr, ai->ai_addrlen, host, hostlen, port,
                          portlen, NI_NUMERICHOST | NI_NUMERICSERV);
  if (error) {
    strncpy(host, "(invalid)", hostlen);
    host[hostlen - 1] = 0;
    strncpy(port, "(invalid)", portlen);
    port[portlen - 1] = 0;
  }
  return error;
}


x86 assembly (clang, -O0):
<lookup_addrinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    0x2e7f(%rip),%r10
or     (%r10),%eax
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11c9 <lookup_addrinfo+0xa9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe70(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe4c(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_addrinfo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    (%rdi),%esi
mov    0x4(%rdi),%edi
mov    0x2e95(%rip),%rax
mov    0x2e9e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    %rbx,%rdx
mov    %r12,%rcx
mov    %r15,%r8
mov    %r14,%r9
call   1040 <getnameinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1197 <lookup_addrinfo+0x77>
lea    0xe91(%rip),%r13
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r12,%rbx,1)
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%r15,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [10 x i8] c"(invalid)\00"
define i32 @lookup_addrinfo(%struct.addrinfo* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca %struct.addrinfo*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%12 = load %struct.addrinfo*, %struct.addrinfo** %6
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load %struct.addrinfo*, %struct.addrinfo** %6
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %7
%19 = load i64, i64* %8
%20 = load i8*, i8** %9
%21 = load i64, i64* %10
%22 = load i32, i32* @NI_NUMERICHOST
%23 = load i32, i32* @NI_NUMERICSERV
%24 = or i32 %22, %23
%25 = call i32 @getnameinfo(i32 %14, i32 %17, i8* %18, i64 %19, i8* %20, i64 %21, i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %5
%29 = load i8*, i8** %7
%30 = load i64, i64* %8
%31 = call i32 @strncpy(i8* %29, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 %30)
%32 = load i8*, i8** %7
%33 = load i64, i64* %8
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %9
%37 = load i64, i64* %10
%38 = call i32 @strncpy(i8* %36, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 %37)
%39 = load i8*, i8** %9
%40 = load i64, i64* %10
%41 = sub i64 %40, 1
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %28, %5
%44 = load i32, i32* %11
ret i32 %44
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [10 x i8] c"(invalid)\00"
define i32 @lookup_addrinfo(%struct.addrinfo* nocapture %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @NI_NUMERICHOST
%11 = load i32, i32* @NI_NUMERICSERV
%12 = or i32 %11, %10
%13 = tail call i32 @getnameinfo(i32 %7, i32 %9, i8* %1, i64 %2, i8* %3, i64 %4, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %5
%16 = tail call i32 @strncpy(i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 %2)
%17 = add i64 %2, -1
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 0, i8* %18
%19 = tail call i32 @strncpy(i8* %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 %4)
%20 = add i64 %4, -1
%21 = getelementptr inbounds i8, i8* %3, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %15, %5
ret i32 %13
}