// File: /workspace/github_c_makefile_star2_O2/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 0x12c89
incl 0xcfde(%rip)            # 0x1da14
movl %eax, %r12d
movzwl 0x10(%r15), %eax
cmpw $0x27, %ax
jbe 0x10c84
andl $-0x8, %r12d
addl $-0x30, %r12d
movq 0x8(%r15), %rcx
movq %rcx, 0x10(%rsp)
addl $-0x28, %eax
movw $0x28, %bp
xorl %r14d, %r14d
pushq $0xe
popq %rdi
pushq $0x30
popq %rsi
movl $0x0, 0x4(%rsp)
movl %r12d, 0xc(%rsp)
testw %ax, %ax
je 0x10c64
cmpw %r12w, %ax
movl %r12d, %ecx
movl %eax, 0x8(%rsp)
cmovbl %eax, %ecx
movq %rcx, 0x18(%rsp)
movl $0x280, %edx            # imm = 0x280
callq 0x4c47
movq %rax, %rbx
movb $-0x1, %al
testq %rbx, %rbx
je 0x10c55
cmpw $0x27, 0x12(%r15)
jbe 0x10c68
movq 0x8(%rbx), %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(%rbx), %rax
movq %rax, 0x30(%rsp)
movzwl %bp, %eax
addq %rax, 0x8(%r15)
movq 0x10(%r15), %xmm0
movd %eax, %xmm1
pshuflw $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0,4,5,6,7]
psubw %xmm1, %xmm0
movd %xmm0, 0x10(%r15)
movq 0x18(%rsp), %rax
movl %eax, %r12d
testw %r12w, %r12w
je 0x10ba2
movzwl 0x12(%r15), %eax
cmpw %ax, %r12w
movl %r12d, %r14d
cmovael %eax, %r14d
testw %ax, %ax
je 0x10b94
pushq $0x6
popq %rdi
callq 0x3b4a
testq %rax, %rax
je 0x10c4b
movq %rax, %rbp
movq 0x8(%r15), %r8
movzwl %r14w, %esi
xorl %edi, %edi
pushq $0x41
popq %rdx
movq %rax, %rcx
movl %esi, %r9d
callq 0x4f27
testq %rax, %rax
je 0x10c43
movq %rax, %r13
movq %r15, %rdi
callq 0x51d4
movq %r15, 0x20(%rbp)
leaq 0x14c(%rip), %rax       # 0x10cbc
movq %rax, 0x18(%rbp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x520d
movl %r12d, %eax
movl $0x0, %r12d
subw %r14w, %ax
je 0x10b06
jmp 0x10b97
movl %r12d, %eax
movq (%r15), %r15
movl %eax, %r12d
jmp 0x10b06
xorl %edi, %edi
movl 0xc(%rsp), %r12d
cmpw %r12w, 0x8(%rsp)
seta %dil
movq 0x10(%rsp), %rax
movb 0x6(%rax), %al
movq 0x30(%rsp), %r13
movb %al, 0x28(%r13)
movb $0x0, 0x29(%r13)
movl 0x4(%rsp), %eax
andl $-0x8, %eax
orl %eax, %edi
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %edi
callq 0x12ed0
movw %ax, 0x2a(%r13)
movl 0xce2b(%rip), %edi      # 0x1da14
callq 0x12ed7
movl %eax, 0x2c(%r13)
movb $0x2c, 0x6(%r13)
movq 0x18(%rsp), %rbp
leal 0x8(%rbp), %edi
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %edi
callq 0x12ed0
movw %ax, 0x4(%r13)
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq 0x28(%rsp), %rdx
callq *0xd0(%rdi)
movq %rbx, %rdi
callq 0x4e16
movl 0x8(%rsp), %eax
subl %ebp, %eax
addl %ebp, 0x4(%rsp)
movl %r14d, %ebp
pushq $0xe
popq %rdi
pushq $0x30
popq %rsi
jmp 0x10a76
movq %rbp, %rdi
callq 0x10cb1
movq %rbx, %rdi
callq 0x4e16
movb $-0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x10c55
leaq 0x365d(%rip), %rdi      # 0x142cc
leaq 0x57da(%rip), %rsi      # 0x16450
leaq 0x5a3b(%rip), %rcx      # 0x166b8
movl $0x310, %edx            # imm = 0x310
jmp 0x10c9e
leaq 0x3641(%rip), %rdi      # 0x142cc
leaq 0x5ae9(%rip), %rsi      # 0x1677b
leaq 0x5a1f(%rip), %rcx      # 0x166b8
movl $0x2e8, %edx            # imm = 0x2E8
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050