// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
shll $0x2, %eax
movzbl %al, %eax
cmpl $0x14, %eax
je 0x1ab06
jmp 0x1aaff
jmp 0x1ab01
jmp 0x1af93
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x251c0
movzwl %ax, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
shll $0x3, %eax
movw %ax, -0x32(%rbp)
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x251c0
movw %ax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
shll $0x2, %eax
movb %al, -0x37(%rbp)
movzbl -0x37(%rbp), %eax
movzwl -0x34(%rbp), %ecx
cmpl %ecx, %eax
jle 0x1ab54
jmp 0x1af93
movzwl -0x34(%rbp), %eax
movzbl -0x37(%rbp), %ecx
subl %ecx, %eax
movw %ax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7bf0
movw %ax, -0x36(%rbp)
movzwl 0x14e62(%rip), %eax     # 0x2f9d8
movzwl -0x36(%rbp), %ecx
addl %ecx, %eax
cmpl $0xa, %eax
jle 0x1abb0
movq -0x20(%rbp), %rdi
movzwl -0x36(%rbp), %esi
callq 0x1afc0
cmpl $0x0, %eax
je 0x1aba5
movzwl 0x14e3e(%rip), %eax     # 0x2f9d8
movzwl -0x36(%rbp), %ecx
addl %ecx, %eax
cmpl $0xa, %eax
jle 0x1abae
jmp 0x1aba7
jmp 0x1aba9
jmp 0x1af93
jmp 0x1abb0
movq 0x14e19(%rip), %rax     # 0x2f9d0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x1ac1b
movq -0x28(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jne 0x1abfe
movq -0x28(%rbp), %rax
movl 0x20(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x1abfe
movq -0x28(%rbp), %rax
movzwl 0x14(%rax), %eax
movq -0x20(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jne 0x1abfe
movb $0x1, %al
testb $0x1, %al
jne 0x1ac06
jmp 0x1ac0c
xorl %eax, %eax
testb $0x1, %al
jne 0x1ac06
jmp 0x1ac0c
jmp 0x1ac08
jmp 0x1ac0a
jmp 0x1ac1b
jmp 0x1ac0e
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x1abbb
cmpq $0x0, -0x28(%rbp)
jne 0x1ac41
movq -0x20(%rbp), %rdi
movzwl -0x36(%rbp), %esi
callq 0x1b110
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x1ac3f
jmp 0x1af93
jmp 0x1ac94
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x251c0
movzwl %ax, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
cmpl $0x0, %eax
jne 0x1ac92
movq -0x28(%rbp), %rax
movzwl 0x16(%rax), %edi
callq 0x251c0
movzwl %ax, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
cmpl $0x0, %eax
je 0x1ac92
movq -0x28(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x18(%rax)
movl 0x10(%rcx), %ecx
movl %ecx, 0x20(%rax)
jmp 0x1ac94
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x1acdf
movzwl -0x32(%rbp), %eax
movzwl -0x34(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x42(%rbp)
movzwl -0x42(%rbp), %eax
movzwl -0x32(%rbp), %ecx
cmpl %ecx, %eax
jl 0x1acd8
movzwl -0x42(%rbp), %eax
cmpl $0xffeb, %eax           # imm = 0xFFEB
jle 0x1acdd
jmp 0x1aefb
jmp 0x1acdf
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x40(%rbp), %edx
callq 0x1b1d0
movl %eax, -0x3c(%rbp)
cmpl $-0x1, -0x3c(%rbp)
jne 0x1acfd
jmp 0x1aefb
movzwl 0x14cd4(%rip), %eax     # 0x2f9d8
movzwl -0x36(%rbp), %ecx
addl %ecx, %eax
movw %ax, 0x14cc7(%rip)      # 0x2f9d8
cmpl $0x0, -0x40(%rbp)
je 0x1ad43
movzwl -0x32(%rbp), %eax
movzwl -0x34(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x44(%rbp)
movw -0x44(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x24(%rax)
movq -0x28(%rbp), %rax
movzbl 0x26(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x26(%rax)
jmp 0x1ad41
jmp 0x1ad43
cmpl $0x1, -0x3c(%rbp)
jne 0x1aeea
movq -0x28(%rbp), %rax
movzwl 0x24(%rax), %eax
addl $0x14, %eax
movw %ax, -0x52(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rdx
movq %rdx, (%rax)
movq 0x18(%rcx), %rdx
movq %rdx, 0x8(%rax)
movl 0x20(%rcx), %ecx
movl %ecx, 0x10(%rax)
movzwl -0x52(%rbp), %edi
callq 0x251c0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x20(%rbp), %rax
movw $0x0, 0x6(%rax)
movq -0x20(%rbp), %rax
movw $0x0, 0xa(%rax)
movq -0x20(%rbp), %rdi
movl $0x14, %esi
callq 0x25f20
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0xa(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x1ae24
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movl $0x14, %esi
callq 0x7950
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7cb0
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x1ade9
movq -0x28(%rbp), %rax
cmpq 0x14ba1(%rip), %rax     # 0x2f9d0
jne 0x1ae3b
movq $0x0, -0x50(%rbp)
jmp 0x1ae6d
movq 0x14b8e(%rip), %rax     # 0x2f9d0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x1ae6b
movq -0x50(%rbp), %rax
movq (%rax), %rax
cmpq -0x28(%rbp), %rax
jne 0x1ae5c
jmp 0x1ae6b
jmp 0x1ae5e
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x1ae46
jmp 0x1ae6d
movq -0x28(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x1b690
movq -0x10(%rbp), %rdi
callq 0x7bf0
movw %ax, -0x36(%rbp)
movzwl 0x14b4a(%rip), %eax     # 0x2f9d8
movzwl -0x36(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1aec7
jmp 0x1ae98
leaq 0xc42d(%rip), %rdi      # 0x272cc
leaq 0xe904(%rip), %rsi      # 0x297aa
movl $0x29b, %edx            # imm = 0x29B
leaq 0xe913(%rip), %rcx      # 0x297c5
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x1aec9
movzwl 0x14b08(%rip), %eax     # 0x2f9d8
movzwl -0x36(%rbp), %ecx
subl %ecx, %eax
movw %ax, 0x14afb(%rip)      # 0x2f9d8
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x1afa8
jmp 0x1aeec
jmp 0x1aeee
movq $0x0, -0x8(%rbp)
jmp 0x1afa8
jmp 0x1aefd
cmpq $0x0, -0x28(%rbp)
jne 0x1af35
jmp 0x1af06
leaq 0xc3bf(%rip), %rdi      # 0x272cc
leaq 0xe8d5(%rip), %rsi      # 0x297e9
movl $0x2a8, %edx            # imm = 0x2A8
leaq 0xe8a5(%rip), %rcx      # 0x297c5
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x1af37
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1af91
jmp 0x1af44
movq -0x28(%rbp), %rax
cmpq 0x14a81(%rip), %rax     # 0x2f9d0
je 0x1af82
jmp 0x1af53
leaq 0xc372(%rip), %rdi      # 0x272cc
leaq 0xe894(%rip), %rsi      # 0x297f5
movl $0x2ab, %edx            # imm = 0x2AB
leaq 0xe858(%rip), %rcx      # 0x297c5
movb $0x0, %al
callq 0x20d0
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0
callq 0x2040
jmp 0x1af84
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1b690
jmp 0x1af93
jmp 0x1af95
jmp 0x1af97
movq -0x10(%rbp), %rdi
callq 0x7260
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)