// File: /workspace/github_c_makefile_star2_O3/FH0[P]tun_direct/lwip-2.1.2/src/core/ipv4/ip4_frag.c
// Function: ip4_reass
struct pbuf *
ip4_reass(struct pbuf *p)
{
  struct pbuf *r;
  struct ip_hdr *fraghdr;
  struct ip_reassdata *ipr;
  struct ip_reass_helper *iprh;
  u16_t offset, len, clen;
  u8_t hlen;
  int valid;
  int is_last;

  IPFRAG_STATS_INC(ip_frag.recv);
  MIB2_STATS_INC(mib2.ipreasmreqds);

  fraghdr = (struct ip_hdr *)p->payload;

  if (IPH_HL_BYTES(fraghdr) != IP_HLEN) {
    LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: IP options currently not supported!\n"));
    IPFRAG_STATS_INC(ip_frag.err);
    goto nullreturn;
  }

  offset = IPH_OFFSET_BYTES(fraghdr);
  len = lwip_ntohs(IPH_LEN(fraghdr));
  hlen = IPH_HL_BYTES(fraghdr);
  if (hlen > len) {
    /* invalid datagram */
    goto nullreturn;
  }
  len = (u16_t)(len - hlen);

  /* Check if we are allowed to enqueue more datagrams. */
  clen = pbuf_clen(p);
  if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
#if IP_REASS_FREE_OLDEST
    if (!ip_reass_remove_oldest_datagram(fraghdr, clen) ||
        ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS))
#endif /* IP_REASS_FREE_OLDEST */
    {
      /* No datagram could be freed and still too many pbufs enqueued */
      LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
                                   ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS));
      IPFRAG_STATS_INC(ip_frag.memerr);
      /* @todo: send ICMP time exceeded here? */
      /* drop this pbuf */
      goto nullreturn;
    }
  }

  /* Look for the datagram the fragment belongs to in the current datagram queue,
   * remembering the previous in the queue for later dequeueing. */
  for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {
    /* Check if the incoming fragment matches the one currently present
       in the reassembly buffer. If so, we proceed with copying the
       fragment into the buffer. */
    if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
      LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: matching previous fragment ID=%"X16_F"\n",
                                   lwip_ntohs(IPH_ID(fraghdr))));
      IPFRAG_STATS_INC(ip_frag.cachehit);
      break;
    }
  }

  if (ipr == NULL) {
    /* Enqueue a new datagram into the datagram queue */
    ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);
    /* Bail if unable to enqueue */
    if (ipr == NULL) {
      goto nullreturn;
    }
  } else {
    if (((lwip_ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) &&
        ((lwip_ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
      /* ipr->iphdr is not the header from the first fragment, but fraghdr is
       * -> copy fraghdr into ipr->iphdr since we want to have the header
       * of the first fragment (for ICMP time exceeded and later, for copying
       * all options, if supported)*/
      SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);
    }
  }

  /* At this point, we have either created a new entry or pointing
   * to an existing one */

  /* check for 'no more fragments', and update queue entry*/
  is_last = (IPH_OFFSET(fraghdr) & PP_NTOHS(IP_MF)) == 0;
  if (is_last) {
    u16_t datagram_len = (u16_t)(offset + len);
    if ((datagram_len < offset) || (datagram_len > (0xFFFF - IP_HLEN))) {
      /* u16_t overflow, cannot handle this */
      goto nullreturn_ipr;
    }
  }
  /* find the right place to insert this pbuf */
  /* @todo: trim pbufs if fragments are overlapping */
  valid = ip_reass_chain_frag_into_datagram_and_validate(ipr, p, is_last);
  if (valid == IP_REASS_VALIDATE_PBUF_DROPPED) {
    goto nullreturn_ipr;
  }
  /* if we come here, the pbuf has been enqueued */

  /* Track the current number of pbufs current 'in-flight', in order to limit
     the number of fragments that may be enqueued at any one time
     (overflow checked by testing against IP_REASS_MAX_PBUFS) */
  ip_reass_pbufcount = (u16_t)(ip_reass_pbufcount + clen);
  if (is_last) {
    u16_t datagram_len = (u16_t)(offset + len);
    ipr->datagram_len = datagram_len;
    ipr->flags |= IP_REASS_FLAG_LASTFRAG;
    LWIP_DEBUGF(IP_REASS_DEBUG,
                ("ip4_reass: last fragment seen, total len %"S16_F"\n",
                 ipr->datagram_len));
  }

  if (valid == IP_REASS_VALIDATE_TELEGRAM_FINISHED) {
    struct ip_reassdata *ipr_prev;
    /* the totally last fragment (flag more fragments = 0) was received at least
     * once AND all fragments are received */
    u16_t datagram_len = (u16_t)(ipr->datagram_len + IP_HLEN);

    /* save the second pbuf before copying the header over the pointer */
    r = ((struct ip_reass_helper *)ipr->p->payload)->next_pbuf;

    /* copy the original ip header back to the first pbuf */
    fraghdr = (struct ip_hdr *)(ipr->p->payload);
    SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);
    IPH_LEN_SET(fraghdr, lwip_htons(datagram_len));
    IPH_OFFSET_SET(fraghdr, 0);
    IPH_CHKSUM_SET(fraghdr, 0);
    /* @todo: do we need to set/calculate the correct checksum? */
#if CHECKSUM_GEN_IP
    IF__NETIF_CHECKSUM_ENABLED(ip_current_input_netif(), NETIF_CHECKSUM_GEN_IP) {
      IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));
    }
#endif /* CHECKSUM_GEN_IP */

    p = ipr->p;

    /* chain together the pbufs contained within the reass_data list. */
    while (r != NULL) {
      iprh = (struct ip_reass_helper *)r->payload;

      /* hide the ip header for every succeeding fragment */
      pbuf_remove_header(r, IP_HLEN);
      pbuf_cat(p, r);
      r = iprh->next_pbuf;
    }

    /* find the previous entry in the linked list */
    if (ipr == reassdatagrams) {
      ipr_prev = NULL;
    } else {
      for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
        if (ipr_prev->next == ipr) {
          break;
        }
      }
    }

    /* release the sources allocate for the fragment queue entry */
    ip_reass_dequeue_datagram(ipr, ipr_prev);

    /* and adjust the number of pbufs currently queued for reassembly. */
    clen = pbuf_clen(p);
    LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount >= clen);
    ip_reass_pbufcount = (u16_t)(ip_reass_pbufcount - clen);

    MIB2_STATS_INC(mib2.ipreasmoks);

    /* Return the pbuf chain */
    return p;
  }
  /* the datagram is not (yet?) reassembled completely */
  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount));
  return NULL;

nullreturn_ipr:
  LWIP_ASSERT("ipr != NULL", ipr != NULL);
  if (ipr->p == NULL) {
    /* dropped pbuf after creating a new datagram entry: remove the entry, too */
    LWIP_ASSERT("not firstalthough just enqueued", ipr == reassdatagrams);
    ip_reass_dequeue_datagram(ipr, NULL);
  }

nullreturn:
  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: nullreturn\n"));
  IPFRAG_STATS_INC(ip_frag.drop);
  pbuf_free(p);
  return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
movq 0x8(%rdi), %r13
movb (%r13), %al
andb $0xf, %al
cmpb $0x5, %al
jne 0xec96
movzwl 0x6(%r13), %edi
callq 0x135b4
movl %eax, 0x4(%rsp)
movzwl 0x2(%r13), %edi
callq 0x135b4
movzbl (%r13), %ebp
shll $0x2, %ebp
andl $0x3c, %ebp
cmpw %bp, %ax
jb 0xec96
movl %eax, %r15d
movq %r14, %rdi
callq 0x539e
movzwl 0xde9f(%rip), %ecx      # 0x1c9c8
movw %ax, 0x2(%rsp)
movzwl %ax, %r12d
addl %r12d, %ecx
cmpl $0xb, %ecx
jb 0xeb60
movq %r13, %rdi
movl %r12d, %esi
callq 0xef5c
testl %eax, %eax
je 0xec96
movzwl 0xde74(%rip), %eax      # 0x1c9c8
addl %r12d, %eax
cmpl $0xa, %eax
ja 0xec96
movq 0xde59(%rip), %rbx      # 0x1c9c0
testq %rbx, %rbx
je 0xeb95
movl 0xc(%r13), %eax
cmpl %eax, 0x1c(%rbx)
jne 0xeb8d
movl 0x20(%rbx), %ecx
cmpl 0x10(%r13), %ecx
jne 0xeb8d
movzwl 0x14(%rbx), %ecx
cmpw 0x4(%r13), %cx
je 0xed13
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0xeb70
movl $0x5, %edi
callq 0x3c6e
testq %rax, %rax
je 0xec73
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x8(%rax)
movb $0xf, 0x27(%rax)
movq 0xddff(%rip), %rax      # 0x1c9c0
movq %rax, (%rbx)
movq %rbx, 0xddf5(%rip)      # 0x1c9c0
movl 0x10(%r13), %eax
movl %eax, 0x20(%rbx)
movups (%r13), %xmm0
movups %xmm0, 0x10(%rbx)
shll $0x3, 0x4(%rsp)
movzwl %bp, %eax
subl %eax, %r15d
movzwl 0x6(%r13), %r12d
testb $0x20, %r12b
jne 0xec07
movl %r15d, %eax
addw 0x4(%rsp), %ax
setb %cl
cmpw $-0x14, %ax
setae %al
orb %cl, %al
jne 0xec53
movq 0x8(%r14), %rbp
movzwl 0x2(%rbp), %edi
callq 0x135b4
movl %eax, %r13d
movzbl (%rbp), %eax
shll $0x2, %eax
andl $0x3c, %eax
cmpw %ax, %r13w
jb 0xec53
movzwl %ax, %eax
subl %eax, %r13d
movzwl 0x6(%rbp), %edi
callq 0x135b4
shll $0x3, %eax
movq 0x8(%r14), %rcx
movq $0x0, (%rcx)
movw %ax, 0x8(%rcx)
addw %ax, %r13w
movw %r13w, 0xa(%rcx)
jae 0xecb3
cmpq $0x0, 0x8(%rbx)
jne 0xec96
cmpq 0xdd5f(%rip), %rbx      # 0x1c9c0
jne 0xef13
movq %rbx, %rdi
xorl %esi, %esi
callq 0xeffa
jmp 0xec96
movq %r13, %rdi
movl %r12d, %esi
callq 0xef5c
cmpl %r12d, %eax
jl 0xec96
movl $0x5, %edi
callq 0x3c6e
testq %rax, %rax
jne 0xeba8
movq %r14, %rdi
callq 0x4fdf
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %r9
testq %r9, %r9
je 0xed41
movl $0x1, %edx
xorl %esi, %esi
xorl %edi, %edi
movq 0x8(%r9), %r8
movzwl 0x8(%r8), %r11d
cmpw %r11w, %ax
jb 0xed55
je 0xec53
movzwl 0xa(%r8), %r10d
cmpw %r10w, %ax
jb 0xec53
testq %rdi, %rdi
je 0xecfa
cmpw %r11w, 0xa(%rdi)
cmovnel %esi, %edx
movq (%r8), %r9
movq %r8, %rdi
testq %r9, %r9
jne 0xecc9
movq %r14, (%r8)
xorl %esi, %esi
cmpw %ax, %r10w
cmovnel %esi, %edx
jmp 0xed8f
movzwl 0x6(%r13), %edi
callq 0x135b4
testl $0x1fff, %eax           # imm = 0x1FFF
jne 0xebdb
movzwl 0x16(%rbx), %edi
callq 0x135b4
testl $0x1fff, %eax           # imm = 0x1FFF
jne 0xebcb
jmp 0xebdb
movq %r14, 0x8(%rbx)
movl $0x1, %edx
movq %rcx, %rsi
testb $0x20, %r12b
jne 0xedb8
jmp 0xedd3
movq %r9, (%rcx)
testq %rdi, %rdi
je 0xed81
cmpw %r11w, %r13w
ja 0xec53
movzwl 0xa(%rdi), %esi
cmpw %si, %ax
jb 0xec53
movq %r14, (%rdi)
xorl %edi, %edi
cmpw %ax, %si
cmovnel %edi, %edx
jmp 0xed8f
cmpw %r11w, %r13w
ja 0xec53
movq %r14, 0x8(%rbx)
testb $0x20, %r12b
jne 0xedb8
testl %edx, %edx
jne 0xedc2
movzwl 0x2(%rsp), %eax
addw %ax, 0xdc23(%rip)       # 0x1c9c8
addl 0x4(%rsp), %r15d
movw %r15w, 0x24(%rbx)
orb $0x1, 0x26(%rbx)
jmp 0xec9e
testb $0x1, 0x26(%rbx)
je 0xee19
testl %edx, %edx
je 0xee19
movq 0x8(%rbx), %r14
testq %r14, %r14
je 0xee2a
movq 0x8(%r14), %rsi
movzwl 0x8(%rsi), %eax
testw %ax, %ax
jne 0xee2d
jmp 0xedeb
movzwl 0xa(%rcx), %edx
movq 0x8(%rax), %rax
movq %rax, %rcx
cmpw 0x8(%rax), %dx
jne 0xee2d
movq (%rcx), %rax
testq %rax, %rax
jne 0xedda
movb $0x1, %al
cmpq %rcx, %rsi
jne 0xee2f
leaq 0x54cb(%rip), %rdi      # 0x142cc
leaq 0x76bc(%rip), %rsi      # 0x164c4
leaq 0x75ca(%rip), %rcx      # 0x163d9
movl $0x1e1, %edx            # imm = 0x1E1
jmp 0xef2d
movzwl 0x2(%rsp), %eax
addw %ax, 0xdba3(%rip)       # 0x1c9c8
jmp 0xec9e
xorl %r14d, %r14d
xorl %eax, %eax
movzwl 0x2(%rsp), %ecx
addw %cx, 0xdb8d(%rip)       # 0x1c9c8
testb $0x20, %r12b
jne 0xee4f
addl 0x4(%rsp), %r15d
movw %r15w, 0x24(%rbx)
orb $0x1, 0x26(%rbx)
testb %al, %al
je 0xec9e
movl 0x24(%rbx), %eax
addl $0x14, %eax
movq 0x8(%r14), %r14
movq (%r14), %r15
movl 0x20(%rbx), %ecx
movl %ecx, 0x10(%r14)
movups 0x10(%rbx), %xmm0
movups %xmm0, (%r14)
movzwl %ax, %edi
callq 0x135b4
movw %ax, 0x2(%r14)
xorl %eax, %eax
movw %ax, 0x6(%r14)
movw %ax, 0xa(%r14)
movq %r14, %rdi
movl $0x14, %esi
callq 0x13be5
movw %ax, 0xa(%r14)
movq 0x8(%rbx), %r14
testq %r15, %r15
je 0xeec9
movq 0x8(%r15), %r12
movl $0x14, %esi
movq %r15, %rdi
callq 0x529b
movq %r14, %rdi
movq %r15, %rsi
callq 0x53e6
movq (%r12), %r15
jmp 0xeea2
movq 0xdaf0(%rip), %rax      # 0x1c9c0
cmpq %rax, %rbx
je 0xeee7
testq %rax, %rax
je 0xeee7
movq %rax, %rsi
movq (%rax), %rax
cmpq %rbx, %rax
jne 0xeed5
jmp 0xeee9
xorl %esi, %esi
movq %rbx, %rdi
callq 0xeffa
movq %r14, %rdi
callq 0x539e
movzwl 0xdac8(%rip), %ecx      # 0x1c9c8
cmpw %ax, %cx
jb 0xef40
subl %eax, %ecx
movw %cx, 0xdaba(%rip)       # 0x1c9c8
jmp 0xeca1
leaq 0x53b2(%rip), %rdi      # 0x142cc
leaq 0x74dc(%rip), %rsi      # 0x163fd
leaq 0x74b1(%rip), %rcx      # 0x163d9
movl $0x2ab, %edx            # imm = 0x2AB
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050
leaq 0x5385(%rip), %rdi      # 0x142cc
leaq 0x7470(%rip), %rsi      # 0x163be
leaq 0x7484(%rip), %rcx      # 0x163d9
movl $0x29b, %edx            # imm = 0x29B
jmp 0xef2d