// File: /workspace/github_c_makefile_star2_O0/cogbee[P]vmir-latest/tlsf/tlsf.c
// Function: tlsf_realloc
void* tlsf_realloc(tlsf_pool tlsf, void* ptr, size_t size)
{
	pool_t* pool = tlsf_cast(pool_t*, tlsf);
	void* p = 0;

	/* Zero-size requests are treated as free. */
	if (ptr && size == 0)
	{
		tlsf_free(tlsf, ptr);
	}
	/* Requests with NULL pointers are treated as malloc. */
	else if (!ptr)
	{
		p = tlsf_malloc(tlsf, size);
	}
	else
	{
		block_header_t* block = block_from_ptr(ptr);
		block_header_t* next = block_next(block);

		const size_t cursize = block_size(block);
		const size_t combined = cursize + block_size(next) + block_header_overhead;
		const size_t adjust = adjust_request_size(size, ALIGN_SIZE);

		/*
		** If the next block is used, or when combined with the current
		** block, does not offer enough space, we must reallocate and copy.
		*/
		if (adjust > cursize && (!block_is_free(next) || adjust > combined))
		{
			p = tlsf_malloc(tlsf, size);
			if (p)
			{
				const size_t minsize = tlsf_min(cursize, size);
				memcpy(p, ptr, minsize);
				tlsf_free(tlsf, ptr);
			}
		}
		else
		{
			/* Do we need to expand to the next block? */
			if (adjust > cursize)
			{
				block_merge_next(pool, block);
				block_mark_as_used(block);
			}

			/* Trim the resulting block and return the original pointer. */
			block_trim_used(pool, block, adjust);
			p = ptr;
		}
	}

	return p;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x3a864
cmpq $0x0, -0x18(%rbp)
jne 0x3a864
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3a5f0
jmp 0x3a998
cmpq $0x0, -0x10(%rbp)
jne 0x3a881
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3a180
movq %rax, -0x28(%rbp)
jmp 0x3a996
movq -0x10(%rbp), %rdi
callq 0x39df0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x39bc0
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x39c30
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rdi
callq 0x39c30
movq %rax, %rcx
movq -0x60(%rbp), %rax
addq %rcx, %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movl $0x8, %esi
callq 0x3a1e0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jbe 0x3a95b
movq -0x38(%rbp), %rdi
callq 0x39b80
cmpl $0x0, %eax
je 0x3a8ff
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jbe 0x3a95b
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3a180
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x3a959
movq -0x40(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x3a92b
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x3a933
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x3330
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3a5f0
jmp 0x3a994
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jbe 0x3a97b
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3a760
movq -0x30(%rbp), %rdi
callq 0x3a9b0
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x3a9f0
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x3a996
jmp 0x3a998
movq -0x28(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)