// File: /workspace/github_c_makefile_star2_O3/mikecat[P]x86_interpreter/dmem_libc_stdlib.c
// Function: dmem_libc_realloc
int dmem_libc_realloc(uint32_t* ret, uint32_t esp) {
	uint32_t old_addr, new_size;
	heap_info_t* ptr = heap_info_head;
	uint32_t addr = heap_start;
	int printf(const char*,...);
	if (!dmem_get_args(esp, 2, &old_addr, &new_size)) return 0;
	if (old_addr == 0) {
		*ret = malloc_core(new_size);
		return 1;
	}
	if (UINT32_MAX - (HEAP_ALIGN - 1) < new_size) {
		return 0;
	}
	new_size = (new_size + (HEAP_ALIGN - 1)) / HEAP_ALIGN * HEAP_ALIGN;
	if (new_size == 0) new_size = HEAP_ALIGN;
	while (ptr != NULL) {
		if (ptr->used && addr == old_addr) {
			if (ptr->size == new_size) {
				/* そのまま */
				*ret = addr;
			} else if (new_size < ptr->size) {
				/* 領域を減らす */
				uint32_t delta = ptr->size - new_size;
				if (ptr->next != NULL && !ptr->next->used) {
					if (UINT32_MAX - delta < ptr->next->size) return 0;
					ptr->next->size += delta;
				} else {
					heap_info_t* new_node = malloc(sizeof(heap_info_t));
					if (new_node == NULL) return 0;
					new_node->size = delta;
					new_node->used = 0;
					new_node->next = ptr->next;
					ptr->next = new_node;
				}
				ptr->size = new_size;
				*ret = addr;
			} else {
				/* 領域を増やす */
				uint32_t delta = new_size - ptr->size;
				if (ptr->next != NULL && !ptr->next->used && ptr->next->size <= delta) {
					/* 今の領域を再利用して増やせる余裕がある */
					if (ptr->next->size == delta) {
						heap_info_t* next_node = ptr->next;
						ptr->next = ptr->next->next;
						free(next_node);
					} else {
						ptr->next->size -= delta;
					}
					ptr->size = new_size;
					*ret = addr;
				} else {
					/* 余裕が無い */
					uint32_t old_size = ptr->size;
					char* buf = malloc(old_size);
					if (buf == NULL) {
						*ret = 0;
					} else {
						uint32_t new_addr;
						dmemory_read(buf, addr, old_size);
						if (!free_core(addr)) {
							free(buf);
							return 0;
						}
						new_addr = malloc_core(new_size);
						if (new_addr != 0) dmemory_write(buf, new_addr, old_size);
						free(buf);
						*ret = new_addr;
					}
				}
			}
			return 1;
		}
	}
	/* 指定の領域が見つからなかった */
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0xd0b1(%rip), %r13      # 0x15d88
movl 0xd0a2(%rip), %r14d     # 0x15d80
xorl %ebp, %ebp
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl %esi, %edi
movl $0x2, %esi
xorl %eax, %eax
callq 0x7157
testl %eax, %eax
je 0x8dab
movl 0xc(%rsp), %eax
movl 0x8(%rsp), %edi
testl %eax, %eax
je 0x8d9f
cmpl $-0x40, %edi
ja 0x8dab
addl $0x3f, %edi
andl $-0x40, %edi
movl $0x40, %r12d
cmovnel %edi, %r12d
movl %r12d, 0x8(%rsp)
testq %r13, %r13
je 0x8dab
cmpl %eax, %r14d
sete %al
cmpl $0x0, 0x4(%r13)
setne %cl
testb %al, %cl
je 0x8d9d
movl (%r13), %r15d
cmpl %r12d, %r15d
je 0x8d98
movq 0x8(%r13), %rdi
cmpl %r12d, %r15d
jbe 0x8dbc
subl %r12d, %r15d
testq %rdi, %rdi
je 0x8d69
cmpl $0x0, 0x4(%rdi)
je 0x8e35
movq %rdi, 0x10(%rsp)
movl $0x10, %edi
callq 0x1100
movq 0x10(%rsp), %rcx
testq %rax, %rax
je 0x8dab
movl %r15d, (%rax)
movl $0x0, 0x4(%rax)
movq %rcx, 0x8(%rax)
movq %rax, 0x8(%r13)
movl %r12d, (%r13)
movl %r14d, (%rbx)
jmp 0x8da6
jmp 0x8d9d
callq 0x8af2
movl %eax, (%rbx)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rdi, %rdi
je 0x8dd5
cmpl $0x0, 0x4(%rdi)
jne 0x8dd5
subl %r15d, %r12d
movl (%rdi), %eax
subl %r12d, %eax
jbe 0x8e58
movq %r15, %rdi
callq 0x1100
testq %rax, %rax
je 0x8e2a
movq %rax, %r12
movq %rax, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x6a34
movl %r14d, %edi
callq 0x8c10
testl %eax, %eax
je 0x8e4b
movl 0x8(%rsp), %edi
callq 0x8af2
movl %eax, %ebp
testl %eax, %eax
je 0x8e1b
movq %r12, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x6b71
movq %r12, %rdi
callq 0x1030
movl %ebp, (%rbx)
jmp 0x8da6
movl $0x0, (%rbx)
jmp 0x8da6
addl (%rdi), %r15d
jb 0x8dab
movl %r15d, (%rdi)
movl 0x8(%rsp), %r12d
jmp 0x8d94
movq %r12, %rdi
callq 0x1030
jmp 0x8dab
jne 0x8e69
movq 0x8(%rdi), %rax
movq %rax, 0x8(%r13)
callq 0x1030
jmp 0x8e6b
movl %eax, (%rdi)
movl 0x8(%rsp), %eax
movl %eax, (%r13)
jmp 0x8d98