// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
movl %esi, %ebx
cmpl $0x181, %edx            # imm = 0x181
jg 0x4e89
cmpl $0x1, %edx
je 0x4e77
cmpl $0x41, %edx
jne 0x4fd3
movzwl %bx, %esi
xorl %edi, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4fef
movl %edi, %ebp
cmpl $0x182, %edx            # imm = 0x182
je 0x4efe
cmpl $0x280, %edx            # imm = 0x280
jne 0x4fd3
movl %ebp, %eax
addw %bx, %ax
setb %cl
movzwl %ax, %edi
addq $0x18, %rdi
movzwl %bx, %eax
cmpq %rax, %rdi
setb %al
orb %cl, %al
jne 0x4f97
callq 0x3c1d
testq %rax, %rax
je 0x4f97
movq %rax, %r14
movzwl %bp, %eax
movl %eax, %eax
addq %r14, %rax
addq $0x18, %rax
movq $0x0, (%r14)
movq %rax, 0x8(%r14)
movw %bx, 0x10(%r14)
movw %bx, 0x12(%r14)
movl $0x10080, 0x14(%r14)    # imm = 0x10080
jmp 0x4f9a
xorl %r12d, %r12d
xorl %r14d, %r14d
movl $0xb, %edi
callq 0x3c9e
movq %rax, %r15
testq %rax, %rax
je 0x4f73
movzwl %bx, %ecx
movzwl %bp, %edx
leal -0x5ea(%rdx), %esi
negl %esi
movzwl %si, %eax
cmpl %ecx, %eax
movl %ecx, %eax
cmovbel %esi, %eax
leaq 0x18(%r15,%rdx), %rsi
movq $0x0, (%r15)
movq %rsi, 0x8(%r15)
movw %cx, 0x10(%r15)
movw %ax, 0x12(%r15)
movl $0x10082, 0x14(%r15)    # imm = 0x10082
cmpl $0x5ea, %edx            # imm = 0x5EA
je 0x4fa6
movq %r15, %rcx
testq %r14, %r14
je 0x4f67
movq %r15, (%r12)
movq %r14, %rcx
subl %eax, %ebx
xorl %ebp, %ebp
movq %r15, %r12
movq %rcx, %r14
jmp 0x4f87
movb $0x1, 0x1896e(%rip)     # 0x1d8e8
testq %r14, %r14
je 0x4f87
movq %r14, %rdi
callq 0x5069
testq %r15, %r15
je 0x4f97
testw %bx, %bx
jne 0x4f04
jmp 0x4f9a
xorl %r14d, %r14d
movq %r14, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1031f(%rip), %rdi     # 0x152cc
leaq 0x10890(%rip), %rsi     # 0x15844
leaq 0x1086e(%rip), %rcx     # 0x15829
movl $0x103, %edx            # imm = 0x103
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050
leaq 0x102f2(%rip), %rdi     # 0x152cc
leaq 0x10897(%rip), %rsi     # 0x15878
leaq 0x10841(%rip), %rcx     # 0x15829
movl $0x127, %edx            # imm = 0x127
jmp 0x4fc0