// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdx, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movzwl 0xe0(%rsi), %ebx
leal -0x14(%rbx), %eax
addl $-0xd, %ebx
testl %eax, %eax
cmovnsl %eax, %ebx
movq 0x8(%rdi), %r14
movb (%r14), %al
andb $0xf, %al
movb $-0x6, %cl
cmpb $0x5, %al
jne 0xf663
movq %rdi, %r15
cmpw $0x13, 0x12(%rdi)
jbe 0xf6bd
movzwl 0x6(%r14), %edi
callq 0x13c50
movzwl 0x10(%r15), %ecx
addw $-0x14, %cx
je 0xf65d
movl %eax, %edx
sarl $0x3, %ebx
leal (,%rbx,8), %eax
movzwl %ax, %eax
movl %eax, 0x8(%rsp)
movw $0x14, %bp
xorl %r13d, %r13d
movswl %bx, %eax
movl %eax, 0x18(%rsp)
movl %edx, 0x1c(%rsp)
movl %edx, %eax
movl %eax, 0x4(%rsp)
movq %r14, 0x28(%rsp)
movzwl %cx, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
movl %ecx, 0xc(%rsp)
movl %ecx, %ebx
cmovbl %eax, %ebx
movl $0xe, %edi
movl $0x14, %esi
movl $0x280, %edx            # imm = 0x280
callq 0x4e57
movb $-0x1, %cl
testq %rax, %rax
je 0xf663
cmpw $0x13, 0x12(%rax)
jbe 0xf6a1
movq 0x8(%rax), %rdx
movl 0x10(%r14), %ecx
movl %ecx, 0x10(%rdx)
movups (%r14), %xmm0
movups %xmm0, (%rdx)
movq %rax, 0x10(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x8(%rsp)
movq %rbx, 0x20(%rsp)
je 0xf5a9
movq %rbx, %rax
movl %eax, %r12d
movzwl 0x12(%r15), %eax
movl %eax, %r13d
subw %bp, %r13w
jb 0xf674
cmpw %r13w, %r12w
cmovbl %r12d, %r13d
cmpw %bp, %ax
jne 0xf50b
movq (%r15), %r15
movl $0xd, %eax
xorl %ebp, %ebp
jmp 0xf592
movl $0x6, %edi
callq 0x3c9e
testq %rax, %rax
je 0xf583
movq %rax, %rbx
movzwl %bp, %r8d
addq 0x8(%r15), %r8
movzwl %r13w, %esi
xorl %edi, %edi
movl $0x41, %edx
movq %rax, %rcx
movl %esi, %r9d
callq 0x516e
testq %rax, %rax
je 0xf576
movq %rax, %r14
movq %r15, %rdi
callq 0x5434
movq %r15, 0x20(%rbx)
leaq 0x183(%rip), %rax       # 0xf6d9
movq %rax, 0x18(%rbx)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x546d
subw %r13w, %r12w
je 0xf572
movq (%r15), %r15
xorl %ebp, %ebp
xorl %eax, %eax
jmp 0xf592
movl $0x6, %edi
movq %rbx, %rsi
callq 0x3d28
movq 0x10(%rsp), %rdi
callq 0x5069
movl $0x8, %eax
cmpl $0xd, %eax
je 0xf59f
testl %eax, %eax
jne 0xf661
testw %r12w, %r12w
jne 0xf4dd
addl %r13d, %ebp
movq 0x30(%rsp), %rbx
movzwl 0xe0(%rbx), %eax
addl $-0x14, %eax
movl 0x4(%rsp), %r12d
movl %r12d, %edi
andl $0x1fff, %edi           # imm = 0x1FFF
movl %edi, %ecx
movl $0x2000, %edx           # imm = 0x2000
orl %edx, %ecx
testl $0x2000, 0x1c(%rsp)     # imm = 0x2000
cmovnel %ecx, %edi
cmpl 0xc(%rsp), %eax
cmovll %ecx, %edi
callq 0x13c50
movq 0x40(%rsp), %r14
movw %ax, 0x6(%r14)
movq 0x20(%rsp), %rax
addl $0x14, %eax
movzwl %ax, %edi
callq 0x13c50
movw %ax, 0x2(%r14)
movw $0x0, 0xa(%r14)
movq %r14, %rdi
movl $0x14, %esi
callq 0x142a0
movw %ax, 0xa(%r14)
movq %rbx, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq 0x38(%rsp), %rdx
callq *0xc0(%rbx)
movq %r14, %rdi
callq 0x5069
movl 0xc(%rsp), %ecx
addl 0x18(%rsp), %r12d
movl %r12d, 0x4(%rsp)
subw 0x20(%rsp), %cx
movq 0x28(%rsp), %r14
jne 0xf46b
xorl %ecx, %ecx
jmp 0xf663
movb $-0x1, %cl
movl %ecx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5c51(%rip), %rdi      # 0x152cc
leaq 0x7e0e(%rip), %rsi      # 0x17490
leaq 0x7d88(%rip), %rcx      # 0x17411
movl $0x32d, %edx            # imm = 0x32D
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050
leaq 0x5c24(%rip), %rdi      # 0x152cc
leaq 0x7dc1(%rip), %rsi      # 0x17470
leaq 0x7d5b(%rip), %rcx      # 0x17411
movl $0x325, %edx            # imm = 0x325
jmp 0xf68e
leaq 0x5c08(%rip), %rdi      # 0x152cc
leaq 0x7d8a(%rip), %rsi      # 0x17455
leaq 0x7d3f(%rip), %rcx      # 0x17411
movl $0x2fc, %edx            # imm = 0x2FC
jmp 0xf68e