// File: /workspace/github_c_makefile_star2_O2/FH0[P]tun_direct/lwip-2.1.2/src/core/ipv4/ip4_frag.c
// Function: ip4_frag
err_t
ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest)
{
  struct pbuf *rambuf;
#if !LWIP_NETIF_TX_SINGLE_PBUF
  struct pbuf *newpbuf;
  u16_t newpbuflen = 0;
  u16_t left_to_copy;
#endif
  struct ip_hdr *original_iphdr;
  struct ip_hdr *iphdr;
  const u16_t nfb = (u16_t)((netif->mtu - IP_HLEN) / 8);
  u16_t left, fragsize;
  u16_t ofo;
  int last;
  u16_t poff = IP_HLEN;
  u16_t tmp;
  int mf_set;

  original_iphdr = (struct ip_hdr *)p->payload;
  iphdr = original_iphdr;
  if (IPH_HL_BYTES(iphdr) != IP_HLEN) {
    /* ip4_frag() does not support IP options */
    return ERR_VAL;
  }
  LWIP_ERROR("ip4_frag(): pbuf too short", p->len >= IP_HLEN, return ERR_VAL);

  /* Save original offset */
  tmp = lwip_ntohs(IPH_OFFSET(iphdr));
  ofo = tmp & IP_OFFMASK;
  /* already fragmented? if so, the last fragment we create must have MF, too */
  mf_set = tmp & IP_MF;

  left = (u16_t)(p->tot_len - IP_HLEN);

  while (left) {
    /* Fill this fragment */
    fragsize = LWIP_MIN(left, (u16_t)(nfb * 8));

#if LWIP_NETIF_TX_SINGLE_PBUF
    rambuf = pbuf_alloc(PBUF_IP, fragsize, PBUF_RAM);
    if (rambuf == NULL) {
      goto memerr;
    }
    LWIP_ASSERT("this needs a pbuf in one piece!",
                (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
    poff += pbuf_copy_partial(p, rambuf->payload, fragsize, poff);
    /* make room for the IP header */
    if (pbuf_add_header(rambuf, IP_HLEN)) {
      pbuf_free(rambuf);
      goto memerr;
    }
    /* fill in the IP header */
    SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
    iphdr = (struct ip_hdr *)rambuf->payload;
#else /* LWIP_NETIF_TX_SINGLE_PBUF */
    /* When not using a static buffer, create a chain of pbufs.
     * The first will be a PBUF_RAM holding the link and IP 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, IP_HLEN, PBUF_RAM);
    if (rambuf == NULL) {
      goto memerr;
    }
    LWIP_ASSERT("this needs a pbuf in one piece!",
                (rambuf->len >= (IP_HLEN)));
    SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
    iphdr = (struct ip_hdr *)rambuf->payload;

    left_to_copy = fragsize;
    while (left_to_copy) {
      struct pbuf_custom_ref *pcr;
      u16_t plen = (u16_t)(p->len - poff);
      LWIP_ASSERT("p->len >= poff", p->len >= poff);
      newpbuflen = LWIP_MIN(left_to_copy, plen);
      /* Is this pbuf already empty? */
      if (!newpbuflen) {
        poff = 0;
        p = p->next;
        continue;
      }
      pcr = ip_frag_alloc_pbuf_custom_ref();
      if (pcr == NULL) {
        pbuf_free(rambuf);
        goto memerr;
      }
      /* Mirror this pbuf, although we might not need all of it. */
      newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc,
                                    (u8_t *)p->payload + poff, newpbuflen);
      if (newpbuf == NULL) {
        ip_frag_free_pbuf_custom_ref(pcr);
        pbuf_free(rambuf);
        goto memerr;
      }
      pbuf_ref(p);
      pcr->original = p;
      pcr->pc.custom_free_function = ipfrag_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) {
        poff = 0;
        p = p->next;
      }
    }
    poff = (u16_t)(poff + newpbuflen);
#endif /* LWIP_NETIF_TX_SINGLE_PBUF */

    /* Correct header */
    last = (left <= netif->mtu - IP_HLEN);

    /* Set new offset and MF flag */
    tmp = (IP_OFFMASK & (ofo));
    if (!last || mf_set) {
      /* the last fragment has MF set if the input frame had it */
      tmp = tmp | IP_MF;
    }
    IPH_OFFSET_SET(iphdr, lwip_htons(tmp));
    IPH_LEN_SET(iphdr, lwip_htons((u16_t)(fragsize + IP_HLEN)));
    IPH_CHKSUM_SET(iphdr, 0);
#if CHECKSUM_GEN_IP
    IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
      IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
    }
#endif /* CHECKSUM_GEN_IP */

    /* No need for separate header pbuf - we allowed room for it in rambuf
     * when allocated.
     */
    netif->output(netif, rambuf, dest);
    IPFRAG_STATS_INC(ip_frag.xmit);

    /* 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 - fragsize);
    ofo = (u16_t)(ofo + nfb);
  }
  MIB2_STATS_INC(mib2.ipfragoks);
  return ERR_OK;
memerr:
  MIB2_STATS_INC(mib2.ipfragfails);
  return ERR_MEM;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %rbx
movzwl 0xe0(%rsi), %eax
addl $-0x14, %eax
pushq $0x8
popq %rcx
cltd
idivl %ecx
movl %eax, %ebp
movq 0x8(%rdi), %rcx
movb (%rcx), %dl
andb $0xf, %dl
movb $-0x6, %al
cmpb $0x5, %dl
jne 0xecd0
movq %rdi, %r14
cmpw $0x13, 0x12(%rdi)
jbe 0xed2c
movq %rsi, %r12
movq %rcx, 0x60(%rsp)
movzwl 0x6(%rcx), %edi
callq 0x12ed0
movq %r14, 0x30(%rsp)
movl 0x10(%r14), %edx
addl $-0x14, %edx
movl %ebp, 0x24(%rsp)
shll $0x3, %ebp
movzwl %bp, %ecx
movw $0x14, %si
movq %rsi, 0x38(%rsp)
xorl %r14d, %r14d
pushq $0xe
popq %rdi
pushq $0x14
popq %rsi
movl %eax, 0x20(%rsp)
movl %eax, 0xc(%rsp)
movq %rbx, 0x50(%rsp)
movq %r12, 0x48(%rsp)
movl %ecx, 0x1c(%rsp)
testw %dx, %dx
je 0xecdf
movl %edx, 0x2c(%rsp)
movzwl %dx, %eax
cmpl %eax, %ecx
movl %eax, 0x28(%rsp)
cmovbl %ecx, %eax
movq %rax, 0x10(%rsp)
movl $0x280, %edx            # imm = 0x280
callq 0x4c47
movq %rax, %rbp
movb $-0x1, %al
testq %rbp, %rbp
je 0xecd0
cmpw $0x13, 0x12(%rbp)
jbe 0xed10
movq 0x8(%rbp), %rax
movq 0x60(%rsp), %rdx
movl 0x10(%rdx), %ecx
movl %ecx, 0x10(%rax)
movups (%rdx), %xmm0
movups %xmm0, (%rax)
movq 0x8(%rbp), %rax
movq %rax, 0x58(%rsp)
movq 0x10(%rsp), %rax
movl %eax, %r12d
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %rbx
movq %rbp, 0x40(%rsp)
movzwl %bx, %ebx
testw %r12w, %r12w
je 0xec00
movzwl 0x12(%r13), %eax
cmpw %bx, %ax
jb 0xece3
movl %eax, %r14d
subw %bx, %r14w
cmpw %r14w, %r12w
cmovbl %r12d, %r14d
cmpw %bx, %ax
je 0xebef
pushq $0x6
popq %rdi
callq 0x3b4a
testq %rax, %rax
je 0xecc4
movq %rax, %rbp
movq 0x8(%r13), %r8
movq %rbx, %r15
addq %rbx, %r8
movzwl %r14w, %esi
xorl %edi, %edi
pushq $0x41
popq %rdx
movq %rax, %rcx
movl %esi, %r9d
callq 0x4f27
testq %rax, %rax
je 0xecbc
movq %rax, %rbx
movq %r13, %rdi
callq 0x51d4
movq %r13, 0x20(%rbp)
leaq 0x190(%rip), %rax       # 0xed53
movq %rax, 0x18(%rbp)
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x520d
movl %r12d, %eax
movl $0x0, %r12d
subw %r14w, %ax
movq %r15, %rbx
je 0xeb46
jmp 0xebf2
movl %r12d, %eax
movq (%r13), %r13
xorl %ebx, %ebx
movl %eax, %r12d
jmp 0xeb43
movq %r13, 0x30(%rsp)
addl %r14d, %ebx
movq %rbx, 0x38(%rsp)
movq 0x48(%rsp), %r12
movzwl 0xe0(%r12), %eax
addl $-0x14, %eax
movl 0xc(%rsp), %r13d
movl %r13d, %edi
andl $0x1fff, %edi           # imm = 0x1FFF
movl %edi, %ecx
movl $0x2000, %edx           # imm = 0x2000
orl %edx, %ecx
testw $0x2000, 0x20(%rsp)     # imm = 0x2000
cmovnel %ecx, %edi
cmpl 0x28(%rsp), %eax
cmovll %ecx, %edi
callq 0x12ed0
movq 0x58(%rsp), %rbx
movw %ax, 0x6(%rbx)
movq 0x10(%rsp), %rax
addl $0x14, %eax
movzwl %ax, %edi
callq 0x12ed0
movw %ax, 0x2(%rbx)
andw $0x0, 0xa(%rbx)
movq %rbx, %rdi
movq %rbp, %r15
pushq $0x14
popq %rbp
movl %ebp, %esi
callq 0x133e3
movw %ax, 0xa(%rbx)
movq %r12, %rdi
movq %r15, %rsi
movq 0x50(%rsp), %rdx
callq *0xc0(%r12)
movq %r15, %rdi
callq 0x4e16
movl %ebp, %esi
movl 0x2c(%rsp), %edx
subl 0x10(%rsp), %edx
addl 0x24(%rsp), %r13d
movl %r13d, 0xc(%rsp)
movl 0x1c(%rsp), %ecx
pushq $0xe
popq %rdi
jmp 0xeacd
movq %rbp, %rdi
callq 0xed48
movq 0x40(%rsp), %rdi
callq 0x4e16
movb $-0x1, %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0xecd0
leaq 0x55e2(%rip), %rdi      # 0x142cc
leaq 0x777f(%rip), %rsi      # 0x16470
leaq 0x76f9(%rip), %rcx      # 0x163f1
movl $0x32d, %edx            # imm = 0x32D
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050
leaq 0x55b5(%rip), %rdi      # 0x142cc
leaq 0x7732(%rip), %rsi      # 0x16450
leaq 0x76cc(%rip), %rcx      # 0x163f1
movl $0x325, %edx            # imm = 0x325
jmp 0xecfd
leaq 0x5599(%rip), %rdi      # 0x142cc
leaq 0x76fb(%rip), %rsi      # 0x16435
leaq 0x76b0(%rip), %rcx      # 0x163f1
movl $0x2fc, %edx            # imm = 0x2FC
jmp 0xecfd