// File: /workspace/github_c_makefile_star2_O3/open-mesh-mirror[P]batmand/allocate.c
// Function: debugFree
void debugFree(void *memoryParameter, int tag)
{
	unsigned char *memory;
	struct chunkHeader *chunkHeader;
	struct chunkTrailer *chunkTrailer;
	struct chunkHeader *walker;
	struct chunkHeader *previous;

	memory = memoryParameter;
	chunkHeader = (struct chunkHeader *)(memory - sizeof(struct chunkHeader) - getHeaderPad());

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

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

	previous = NULL;

	pthread_mutex_lock(&chunk_mutex);
	for (walker = chunkList; walker != NULL; walker = walker->next)
	{
		if (walker == chunkHeader)
			break;

		previous = walker;
	}

	if (walker == NULL)
	{
		debug_output( 0, "Double free detected, malloc tag = %d, free tag = %d\n", chunkHeader->tag, tag );
		pthread_mutex_unlock(&chunk_mutex);
		restore_and_exit(0);
	}

	if (previous == NULL)
		chunkList = walker->next;

	else
		previous->next = walker->next;

	pthread_mutex_unlock(&chunk_mutex);

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

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

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

#if defined MEMORY_USAGE

	removeMemory( chunkHeader->tag, tag );

#endif

	free(chunkHeader);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r15
movl -0x8(%rdi), %edx
cmpl $0x12345678, %edx       # imm = 0x12345678
jne 0x63ce
leaq -0x18(%r15), %r14
leaq 0x1121f(%rip), %rdi     # 0x174d0
callq 0x24c0
leaq 0x111db(%rip), %rax     # 0x17498
xorl %edx, %edx
movq %rdx, %rcx
movq (%rax), %rax
testq %rax, %rax
je 0x62d2
movq %rax, %rdx
cmpq %r14, %rax
jne 0x62bf
testq %rax, %rax
je 0x63db
testq %rcx, %rcx
movq (%rax), %rax
leaq 0x111b0(%rip), %rdx     # 0x17498
cmovneq %rcx, %rdx
movq %rax, (%rdx)
leaq 0x111da(%rip), %rdi     # 0x174d0
callq 0x22d0
movl -0x10(%r15), %esi
leaq (%r15,%rsi), %rax
andl $0x7, %esi
movl $0x8, %ecx
subq %rsi, %rcx
testq %rsi, %rsi
cmoveq %rsi, %rcx
movl (%rcx,%rax), %edx
cmpl $0x12345678, %edx       # imm = 0x12345678
jne 0x63fa
testq %rsi, %rsi
je 0x633f
xorl %edx, %edx
xorl %esi, %esi
addb $-0x59, %sil
cmpb (%rax,%rdx), %sil
jne 0x63b3
incq %rdx
cmpq %rdx, %rcx
jne 0x632d
movl -0xc(%r15), %ebp
leaq 0x1115e(%rip), %rdi     # 0x174a8
callq 0x24c0
leaq 0x1114a(%rip), %rax     # 0x174a0
movq (%rax), %rax
testq %rax, %rax
je 0x6391
cmpl %ebp, 0x10(%rax)
jne 0x6356
movl 0xc(%rax), %ecx
testl %ecx, %ecx
je 0x6413
decl %ecx
movl %ecx, 0xc(%rax)
leaq 0x1112e(%rip), %rdi     # 0x174a8
callq 0x22d0
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x2050
leaq 0xa6da(%rip), %rsi      # 0x10a72
xorl %edi, %edi
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xd88c
leaq 0x110fc(%rip), %rdi     # 0x174a8
callq 0x22d0
jmp 0x63c7
movl -0xc(%r15), %edx
leaq 0xa62d(%rip), %rsi      # 0x109eb
xorl %edi, %edi
xorl %eax, %eax
callq 0xd88c
xorl %edi, %edi
callq 0xc084
movl -0xc(%r15), %ecx
leaq 0xa537(%rip), %rsi      # 0x10910
jmp 0x6405
movl -0xc(%r15), %edx
leaq 0xa57c(%rip), %rsi      # 0x10962
xorl %edi, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0xd88c
leaq 0x110d8(%rip), %rdi     # 0x174d0
jmp 0x63ac
movl -0xc(%r15), %ecx
leaq 0xa593(%rip), %rsi      # 0x10998
xorl %edi, %edi
movl %ebx, %r8d
xorl %eax, %eax
callq 0xd88c
jmp 0x63c7
leaq 0xa610(%rip), %rsi      # 0x10a2a
jmp 0x6398
nop