// File: /workspace/github_c_makefile_star2_O2/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, %r12
movq 0x8(%rdi), %r13
movb (%r13), %al
andb $0xf, %al
cmpb $0x5, %al
jne 0xe66a
movzwl 0x6(%r13), %edi
callq 0x12ed0
movl %eax, %ebp
movzwl 0x2(%r13), %edi
callq 0x12ed0
movzbl (%r13), %ebx
shll $0x2, %ebx
andl $0x3c, %ebx
cmpw %bx, %ax
jb 0xe66a
movl %eax, %r14d
movq %r12, %rdi
callq 0x51c5
movzwl 0xf4ee(%rip), %ecx      # 0x1d9d8
movw %ax, 0x2(%rsp)
movzwl %ax, %r15d
addl %r15d, %ecx
cmpl $0xb, %ecx
jb 0xe521
movq %r13, %rdi
movl %r15d, %esi
callq 0xe942
testl %eax, %eax
je 0xe66a
movzwl 0xf4c3(%rip), %eax      # 0x1d9d8
addl %r15d, %eax
cmpl $0xa, %eax
ja 0xe66a
shll $0x3, %ebp
movzwl %bx, %eax
subl %eax, %r14d
leaq 0xf49f(%rip), %rbx      # 0x1d9d0
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xe577
movl 0x1c(%rbx), %eax
cmpl 0xc(%r13), %eax
jne 0xe531
movl 0x20(%rbx), %eax
cmpl 0x10(%r13), %eax
jne 0xe531
movzwl 0x14(%rbx), %eax
cmpw 0x4(%r13), %ax
jne 0xe531
movzwl 0x6(%r13), %edi
callq 0x12ed0
testw $0x1fff, %ax            # imm = 0x1FFF
jne 0xe5bb
movzwl 0x16(%rbx), %edi
callq 0x12ed0
testw $0x1fff, %ax            # imm = 0x1FFF
jne 0xe5ab
jmp 0xe5bb
pushq $0x5
popq %rdi
callq 0x3b4a
testq %rax, %rax
je 0xe649
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x8(%rax)
movb $0xf, 0x27(%rax)
movq 0xf42f(%rip), %rax      # 0x1d9d0
movq %rax, (%rbx)
movq %rbx, 0xf425(%rip)      # 0x1d9d0
movl 0x10(%r13), %eax
movl %eax, 0x20(%rbx)
movups (%r13), %xmm0
movups %xmm0, 0x10(%rbx)
movzwl 0x6(%r13), %eax
movl %eax, 0x4(%rsp)
testb $0x20, %al
jne 0xe5dc
movl %r14d, %eax
addw %bp, %ax
setb %cl
cmpw $-0x14, %ax
setae %al
orb %cl, %al
jne 0xe629
movq 0x8(%r12), %r15
movzwl 0x2(%r15), %edi
callq 0x12ed0
movl %eax, %r13d
movzbl (%r15), %eax
shll $0x2, %eax
andl $0x3c, %eax
cmpw %ax, %r13w
jb 0xe629
movzwl %ax, %eax
subl %eax, %r13d
movzwl 0x6(%r15), %edi
callq 0x12ed0
movq 0x8(%r12), %rcx
andq $0x0, (%rcx)
shll $0x3, %eax
movw %ax, 0x8(%rcx)
addw %ax, %r13w
movw %r13w, 0xa(%rcx)
jae 0xe687
cmpq $0x0, 0x8(%rbx)
jne 0xe66a
cmpq 0xf399(%rip), %rbx      # 0x1d9d0
jne 0xe8f9
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe9e2
jmp 0xe66a
movq %r13, %rdi
movl %r15d, %esi
callq 0xe942
cmpl %r15d, %eax
jl 0xe66a
pushq $0x5
popq %rdi
callq 0x3b4a
testq %rax, %rax
jne 0xe588
movq %r12, %rdi
callq 0x4e16
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8(%rbx), %rdx
pushq $0x1
popq %rsi
xorl %r8d, %r8d
movq %rdx, %r9
xorl %r11d, %r11d
movq %r11, %rdi
movq (%r9), %r11
testq %r11, %r11
je 0xe6d8
movq 0x8(%r11), %r9
movzwl 0x8(%r9), %r10d
cmpw %r10w, %ax
jb 0xe707
je 0xe629
cmpw 0xa(%r9), %ax
jb 0xe629
movq %r9, %r11
testq %rdi, %rdi
je 0xe697
cmpw %r10w, 0xa(%rdi)
cmovnel %r8d, %esi
movq %r9, %r11
jmp 0xe697
testq %rdi, %rdi
je 0xe736
movzwl 0xa(%rdi), %r8d
cmpw %ax, %r8w
jbe 0xe728
leaq 0x5bdd(%rip), %rdi      # 0x142cc
leaq 0x7de6(%rip), %rsi      # 0x164dc
leaq 0x7cf4(%rip), %rcx      # 0x163f1
movl $0x1b6, %edx            # imm = 0x1B6
jmp 0xe913
movq %r11, (%rcx)
testq %rdi, %rdi
je 0xe75b
cmpw %r10w, %r13w
ja 0xe629
movzwl 0xa(%rdi), %r8d
cmpw %r8w, %ax
jb 0xe629
movq %r12, (%rdi)
xorl %edi, %edi
cmpw %ax, %r8w
cmovnel %edi, %esi
jmp 0xe768
cmpq $0x0, (%rdx)
je 0xe765
leaq 0x5b89(%rip), %rdi      # 0x142cc
leaq 0x7db0(%rip), %rsi      # 0x164fa
leaq 0x7ca0(%rip), %rcx      # 0x163f1
movl $0x1bf, %edx            # imm = 0x1BF
jmp 0xe913
cmpw %r10w, %r13w
ja 0xe629
movq %r12, (%rdx)
movl 0x4(%rsp), %edi
testb $0x20, %dil
jne 0xe793
testl %esi, %esi
jne 0xe7a5
movzwl 0x2(%rsp), %eax
addw %ax, 0xf256(%rip)       # 0x1d9d8
addl %ebp, %r14d
movw %r14w, 0x24(%rbx)
orb $0x1, 0x26(%rbx)
jmp 0xe672
testb $0x1, 0x26(%rbx)
je 0xe8bc
testl %esi, %esi
je 0xe8bc
movq (%rdx), %rax
testq %rax, %rax
je 0xe7bc
movq 0x8(%rax), %rdx
cmpw $0x0, 0x8(%rdx)
je 0xe86f
xorl %eax, %eax
movzwl 0x2(%rsp), %ecx
addw %cx, 0xf20e(%rip)       # 0x1d9d8
testb $0x20, %dil
jne 0xe7dc
addl %ebp, %r14d
movw %r14w, 0x24(%rbx)
orb $0x1, 0x26(%rbx)
testb %al, %al
je 0xe672
movl 0x24(%rbx), %eax
addl $0x14, %eax
movq 0x8(%rbx), %rcx
movq 0x8(%rcx), %r14
movq (%r14), %r15
movl 0x20(%rbx), %ecx
movl %ecx, 0x10(%r14)
movups 0x10(%rbx), %xmm0
movups %xmm0, (%r14)
movzwl %ax, %edi
callq 0x12ed0
movw %ax, 0x2(%r14)
andw $0x0, 0x6(%r14)
andw $0x0, 0xa(%r14)
pushq $0x14
popq %rsi
movq %r14, %rdi
callq 0x133e3
movw %ax, 0xa(%r14)
movq 0x8(%rbx), %r14
pushq $0x14
popq %r12
testq %r15, %r15
je 0xe89e
movq 0x8(%r15), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x50dd
movq %r14, %rdi
movq %r15, %rsi
callq 0x520d
movq (%r13), %r15
jmp 0xe835
movzwl 0xa(%rcx), %esi
movq 0x8(%rax), %rax
movq %rax, %rcx
cmpw 0x8(%rax), %si
jne 0xe7bc
movq (%rcx), %rax
testq %rax, %rax
jne 0xe85a
movb $0x1, %al
cmpq %rcx, %rdx
jne 0xe7be
leaq 0x5a43(%rip), %rdi      # 0x142cc
leaq 0x7ca1(%rip), %rsi      # 0x16531
leaq 0x7b5a(%rip), %rcx      # 0x163f1
movl $0x1e1, %edx            # imm = 0x1E1
jmp 0xe913
movq 0xf12b(%rip), %rax      # 0x1d9d0
cmpq %rax, %rbx
je 0xe8cd
testq %rax, %rax
je 0xe8cd
movq %rax, %rsi
movq (%rax), %rax
cmpq %rbx, %rax
jne 0xe8aa
jmp 0xe8cf
movzwl 0x2(%rsp), %eax
addw %ax, 0xf110(%rip)       # 0x1d9d8
jmp 0xe672
xorl %esi, %esi
movq %rbx, %rdi
callq 0xe9e2
movq %r14, %rdi
callq 0x51c5
movzwl 0xf0f2(%rip), %ecx      # 0x1d9d8
cmpw %ax, %cx
jb 0xe926
subl %eax, %ecx
movw %cx, 0xf0e4(%rip)       # 0x1d9d8
jmp 0xe675
leaq 0x59cc(%rip), %rdi      # 0x142cc
leaq 0x7b0e(%rip), %rsi      # 0x16415
leaq 0x7ae3(%rip), %rcx      # 0x163f1
movl $0x2ab, %edx            # imm = 0x2AB
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050
leaq 0x599f(%rip), %rdi      # 0x142cc
leaq 0x7aa2(%rip), %rsi      # 0x163d6
leaq 0x7ab6(%rip), %rcx      # 0x163f1
movl $0x29b, %edx            # imm = 0x29B
jmp 0xe913