// File: /workspace/github_c_makefile_star2_O0/open-mesh-mirror[P]batmand/allocate.c
// Function: debugRealloc
void *debugRealloc(void *memoryParameter, uint32_t length, int32_t tag)
{
	unsigned char *memory;
	struct chunkHeader *chunkHeader=NULL;
	struct chunkTrailer *chunkTrailer;
	unsigned char *result;
	uint32_t copyLength;

	if (memoryParameter) { /* if memoryParameter==NULL, realloc() should work like malloc() !! */
		memory = memoryParameter;
		chunkHeader = (struct chunkHeader *)(memory - sizeof(struct chunkHeader) - getHeaderPad());

		if (chunkHeader->magicNumber != MAGIC_NUMBER)
		{
			debug_output( 0, "debugRealloc - invalid magic number in header: %08x, malloc tag = %d\n", chunkHeader->magicNumber, chunkHeader->tag );
			restore_and_exit(0);
		}

		if (checkPadding(memory - getHeaderPad(), getHeaderPad()) == 0) {
			debug_output( 0, "debugRealloc - invalid magic padding in header, malloc tag = %d\n", chunkHeader->tag );
			restore_and_exit(0);
		}

		chunkTrailer = (struct chunkTrailer *)(memory + chunkHeader->length + getTrailerPad(chunkHeader->length));

		if (chunkTrailer->magicNumber != MAGIC_NUMBER)
		{
			debug_output( 0, "debugRealloc - invalid magic number in trailer: %08x, malloc tag = %d\n", chunkTrailer->magicNumber, chunkHeader->tag );
			restore_and_exit(0);
		}

		if (checkPadding(memory + chunkHeader->length, getTrailerPad(chunkHeader->length)) == 0) {
			debug_output( 0, "debugRealloc - invalid magic padding in trailer, malloc tag = %d\n", chunkHeader->tag );
			restore_and_exit(0);
		}
	}


	result = debugMalloc(length, tag);
	if (memoryParameter) {
		copyLength = length;

		if (copyLength > chunkHeader->length)
			copyLength = chunkHeader->length;

		memcpy(result, memoryParameter, copyLength);
		debugFree(memoryParameter, 9999);
	}

	return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x7646
movq -0x8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $-0x18, %rax
movq %rax, -0x40(%rbp)
callq 0x7110
movq %rax, %rdx
movq -0x40(%rbp), %rax
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x12345678, 0x10(%rax) # imm = 0x12345678
je 0x7543
movq -0x20(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %ecx
xorl %edi, %edi
leaq 0xe270(%rip), %rsi      # 0x157a5
movb $0x0, %al
callq 0x123f0
xorl %edi, %edi
callq 0x10180
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
callq 0x7110
movq %rax, %rdx
movq -0x50(%rbp), %rax
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
callq 0x7110
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x76b0
cmpl $0x0, %eax
jne 0x7597
movq -0x20(%rbp), %rax
movl 0xc(%rax), %edx
xorl %edi, %edi
leaq 0xe262(%rip), %rsi      # 0x157eb
movb $0x0, %al
callq 0x123f0
xorl %edi, %edi
callq 0x10180
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0x8(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edi
callq 0x7160
movq %rax, %rcx
movq -0x58(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x12345678, (%rax)     # imm = 0x12345678
je 0x75f5
movq -0x28(%rbp), %rax
movl (%rax), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %ecx
xorl %edi, %edi
leaq 0xe245(%rip), %rsi      # 0x1582c
movb $0x0, %al
callq 0x123f0
xorl %edi, %edi
callq 0x10180
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0x8(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edi
callq 0x7160
movq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0x76b0
cmpl $0x0, %eax
jne 0x7644
movq -0x20(%rbp), %rax
movl 0xc(%rax), %edx
xorl %edi, %edi
leaq 0xe23d(%rip), %rsi      # 0x15873
movb $0x0, %al
callq 0x123f0
xorl %edi, %edi
callq 0x10180
jmp 0x7646
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
callq 0x7220
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x7698
movl -0xc(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jbe 0x7678
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x34(%rbp), %eax
movl %eax, %edx
callq 0x22c0
movq -0x8(%rbp), %rdi
movl $0x270f, %esi           # imm = 0x270F
callq 0x7720
movq -0x30(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)