// File: /workspace/github_c_makefile_star2_O1/FH0[P]tun_direct/lwip-2.1.2/src/core/ipv6/ip6_frag.c
// Function: ip6_frag
err_t
ip6_frag(struct pbuf *p, struct netif *netif, const ip6_addr_t *dest)
{
  struct ip6_hdr *original_ip6hdr;
  struct ip6_hdr *ip6hdr;
  struct ip6_frag_hdr *frag_hdr;
  struct pbuf *rambuf;
#if !LWIP_NETIF_TX_SINGLE_PBUF
  struct pbuf *newpbuf;
  u16_t newpbuflen = 0;
  u16_t left_to_copy;
#endif
  static u32_t identification;
  u16_t left, cop;
  const u16_t mtu = nd6_get_destination_mtu(dest, netif);
  const u16_t nfb = (u16_t)((mtu - (IP6_HLEN + IP6_FRAG_HLEN)) & IP6_FRAG_OFFSET_MASK);
  u16_t fragment_offset = 0;
  u16_t last;
  u16_t poff = IP6_HLEN;

  identification++;

  original_ip6hdr = (struct ip6_hdr *)p->payload;

  /* @todo we assume there are no options in the unfragmentable part (IPv6 header). */
  LWIP_ASSERT("p->tot_len >= IP6_HLEN", p->tot_len >= IP6_HLEN);
  left = (u16_t)(p->tot_len - IP6_HLEN);

  while (left) {
    last = (left <= nfb);

    /* Fill this fragment */
    cop = last ? left : nfb;

#if LWIP_NETIF_TX_SINGLE_PBUF
    rambuf = pbuf_alloc(PBUF_IP, cop + IP6_FRAG_HLEN, PBUF_RAM);
    if (rambuf == NULL) {
      IP6_FRAG_STATS_INC(ip6_frag.memerr);
      return ERR_MEM;
    }
    LWIP_ASSERT("this needs a pbuf in one piece!",
      (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
    poff += pbuf_copy_partial(p, (u8_t*)rambuf->payload + IP6_FRAG_HLEN, cop, poff);
    /* make room for the IP header */
    if (pbuf_add_header(rambuf, IP6_HLEN)) {
      pbuf_free(rambuf);
      IP6_FRAG_STATS_INC(ip6_frag.memerr);
      return ERR_MEM;
    }
    /* fill in the IP header */
    SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
    ip6hdr = (struct ip6_hdr *)rambuf->payload;
    frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN);
#else
    /* When not using a static buffer, create a chain of pbufs.
     * The first will be a PBUF_RAM holding the link, IPv6, and Fragment header.
     * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
     * but limited to the size of an mtu.
     */
    rambuf = pbuf_alloc(PBUF_LINK, IP6_HLEN + IP6_FRAG_HLEN, PBUF_RAM);
    if (rambuf == NULL) {
      IP6_FRAG_STATS_INC(ip6_frag.memerr);
      return ERR_MEM;
    }
    LWIP_ASSERT("this needs a pbuf in one piece!",
                (p->len >= (IP6_HLEN)));
    SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
    ip6hdr = (struct ip6_hdr *)rambuf->payload;
    frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN);

    /* Can just adjust p directly for needed offset. */
    p->payload = (u8_t *)p->payload + poff;
    p->len = (u16_t)(p->len - poff);
    p->tot_len = (u16_t)(p->tot_len - poff);

    left_to_copy = cop;
    while (left_to_copy) {
      struct pbuf_custom_ref *pcr;
      newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
      /* Is this pbuf already empty? */
      if (!newpbuflen) {
        p = p->next;
        continue;
      }
      pcr = ip6_frag_alloc_pbuf_custom_ref();
      if (pcr == NULL) {
        pbuf_free(rambuf);
        IP6_FRAG_STATS_INC(ip6_frag.memerr);
        return ERR_MEM;
      }
      /* Mirror this pbuf, although we might not need all of it. */
      newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, p->payload, newpbuflen);
      if (newpbuf == NULL) {
        ip6_frag_free_pbuf_custom_ref(pcr);
        pbuf_free(rambuf);
        IP6_FRAG_STATS_INC(ip6_frag.memerr);
        return ERR_MEM;
      }
      pbuf_ref(p);
      pcr->original = p;
      pcr->pc.custom_free_function = ip6_frag_free_pbuf_custom;

      /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
       * so that it is removed when pbuf_dechain is later called on rambuf.
       */
      pbuf_cat(rambuf, newpbuf);
      left_to_copy = (u16_t)(left_to_copy - newpbuflen);
      if (left_to_copy) {
        p = p->next;
      }
    }
    poff = newpbuflen;
#endif /* LWIP_NETIF_TX_SINGLE_PBUF */

    /* Set headers */
    frag_hdr->_nexth = original_ip6hdr->_nexth;
    frag_hdr->reserved = 0;
    frag_hdr->_fragment_offset = lwip_htons((u16_t)((fragment_offset & IP6_FRAG_OFFSET_MASK) | (last ? 0 : IP6_FRAG_MORE_FLAG)));
    frag_hdr->_identification = lwip_htonl(identification);

    IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_FRAGMENT);
    IP6H_PLEN_SET(ip6hdr, (u16_t)(cop + IP6_FRAG_HLEN));

    /* No need for separate header pbuf - we allowed room for it in rambuf
     * when allocated.
     */
    IP6_FRAG_STATS_INC(ip6_frag.xmit);
    netif->output_ip6(netif, rambuf, dest);

    /* Unfortunately we can't reuse rambuf - the hardware may still be
     * using the buffer. Instead we free it (and the ensuing chain) and
     * recreate it next time round the loop. If we're lucky the hardware
     * will have already sent the packet, the free will really free, and
     * there will be zero memory penalty.
     */

    pbuf_free(rambuf);
    left = (u16_t)(left - cop);
    fragment_offset = (u16_t)(fragment_offset + cop);
  }
  return ERR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r15
movq %rdx, 0x28(%rsp)
movq %rdx, %rdi
movq %rsi, 0x20(%rsp)
callq 0x139d1
incl 0xc4bb(%rip)            # 0x1da04
movl %eax, %r13d
movzwl 0x10(%r15), %eax
cmpw $0x27, %ax
jbe 0x11798
addw $-0x28, %ax
je 0x11767
movl $0x0, (%rsp)
andl $-0x8, %r13d
addl $-0x30, %r13d
movq 0x8(%r15), %rcx
movq %rcx, 0x10(%rsp)
movw $0x28, %bx
xorl %ebp, %ebp
movl %r13d, 0x8(%rsp)
cmpw %r13w, %ax
movl %r13d, %ecx
movl %eax, 0x4(%rsp)
cmovbl %eax, %ecx
movq %rcx, 0x18(%rsp)
movl $0xe, %edi
movl $0x30, %esi
movl $0x280, %edx            # imm = 0x280
callq 0x4e57
movq %rax, %r12
movb $-0x1, %al
testq %r12, %r12
je 0x1176d
cmpw $0x27, 0x12(%r15)
jbe 0x1177c
movq 0x8(%r12), %rax
movq 0x10(%rsp), %rdx
movq 0x20(%rdx), %rcx
movq %rcx, 0x20(%rax)
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x8(%r12), %rax
movq %rax, 0x30(%rsp)
movzwl %bx, %eax
addq %rax, 0x8(%r15)
subw %ax, 0x12(%r15)
subw %ax, 0x10(%r15)
testw %r13w, %r13w
je 0x116c7
movq 0x18(%rsp), %rax
movl %eax, %r13d
movzwl 0x12(%r15), %eax
cmpw %ax, %r13w
movl %r13d, %ebp
cmovael %eax, %ebp
testw %ax, %ax
je 0x1168c
movl $0x6, %edi
callq 0x3c9e
testq %rax, %rax
je 0x116a3
movq %rax, %rbx
movq 0x8(%r15), %r8
movzwl %bp, %esi
xorl %edi, %edi
movl $0x41, %edx
movq %rax, %rcx
movl %esi, %r9d
callq 0x516e
testq %rax, %rax
je 0x11696
movq %rax, %r14
movq %r15, %rdi
callq 0x5434
movq %r15, 0x20(%rbx)
leaq 0x155(%rip), %rax       # 0x117c5
movq %rax, 0x18(%rbx)
movq %r12, %rdi
movq %r14, %rsi
callq 0x546d
xorl %eax, %eax
subw %bp, %r13w
je 0x116b0
movq (%r15), %r15
jmp 0x116b0
movq (%r15), %r15
movl $0xc, %eax
jmp 0x116b0
movl $0x6, %edi
movq %rbx, %rsi
callq 0x3d28
movq %r12, %rdi
callq 0x5069
movl $0x1, %eax
cmpl $0xc, %eax
je 0x116bd
testl %eax, %eax
jne 0x1176b
testw %r13w, %r13w
jne 0x11616
xorl %edi, %edi
movl 0x8(%rsp), %r13d
cmpw %r13w, 0x4(%rsp)
seta %dil
movq 0x10(%rsp), %rax
movb 0x6(%rax), %al
movq 0x30(%rsp), %r14
movb %al, 0x28(%r14)
movb $0x0, 0x29(%r14)
movl (%rsp), %ebx
movl %ebx, %eax
andl $0xfff8, %eax           # imm = 0xFFF8
orl %eax, %edi
callq 0x13c50
movw %ax, 0x2a(%r14)
movl 0xc2fa(%rip), %edi      # 0x1da04
callq 0x13c57
movl %eax, 0x2c(%r14)
movb $0x2c, 0x6(%r14)
movq 0x18(%rsp), %rax
movzwl %ax, %ecx
movl %ecx, 0xc(%rsp)
addl $0x8, %eax
movzwl %ax, %edi
callq 0x13c50
movw %ax, 0x4(%r14)
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq 0x28(%rsp), %rdx
callq *0xd0(%rdi)
movq %r12, %rdi
callq 0x5069
movl 0x4(%rsp), %eax
movl 0xc(%rsp), %ecx
addl %ecx, %ebx
movl %ebx, (%rsp)
movl %ebp, %ebx
subw %cx, %ax
jne 0x11588
xorl %eax, %eax
jmp 0x1176d
movb $-0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3b49(%rip), %rdi      # 0x152cc
leaq 0x5ce6(%rip), %rsi      # 0x17470
leaq 0x5f67(%rip), %rcx      # 0x176f8
movl $0x310, %edx            # imm = 0x310
jmp 0x117b2
leaq 0x3b2d(%rip), %rdi      # 0x152cc
leaq 0x6015(%rip), %rsi      # 0x177bb
leaq 0x5f4b(%rip), %rcx      # 0x176f8
movl $0x2e8, %edx            # imm = 0x2E8
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050