// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %rdi, %r14
movq 0x8(%rdi), %r13
movb (%r13), %al
andb $0xf, %al
cmpb $0x5, %al
jne 0xedad
movzwl 0x6(%r13), %edi
callq 0x13c50
movl %eax, %r15d
movzwl 0x2(%r13), %edi
callq 0x13c50
movzbl (%r13), %ebp
shll $0x2, %ebp
andl $0x3c, %ebp
movl %eax, 0xc(%rsp)
cmpw %bp, %ax
jae 0xedca
movq %r14, %rdi
callq 0x5069
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x5425
movzwl 0xebef(%rip), %ecx      # 0x1d9c8
movw %ax, 0x12(%rsp)
movzwl %ax, %r12d
addl %r12d, %ecx
cmpl $0xb, %ecx
jb 0xee08
movq %r13, %rdi
movl %r12d, %esi
callq 0xf2d8
testl %eax, %eax
je 0xedad
movzwl 0xebc8(%rip), %eax      # 0x1d9c8
addl %r12d, %eax
cmpl $0xa, %eax
ja 0xedad
movq 0xebb1(%rip), %rbx      # 0x1d9c0
testq %rbx, %rbx
je 0xee3d
movl 0xc(%r13), %eax
cmpl %eax, 0x1c(%rbx)
jne 0xee35
movl 0x20(%rbx), %ecx
cmpl 0x10(%r13), %ecx
jne 0xee35
movzwl 0x14(%rbx), %ecx
cmpw 0x4(%r13), %cx
je 0xefac
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0xee18
movl $0x5, %edi
callq 0x3c9e
movq %rax, %rbx
testq %rax, %rax
jne 0xee79
movq %r13, %rdi
movl %r12d, %esi
callq 0xf2d8
cmpl %r12d, %eax
jge 0xee63
xorl %ebx, %ebx
jmp 0xee70
movl $0x5, %edi
callq 0x3c9e
movq %rax, %rbx
testq %rbx, %rbx
je 0xefea
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
movq $0x0, 0x20(%rbx)
movb $0xf, 0x27(%rbx)
movq 0xeb2a(%rip), %rax      # 0x1d9c0
movq %rax, (%rbx)
movq %rbx, 0xeb20(%rip)      # 0x1d9c0
movl 0x10(%r13), %eax
movl %eax, 0x20(%rbx)
movups (%r13), %xmm0
movups %xmm0, 0x10(%rbx)
testq %rbx, %rbx
je 0xedad
shll $0x3, %r15d
movzwl %bp, %eax
movl 0xc(%rsp), %edx
subl %eax, %edx
movzwl 0x6(%r13), %eax
movl %eax, 0x14(%rsp)
testb $0x20, %al
jne 0xeeeb
movl %edx, %eax
addw %r15w, %ax
setb %cl
cmpw $-0x14, %ax
setae %al
orb %cl, %al
jne 0xf0f0
movl %edx, 0xc(%rsp)
movq 0x8(%r14), %rbp
movzwl 0x2(%rbp), %edi
callq 0x13c50
movl %eax, %r13d
movzbl (%rbp), %eax
shll $0x2, %eax
andl $0x3c, %eax
movb $0x1, %r12b
cmpw %ax, %r13w
jb 0xf0e4
movzwl %ax, %eax
subl %eax, %r13d
movzwl 0x6(%rbp), %edi
callq 0x13c50
shll $0x3, %eax
movq 0x8(%r14), %rcx
movq $0x0, (%rcx)
movw %ax, 0x8(%rcx)
addw %ax, %r13w
movw %r13w, 0xa(%rcx)
jb 0xf0e4
movq 0x8(%rbx), %r10
testq %r10, %r10
sete %dil
je 0xeff1
movq 0x8(%r10), %r11
movzwl 0x8(%r11), %r9d
xorl %r8d, %r8d
movl $0x1, %edx
cmpw %r9w, %ax
jae 0xf020
xorl %esi, %esi
movq %r10, (%rcx)
testq %rsi, %rsi
je 0xf06b
xorl %r8d, %r8d
cmpw %r9w, %r13w
ja 0xf0e7
movzwl 0xa(%rsi), %r9d
cmpw %r9w, %ax
jb 0xf0e7
movq %r14, (%rsi)
xorl %r8d, %r8d
cmpw %ax, %r9w
cmovnel %r8d, %edx
jmp 0xeff8
movzwl 0x6(%r13), %edi
callq 0x13c50
testl $0x1fff, %eax           # imm = 0x1FFF
jne 0xeeb9
movzwl 0x16(%rbx), %edi
callq 0x13c50
testl $0x1fff, %eax           # imm = 0x1FFF
je 0xeeb9
movl 0x10(%r13), %eax
movl %eax, 0x20(%rbx)
movups (%r13), %xmm0
movups %xmm0, 0x10(%rbx)
jmp 0xeeb9
xorl %ebx, %ebx
jmp 0xeeb0
xorl %esi, %esi
movl $0x1, %edx
testb %dil, %dil
je 0xf086
testq %rsi, %rsi
je 0xf077
movzwl 0xa(%rsi), %edi
cmpw %ax, %di
ja 0xf265
movq %r14, (%rsi)
xorl %esi, %esi
cmpw %ax, %di
cmovnel %esi, %edx
jmp 0xf086
xorl %ebp, %ebp
cmpw %r9w, %ax
je 0xf0e4
movq %r11, %rsi
cmpw 0xa(%r11), %ax
jb 0xf0e4
testq %rbp, %rbp
je 0xf048
cmpw %r9w, 0xa(%rbp)
cmovnel %r8d, %edx
movq (%rsi), %r10
testq %r10, %r10
sete %dil
je 0xeff8
movq 0x8(%r10), %r11
movzwl 0x8(%r11), %r9d
movq %rsi, %rbp
cmpw %r9w, %ax
jae 0xf022
jmp 0xef74
cmpw %r9w, %r13w
ja 0xf0e4
movq %r14, 0x8(%rbx)
jmp 0xeff8
cmpq $0x0, 0x8(%rbx)
jne 0xf281
movq %r14, 0x8(%rbx)
testb $0x20, 0x14(%rsp)
jne 0xf093
testl %edx, %edx
jne 0xf0a6
jmp 0xf0e1
xorl %r12d, %r12d
testb $0x1, 0x26(%rbx)
je 0xf0e4
movl $0x0, %r8d
testl %edx, %edx
je 0xf0e7
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0xf0e1
movq 0x8(%rax), %rax
cmpw $0x0, 0x8(%rax)
jne 0xf0e1
jmp 0xf0cd
movq 0x8(%rsi), %rdx
movzwl 0xa(%rcx), %edi
movq %rdx, %rcx
cmpw 0x8(%rdx), %di
jne 0xf0d8
movq (%rcx), %rsi
testq %rsi, %rsi
jne 0xf0bc
movq %rcx, %rdx
testq %rsi, %rsi
je 0xf1fe
xorl %r12d, %r12d
xorl %r8d, %r8d
testb %r12b, %r12b
movl 0xc(%rsp), %eax
je 0xf117
cmpq $0x0, 0x8(%rbx)
jne 0xedad
cmpq 0xe8be(%rip), %rbx      # 0x1d9c0
jne 0xf21c
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf37e
jmp 0xedad
movzwl 0x12(%rsp), %ecx
addw %cx, 0xe8a5(%rip)       # 0x1d9c8
testb $0x20, 0x14(%rsp)
jne 0xf135
addl %r15d, %eax
movw %ax, 0x24(%rbx)
orb $0x1, 0x26(%rbx)
testb %r8b, %r8b
je 0xedb5
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 0x13c50
movw %ax, 0x2(%r14)
xorl %eax, %eax
movw %ax, 0x6(%r14)
movw %ax, 0xa(%r14)
movq %r14, %rdi
movl $0x14, %esi
callq 0x142a0
movw %ax, 0xa(%r14)
movq 0x8(%rbx), %r14
testq %r15, %r15
je 0xf1b4
movq 0x8(%r15), %r12
movl $0x14, %esi
movq %r15, %rdi
callq 0x532c
movq %r14, %rdi
movq %r15, %rsi
callq 0x546d
movq (%r12), %r15
jmp 0xf18d
movq 0xe805(%rip), %rax      # 0x1d9c0
cmpq %rax, %rbx
je 0xf1d2
testq %rax, %rax
je 0xf1d2
movq %rax, %rsi
movq (%rax), %rax
cmpq %rbx, %rax
jne 0xf1c0
jmp 0xf1d4
xorl %esi, %esi
movq %rbx, %rdi
callq 0xf37e
movq %r14, %rdi
callq 0x5425
movzwl 0xe7dd(%rip), %ecx      # 0x1d9c8
cmpw %ax, %cx
jb 0xf238
subl %eax, %ecx
movw %cx, 0xe7cf(%rip)       # 0x1d9c8
jmp 0xedb8
cmpq %rdx, %rax
je 0xf29d
cmpq $0x0, (%rdx)
jne 0xf2b9
movb $0x1, %r8b
xorl %r12d, %r12d
jmp 0xf0e7
leaq 0x60a9(%rip), %rdi      # 0x152cc
leaq 0x820b(%rip), %rsi      # 0x17435
leaq 0x81e0(%rip), %rcx      # 0x17411
movl $0x2ab, %edx            # imm = 0x2AB
jmp 0xf252
leaq 0x608d(%rip), %rdi      # 0x152cc
leaq 0x81b0(%rip), %rsi      # 0x173f6
leaq 0x81c4(%rip), %rcx      # 0x17411
movl $0x29b, %edx            # imm = 0x29B
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050
leaq 0x6060(%rip), %rdi      # 0x152cc
leaq 0x8289(%rip), %rsi      # 0x174fc
leaq 0x8197(%rip), %rcx      # 0x17411
movl $0x1b6, %edx            # imm = 0x1B6
jmp 0xf252
leaq 0x6044(%rip), %rdi      # 0x152cc
leaq 0x828b(%rip), %rsi      # 0x1751a
leaq 0x817b(%rip), %rcx      # 0x17411
movl $0x1bf, %edx            # imm = 0x1BF
jmp 0xf252
leaq 0x6028(%rip), %rdi      # 0x152cc
leaq 0x82a6(%rip), %rsi      # 0x17551
leaq 0x815f(%rip), %rcx      # 0x17411
movl $0x1e1, %edx            # imm = 0x1E1
jmp 0xf252
leaq 0x600c(%rip), %rdi      # 0x152cc
leaq 0x8297(%rip), %rsi      # 0x1755e
leaq 0x8143(%rip), %rcx      # 0x17411
movl $0x1e3, %edx            # imm = 0x1E3
jmp 0xf252