#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_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  int ai_family;
  scalar_t__ ai_addr;
};
typedef int /*<<< orphan*/ buf2;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ *inet_ntop(int, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int /*<<< orphan*/);
char *strdup(char *);
char *_common_lookup_print(struct addrinfo *ai) {
  char buf[128];
  char buf2[128];
  struct sockaddr_in *sin;
  struct sockaddr_in6 *sin6;
  switch (ai->ai_family) {
  case AF_INET:
    sin = (struct sockaddr_in *)ai->ai_addr;
    if (inet_ntop(ai->ai_family, &sin->sin_addr, buf, sizeof(buf)) == NULL)
      return NULL;
    snprintf(buf2, sizeof(buf2), "%s:%hu", buf, ntohs(sin->sin_port));
    break;
  case AF_INET6:
    sin6 = (struct sockaddr_in6 *)ai->ai_addr;
    if (inet_ntop(ai->ai_family, &sin6->sin6_addr, buf, sizeof(buf)) == NULL)
      return NULL;
    snprintf(buf2, sizeof(buf2), "[%s]:%hu", buf, ntohs(sin6->sin6_port));
    break;
  default:
    return NULL;
  }
  return strdup(buf2);
}


x86 assembly (clang, -O0):
<_common_lookup_print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
sub    $0x80,%eax
je     121a <_common_lookup_print+0xda>
jmp    116b <_common_lookup_print+0x2b>
mov    -0x124(%rbp),%eax
sub    $0x81,%eax
jne    12b3 <_common_lookup_print+0x173>
jmp    1181 <_common_lookup_print+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x118(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x118(%rbp),%rsi
add    $0x4,%rsi
lea    -0x90(%rbp),%rdx
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    11c9 <_common_lookup_print+0x89>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <_common_lookup_print+0x190>
lea    -0x110(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    -0x118(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    -0x138(%rbp),%rdi
mov    -0x130(%rbp),%rcx
mov    %eax,%r8d
mov    $0x80,%esi
lea    0xdf0(%rip),%rdx
call   1040 <snprintf@plt>
jmp    12c0 <_common_lookup_print+0x180>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x120(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x120(%rbp),%rsi
add    $0x4,%rsi
lea    -0x90(%rbp),%rdx
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    1262 <_common_lookup_print+0x122>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <_common_lookup_print+0x190>
lea    -0x110(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    -0x120(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    -0x148(%rbp),%rdi
mov    -0x140(%rbp),%rcx
mov    %eax,%r8d
mov    $0x80,%esi
lea    0xd5e(%rip),%rdx
call   1040 <snprintf@plt>
jmp    12c0 <_common_lookup_print+0x180>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <_common_lookup_print+0x190>
lea    -0x110(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_common_lookup_print>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    (%rdi),%eax
cmp    $0x80,%eax
je     1182 <_common_lookup_print+0x42>
cmp    $0x81,%eax
jne    11d7 <_common_lookup_print+0x97>
mov    0x8(%rdi),%rbx
lea    0x4(%rbx),%rsi
mov    %rsp,%rdx
mov    $0x81,%edi
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     11d7 <_common_lookup_print+0x97>
lea    0xe80(%rip),%r14
jmp    11a8 <_common_lookup_print+0x68>
mov    0x8(%rdi),%rbx
lea    0x4(%rbx),%rsi
mov    %rsp,%rdx
mov    $0x80,%edi
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     11d7 <_common_lookup_print+0x97>
lea    0xe5f(%rip),%r14
mov    (%rbx),%edi
call   1050 <ntohs@plt>
lea    0x80(%rsp),%rbx
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
mov    %eax,%r8d
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <strdup@plt>
jmp    11d9 <_common_lookup_print+0x99>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@.str = [7 x i8] c"%s:%hu\00"
@.str.1 = [9 x i8] c"[%s]:%hu\00"
define i8* @_common_lookup_print(%struct.addrinfo* %0) {
%2 = alloca i8*
%3 = alloca %struct.addrinfo*
%4 = alloca [128 x i8]
%5 = alloca [128 x i8]
%6 = alloca %struct.sockaddr_in*
%7 = alloca %struct.sockaddr_in6*
store %struct.addrinfo* %0, %struct.addrinfo** %3
%8 = load %struct.addrinfo*, %struct.addrinfo** %3
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
%10 = load i32, i32* %9
switch i32 %10, label %55 [
i32 129, label %11
i32 128, label %33
]
11: ; preds = %1
%12 = load %struct.addrinfo*, %struct.addrinfo** %3
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 1
%14 = load i64, i64* %13
%15 = inttoptr i64 %14 to %struct.sockaddr_in*
store %struct.sockaddr_in* %15, %struct.sockaddr_in** %6
%16 = load %struct.addrinfo*, %struct.addrinfo** %3
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 1
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%22 = call i32* @inet_ntop(i32 %18, i32* %20, i8* %21, i32 128)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %11
store i8* null, i8** %2
br label %59
25: ; preds = %11
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @ntohs(i32 %30)
%32 = call i32 @snprintf(i8* %26, i32 128, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %27, i32 %31)
br label %56
33: ; preds = %1
%34 = load %struct.addrinfo*, %struct.addrinfo** %3
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 1
%36 = load i64, i64* %35
%37 = inttoptr i64 %36 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %37, %struct.sockaddr_in6** %7
%38 = load %struct.addrinfo*, %struct.addrinfo** %3
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%42 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %41, i32 0, i32 1
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%44 = call i32* @inet_ntop(i32 %40, i32* %42, i8* %43, i32 128)
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %47
46: ; preds = %33
store i8* null, i8** %2
br label %59
47: ; preds = %33
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%50 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%51 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @ntohs(i32 %52)
%54 = call i32 @snprintf(i8* %48, i32 128, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %49, i32 %53)
br label %56
55: ; preds = %1
store i8* null, i8** %2
br label %59
56: ; preds = %47, %25
%57 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%58 = call i8* @strdup(i8* %57)
store i8* %58, i8** %2
br label %59
59: ; preds = %56, %55, %46, %24
%60 = load i8*, i8** %2
ret i8* %60
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@.str = [7 x i8] c"%s:%hu\00"
@.str.1 = [9 x i8] c"[%s]:%hu\00"
define noalias i8* @_common_lookup_print(%struct.addrinfo* nocapture %0) {
%2 = alloca [128 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%7 = load i32, i32* %6
switch i32 %7, label %33 [
i32 129, label %8
i32 128, label %17
]
8: ; preds = %1
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%10 = load i64, i64* %9
%11 = inttoptr i64 %10 to %struct.sockaddr_in*
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i64 0, i32 1
%13 = call i32* @inet_ntop(i32 129, i32* %12, i8* %4, i32 128)
%14 = icmp eq i32* %13, null
br i1 %14, label %33, label %15
15: ; preds = %8
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i64 0, i32 0
br label %26
17: ; preds = %1
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to %struct.sockaddr_in6*
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %20, i64 0, i32 1
%22 = call i32* @inet_ntop(i32 128, i32* %21, i8* %4, i32 128)
%23 = icmp eq i32* %22, null
br i1 %23, label %33, label %24
24: ; preds = %17
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %20, i64 0, i32 0
br label %26
26: ; preds = %24, %15
%27 = phi i32* [ %25, %24 ], [ %16, %15 ]
%28 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %24 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %15 ]
%29 = load i32, i32* %27
%30 = call i32 @ntohs(i32 %29)
%31 = call i32 @snprintf(i8* %5, i32 128, i8* %28, i8* %4, i32 %30)
%32 = call i8* @strdup(i8* %5)
br label %33
33: ; preds = %1, %17, %8, %26
%34 = phi i8* [ %32, %26 ], [ null, %8 ], [ null, %17 ], [ null, %1 ]
ret i8* %34
}