// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %rsi, %rbp
cmpl $0x0, (%rdi)
jne 0x10a3b
cmpl $0x0, 0x4(%rdi)
jne 0x10a3b
cmpl $0xffff0000, 0x8(%rdi)  # imm = 0xFFFF0000
jne 0x10a3b
cmpl $0x8, %edx
jl 0x10c1f
addl $-0x7, %edx
leaq 0x7(%rbp), %r15
movabsq $0x3a464646463a3a, %rax # imm = 0x3A464646463A3A
movq %rax, (%rbp)
movl 0xc(%rdi), %eax
leaq 0x14(%rsp), %rdi
movl %eax, (%rdi)
movq %r15, %rsi
callq 0xeb24
xorl %ecx, %ecx
cmpq %r15, %rax
cmovneq %rcx, %rbp
jmp 0x10c21
xorl %r14d, %r14d
xorl %r8d, %r8d
xorl %r13d, %r13d
movq %rdi, 0x8(%rsp)
movl %edx, 0x4(%rsp)
movl %r8d, %ebx
movl %r14d, %eax
shrl %eax
movl (%rdi,%rax,4), %edi
callq 0x13c57
movl %eax, %r15d
movl %r14d, %r12d
andl $0x1, %r12d
shll $0x4, %r12d
movl %r12d, %ecx
xorb $0x10, %cl
shrl %cl, %r15d
testw %r15w, %r15w
je 0x10aac
movl %ebx, %r8d
cmpb $0x1, %r8b
movzbl %r8b, %r8d
movl $0x2, %eax
cmovel %eax, %r8d
testl %r14d, %r14d
je 0x10ad3
movslq %r13d, %rax
incl %r13d
movb $0x3a, (%rbp,%rax)
movl 0x4(%rsp), %edx
cmpl %edx, %r13d
jl 0x10ad7
jmp 0x10c1f
cmpl $0x7, %r14d
movl %ebx, %r8d
jne 0x10abf
cmpb $0x1, %r8b
je 0x10c03
testb %r8b, %r8b
je 0x10bcb
cmpb $0x1, %r8b
jne 0x10a8e
jmp 0x10bb7
movl 0x4(%rsp), %edx
movl %r15d, %eax
andl $0xf000, %eax           # imm = 0xF000
je 0x10b11
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, (%rbp,%rax)
xorl %eax, %eax
cmpl %edx, %r13d
jl 0x10b16
jmp 0x10c1f
movl $0x1, %eax
testl $0xff00, %r15d          # imm = 0xFF00
je 0x10b50
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, (%rbp,%rax)
xorl %eax, %eax
cmpl %edx, %r13d
jge 0x10c1f
movl %r15d, %ecx
andl $0xf0, %ecx
sete %sil
testb %al, %sil
jne 0x10b8f
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, (%rbp,%rax)
cmpl %edx, %r13d
jge 0x10c1f
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, (%rbp,%rcx)
cmpl %edx, %r13d
jge 0x10c1f
incl %r14d
cmpl $0x8, %r14d
movq 0x8(%rsp), %rdi
jne 0x10a4d
jmp 0x10c15
leal 0x1(%r14), %eax
shrl %eax
movq 0x8(%rsp), %rcx
movl (%rcx,%rax,4), %edi
callq 0x13c57
movl %ebx, %r8d
movl %r12d, %ecx
shrl %cl, %eax
testw %ax, %ax
jne 0x10a8e
movslq %r13d, %rax
incl %r13d
movb $0x3a, (%rbp,%rax)
movb $0x1, %r8b
movl 0x4(%rsp), %edx
jmp 0x10bb2
movslq %r13d, %rax
incl %r13d
movb $0x3a, (%rbp,%rax)
cmpl 0x4(%rsp), %r13d
jge 0x10c1f
movslq %r13d, %rax
movb $0x0, (%rbp,%rax)
jmp 0x10c21
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop