// File: /workspace/github_c_makefile_star2_O0/nimble-code[P]Uno/Src/heap.c
// Function: HeapAlloc_Gen
void *
HeapAlloc_Gen(Heap *heap)
{	int     asze, chnk;
	void   *avail, *pnt;

	if (!heap)
		return ((void *) NULL);

	if (free_chunks)
	{
#ifdef DEBUG
		printf("-- %p	%d\n", free_chunks, --gh_sz);
#endif
		avail = (void *) free_chunks;
		free_chunks = free_chunks->fc.nxt;
		memset(avail, 0, sizeof(void *));
		return avail;
	}

	if (heap->num_hunks)
	{	chnk = (heap->next_chunk)++;

		if (chnk == (int) heap->ch_ratio)
		{
			if (heap->num_hunks == heap->hunk_array_sze)
			{	asze = heap->hunk_array_sze * 2;	/* double array size */
				pnt = (void *) emalloc(asze * PNTR_SZE);
	
				memcpy(pnt, (void *) heap->hunks, (heap->hunk_array_sze * PNTR_SZE));
	
				efree(heap->hunks);
				heap->hunks = pnt;
				heap->hunk_array_sze = asze;
			}
	
			heap->hunks[heap->num_hunks] =
				(void *) emalloc((size_t) heap->chnk_sze * (size_t) heap->ch_ratio);
	
#ifdef DEBUG
	printf("2 chunk allocate at %p -- %d * %d bytes\n",
		heap->hunks[heap->num_hunks],
		heap->chnk_sze, heap->ch_ratio);
#endif
			(heap->num_hunks)++;
			heap->next_chunk = 1;
			(heap->num_alloc)++;
			return (heap->hunks[(heap->num_hunks)-1]);
		} else
		{	(heap->num_alloc)++;
			return((void *) ((char*) (heap->hunks[(heap->num_hunks)-1])
				+ (heap->chnk_sze * chnk))); 
		}
	} else
	{	heap->hunks[0] = (void *) emalloc((size_t) heap->chnk_sze * (size_t) heap->ch_ratio);
#ifdef DEBUG
	printf("1 chunk allocate at %p -- %d * %d bytes\n",
		heap->hunks[0], heap->chnk_sze, heap->ch_ratio);
#endif
		heap->num_hunks = 1;
		heap->next_chunk = 1;
		(heap->num_alloc)++;
		return (heap->hunks[0]);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x5070
movq $0x0, -0x8(%rbp)
jmp 0x5257
cmpq $0x0, 0x49360(%rip)     # 0x4e3d8
je 0x50b3
movq 0x49357(%rip), %rax     # 0x4e3d8
movq %rax, -0x20(%rbp)
movq 0x4934c(%rip), %rax     # 0x4e3d8
movq (%rax), %rax
movq %rax, 0x49342(%rip)     # 0x4e3d8
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x3140
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x5257
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x51ff
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x18(%rcx)
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x51c5
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jne 0x5151
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
shll %eax
movl %eax, -0x14(%rbp)
movslq -0x14(%rbp), %rdi
shlq $0x3, %rdi
callq 0x54c0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %edx
shlq $0x3, %rdx
callq 0x3190
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x54b0
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, %edi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
imulq %rax, %rdi
callq 0x54c0
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x18(%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
jmp 0x5257
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
imull -0x18(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x5257
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, %edi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
imulq %rax, %rdi
callq 0x54c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x18(%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)