// File: /workspace/github_c_makefile_star2_O3/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 0x13337
incl 0xb9f3(%rip)            # 0x1ca04
movzwl 0x10(%r15), %r13d
cmpw $0x27, %r13w
jbe 0x11252
addw $-0x28, %r13w
je 0x1120c
movl %eax, %ebx
andl $-0x8, %ebx
addl $-0x30, %ebx
movq 0x8(%r15), %rax
movq %rax, 0x10(%rsp)
movw $0x28, %r14w
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
movl %ebx, 0x8(%rsp)
cmpw %bx, %r13w
movl %ebx, %eax
cmovbl %r13d, %eax
movq %rax, 0x18(%rsp)
movl $0xe, %edi
movl $0x30, %esi
movl $0x280, %edx            # imm = 0x280
callq 0x4df7
movq %rax, %r12
movb $-0x1, %al
testq %r12, %r12
je 0x11227
cmpw $0x27, 0x12(%r15)
jbe 0x11236
movl %r13d, 0xc(%rsp)
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 %r14w, %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)
testw %bx, %bx
je 0x1116e
movq 0x18(%rsp), %rax
movl %eax, %r13d
movzwl 0x12(%r15), %eax
cmpw %ax, %r13w
movl %r13d, %ebp
cmovael %eax, %ebp
testw %ax, %ax
je 0x11165
movl $0x6, %edi
callq 0x3c6e
testq %rax, %rax
je 0x1121d
movq %rax, %rbx
movq 0x8(%r15), %r8
movzwl %bp, %esi
xorl %edi, %edi
movl $0x41, %edx
movq %rax, %rcx
movl %esi, %r9d
callq 0x50dd
testq %rax, %rax
je 0x11210
movq %rax, %r14
movq %r15, %rdi
callq 0x53ad
movq %r15, 0x20(%rbx)
leaq 0x12f(%rip), %rax       # 0x1127f
movq %rax, 0x18(%rbx)
movq %r12, %rdi
movq %r14, %rsi
callq 0x53e6
subw %bp, %r13w
je 0x1116e
movq (%r15), %r15
testw %r13w, %r13w
jne 0x110ee
xorl %edi, %edi
movl 0xc(%rsp), %r13d
cmpw 0x8(%rsp), %r13w
seta %dil
movq 0x10(%rsp), %rax
movb 0x6(%rax), %al
movq 0x30(%rsp), %r14
movb %al, 0x28(%r14)
movb $0x0, 0x29(%r14)
movl 0x4(%rsp), %eax
andl $-0x8, %eax
orl %eax, %edi
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %edi
callq 0x135b4
movw %ax, 0x2a(%r14)
movl 0xb84f(%rip), %edi      # 0x1ca04
callq 0x135bb
movl %eax, 0x2c(%r14)
movb $0x2c, 0x6(%r14)
movq 0x18(%rsp), %rbx
leal 0x8(%rbx), %edi
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %edi
callq 0x135b4
movw %ax, 0x4(%r14)
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq 0x28(%rsp), %rdx
callq *0xd0(%rdi)
movq %r12, %rdi
callq 0x4fdf
addl %ebx, 0x4(%rsp)
movl %ebp, %r14d
subw %bx, %r13w
movl 0x8(%rsp), %ebx
jne 0x11050
xorl %eax, %eax
jmp 0x11227
movl $0x6, %edi
movq %rbx, %rsi
callq 0x3cf8
movq %r12, %rdi
callq 0x4fdf
movb $-0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x308f(%rip), %rdi      # 0x142cc
leaq 0x51f4(%rip), %rsi      # 0x16438
leaq 0x53dd(%rip), %rcx      # 0x16628
movl $0x310, %edx            # imm = 0x310
jmp 0x1126c
leaq 0x3073(%rip), %rdi      # 0x142cc
leaq 0x54a9(%rip), %rsi      # 0x16709
leaq 0x53c1(%rip), %rcx      # 0x16628
movl $0x2e8, %edx            # imm = 0x2E8
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050