// File: /workspace/github_c_makefile_star2_O3/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 0x224c8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22358
xorl %ebx, %ebx
jmp 0x225b1
movq %rdx, %r15
testq %rbx, %rbx
je 0x22558
movq -0x8(%rbx), %rax
cmpq $0x3, %rax
jbe 0x225ff
leaq -0x10(%rbx), %r12
leaq -0x1(%r15), %rcx
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
cmpq %rdx, %rcx
ja 0x22571
movq %rax, %rbp
andq $-0x4, %rbp
leaq 0x7(%r15), %rcx
movabsq $0x1fffffff8, %rdx      # imm = 0x1FFFFFFF8
andq %rcx, %rdx
cmpq $0x19, %rdx
movl $0x18, %r13d
cmovaeq %rdx, %r13
cmpq %rbp, %r13
jbe 0x22574
movq (%rbx,%rbp), %rax
testb $0x1, %al
je 0x225c3
andq $-0x4, %rax
addq %rbp, %rax
addq $0x8, %rax
cmpq %rax, %r13
ja 0x225c3
movq %r14, %rdi
movq %r12, %rsi
callq 0x22414
movq %r12, %rdi
callq 0x2263d
movq -0x8(%rbx), %rax
jmp 0x22574
movq %r14, %rdi
movq %r15, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21fab
xorl %r13d, %r13d
testb $0x1, %al
jne 0x2261e
andq $-0x4, %rax
leaq 0x20(%r13), %rcx
cmpq %rcx, %rax
jb 0x225b1
movq %r12, %rdi
movq %r13, %rsi
callq 0x22719
movq %rax, %r15
andb $-0x3, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x22414
movq %r14, %rdi
movq %r15, %rsi
callq 0x21e84
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %r15, %rsi
callq 0x21fab
testq %rax, %rax
je 0x224c1
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 0x22358
movq %r12, %rbx
jmp 0x225b1
leaq 0x5feb(%rip), %rdi      # 0x285f1
leaq 0x5c57(%rip), %rsi      # 0x28264
leaq 0x5ff3(%rip), %rcx      # 0x28607
movl $0xf8, %edx
callq 0x31e0
leaq 0x65c4(%rip), %rdi      # 0x28be9
leaq 0x5c38(%rip), %rsi      # 0x28264
leaq 0x65e4(%rip), %rcx      # 0x28c17
movl $0x20f, %edx            # imm = 0x20F
callq 0x31e0