// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rdi, %rbx
movq 0xc8cd(%rip), %r13      # 0x15d88
movl 0xc8be(%rip), %r14d     # 0x15d80
xorl %ebp, %ebp
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
movl %esi, %edi
movl $0x2, %esi
xorl %eax, %eax
callq 0x735a
testl %eax, %eax
je 0x9583
movl 0x4(%rsp), %ecx
movl (%rsp), %edi
testl %ecx, %ecx
je 0x9577
cmpl $-0x40, %edi
ja 0x9583
addl $0x3f, %edi
andl $-0x40, %edi
movl $0x40, %eax
cmovnel %edi, %eax
movl %eax, (%rsp)
testq %r13, %r13
je 0x9583
cmpl %ecx, %r14d
sete %cl
cmpl $0x0, 0x4(%r13)
setne %dl
testb %cl, %dl
je 0x9575
movl (%r13), %r15d
cmpl %eax, %r15d
je 0x9570
movq 0x8(%r13), %r12
cmpl %eax, %r15d
jbe 0x9594
subl %eax, %r15d
testq %r12, %r12
je 0x9548
cmpl $0x0, 0x4(%r12)
je 0x9612
movl $0x10, %edi
callq 0x1100
testq %rax, %rax
je 0x9583
movl %r15d, (%rax)
movl $0x0, 0x4(%rax)
movq %r12, 0x8(%rax)
movq %rax, 0x8(%r13)
movl (%rsp), %eax
movl %eax, (%r13)
movl %r14d, (%rbx)
jmp 0x957e
jmp 0x9575
callq 0x92e1
movl %eax, (%rbx)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r12, %r12
je 0x95b0
cmpl $0x0, 0x4(%r12)
jne 0x95b0
subl %r15d, %eax
movl (%r12), %ecx
subl %eax, %ecx
jbe 0x963b
movq %r15, %rdi
callq 0x1100
testq %rax, %rax
je 0x9607
movq %rax, %r12
movq %rax, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x6c34
movl %r14d, %edi
callq 0x93f7
movl %eax, %r14d
testl %eax, %eax
je 0x9625
movl (%rsp), %edi
callq 0x92e1
movl %eax, %r13d
testl %eax, %eax
je 0x95fa
movq %r12, %rdi
movl %r13d, %esi
movl %r15d, %edx
callq 0x6d8a
movq %r12, %rdi
callq 0x1030
movl %r13d, (%rbx)
jmp 0x962d
movl $0x0, (%rbx)
jmp 0x957e
addl (%r12), %r15d
jb 0x9583
movl %r15d, (%r12)
jmp 0x9569
movq %r12, %rdi
callq 0x1030
testl %r14d, %r14d
jne 0x957e
jmp 0x9583
jne 0x9653
movq 0x8(%r12), %rax
movq %rax, 0x8(%r13)
movq %r12, %rdi
callq 0x1030
jmp 0x9569
movl %ecx, (%r12)
jmp 0x9569