#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;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ port;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ ENOSPC;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                              size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int ntohs(int /*<<< orphan*/);
size_t snprintf(char *, int, char *, int);
size_t strlen(char *);
ssize_t sancus_socket_ntop_ipv6(char *str, size_t size,
                                const struct sockaddr_in6 *sin6) {
  if (inet_ntop(AF_INET6, &sin6->sin6_addr, str + 1, size - 1) != NULL) {
    char port[8];
    size_t l = strlen(str + 1);
    size_t lp = snprintf(port, sizeof(port), ":%u", ntohs(sin6->sin6_port));
    str[0] = '[';
    str[++l] = ']';
    l++;
    if (l + lp < size) {
      memcpy(str + l, port, lp + 1);
      return l + lp;
    }
    errno = ENOSPC;
  }
  return -1;
}


x86 assembly (clang, -O0):
<sancus_socket_ntop_ipv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
add    $0x1,%rdx
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
je     124c <sancus_socket_ntop_ipv6+0xfc>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%ecx
mov    $0x8,%esi
lea    0xe36(%rip),%rdx
call   1050 <snprintf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x5b,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5d,0x1(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    123a <sancus_socket_ntop_ipv6+0xea>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
add    $0x1,%rdx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1254 <sancus_socket_ntop_ipv6+0x104>
mov    0x2d8f(%rip),%rax
mov    (%rax),%ecx
mov    0x2d8e(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sancus_socket_ntop_ipv6>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rdx),%rsi
lea    0x1(%r12),%rbx
lea    -0x1(%r15),%rcx
mov    %rbx,%rdx
call   1030 <inet_ntop@plt>
mov    $0xffffffffffffffff,%r14
test   %rax,%rax
je     1206 <sancus_socket_ntop_ipv6+0xb6>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    0x0(%r13),%edi
call   1070 <ntohs@plt>
lea    0xe55(%rip),%rdx
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
movb   $0x5b,(%r12)
movb   $0x5d,0x1(%rbx,%r12,1)
lea    (%rax,%rbx,1),%r13
add    $0x2,%r13
cmp    %r15,%r13
jae    11f4 <sancus_socket_ntop_ipv6+0xa4>
add    $0x2,%rbx
add    %rbx,%r12
add    $0x1,%rax
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
mov    %rax,%rdx
call   1060 <memcpy@plt>
mov    %r13,%r14
jmp    1206 <sancus_socket_ntop_ipv6+0xb6>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    0x2dd4(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET6 = global i32 0
@.str = [4 x i8] c":%u\00"
@ENOSPC = global i32 0
@errno = global i32 0
define i64 @sancus_socket_ntop_ipv6(i8* %0, i64 %1, %struct.sockaddr_in6* %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.sockaddr_in6*
%8 = alloca [8 x i8]
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store %struct.sockaddr_in6* %2, %struct.sockaddr_in6** %7
%11 = load i32, i32* @AF_INET6
%12 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%13 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i32 0, i32 1
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i64, i64* %6
%17 = sub i64 %16, 1
%18 = call i32* @inet_ntop(i32 %11, i32* %13, i8* %15, i64 %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %56
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %9
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%25 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%26 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @ntohs(i32 %27)
%29 = call i64 @snprintf(i8* %24, i32 8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %28)
store i64 %29, i64* %10
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 0
store i8 91, i8* %31
%32 = load i8*, i8** %5
%33 = load i64, i64* %9
%34 = add i64 %33, 1
store i64 %34, i64* %9
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 93, i8* %35
%36 = load i64, i64* %9
%37 = add i64 %36, 1
store i64 %37, i64* %9
%38 = load i64, i64* %9
%39 = load i64, i64* %10
%40 = add i64 %38, %39
%41 = load i64, i64* %6
%42 = icmp ult i64 %40, %41
br i1 %42, label %43, label %54
43: ; preds = %20
%44 = load i8*, i8** %5
%45 = load i64, i64* %9
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%48 = load i64, i64* %10
%49 = add i64 %48, 1
%50 = call i32 @memcpy(i8* %46, i8* %47, i64 %49)
%51 = load i64, i64* %9
%52 = load i64, i64* %10
%53 = add i64 %51, %52
store i64 %53, i64* %4
br label %57
54: ; preds = %20
%55 = load i32, i32* @ENOSPC
store i32 %55, i32* @errno
br label %56
56: ; preds = %54, %3
store i64 -1, i64* %4
br label %57
57: ; preds = %56, %43
%58 = load i64, i64* %4
ret i64 %58
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET6 = global i32 0
@.str = [4 x i8] c":%u\00"
@ENOSPC = global i32 0
@errno = global i32 0
define i64 @sancus_socket_ntop_ipv6(i8* %0, i64 %1, %struct.sockaddr_in6* %2) {
%4 = alloca [8 x i8]
%5 = load i32, i32* @AF_INET6
%6 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = add i64 %1, -1
%9 = tail call i32* @inet_ntop(i32 %5, i32* %6, i8* %7, i64 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %29, label %11
11: ; preds = %3
%12 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%13 = tail call i64 @strlen(i8* %7)
%14 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @ntohs(i32 %15)
%17 = call i64 @snprintf(i8* %12, i32 8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
store i8 91, i8* %0
%18 = add i64 %13, 1
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 93, i8* %19
%20 = add i64 %13, 2
%21 = add i64 %17, %20
%22 = icmp ult i64 %21, %1
br i1 %22, label %25, label %23
23: ; preds = %11
%24 = load i32, i32* @ENOSPC
store i32 %24, i32* @errno
br label %29
25: ; preds = %11
%26 = getelementptr inbounds i8, i8* %0, i64 %20
%27 = add i64 %17, 1
%28 = call i32 @memcpy(i8* %26, i8* %12, i64 %27)
br label %29
29: ; preds = %3, %23, %25
%30 = phi i64 [ %21, %25 ], [ -1, %23 ], [ -1, %3 ]
ret i64 %30
}