#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 */

/* Variables and functions */
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ *inet_ntop(int, void const *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
size_t strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
void addr2str(int af, const void *addr, unsigned port, char *buf) {
  if (inet_ntop(af, addr, buf, INET6_ADDRSTRLEN + 1 + 5 + 1) == NULL) {
    *buf = '\0';
    return;
  }
  size_t len = strlen(buf);
  if (port) {
    snprintf(buf + len, INET6_ADDRSTRLEN + 1 + 5 + 1 - len, ":%d", port);
  } else {
    strncat(buf + len, ":*", INET6_ADDRSTRLEN + 1 + 5 + 1 - len - 1);
  }
}


x86 assembly (clang, -O0):
<addr2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
add    $0x5,%ecx
add    $0x1,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    118e <addr2str+0x4e>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    120d <addr2str+0xcd>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11db <addr2str+0x9b>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
add    $0x5,%eax
add    $0x1,%eax
cltq
sub    -0x28(%rbp),%rax
mov    %eax,%esi
mov    -0x14(%rbp),%ecx
lea    0xe2f(%rip),%rdx
call   1050 <snprintf@plt>
jmp    120d <addr2str+0xcd>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
add    $0x5,%eax
add    $0x1,%eax
cltq
sub    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%edx
lea    0xdfc(%rip),%rsi
call   1060 <strncat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr2str>:
push   %rbp
push   %r14
push   %rbx
mov    %rcx,%rbx
mov    %edx,%ebp
mov    0x2e98(%rip),%r14
mov    (%r14),%ecx
add    $0x7,%ecx
mov    %rbx,%rdx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     1191 <addr2str+0x51>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %rax,%rbx
mov    (%r14),%ecx
sub    %eax,%ecx
test   %ebp,%ebp
je     1199 <addr2str+0x59>
add    $0x7,%ecx
lea    0xe7f(%rip),%rdx
mov    %rbx,%rdi
mov    %ecx,%esi
mov    %ebp,%ecx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <snprintf@plt>
movb   $0x0,(%rbx)
pop    %rbx
pop    %r14
pop    %rbp
ret
add    $0x6,%ecx
lea    0xe61(%rip),%rsi
mov    %rbx,%rdi
mov    %ecx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <strncat@plt>

LLVM IR (-O0):
@INET6_ADDRSTRLEN = global i32 0
@.str = [4 x i8] c":%d\00"
@.str.1 = [3 x i8] c":*\00"
define void @addr2str(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = load i8*, i8** %8
%13 = load i32, i32* @INET6_ADDRSTRLEN
%14 = add nsw i32 %13, 1
%15 = add nsw i32 %14, 5
%16 = add nsw i32 %15, 1
%17 = call i32* @inet_ntop(i32 %10, i8* %11, i8* %12, i32 %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = load i8*, i8** %8
store i8 0, i8* %20
br label %54
21: ; preds = %4
%22 = load i8*, i8** %8
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %9
%24 = load i32, i32* %7
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %21
%27 = load i8*, i8** %8
%28 = load i64, i64* %9
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i32, i32* @INET6_ADDRSTRLEN
%31 = add nsw i32 %30, 1
%32 = add nsw i32 %31, 5
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = load i64, i64* %9
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
%38 = load i32, i32* %7
%39 = call i32 @snprintf(i8* %29, i32 %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %38)
br label %54
40: ; preds = %21
%41 = load i8*, i8** %8
%42 = load i64, i64* %9
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = load i32, i32* @INET6_ADDRSTRLEN
%45 = add nsw i32 %44, 1
%46 = add nsw i32 %45, 5
%47 = add nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = load i64, i64* %9
%50 = sub i64 %48, %49
%51 = sub i64 %50, 1
%52 = trunc i64 %51 to i32
%53 = call i32 @strncat(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %19, %40, %26
ret void
}

LLVM IR (-O2):
@INET6_ADDRSTRLEN = global i32 0
@.str = [4 x i8] c":%d\00"
@.str.1 = [3 x i8] c":*\00"
define void @addr2str(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = load i32, i32* @INET6_ADDRSTRLEN
%6 = add nsw i32 %5, 7
%7 = tail call i32* @inet_ntop(i32 %0, i8* %1, i8* %3, i32 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %4
store i8 0, i8* %3
br label %24
10: ; preds = %4
%11 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%12 = icmp eq i32 %2, 0
%13 = getelementptr inbounds i8, i8* %3, i64 %11
%14 = load i32, i32* @INET6_ADDRSTRLEN
%15 = trunc i64 %11 to i32
br i1 %12, label %20, label %16
16: ; preds = %10
%17 = sub i32 7, %15
%18 = add i32 %17, %14
%19 = tail call i32 @snprintf(i8* %13, i32 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %24
20: ; preds = %10
%21 = sub i32 6, %15
%22 = add i32 %21, %14
%23 = tail call i32 @strncat(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %16, %20, %9
ret void
}