// File: /workspace/github_c_makefile_star2_O0/nimble-code[P]Modex/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)
	{	if (0) printf("-- %p	%d\n", free_chunks, --gh_sz);
		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 *) e_malloc(asze * PNTR_SZE);
	
				memcpy(pnt, (void *) heap->hunks, (heap->hunk_array_sze * PNTR_SZE));
	
				e_free(heap->hunks);
				heap->hunks = pnt;
				heap->hunk_array_sze = asze;
			}
	
			heap->hunks[heap->num_hunks] =
				(void *) e_malloc(heap->chnk_sze * heap->ch_ratio);
	
			if (0) printf("2 chunk allocate at %p -- %d * %d bytes\n",
				heap->hunks[heap->num_hunks],
				heap->chnk_sze, heap->ch_ratio);

			(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 *) e_malloc(heap->chnk_sze * heap->ch_ratio);
		if (0) printf("1 chunk allocate at %p -- %d * %d bytes\n",
			heap->hunks[0], heap->chnk_sze, heap->ch_ratio);
		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 0x4630
movq $0x0, -0x8(%rbp)
jmp 0x480f
cmpq $0x0, 0x41510(%rip)     # 0x45b48
je 0x4673
movq 0x41507(%rip), %rax     # 0x45b48
movq %rax, -0x20(%rbp)
movq 0x414fc(%rip), %rax     # 0x45b48
movq (%rax), %rax
movq %rax, 0x414f2(%rip)     # 0x45b48
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x4160
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x480f
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x47bc
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 0x4782
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jne 0x4713
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
shll %eax
movl %eax, -0x14(%rbp)
movslq -0x14(%rbp), %rax
shlq $0x3, %rax
movl %eax, %edi
callq 0x4a70
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 0x41c0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x4a60
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), %edi
movq -0x10(%rbp), %rax
imull 0x4(%rax), %edi
callq 0x4a70
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 0x480f
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 0x480f
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x10(%rbp), %rax
imull 0x4(%rax), %edi
callq 0x4a70
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
nopl (%rax)