// File: /workspace/github_c_makefile_star2_O2/FH0[P]tun_direct/lwip-2.1.2/src/core/ipv6/ip6_addr.c
// Function: ip6addr_ntoa_r
char *
ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen)
{
  u32_t current_block_index, current_block_value, next_block_value;
  s32_t i;
  u8_t zero_flag, empty_block_flag;

#if LWIP_IPV4
  if (ip6_addr_isipv4mappedipv6(addr)) {
    /* This is an IPv4 mapped address */
    ip4_addr_t addr4;
    char *ret;
#define IP4MAPPED_HEADER "::FFFF:"
    char *buf_ip4 = buf + sizeof(IP4MAPPED_HEADER) - 1;
    int buflen_ip4 = buflen - sizeof(IP4MAPPED_HEADER) + 1;
    if (buflen < (int)sizeof(IP4MAPPED_HEADER)) {
      return NULL;
    }
    memcpy(buf, IP4MAPPED_HEADER, sizeof(IP4MAPPED_HEADER));
    addr4.addr = addr->addr[3];
    ret = ip4addr_ntoa_r(&addr4, buf_ip4, buflen_ip4);
    if (ret != buf_ip4) {
      return NULL;
    }
    return buf;
  }
#endif /* LWIP_IPV4 */
  i = 0;
  empty_block_flag = 0; /* used to indicate a zero chain for "::' */

  for (current_block_index = 0; current_block_index < 8; current_block_index++) {
    /* get the current 16-bit block */
    current_block_value = lwip_htonl(addr->addr[current_block_index >> 1]);
    if ((current_block_index & 0x1) == 0) {
      current_block_value = current_block_value >> 16;
    }
    current_block_value &= 0xffff;

    /* Check for empty block. */
    if (current_block_value == 0) {
      if (current_block_index == 7 && empty_block_flag == 1) {
        /* special case, we must render a ':' for the last block. */
        buf[i++] = ':';
        if (i >= buflen) {
          return NULL;
        }
        break;
      }
      if (empty_block_flag == 0) {
        /* generate empty block "::", but only if more than one contiguous zero block,
         * according to current formatting suggestions RFC 5952. */
        next_block_value = lwip_htonl(addr->addr[(current_block_index + 1) >> 1]);
        if ((current_block_index & 0x1) == 0x01) {
            next_block_value = next_block_value >> 16;
        }
        next_block_value &= 0xffff;
        if (next_block_value == 0) {
          empty_block_flag = 1;
          buf[i++] = ':';
          if (i >= buflen) {
            return NULL;
          }
          continue; /* move on to next block. */
        }
      } else if (empty_block_flag == 1) {
        /* move on to next block. */
        continue;
      }
    } else if (empty_block_flag == 1) {
      /* Set this flag value so we don't produce multiple empty blocks. */
      empty_block_flag = 2;
    }

    if (current_block_index > 0) {
      buf[i++] = ':';
      if (i >= buflen) {
        return NULL;
      }
    }

    if ((current_block_value & 0xf000) == 0) {
      zero_flag = 1;
    } else {
      buf[i++] = lwip_xchar(((current_block_value & 0xf000) >> 12));
      zero_flag = 0;
      if (i >= buflen) {
        return NULL;
      }
    }

    if (((current_block_value & 0xf00) == 0) && (zero_flag)) {
      /* do nothing */
    } else {
      buf[i++] = lwip_xchar(((current_block_value & 0xf00) >> 8));
      zero_flag = 0;
      if (i >= buflen) {
        return NULL;
      }
    }

    if (((current_block_value & 0xf0) == 0) && (zero_flag)) {
      /* do nothing */
    }
    else {
      buf[i++] = lwip_xchar(((current_block_value & 0xf0) >> 4));
      zero_flag = 0;
      if (i >= buflen) {
        return NULL;
      }
    }

    buf[i++] = lwip_xchar((current_block_value & 0xf));
    if (i >= buflen) {
      return NULL;
    }
  }

  buf[i] = 0;

  return buf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
cmpl $0x0, (%rdi)
jne 0xff55
cmpl $0x0, 0x4(%rdi)
jne 0xff55
cmpl $0xffff0000, 0x8(%rdi)  # imm = 0xFFFF0000
jne 0xff55
cmpl $0x8, %edx
jl 0x10181
addl $-0x7, %edx
leaq 0x7(%r12), %r15
movabsq $0x3a464646463a3a, %rax # imm = 0x3A464646463A3A
movq %rax, (%r12)
movl 0xc(%rdi), %eax
leaq 0x24(%rsp), %rdi
movl %eax, (%rdi)
movq %r15, %rsi
callq 0xe266
xorl %ecx, %ecx
cmpq %r15, %rax
cmovneq %rcx, %r12
jmp 0x10184
xorl %r8d, %r8d
xorl %r13d, %r13d
xorl %r10d, %r10d
xorl %r14d, %r14d
movq %rdi, 0x18(%rsp)
movl %edx, 0x4(%rsp)
movq %r12, 0x8(%rsp)
cmpl $0x80, %r8d
je 0x10177
movl %r10d, %ebx
movl %r14d, %eax
shrl %eax
movl (%rdi,%rax,4), %edi
movl %r8d, %ebp
callq 0x12ed7
movl %ebp, %r8d
movl %eax, %r15d
movl %ebp, %r12d
andl $0x10, %r12d
movl %r12d, %ecx
xorb $0x10, %cl
shrl %cl, %r15d
movslq %r13d, %rcx
testw %r15w, %r15w
je 0x1001a
movl %ebx, %r10d
cmpb $0x1, %r10b
movzbl %r10b, %r10d
pushq $0x2
popq %rax
cmovel %eax, %r10d
movq 0x8(%rsp), %r12
testl %r8d, %r8d
movl 0x4(%rsp), %edx
je 0xffe0
leal 0x1(%rcx), %r13d
movb $0x3a, (%r12,%rcx)
cmpl %edx, %r13d
jge 0x10181
movl %r15d, %eax
andl $0xf000, %eax           # imm = 0xF000
je 0x10049
movl %eax, %ecx
shrl $0xc, %ecx
leal 0x37(%rcx), %esi
orb $0x30, %cl
cmpl $0xa000, %eax           # imm = 0xA000
movzbl %cl, %eax
movzbl %sil, %ecx
cmovbl %eax, %ecx
movslq %r13d, %rax
incl %r13d
movb %cl, (%r12,%rax)
xorl %eax, %eax
cmpl %edx, %r13d
jl 0x1004c
jmp 0x10181
cmpl $0x7, %r14d
movl %ebx, %r10d
jne 0x1002d
cmpb $0x1, %r10b
je 0x10162
testb %r10b, %r10b
je 0x10101
cmpb $0x1, %r10b
movq 0x8(%rsp), %r12
jne 0xffc5
movb $0x1, %r10b
jmp 0x100f0
pushq $0x1
popq %rax
testw $0xff00, %r15w          # imm = 0xFF00
je 0x10085
movl %r15d, %eax
shrl $0x8, %eax
andl $0xf, %eax
leal 0x37(%rax), %ecx
leal 0x30(%rax), %esi
cmpl $0xa, %eax
movzbl %sil, %eax
movzbl %cl, %ecx
cmovbl %eax, %ecx
movslq %r13d, %rax
incl %r13d
movb %cl, (%r12,%rax)
xorl %eax, %eax
cmpl %edx, %r13d
jge 0x10181
movl %r15d, %ecx
andl $0xf0, %ecx
sete %sil
testb %al, %sil
jne 0x100c4
movl %ecx, %eax
shrl $0x4, %eax
leal 0x37(%rax), %esi
orb $0x30, %al
cmpl $0xa0, %ecx
movzbl %al, %eax
movzbl %sil, %ecx
cmovbl %eax, %ecx
movslq %r13d, %rax
incl %r13d
movb %cl, (%r12,%rax)
cmpl %edx, %r13d
jge 0x10181
andl $0xf, %r15d
leal 0x37(%r15), %eax
leal 0x30(%r15), %ecx
cmpl $0xa, %r15d
movzbl %cl, %ecx
movzbl %al, %eax
cmovbl %ecx, %eax
movslq %r13d, %rcx
incl %r13d
movb %al, (%r12,%rcx)
cmpl %edx, %r13d
jge 0x10181
incl %r14d
addl $0x10, %r8d
movq 0x18(%rsp), %rdi
jmp 0xff6f
movq %rcx, %rbx
movq %r14, 0x10(%rsp)
leal 0x1(%r14), %eax
shrl %eax
movq 0x18(%rsp), %rcx
movl (%rcx,%rax,4), %edi
callq 0x12ed7
movl %r12d, %ecx
shrl %cl, %eax
testw %ax, %ax
je 0x1013e
xorl %r10d, %r10d
movq 0x8(%rsp), %r12
movl %ebp, %r8d
movq 0x10(%rsp), %r14
movq %rbx, %rcx
jmp 0xffc5
leal 0x1(%rbx), %r13d
movq 0x8(%rsp), %r12
movb $0x3a, (%r12,%rbx)
movb $0x1, %r10b
movl 0x4(%rsp), %edx
cmpl %edx, %r13d
movl %ebp, %r8d
movq 0x10(%rsp), %r14
jl 0x100f0
jmp 0x10181
leal 0x1(%rcx), %r13d
movq 0x8(%rsp), %r12
movb $0x3a, (%r12,%rcx)
cmpl 0x4(%rsp), %r13d
jge 0x10181
movslq %r13d, %rax
movb $0x0, (%r12,%rax)
jmp 0x10184
xorl %r12d, %r12d
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop