// File: /workspace/github_c_makefile_star2_O3/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 0x1057e
cmpl $0x0, 0x4(%rdi)
jne 0x1057e
cmpl $0xffff0000, 0x8(%rdi)  # imm = 0xFFFF0000
jne 0x1057e
cmpl $0x8, %edx
jl 0x1076c
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 0xe890
xorl %ecx, %ecx
cmpq %r15, %rax
cmovneq %rcx, %r12
jmp 0x1076f
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r13d, %r13d
movl %edx, 0xc(%rsp)
movq %rdi, 0x18(%rsp)
movq %r12, 0x10(%rsp)
movl %ebx, %eax
shrl %eax
movl (%rdi,%rax,4), %edi
callq 0x135bb
movl %eax, %r15d
movl %r14d, %r12d
andl $0x10, %r12d
movl %r12d, %ecx
xorb $0x10, %cl
shrl %cl, %r15d
testw %r15w, %r15w
je 0x1066a
cmpb $0x1, %bpl
movzbl %bpl, %ebp
movl $0x2, %eax
cmovel %eax, %ebp
movq 0x10(%rsp), %r12
testl %r14d, %r14d
movl 0xc(%rsp), %edx
je 0x105f1
movslq %r13d, %rax
incl %r13d
movb $0x3a, (%r12,%rax)
cmpl %edx, %r13d
jge 0x1076c
movl %r15d, %eax
andl $0xf000, %eax           # imm = 0xF000
je 0x10628
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)
cmpl %edx, %r13d
jge 0x1076c
testl $0xff00, %r15d          # imm = 0xFF00
je 0x10696
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)
cmpl %edx, %r13d
jge 0x1076c
movl %r15d, %eax
andl $0xf0, %eax
jmp 0x106a0
cmpl $0x7, %ebx
jne 0x10679
cmpb $0x1, %bpl
je 0x1074b
testb %bpl, %bpl
je 0x1070f
cmpb $0x1, %bpl
movq 0x10(%rsp), %r12
jne 0x105d4
movb $0x1, %bpl
jmp 0x106f5
movl %r15d, %eax
andl $0xf0, %eax
je 0x106cd
movl %eax, %ecx
shrl $0x4, %ecx
leal 0x37(%rcx), %esi
orb $0x30, %cl
cmpl $0xa0, %eax
movzbl %cl, %eax
movzbl %sil, %ecx
cmovbl %eax, %ecx
movslq %r13d, %rax
incl %r13d
movb %cl, (%r12,%rax)
cmpl %edx, %r13d
jge 0x1076c
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 0x1076c
incl %ebx
addl $0x10, %r14d
cmpl $0x80, %r14d
movq 0x18(%rsp), %rdi
jne 0x10596
jmp 0x10762
leal 0x1(%rbx), %eax
shrl %eax
movq 0x18(%rsp), %rcx
movl (%rcx,%rax,4), %edi
callq 0x135bb
movl %r12d, %ecx
shrl %cl, %eax
testw %ax, %ax
je 0x10732
xorl %ebp, %ebp
jmp 0x105cf
movslq %r13d, %rax
incl %r13d
movq 0x10(%rsp), %r12
movb $0x3a, (%r12,%rax)
movb $0x1, %bpl
movl 0xc(%rsp), %edx
jmp 0x106f0
movslq %r13d, %rax
incl %r13d
movq 0x10(%rsp), %r12
movb $0x3a, (%r12,%rax)
cmpl 0xc(%rsp), %r13d
jge 0x1076c
movslq %r13d, %rax
movb $0x0, (%r12,%rax)
jmp 0x1076f
xorl %r12d, %r12d
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)