#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 {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int INET6_ADDRSTRLEN;
int INET_ADDRSTRLEN;
char *inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, int);
int ntohs(int /*<<< orphan*/);
int snprintf(char *, int, char *, ...);
int straddr(const struct addrinfo *aip, char *buf, int buflen) {
  struct sockaddr_in *sinp;
  struct sockaddr_in6 *sin6p;
  const char *addr;
  char abuf[INET_ADDRSTRLEN];
  char a6buf[INET6_ADDRSTRLEN];
  int ret;
  if (aip->ai_family == AF_INET) {
    sinp = (struct sockaddr_in *)aip->ai_addr;
    addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf, INET_ADDRSTRLEN);
    ret = snprintf(buf, buflen, "inet address %s port %d",
                   addr ? addr : "unknown", ntohs(sinp->sin_port));
  } else if (aip->ai_family == AF_INET6) {
    sin6p = (struct sockaddr_in6 *)aip->ai_addr;
    addr = inet_ntop(AF_INET6, &sin6p->sin6_addr, a6buf, INET6_ADDRSTRLEN);
    ret = snprintf(buf, buflen, "inet6 address %s port %d",
                   addr ? addr : "unknown", ntohs(sin6p->sin6_port));
  } else
    ret = snprintf(buf, buflen, "unknown");
  return ret;
}


x86 assembly (clang, -O0):
<straddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    124f <straddr+0x11f>
mov    -0x60(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x64(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1208 <straddr+0xd8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x78(%rbp)
jmp    1218 <straddr+0xe8>
lea    0xe09(%rip),%rax
mov    %rax,-0x78(%rbp)
jmp    1218 <straddr+0xe8>
mov    -0x78(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    -0x70(%rbp),%rdi
mov    -0x64(%rbp),%esi
mov    -0x80(%rbp),%rcx
mov    %eax,%r8d
lea    0xdc0(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x4c(%rbp)
jmp    133f <straddr+0x20f>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2d7b(%rip),%rcx
cmp    (%rcx),%rax
jne    1322 <straddr+0x1f2>
mov    -0x58(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    0x2d59(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x84(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12c9 <straddr+0x199>
mov    -0x30(%rbp),%rax
mov    %rax,-0x98(%rbp)
jmp    12dc <straddr+0x1ac>
lea    0xd48(%rip),%rax
mov    %rax,-0x98(%rbp)
jmp    12dc <straddr+0x1ac>
mov    -0x98(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    -0x90(%rbp),%rdi
mov    -0x84(%rbp),%esi
mov    -0xa0(%rbp),%rcx
mov    %eax,%r8d
lea    0xd0d(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x4c(%rbp)
jmp    133a <straddr+0x20a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    0xce8(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x4c(%rbp)
jmp    133f <straddr+0x20f>
mov    -0x4c(%rbp),%eax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<straddr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdx
mov    %rdx,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%r8d
mov    %rsp,%rbx
lea    0xf(%r8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%rdi),%rax
mov    0x2e60(%rip),%rsi
cmp    (%rsi),%rax
jne    11b6 <straddr+0x86>
mov    0x8(%rdi),%r12
lea    0x4(%r12),%rsi
mov    %rax,%rdi
call   1030 <inet_ntop@plt>
test   %rax,%rax
lea    0xe78(%rip),%rbx
cmovne %rax,%rbx
mov    (%r12),%edi
call   1050 <ntohs@plt>
lea    0xe4c(%rip),%rdx
jmp    11f7 <straddr+0xc7>
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <straddr+0xe7>
mov    0x8(%rdi),%r12
lea    0x4(%r12),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r8d,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
lea    0xe35(%rip),%rbx
cmovne %rax,%rbx
mov    (%r12),%edi
call   1050 <ntohs@plt>
lea    0xe29(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <snprintf@plt>

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@.str = [24 x i8] c"inet address %s port %d\00"
@.str.1 = [8 x i8] c"unknown\00"
@AF_INET6 = global i64 0
@.str.2 = [25 x i8] c"inet6 address %s port %d\00"
define i32 @straddr(%struct.addrinfo* nocapture %0, i8* %1, i32 %2) {
%4 = load i32, i32* @INET_ADDRSTRLEN
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = load i32, i32* @INET6_ADDRSTRLEN
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* @AF_INET
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %3
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to %struct.sockaddr_in*
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i64 0, i32 1
%19 = call i8* @inet_ntop(i64 %11, i32* %18, i8* %6, i32 %4)
%20 = icmp eq i8* %19, null
%21 = select i1 %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %19
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
%25 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %24)
br label %43
26: ; preds = %3
%27 = load i64, i64* @AF_INET6
%28 = icmp eq i64 %11, %27
br i1 %28, label %29, label %41
29: ; preds = %26
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.sockaddr_in6*
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %32, i64 0, i32 1
%34 = call i8* @inet_ntop(i64 %11, i32* %33, i8* %9, i32 %7)
%35 = icmp eq i8* %34, null
%36 = select i1 %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %34
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %32, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @ntohs(i32 %38)
%40 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %36, i32 %39)
br label %43
41: ; preds = %26
%42 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %29, %41, %14
%44 = phi i32 [ %25, %14 ], [ %40, %29 ], [ %42, %41 ]
ret i32 %44
}