// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq 0xce02(%rip), %rax      # 0x17e48
movq %rax, -0x28(%rbp)
movl 0xcdf0(%rip), %eax      # 0x17e40
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %edi
movl $0x2, %esi
leaq -0x18(%rbp), %rdx
leaq -0x1c(%rbp), %rcx
movb $0x0, %al
callq 0x8280
cmpl $0x0, %eax
jne 0xb07b
movl $0x0, -0x4(%rbp)
jmp 0xb317
cmpl $0x0, -0x18(%rbp)
jne 0xb09d
movl -0x1c(%rbp), %edi
callq 0xad10
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0xb317
movl $0xffffffc0, %eax       # imm = 0xFFFFFFC0
cmpl -0x1c(%rbp), %eax
jae 0xb0b3
movl $0x0, -0x4(%rbp)
jmp 0xb317
movl -0x1c(%rbp), %eax
addl $0x3f, %eax
shrl $0x6, %eax
shll $0x6, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0xb0cf
movl $0x40, -0x1c(%rbp)
jmp 0xb0d1
cmpq $0x0, -0x28(%rbp)
je 0xb310
movq -0x28(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0xb30b
movl -0x2c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0xb30b
movq -0x28(%rbp), %rax
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jne 0xb10f
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
jmp 0xb302
movl -0x1c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jae 0xb1dc
movq -0x28(%rbp), %rax
movl (%rax), %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0xb174
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x0, 0x4(%rax)
jne 0xb174
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl -0x30(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpl (%rcx), %eax
jae 0xb163
movl $0x0, -0x4(%rbp)
jmp 0xb317
movl -0x30(%rbp), %ecx
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0xb1c5
movl $0x10, %edi
callq 0x1110
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xb195
movl $0x0, -0x4(%rbp)
jmp 0xb317
movl -0x30(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x1c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
jmp 0xb300
movl -0x1c(%rbp), %eax
movq -0x28(%rbp), %rcx
subl (%rcx), %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0xb272
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x0, 0x4(%rax)
jne 0xb272
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %eax
cmpl -0x3c(%rbp), %eax
ja 0xb272
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %eax
cmpl -0x3c(%rbp), %eax
jne 0xb24a
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rdi
callq 0x1030
jmp 0xb25b
movl -0x3c(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
movl -0x1c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
jmp 0xb2fe
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, %edi
callq 0x1110
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0xb29c
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
jmp 0xb2fc
movq -0x58(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl -0x4c(%rbp), %edx
callq 0x7890
movl -0x2c(%rbp), %edi
callq 0xaf00
cmpl $0x0, %eax
jne 0xb2ca
movq -0x58(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0xb317
movl -0x1c(%rbp), %edi
callq 0xad10
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0xb2ea
movq -0x58(%rbp), %rdi
movl -0x5c(%rbp), %esi
movl -0x4c(%rbp), %edx
callq 0x7ad0
movq -0x58(%rbp), %rdi
callq 0x1030
movl -0x5c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
jmp 0xb2fe
jmp 0xb300
jmp 0xb302
movl $0x1, -0x4(%rbp)
jmp 0xb317
jmp 0xb0d1
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq