// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
testq %rsi, %rsi
sete %al
testq %rdx, %rdx
setne %cl
orb %al, %cl
jne 0x1d5b0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1d439
xorl %ebx, %ebx
jmp 0x1d698
movq %rdx, %r12
testq %rbx, %rbx
je 0x1d63e
leaq -0x10(%rbx), %r15
movq %r15, %rdi
callq 0x1ce5a
movq -0x8(%rbx), %rcx
leaq -0x1(%r12), %rdx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
cmpq %rsi, %rdx
ja 0x1d657
movq %rcx, %rbp
andq $-0x4, %rbp
leaq 0x7(%r12), %rdx
movabsq $0x1fffffff8, %rsi      # imm = 0x1FFFFFFF8
andq %rdx, %rsi
cmpq $0x19, %rsi
pushq $0x18
popq %r13
cmovaeq %rsi, %r13
cmpq %rbp, %r13
jbe 0x1d65a
movq 0x8(%rax), %rax
testb $0x1, %al
je 0x1d6aa
andq $-0x4, %rax
addq %rbp, %rax
addq $0x8, %rax
cmpq %rax, %r13
ja 0x1d6aa
movq %r14, %rdi
movq %r15, %rsi
callq 0x1d4f8
movq %r15, %rdi
callq 0x1d705
movq -0x8(%rbx), %rcx
jmp 0x1d65a
movq %r14, %rdi
movq %r12, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1d0a7
xorl %r13d, %r13d
testb $0x1, %cl
jne 0x1d6e6
andq $-0x4, %rcx
leaq 0x20(%r13), %rax
cmpq %rax, %rcx
jb 0x1d698
movq %r15, %rdi
movq %r13, %rsi
callq 0x1d7d5
movq %rax, %r15
andb $-0x3, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1d4f8
movq %r14, %rdi
movq %r15, %rsi
callq 0x1cfc5
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %r12, %rsi
callq 0x1d0a7
testq %rax, %rax
je 0x1d5a9
movq %rax, %r15
cmpq %r12, %rbp
cmovbq %rbp, %r12
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3360
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1d439
movq %r15, %rbx
jmp 0x1d698
leaq 0x66a0(%rip), %rdi      # 0x23d8d
leaq 0x5cdb(%rip), %rsi      # 0x233cf
leaq 0x66c0(%rip), %rcx      # 0x23dbb
movl $0x20f, %edx            # imm = 0x20F
callq 0x31e0