#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ assert(int);
char *inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, size_t);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, size_t, char *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
const char *fmt_sockaddr_port(const struct sockaddr *sa, char *buffer,
                              size_t buffer_size) {
  const char *retval = 0;
  size_t len = 0;
  uint16_t port;
  const char placeholder[] = "???:???";
  const struct sockaddr_in *sa4 = (const struct sockaddr_in *)sa;
  const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
  assert(buffer_size >= (1 + INET6_ADDRSTRLEN + 1 + 1 + 5 + 1));
  if (sa->sa_family == AF_INET) {
    retval = inet_ntop(AF_INET, &sa4->sin_addr, buffer, buffer_size);
    port = sa4->sin_port;
  } else if (sa->sa_family == AF_INET6) {
    buffer[0] = '[';
    retval = inet_ntop(AF_INET6, &sa6->sin6_addr, buffer + 1, buffer_size - 1);
    port = sa6->sin6_port;
  }
  if (retval) {
    len = strlen(retval);
    if (sa->sa_family == AF_INET6)
      snprintf(buffer + len + 1, buffer_size - len - 1, "]:%d", ntohs(port));
    else
      snprintf(buffer + len, buffer_size - len, ":%d", ntohs(port));
  } else {
    strcpy(buffer, placeholder);
  }
  buffer[buffer_size - 1] = '\x0';
  return buffer;
}


x86 assembly (clang, -O0):
<fmt_sockaddr_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0xe75(%rip),%rax
mov    %rax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
add    $0x1,%ecx
add    $0x1,%ecx
add    $0x5,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    1216 <fmt_sockaddr_port+0xb6>
mov    0x2df4(%rip),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    %rax,-0x20(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    126d <fmt_sockaddr_port+0x10d>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%rax
jne    1268 <fmt_sockaddr_port+0x108>
mov    -0x10(%rbp),%rax
movb   $0x5b,(%rax)
mov    0x2d9d(%rip),%rax
mov    (%rax),%rdi
mov    -0x48(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
add    $0x1,%rdx
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
call   1030 <inet_ntop@plt>
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    126d <fmt_sockaddr_port+0x10d>
cmpq   $0x0,-0x20(%rbp)
je     131a <fmt_sockaddr_port+0x1ba>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%rax
jne    12df <fmt_sockaddr_port+0x17f>
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <ntohs@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%ecx
lea    0xd33(%rip),%rdx
call   1060 <snprintf@plt>
jmp    1315 <fmt_sockaddr_port+0x1b5>
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <ntohs@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%ecx
lea    0xcf9(%rip),%rdx
call   1060 <snprintf@plt>
jmp    1327 <fmt_sockaddr_port+0x1c7>
mov    -0x10(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_sockaddr_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movabs $0x3f3f3f3a3f3f3f,%rax
mov    %rax,(%rsp)
mov    0x2e5f(%rip),%rax
movslq (%rax),%rax
add    $0x9,%rax
xor    %edi,%edi
cmp    %rdx,%rax
setbe  %dil
call   1070 <assert@plt>
mov    (%rbx),%rdi
mov    0x2e38(%rip),%rax
cmp    (%rax),%rdi
jne    11b9 <fmt_sockaddr_port+0x59>
lea    0x4(%rbx),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
jmp    11d8 <fmt_sockaddr_port+0x78>
mov    0x2e18(%rip),%rax
cmp    (%rax),%rdi
jne    1224 <fmt_sockaddr_port+0xc4>
movb   $0x5b,(%r15)
mov    (%rax),%rdi
lea    0x4(%rbx),%rsi
lea    0x1(%r15),%rdx
lea    -0x1(%r14),%rcx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     1224 <fmt_sockaddr_port+0xc4>
mov    (%rbx),%r12d
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    (%rbx),%rax
mov    0x2dde(%rip),%rcx
lea    (%r15,%rbp,1),%r13
cmp    (%rcx),%rax
jne    1231 <fmt_sockaddr_port+0xd1>
add    $0x1,%r13
not    %rbp
add    %r14,%rbp
mov    %r12d,%edi
call   1080 <ntohs@plt>
lea    0xde4(%rip),%rdx
mov    %r13,%rdi
mov    %rbp,%rsi
jmp    124c <fmt_sockaddr_port+0xec>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
jmp    1253 <fmt_sockaddr_port+0xf3>
mov    %r14,%rbx
sub    %rbp,%rbx
mov    %r12d,%edi
call   1080 <ntohs@plt>
lea    0xdbb(%rip),%rdx
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%ecx
call   1060 <snprintf@plt>
movb   $0x0,-0x1(%r14,%r15,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@__const.fmt_sockaddr_port.placeholder = [8 x i8] c"???:???\00"
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [5 x i8] c"]:%d\00"
@.str.1 = [4 x i8] c":%d\00"
define i8* @fmt_sockaddr_port(%struct.sockaddr* %0, i8* %1, i64 %2) {
%4 = alloca %struct.sockaddr*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca [8 x i8]
%11 = alloca %struct.sockaddr_in*
%12 = alloca %struct.sockaddr_in6*
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i8* null, i8** %7
store i64 0, i64* %8
%13 = bitcast [8 x i8]* %10 to i8*
%14 = load %struct.sockaddr*, %struct.sockaddr** %4
%15 = bitcast %struct.sockaddr* %14 to %struct.sockaddr_in*
store %struct.sockaddr_in* %15, %struct.sockaddr_in** %11
%16 = load %struct.sockaddr*, %struct.sockaddr** %4
%17 = bitcast %struct.sockaddr* %16 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %17, %struct.sockaddr_in6** %12
%18 = load i64, i64* %6
%19 = load i32, i32* @INET6_ADDRSTRLEN
%20 = add nsw i32 1, %19
%21 = add nsw i32 %20, 1
%22 = add nsw i32 %21, 1
%23 = add nsw i32 %22, 5
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = icmp uge i64 %18, %25
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load %struct.sockaddr*, %struct.sockaddr** %4
%30 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load i64, i64* @AF_INET
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %44
34: ; preds = %3
%35 = load i64, i64* @AF_INET
%36 = load %struct.sockaddr_in*, %struct.sockaddr_in** %11
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i32 0, i32 1
%38 = load i8*, i8** %5
%39 = load i64, i64* %6
%40 = call i8* @inet_ntop(i64 %35, i32* %37, i8* %38, i64 %39)
store i8* %40, i8** %7
%41 = load %struct.sockaddr_in*, %struct.sockaddr_in** %11
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %41, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %9
br label %65
44: ; preds = %3
%45 = load %struct.sockaddr*, %struct.sockaddr** %4
%46 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %45, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i64, i64* @AF_INET6
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %64
50: ; preds = %44
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i64 0
store i8 91, i8* %52
%53 = load i64, i64* @AF_INET6
%54 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %12
%55 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %54, i32 0, i32 1
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i64 1
%58 = load i64, i64* %6
%59 = sub i64 %58, 1
%60 = call i8* @inet_ntop(i64 %53, i32* %55, i8* %57, i64 %59)
store i8* %60, i8** %7
%61 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %12
%62 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %61, i32 0, i32 0
%63 = load i32, i32* %62
store i32 %63, i32* %9
br label %64
64: ; preds = %50, %44
br label %65
65: ; preds = %64, %34
%66 = load i8*, i8** %7
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %99
68: ; preds = %65
%69 = load i8*, i8** %7
%70 = call i64 @strlen(i8* %69)
store i64 %70, i64* %8
%71 = load %struct.sockaddr*, %struct.sockaddr** %4
%72 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %71, i32 0, i32 0
%73 = load i64, i64* %72
%74 = load i64, i64* @AF_INET6
%75 = icmp eq i64 %73, %74
br i1 %75, label %76, label %88
76: ; preds = %68
%77 = load i8*, i8** %5
%78 = load i64, i64* %8
%79 = getelementptr inbounds i8, i8* %77, i64 %78
%80 = getelementptr inbounds i8, i8* %79, i64 1
%81 = load i64, i64* %6
%82 = load i64, i64* %8
%83 = sub i64 %81, %82
%84 = sub i64 %83, 1
%85 = load i32, i32* %9
%86 = call i32 @ntohs(i32 %85)
%87 = call i32 @snprintf(i8* %80, i64 %84, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %86)
br label %98
88: ; preds = %68
%89 = load i8*, i8** %5
%90 = load i64, i64* %8
%91 = getelementptr inbounds i8, i8* %89, i64 %90
%92 = load i64, i64* %6
%93 = load i64, i64* %8
%94 = sub i64 %92, %93
%95 = load i32, i32* %9
%96 = call i32 @ntohs(i32 %95)
%97 = call i32 @snprintf(i8* %91, i64 %94, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %96)
br label %98
98: ; preds = %88, %76
br label %103
99: ; preds = %65
%100 = load i8*, i8** %5
%101 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%102 = call i32 @strcpy(i8* %100, i8* %101)
br label %103
103: ; preds = %99, %98
%104 = load i8*, i8** %5
%105 = load i64, i64* %6
%106 = sub i64 %105, 1
%107 = getelementptr inbounds i8, i8* %104, i64 %106
store i8 0, i8* %107
%108 = load i8*, i8** %5
ret i8* %108
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [5 x i8] c"]:%d\00"
@.str.1 = [4 x i8] c":%d\00"
define i8* @fmt_sockaddr_port(%struct.sockaddr* %0, i8* returned %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 17802464325484351, i64* %4
%6 = bitcast %struct.sockaddr* %0 to %struct.sockaddr_in6*
%7 = load i32, i32* @INET6_ADDRSTRLEN
%8 = add nsw i32 %7, 9
%9 = sext i32 %8 to i64
%10 = icmp ule i64 %9, %2
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @AF_INET
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = bitcast %struct.sockaddr* %0 to %struct.sockaddr_in*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i64 0, i32 1
%20 = tail call i8* @inet_ntop(i64 %14, i32* %19, i8* %1, i64 %2)
br label %30
21: ; preds = %3
%22 = load i64, i64* @AF_INET6
%23 = icmp eq i64 %14, %22
br i1 %23, label %24, label %51
24: ; preds = %21
store i8 91, i8* %1
%25 = load i64, i64* @AF_INET6
%26 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i64 0, i32 1
%27 = getelementptr inbounds i8, i8* %1, i64 1
%28 = add i64 %2, -1
%29 = tail call i8* @inet_ntop(i64 %25, i32* %26, i8* %27, i64 %28)
br label %30
30: ; preds = %24, %17
%31 = phi i8* [ %20, %17 ], [ %29, %24 ]
%32 = bitcast %struct.sockaddr* %0 to i32*
%33 = load i32, i32* %32
%34 = icmp eq i8* %31, null
br i1 %34, label %51, label %35
35: ; preds = %30
%36 = tail call i64 @strlen(i8* %31)
%37 = load i64, i64* %13
%38 = load i64, i64* @AF_INET6
%39 = icmp eq i64 %37, %38
%40 = getelementptr inbounds i8, i8* %1, i64 %36
br i1 %39, label %41, label %47
41: ; preds = %35
%42 = getelementptr inbounds i8, i8* %40, i64 1
%43 = xor i64 %36, -1
%44 = add i64 %43, %2
%45 = tail call i32 @ntohs(i32 %33)
%46 = tail call i32 @snprintf(i8* %42, i64 %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %45)
br label %53
47: ; preds = %35
%48 = sub i64 %2, %36
%49 = tail call i32 @ntohs(i32 %33)
%50 = tail call i32 @snprintf(i8* %40, i64 %48, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %49)
br label %53
51: ; preds = %21, %30
%52 = call i32 @strcpy(i8* %1, i8* %5)
br label %53
53: ; preds = %41, %47, %51
%54 = add i64 %2, -1
%55 = getelementptr inbounds i8, i8* %1, i64 %54
store i8 0, i8* %55
ret i8* %1
}