// File: /workspace/github_c_makefile_star2_O2/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
movl %esi, %eax
movq %rdi, %rbx
movq 0xd13c(%rip), %r13      # 0x14d88
movl 0xd12d(%rip), %r14d     # 0x14d80
xorl %ebp, %ebp
pushq $0x2
popq %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl %eax, %edi
xorl %eax, %eax
callq 0x626c
testl %eax, %eax
je 0x7d15
movl 0xc(%rsp), %eax
movl 0x8(%rsp), %edi
testl %eax, %eax
je 0x7d0b
cmpl $-0x40, %edi
ja 0x7d15
addl $0x3f, %edi
andl $-0x40, %edi
pushq $0x40
popq %r12
cmovnel %edi, %r12d
movl %r12d, 0x8(%rsp)
testq %r13, %r13
je 0x7d15
cmpl %eax, %r14d
sete %al
cmpl $0x0, 0x4(%r13)
setne %cl
testb %al, %cl
je 0x7d09
movl (%r13), %r15d
cmpl %r12d, %r15d
je 0x7d04
movq 0x8(%r13), %rdi
cmpl %r12d, %r15d
jbe 0x7d26
subl %r12d, %r15d
testq %rdi, %rdi
je 0x7cda
cmpl $0x0, 0x4(%rdi)
je 0x7d92
movq %rdi, 0x10(%rsp)
pushq $0x10
popq %rdi
callq 0x1100
movq 0x10(%rsp), %rcx
testq %rax, %rax
je 0x7d15
movl %r15d, (%rax)
andl $0x0, 0x4(%rax)
movq %rcx, 0x8(%rax)
movq %rax, 0x8(%r13)
movl %r12d, (%r13)
movl %r14d, (%rbx)
jmp 0x7d12
jmp 0x7d09
callq 0x7a96
movl %eax, (%rbx)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rdi, %rdi
je 0x7d3b
cmpl $0x0, 0x4(%rdi)
jne 0x7d3b
subl %r15d, %r12d
movl (%rdi), %eax
subl %r12d, %eax
jbe 0x7db5
movq %r15, %rdi
callq 0x1100
testq %rax, %rax
je 0x7d8d
movq %rax, %r12
movq %rax, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x5bd0
movl %r14d, %edi
callq 0x7b94
testl %eax, %eax
je 0x7da8
movl 0x8(%rsp), %edi
callq 0x7a96
movl %eax, %ebp
testl %eax, %eax
je 0x7d81
movq %r12, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x5d3e
movq %r12, %rdi
callq 0x1030
movl %ebp, (%rbx)
jmp 0x7d12
andl $0x0, (%rbx)
jmp 0x7d12
addl (%rdi), %r15d
jb 0x7d15
movl %r15d, (%rdi)
movl 0x8(%rsp), %r12d
jmp 0x7d00
movq %r12, %rdi
callq 0x1030
jmp 0x7d15
jne 0x7dc6
movq 0x8(%rdi), %rax
movq %rax, 0x8(%r13)
callq 0x1030
jmp 0x7dc8
movl %eax, (%rdi)
movl 0x8(%rsp), %eax
movl %eax, (%r13)
jmp 0x7d04
nopl (%rax)