// File: /workspace/github_c_makefile_star2_O0/mikecat[P]x86_interpreter/dmem_libc_stdlib.c
// Function: malloc_core
static uint32_t malloc_core(uint32_t size) {
	heap_info_t** pptr = &heap_info_head;
	heap_info_t* ptr = heap_info_head;
	heap_info_t* new_node;
	uint32_t addr = heap_start;
	if (UINT32_MAX - (HEAP_ALIGN - 1) < size) {
		return 0;
	}
	size = (size + (HEAP_ALIGN - 1)) / HEAP_ALIGN * HEAP_ALIGN;
	if (size == 0) size = HEAP_ALIGN;
	while(ptr != NULL) {
		if (!ptr->used && ptr->size >= size) {
			/* 十分な空き領域が見つかった */
			if (ptr->size == size) {
				/* ちょうどいい大きさ */
				ptr->used = 1;
			} else {
				/* 余る */
				new_node = malloc(sizeof(heap_info_t));
				if (new_node == NULL) {
					return 0;
				}
				new_node->size = ptr->size - size;
				new_node->used = 0;
				new_node->next = ptr->next;
				ptr->size = size;
				ptr->used = 1;
				ptr->next = new_node;
			}
			return addr;
		} else {
			addr += ptr->size;
			pptr = &ptr->next;
			ptr = ptr->next;
		}
	}
	/* 空き領域が見つからなかったので、作る */
	new_node = malloc(sizeof(heap_info_t));
	if (new_node == NULL) {
		return 0;
	}
	dmemory_allocate(addr, size);
	new_node->size = size;
	new_node->used = 1;
	new_node->next = NULL;
	*pptr = new_node;
	return addr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
leaq 0xd126(%rip), %rax      # 0x17e48
movq %rax, -0x10(%rbp)
movq 0xd11b(%rip), %rax      # 0x17e48
movq %rax, -0x18(%rbp)
movl 0xd109(%rip), %eax      # 0x17e40
movl %eax, -0x24(%rbp)
movl $0xffffffc0, %eax       # imm = 0xFFFFFFC0
cmpl -0x8(%rbp), %eax
jae 0xad50
movl $0x0, -0x4(%rbp)
jmp 0xaea7
movl -0x8(%rbp), %eax
addl $0x3f, %eax
shrl $0x6, %eax
shll $0x6, %eax
movl %eax, -0x8(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0xad6c
movl $0x40, -0x8(%rbp)
jmp 0xad6e
cmpq $0x0, -0x18(%rbp)
je 0xae4d
movq -0x18(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0xae24
movq -0x18(%rbp), %rax
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jb 0xae24
movq -0x18(%rbp), %rax
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jne 0xadae
movq -0x18(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0xae19
movl $0x10, %edi
callq 0x1110
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xadcf
movl $0x0, -0x4(%rbp)
jmp 0xaea7
movq -0x18(%rbp), %rax
movl (%rax), %ecx
subl -0x8(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x8(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x4(%rax)
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xaea7
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0xad6e
movl $0x10, %edi
callq 0x1110
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xae6b
movl $0x0, -0x4(%rbp)
jmp 0xaea7
movl -0x24(%rbp), %edi
movl -0x8(%rbp), %esi
callq 0x7c60
movl -0x8(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl $0x1, 0x4(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq