// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/tre/lib/tre-mem.c
// Function: tre_mem_alloc_impl
void *
tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
		   int zero, size_t size)
{
  void *ptr;

  if (mem->failed)
    {
      DPRINT(("tre_mem_alloc: oops, called after failure?!\n"));
      return NULL;
    }

#ifdef MALLOC_DEBUGGING
  if (!provided)
    {
      ptr = xmalloc(1);
      if (ptr == NULL)
	{
	  DPRINT(("tre_mem_alloc: xmalloc forced failure\n"));
	  mem->failed = 1;
	  return NULL;
	}
      xfree(ptr);
    }
#endif /* MALLOC_DEBUGGING */

  if (mem->n < size)
    {
      /* We need more memory than is available in the current block.
	 Allocate a new block. */
      tre_list_t *l;
      if (provided)
	{
	  DPRINT(("tre_mem_alloc: using provided block\n"));
	  if (provided_block == NULL)
	    {
	      DPRINT(("tre_mem_alloc: provided block was NULL\n"));
	      mem->failed = 1;
	      return NULL;
	    }
	  mem->ptr = provided_block;
	  mem->n = TRE_MEM_BLOCK_SIZE;
	}
      else
	{
	  int block_size;
	  if (size * 8 > TRE_MEM_BLOCK_SIZE)
	    block_size = size * 8;
	  else
	    block_size = TRE_MEM_BLOCK_SIZE;
	  DPRINT(("tre_mem_alloc: allocating new %d byte block\n",
		  block_size));
	  l = xmalloc(sizeof(*l));
	  if (l == NULL)
	    {
	      mem->failed = 1;
	      return NULL;
	    }
	  l->data = xmalloc(block_size);
	  if (l->data == NULL)
	    {
	      xfree(l);
	      mem->failed = 1;
	      return NULL;
	    }
	  l->next = NULL;
	  if (mem->current != NULL)
	    mem->current->next = l;
	  if (mem->blocks == NULL)
	    mem->blocks = l;
	  mem->current = l;
	  mem->ptr = l->data;
	  mem->n = block_size;
	}
    }

  /* Make sure the next pointer will be aligned. */
  size += ALIGN(mem->ptr + size, GtWord);

  /* Allocate from current block. */
  ptr = mem->ptr;
  mem->ptr += size;
  mem->n -= size;

  /* Set to zero if needed. */
  if (zero)
    memset(ptr, 0, size);

  return ptr;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movl %esi, 0x44(%rsp)
movq %rdx, 0x38(%rsp)
movl %ecx, 0x34(%rsp)
movq %r8, 0x28(%rsp)
movq 0x48(%rsp), %rax
cmpl $0x0, 0x20(%rax)
je 0xc32a8
jmp 0xc3298
jmp 0xc329a
movq $0x0, 0x50(%rsp)
jmp 0xc34c8
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rax
cmpq 0x28(%rsp), %rax
jae 0xc3420
cmpl $0x0, 0x44(%rsp)
je 0xc330d
jmp 0xc32c5
jmp 0xc32c7
cmpq $0x0, 0x38(%rsp)
jne 0xc32ed
jmp 0xc32d1
jmp 0xc32d3
movq 0x48(%rsp), %rax
movl $0x1, 0x20(%rax)
movq $0x0, 0x50(%rsp)
jmp 0xc34c8
movq 0x38(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x48(%rsp), %rax
movq $0x400, 0x18(%rax)      # imm = 0x400
jmp 0xc341e
movq 0x28(%rsp), %rax
shlq $0x3, %rax
cmpq $0x400, %rax            # imm = 0x400
jbe 0xc332d
movq 0x28(%rsp), %rax
shlq $0x3, %rax
movl %eax, 0x14(%rsp)
jmp 0xc3335
movl $0x400, 0x14(%rsp)      # imm = 0x400
jmp 0xc3337
jmp 0xc3339
movl $0x10, %edi
callq 0xbcb8
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0xc336a
movq 0x48(%rsp), %rax
movl $0x1, 0x20(%rax)
movq $0x0, 0x50(%rsp)
jmp 0xc34c8
movslq 0x14(%rsp), %rdi
callq 0xbcb8
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rax
cmpq $0x0, (%rax)
jne 0xc33ae
movq 0x18(%rsp), %rdi
callq 0xb760
movq 0x48(%rsp), %rax
movl $0x1, 0x20(%rax)
movq $0x0, 0x50(%rsp)
jmp 0xc34c8
movq 0x18(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x48(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0xc33d9
movq 0x18(%rsp), %rcx
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x48(%rsp), %rax
cmpq $0x0, (%rax)
jne 0xc33f1
movq 0x18(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x18(%rsp), %rax
movq (%rax), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x10(%rax)
movslq 0x14(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x18(%rax)
jmp 0xc3420
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
addq 0x28(%rsp), %rax
andq $0x7, %rax
cmpq $0x0, %rax
je 0xc3459
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rcx
addq 0x28(%rsp), %rcx
andq $0x7, %rcx
movl $0x8, %eax
subq %rcx, %rax
movq %rax, 0x8(%rsp)
jmp 0xc3462
xorl %eax, %eax
movq %rax, 0x8(%rsp)
jmp 0xc3462
movq 0x8(%rsp), %rax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rcx
movq 0x48(%rsp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rdx
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x18(%rax)
cmpl $0x0, 0x34(%rsp)
je 0xc34be
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
xorl %esi, %esi
callq 0xb2d0
movq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)