// File: /workspace/github_c_makefile_star2_O1/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 0x2240a
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2228d
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r15
testq %rbx, %rbx
je 0x22453
movq -0x8(%rbx), %rbp
cmpq $0x3, %rbp
jbe 0x2252d
andq $-0x4, %rbp
leaq -0x1(%r15), %rax
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
cmpq %rcx, %rax
ja 0x2246c
leaq 0x7(%r15), %rax
movabsq $0x1fffffff8, %rcx      # imm = 0x1FFFFFFF8
andq %rax, %rcx
cmpq $0x19, %rcx
movl $0x18, %r12d
cmovaeq %rcx, %r12
jmp 0x2246f
movq %r14, %rdi
movq %r15, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21ed0
xorl %r12d, %r12d
leaq -0x10(%rbx), %r13
cmpq %rbp, %r12
jbe 0x224a3
movq (%rbx,%rbp), %rax
testb $0x1, %al
je 0x224ee
andq $-0x4, %rax
addq %rbp, %rax
addq $0x8, %rax
cmpq %rax, %r12
ja 0x224ee
movq %r14, %rdi
movq %r13, %rsi
callq 0x22349
movq %r13, %rdi
callq 0x2256b
movq -0x8(%rbx), %rax
testb $0x1, %al
jne 0x2254c
andq $-0x4, %rax
leaq 0x20(%r12), %rcx
cmpq %rcx, %rax
jb 0x223f8
movq %r13, %rdi
movq %r12, %rsi
callq 0x22647
movq %rax, %r15
andb $-0x3, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x22349
movq %r14, %rdi
movq %r15, %rsi
callq 0x21d85
jmp 0x223f8
movq %r14, %rdi
movq %r15, %rsi
callq 0x21ed0
testq %rax, %rax
je 0x223f6
movq %rax, %r12
cmpq %r15, %rbp
cmovbq %rbp, %r15
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3360
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2228d
movq %r12, %rbx
jmp 0x223f8
leaq 0x6073(%rip), %rdi      # 0x285a7
leaq 0x5cdf(%rip), %rsi      # 0x2821a
leaq 0x607b(%rip), %rcx      # 0x285bd
movl $0xf8, %edx
callq 0x31e0
leaq 0x6685(%rip), %rdi      # 0x28bd8
leaq 0x5cc0(%rip), %rsi      # 0x2821a
leaq 0x66a5(%rip), %rcx      # 0x28c06
movl $0x20f, %edx            # imm = 0x20F
callq 0x31e0