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

/* Variables and functions */
int /*<<< orphan*/ append_hexdigit(char *, size_t, size_t *,
                                   unsigned int const);
void format_ipv6(char *valbuf, size_t sizeof_valbuf, size_t *r_vallen,
                 const unsigned char *value) {
  unsigned i;
  unsigned nulls = 0;
  size_t vallen = *r_vallen;
  valbuf[vallen++] = '[';
  for (i = 0; i < 8; i++) {
    const unsigned n = (*((unsigned char *)(value + i * 2) + 0) << 8 |
                        *((unsigned char *)(value + i * 2) + 1) << 0);
    if (n == 0) {
      if (nulls == 0) {
        if (vallen + 2 < sizeof(valbuf))
          valbuf[vallen++] = ':';
        nulls = 1;
      } else if (nulls == 1)
        ;
      else if (vallen + 7 < sizeof_valbuf) {
        valbuf[vallen++] = ':';
        append_hexdigit(valbuf, sizeof_valbuf, &vallen, (n >> 12) & 0xF);
        append_hexdigit(valbuf, sizeof_valbuf, &vallen, (n >> 8) & 0xF);
        append_hexdigit(valbuf, sizeof_valbuf, &vallen, (n >> 4) & 0xF);
        append_hexdigit(valbuf, sizeof_valbuf, &vallen, (n >> 0) & 0xF);
      }
    } else {
      if (nulls == 1)
        nulls = 2;
      if (i != 0)
        valbuf[vallen++] = ':';
      append_hexdigit(valbuf, sizeof_valbuf, &vallen, (n >> 12) & 0xF);
      append_hexdigit(valbuf, sizeof_valbuf, &vallen, (n >> 8) & 0xF);
      append_hexdigit(valbuf, sizeof_valbuf, &vallen, (n >> 4) & 0xF);
      append_hexdigit(valbuf, sizeof_valbuf, &vallen, (n >> 0) & 0xF);
    }
  }
  valbuf[vallen++] = ']';
  valbuf[vallen] = '\0';
  *r_vallen = vallen;
}


x86 assembly (clang, -O0):
<format_ipv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5b,(%rax,%rcx,1)
movl   $0x0,-0x24(%rbp)
cmpl   $0x8,-0x24(%rbp)
jae    1333 <format_ipv6+0x223>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
shl    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
shl    $0x8,%eax
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%edx
shl    $0x1,%edx
mov    %edx,%edx
movzbl 0x1(%rcx,%rdx,1),%ecx
shl    $0x0,%ecx
or     %ecx,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1286 <format_ipv6+0x176>
cmpl   $0x0,-0x28(%rbp)
jne    11d7 <format_ipv6+0xc7>
mov    -0x30(%rbp),%rax
add    $0x2,%rax
cmp    $0x8,%rax
jae    11cb <format_ipv6+0xbb>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x3a,(%rax,%rcx,1)
movl   $0x1,-0x28(%rbp)
jmp    1281 <format_ipv6+0x171>
cmpl   $0x1,-0x28(%rbp)
jne    11e6 <format_ipv6+0xd6>
jmp    127c <format_ipv6+0x16c>
mov    -0x30(%rbp),%rax
add    $0x7,%rax
cmp    -0x10(%rbp),%rax
jae    1277 <format_ipv6+0x167>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x3a,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%ecx
shr    $0xc,%ecx
and    $0xf,%ecx
lea    -0x30(%rbp),%rdx
call   1030 <append_hexdigit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%ecx
shr    $0x8,%ecx
and    $0xf,%ecx
lea    -0x30(%rbp),%rdx
call   1030 <append_hexdigit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%ecx
shr    $0x4,%ecx
and    $0xf,%ecx
lea    -0x30(%rbp),%rdx
call   1030 <append_hexdigit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%ecx
shr    $0x0,%ecx
and    $0xf,%ecx
lea    -0x30(%rbp),%rdx
call   1030 <append_hexdigit@plt>
jmp    127c <format_ipv6+0x16c>
jmp    1281 <format_ipv6+0x171>
jmp    1320 <format_ipv6+0x210>
cmpl   $0x1,-0x28(%rbp)
jne    1297 <format_ipv6+0x187>
movl   $0x2,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12b8 <format_ipv6+0x1a8>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x3a,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%ecx
shr    $0xc,%ecx
and    $0xf,%ecx
lea    -0x30(%rbp),%rdx
call   1030 <append_hexdigit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%ecx
shr    $0x8,%ecx
and    $0xf,%ecx
lea    -0x30(%rbp),%rdx
call   1030 <append_hexdigit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%ecx
shr    $0x4,%ecx
and    $0xf,%ecx
lea    -0x30(%rbp),%rdx
call   1030 <append_hexdigit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%ecx
shr    $0x0,%ecx
and    $0xf,%ecx
lea    -0x30(%rbp),%rdx
call   1030 <append_hexdigit@plt>
jmp    1325 <format_ipv6+0x215>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1158 <format_ipv6+0x48>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x5d,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_ipv6>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%r13
mov    %rdx,0x18(%rsp)
mov    (%rdx),%rax
lea    0x1(%rax),%rcx
mov    %rcx,(%rsp)
movb   $0x5b,(%rdi,%rax,1)
xor    %ebx,%ebx
xor    %r12d,%r12d
jmp    11ba <format_ipv6+0xaa>
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%ecx
shr    $0x4,%ecx
mov    %r13,%rdi
mov    0x10(%rsp),%r12
mov    %r12,%rsi
mov    %rsp,%r15
mov    %r15,%rdx
call   1030 <append_hexdigit@plt>
and    $0xf,%r14d
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <append_hexdigit@plt>
mov    %ebp,%ecx
shr    $0x4,%ecx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <append_hexdigit@plt>
and    $0xf,%ebp
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %ebp,%ecx
call   1030 <append_hexdigit@plt>
mov    0xc(%rsp),%r12d
add    $0x1,%rbx
cmp    $0x8,%rbx
je     12b2 <format_ipv6+0x1a2>
mov    0x20(%rsp),%rcx
movzbl (%rcx,%rbx,2),%r14d
mov    %r14d,%eax
shl    $0x8,%eax
movzbl 0x1(%rcx,%rbx,2),%ebp
or     %ebp,%eax
je     1210 <format_ipv6+0x100>
cmp    $0x1,%r12d
mov    $0x2,%eax
cmove  %eax,%r12d
mov    %r12d,0xc(%rsp)
test   %rbx,%rbx
je     1150 <format_ipv6+0x40>
mov    (%rsp),%rax
lea    0x1(%rax),%rcx
mov    %rcx,(%rsp)
movb   $0x3a,0x0(%r13,%rax,1)
jmp    1150 <format_ipv6+0x40>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x1,%r12d
je     11ac <format_ipv6+0x9c>
test   %r12d,%r12d
jne    1246 <format_ipv6+0x136>
mov    (%rsp),%rax
lea    0x2(%rax),%rcx
mov    $0x1,%r12d
cmp    $0x7,%rcx
ja     11ac <format_ipv6+0x9c>
lea    0x1(%rax),%rcx
mov    %rcx,(%rsp)
movb   $0x3a,0x0(%r13,%rax,1)
jmp    11ac <format_ipv6+0x9c>
mov    (%rsp),%rax
lea    0x7(%rax),%rcx
mov    0x10(%rsp),%r14
cmp    %r14,%rcx
jae    11ac <format_ipv6+0x9c>
lea    0x1(%rax),%rcx
mov    %rcx,(%rsp)
movb   $0x3a,0x0(%r13,%rax,1)
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rsp,%rbp
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1030 <append_hexdigit@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1030 <append_hexdigit@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1030 <append_hexdigit@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1030 <append_hexdigit@plt>
jmp    11ac <format_ipv6+0x9c>
mov    (%rsp),%rax
movw   $0x5d,0x0(%r13,%rax,1)
add    $0x1,%rax
mov    0x18(%rsp),%rcx
mov    %rax,(%rcx)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @format_ipv6(i8* %0, i64 %1, i64* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i8* %3, i8** %8
store i32 0, i32* %10
%13 = load i64*, i64** %7
%14 = load i64, i64* %13
store i64 %14, i64* %11
%15 = load i8*, i8** %5
%16 = load i64, i64* %11
%17 = add i64 %16, 1
store i64 %17, i64* %11
%18 = getelementptr inbounds i8, i8* %15, i64 %16
store i8 91, i8* %18
store i32 0, i32* %9
br label %19
19: ; preds = %136, %4
%20 = load i32, i32* %9
%21 = icmp ult i32 %20, 8
br i1 %21, label %22, label %139
22: ; preds = %19
%23 = load i8*, i8** %8
%24 = load i32, i32* %9
%25 = mul i32 %24, 2
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = shl i32 %30, 8
%32 = load i8*, i8** %8
%33 = load i32, i32* %9
%34 = mul i32 %33, 2
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = shl i32 %39, 0
%41 = or i32 %31, %40
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %98
44: ; preds = %22
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %57
47: ; preds = %44
%48 = load i64, i64* %11
%49 = add i64 %48, 2
%50 = icmp ult i64 %49, 8
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i8*, i8** %5
%53 = load i64, i64* %11
%54 = add i64 %53, 1
store i64 %54, i64* %11
%55 = getelementptr inbounds i8, i8* %52, i64 %53
store i8 58, i8* %55
br label %56
56: ; preds = %51, %47
store i32 1, i32* %10
br label %97
57: ; preds = %44
%58 = load i32, i32* %10
%59 = icmp eq i32 %58, 1
br i1 %59, label %60, label %61
60: ; preds = %57
br label %96
61: ; preds = %57
%62 = load i64, i64* %11
%63 = add i64 %62, 7
%64 = load i64, i64* %6
%65 = icmp ult i64 %63, %64
br i1 %65, label %66, label %95
66: ; preds = %61
%67 = load i8*, i8** %5
%68 = load i64, i64* %11
%69 = add i64 %68, 1
store i64 %69, i64* %11
%70 = getelementptr inbounds i8, i8* %67, i64 %68
store i8 58, i8* %70
%71 = load i8*, i8** %5
%72 = load i64, i64* %6
%73 = load i32, i32* %12
%74 = lshr i32 %73, 12
%75 = and i32 %74, 15
%76 = call i32 @append_hexdigit(i8* %71, i64 %72, i64* %11, i32 %75)
%77 = load i8*, i8** %5
%78 = load i64, i64* %6
%79 = load i32, i32* %12
%80 = lshr i32 %79, 8
%81 = and i32 %80, 15
%82 = call i32 @append_hexdigit(i8* %77, i64 %78, i64* %11, i32 %81)
%83 = load i8*, i8** %5
%84 = load i64, i64* %6
%85 = load i32, i32* %12
%86 = lshr i32 %85, 4
%87 = and i32 %86, 15
%88 = call i32 @append_hexdigit(i8* %83, i64 %84, i64* %11, i32 %87)
%89 = load i8*, i8** %5
%90 = load i64, i64* %6
%91 = load i32, i32* %12
%92 = lshr i32 %91, 0
%93 = and i32 %92, 15
%94 = call i32 @append_hexdigit(i8* %89, i64 %90, i64* %11, i32 %93)
br label %95
95: ; preds = %66, %61
br label %96
96: ; preds = %95, %60
br label %97
97: ; preds = %96, %56
br label %135
98: ; preds = %22
%99 = load i32, i32* %10
%100 = icmp eq i32 %99, 1
br i1 %100, label %101, label %102
101: ; preds = %98
store i32 2, i32* %10
br label %102
102: ; preds = %101, %98
%103 = load i32, i32* %9
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %110
105: ; preds = %102
%106 = load i8*, i8** %5
%107 = load i64, i64* %11
%108 = add i64 %107, 1
store i64 %108, i64* %11
%109 = getelementptr inbounds i8, i8* %106, i64 %107
store i8 58, i8* %109
br label %110
110: ; preds = %105, %102
%111 = load i8*, i8** %5
%112 = load i64, i64* %6
%113 = load i32, i32* %12
%114 = lshr i32 %113, 12
%115 = and i32 %114, 15
%116 = call i32 @append_hexdigit(i8* %111, i64 %112, i64* %11, i32 %115)
%117 = load i8*, i8** %5
%118 = load i64, i64* %6
%119 = load i32, i32* %12
%120 = lshr i32 %119, 8
%121 = and i32 %120, 15
%122 = call i32 @append_hexdigit(i8* %117, i64 %118, i64* %11, i32 %121)
%123 = load i8*, i8** %5
%124 = load i64, i64* %6
%125 = load i32, i32* %12
%126 = lshr i32 %125, 4
%127 = and i32 %126, 15
%128 = call i32 @append_hexdigit(i8* %123, i64 %124, i64* %11, i32 %127)
%129 = load i8*, i8** %5
%130 = load i64, i64* %6
%131 = load i32, i32* %12
%132 = lshr i32 %131, 0
%133 = and i32 %132, 15
%134 = call i32 @append_hexdigit(i8* %129, i64 %130, i64* %11, i32 %133)
br label %135
135: ; preds = %110, %97
br label %136
136: ; preds = %135
%137 = load i32, i32* %9
%138 = add i32 %137, 1
store i32 %138, i32* %9
br label %19
139: ; preds = %19
%140 = load i8*, i8** %5
%141 = load i64, i64* %11
%142 = add i64 %141, 1
store i64 %142, i64* %11
%143 = getelementptr inbounds i8, i8* %140, i64 %141
store i8 93, i8* %143
%144 = load i8*, i8** %5
%145 = load i64, i64* %11
%146 = getelementptr inbounds i8, i8* %144, i64 %145
store i8 0, i8* %146
%147 = load i64, i64* %11
%148 = load i64*, i64** %7
store i64 %147, i64* %148
ret void
}

LLVM IR (-O2):
define void @format_ipv6(i8* %0, i64 %1, i64* nocapture %2, i8* nocapture %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = load i64, i64* %2
%8 = add i64 %7, 1
store i64 %8, i64* %5
%9 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 91, i8* %9
br label %10
10: ; preds = %4, %59
%11 = phi i64 [ 0, %4 ], [ %61, %59 ]
%12 = phi i32 [ 0, %4 ], [ %60, %59 ]
%13 = shl nuw nsw i64 %11, 1
%14 = getelementptr inbounds i8, i8* %3, i64 %13
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = shl nuw nsw i32 %16, 8
%18 = getelementptr inbounds i8, i8* %14, i64 1
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = or i32 %17, %20
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %42
23: ; preds = %10
switch i32 %12, label %31 [
i32 0, label %24
i32 1, label %59
]
24: ; preds = %23
%25 = load i64, i64* %5
%26 = add i64 %25, 2
%27 = icmp ult i64 %26, 8
br i1 %27, label %28, label %59
28: ; preds = %24
%29 = add nsw i64 %25, 1
store i64 %29, i64* %5
%30 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 58, i8* %30
br label %59
31: ; preds = %23
%32 = load i64, i64* %5
%33 = add i64 %32, 7
%34 = icmp ult i64 %33, %1
br i1 %34, label %35, label %59
35: ; preds = %31
%36 = add i64 %32, 1
store i64 %36, i64* %5
%37 = getelementptr inbounds i8, i8* %0, i64 %32
store i8 58, i8* %37
%38 = call i32 @append_hexdigit(i8* %0, i64 %1, i64* %5, i32 0)
%39 = call i32 @append_hexdigit(i8* %0, i64 %1, i64* %5, i32 0)
%40 = call i32 @append_hexdigit(i8* %0, i64 %1, i64* %5, i32 0)
%41 = call i32 @append_hexdigit(i8* %0, i64 %1, i64* %5, i32 0)
br label %59
42: ; preds = %10
%43 = icmp eq i32 %12, 1
%44 = select i1 %43, i32 2, i32 %12
%45 = icmp eq i64 %11, 0
br i1 %45, label %50, label %46
46: ; preds = %42
%47 = load i64, i64* %5
%48 = add i64 %47, 1
store i64 %48, i64* %5
%49 = getelementptr inbounds i8, i8* %0, i64 %47
store i8 58, i8* %49
br label %50
50: ; preds = %46, %42
%51 = lshr i32 %16, 4
%52 = call i32 @append_hexdigit(i8* %0, i64 %1, i64* %5, i32 %51)
%53 = and i32 %16, 15
%54 = call i32 @append_hexdigit(i8* %0, i64 %1, i64* %5, i32 %53)
%55 = lshr i32 %20, 4
%56 = call i32 @append_hexdigit(i8* %0, i64 %1, i64* %5, i32 %55)
%57 = and i32 %20, 15
%58 = call i32 @append_hexdigit(i8* %0, i64 %1, i64* %5, i32 %57)
br label %59
59: ; preds = %23, %24, %28, %31, %35, %50
%60 = phi i32 [ %12, %35 ], [ %12, %31 ], [ %44, %50 ], [ 1, %28 ], [ 1, %24 ], [ %12, %23 ]
%61 = add nuw nsw i64 %11, 1
%62 = icmp eq i64 %61, 8
br i1 %62, label %63, label %10
63: ; preds = %59
%64 = load i64, i64* %5
%65 = add i64 %64, 1
%66 = getelementptr inbounds i8, i8* %0, i64 %64
store i8 93, i8* %66
%67 = getelementptr inbounds i8, i8* %0, i64 %65
store i8 0, i8* %67
store i64 %65, i64* %2
ret void
}