// File: /workspace/github_c_makefile_star2_O3/FH0[P]tun_direct/lwip-2.1.2/src/core/pbuf.c
// Function: pbuf_alloc
struct pbuf *
pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
{
  struct pbuf *p;
  u16_t offset = (u16_t)layer;
  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));

  switch (type) {
    case PBUF_REF: /* fall through */
    case PBUF_ROM:
      p = pbuf_alloc_reference(NULL, length, type);
      break;
    case PBUF_POOL: {
      struct pbuf *q, *last;
      u16_t rem_len; /* remaining length */
      p = NULL;
      last = NULL;
      rem_len = length;
      do {
        u16_t qlen;
        q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
        if (q == NULL) {
          PBUF_POOL_IS_EMPTY();
          /* free chain so far allocated */
          if (p) {
            pbuf_free(p);
          }
          /* bail out unsuccessfully */
          return NULL;
        }
        qlen = LWIP_MIN(rem_len, (u16_t)(PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)));
        pbuf_init_alloced_pbuf(q, LWIP_MEM_ALIGN((void *)((u8_t *)q + SIZEOF_STRUCT_PBUF + offset)),
                               rem_len, qlen, type, 0);
        LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
                    ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
        LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
                    (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );
        if (p == NULL) {
          /* allocated head of pbuf chain (into p) */
          p = q;
        } else {
          /* make previous pbuf point to this pbuf */
          last->next = q;
        }
        last = q;
        rem_len = (u16_t)(rem_len - qlen);
        offset = 0;
      } while (rem_len > 0);
      break;
    }
    case PBUF_RAM: {
      u16_t payload_len = (u16_t)(LWIP_MEM_ALIGN_SIZE(offset) + LWIP_MEM_ALIGN_SIZE(length));
      mem_size_t alloc_len = (mem_size_t)(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF) + payload_len);

      /* bug #50040: Check for integer overflow when calculating alloc_len */
      if ((payload_len < LWIP_MEM_ALIGN_SIZE(length)) ||
          (alloc_len < LWIP_MEM_ALIGN_SIZE(length))) {
        return NULL;
      }

      /* If pbuf is to be allocated in RAM, allocate memory for it. */
      p = (struct pbuf *)mem_malloc(alloc_len);
      if (p == NULL) {
        return NULL;
      }
      pbuf_init_alloced_pbuf(p, LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset)),
                             length, length, type, 0);
      LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
                  ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
      break;
    }
    default:
      LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
      return NULL;
  }
  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
  return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %esi, %ebx
cmpl $0x181, %edx            # imm = 0x181
jg 0x4e2a
cmpl $0x1, %edx
je 0x4e16
cmpl $0x41, %edx
jne 0x4f4d
movzwl %bx, %esi
xorl %edi, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x4f69
movl %edi, %ebp
cmpl $0x182, %edx            # imm = 0x182
je 0x4e8b
cmpl $0x280, %edx            # imm = 0x280
jne 0x4f4d
movl %ebp, %eax
addw %bx, %ax
jb 0x4f13
movzwl %ax, %edi
addq $0x18, %rdi
callq 0x3bed
testq %rax, %rax
je 0x4f13
movzwl %bp, %ecx
movl %ecx, %ecx
addq %rax, %rcx
addq $0x18, %rcx
movq $0x0, (%rax)
movq %rcx, 0x8(%rax)
movw %bx, 0x10(%rax)
movw %bx, 0x12(%rax)
movl $0x10080, 0x14(%rax)    # imm = 0x10080
jmp 0x4f15
xorl %r15d, %r15d
xorl %r14d, %r14d
movl $0xb, %edi
callq 0x3c6e
testq %rax, %rax
je 0x4eff
movq %rax, %rcx
movzwl %bx, %eax
movzwl %bp, %esi
leal -0x5ea(%rsi), %edi
negl %edi
movzwl %di, %edx
cmpl %eax, %edx
movl %eax, %edx
cmovbel %edi, %edx
leaq 0x18(%rcx,%rsi), %rdi
movq $0x0, (%rcx)
movq %rdi, 0x8(%rcx)
movw %ax, 0x10(%rcx)
movw %dx, 0x12(%rcx)
movl $0x10082, 0x14(%rcx)    # imm = 0x10082
cmpl $0x5ea, %esi            # imm = 0x5EA
je 0x4f20
movq %rcx, %rax
testq %r14, %r14
je 0x4ef0
movq %rcx, (%r15)
movq %r14, %rax
xorl %ebp, %ebp
movq %rcx, %r15
movq %rax, %r14
subw %dx, %bx
jne 0x4e91
jmp 0x4f15
movb $0x1, 0x179e2(%rip)     # 0x1c8e8
testq %r14, %r14
je 0x4f13
movq %r14, %rdi
callq 0x4fdf
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf3a5(%rip), %rdi      # 0x142cc
leaq 0xf916(%rip), %rsi      # 0x14844
leaq 0xf8f4(%rip), %rcx      # 0x14829
movl $0x103, %edx            # imm = 0x103
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050
leaq 0xf378(%rip), %rdi      # 0x142cc
leaq 0xf91d(%rip), %rsi      # 0x14878
leaq 0xf8c7(%rip), %rcx      # 0x14829
movl $0x127, %edx            # imm = 0x127
jmp 0x4f3a