// File: /workspace/github_c_makefile_star2_O2/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 0x5c49
movq %r15, %rdi
xorl %esi, %esi
callq 0x5afc
testl %eax, %eax
je 0x5c56
leaq -0x18(%r15), %r14
leaq 0x1197e(%rip), %rdi     # 0x174d0
callq 0x24c0
leaq 0x1193a(%rip), %rax     # 0x17498
xorl %edx, %edx
movq %rdx, %rcx
movq (%rax), %rax
testq %rax, %rax
je 0x5b73
movq %rax, %rdx
cmpq %r14, %rax
jne 0x5b60
testq %rax, %rax
je 0x5c65
testq %rcx, %rcx
movq (%rax), %rax
leaq 0x1190f(%rip), %rdx     # 0x17498
cmovneq %rcx, %rdx
movq %rax, (%rdx)
leaq 0x11939(%rip), %rdi     # 0x174d0
callq 0x22d0
movl -0x10(%r15), %eax
leaq (%r15,%rax), %rdi
andl $0x7, %eax
pushq $0x8
popq %rsi
subq %rax, %rsi
testq %rax, %rax
cmoveq %rax, %rsi
movl (%rsi,%rdi), %edx
cmpl $0x12345678, %edx       # imm = 0x12345678
jne 0x5c84
callq 0x5afc
movl -0xc(%r15), %ebp
testl %eax, %eax
je 0x5c9d
leaq 0x118cd(%rip), %rdi     # 0x174a8
callq 0x24c0
leaq 0x118b9(%rip), %rax     # 0x174a0
movq (%rax), %rax
testq %rax, %rax
je 0x5c22
cmpl %ebp, 0x10(%rax)
jne 0x5be7
movl 0xc(%rax), %ecx
testl %ecx, %ecx
je 0x5cb1
decl %ecx
movl %ecx, 0xc(%rax)
leaq 0x1189d(%rip), %rdi     # 0x174a8
callq 0x22d0
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x2050
leaq 0x9e2b(%rip), %rsi      # 0xfa54
xorl %edi, %edi
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xcb1c
leaq 0x1186b(%rip), %rdi     # 0x174a8
callq 0x22d0
xorl %edi, %edi
callq 0xb4f2
movl -0xc(%r15), %ecx
leaq 0x9c60(%rip), %rsi      # 0xf8b4
jmp 0x5c8f
movl -0xc(%r15), %edx
leaq 0x9ca5(%rip), %rsi      # 0xf906
xorl %edi, %edi
jmp 0x5ca8
movl -0xc(%r15), %edx
leaq 0x9cd4(%rip), %rsi      # 0xf944
xorl %edi, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0xcb1c
leaq 0x1184e(%rip), %rdi     # 0x174d0
jmp 0x5c3d
movl -0xc(%r15), %ecx
leaq 0x9ceb(%rip), %rsi      # 0xf97a
xorl %edi, %edi
movl %ebx, %r8d
xorl %eax, %eax
callq 0xcb1c
jmp 0x5c42
leaq 0x9d29(%rip), %rsi      # 0xf9cd
xorl %edi, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0xcb1c
jmp 0x5c42
leaq 0x9d54(%rip), %rsi      # 0xfa0c
jmp 0x5c29
nopl (%rax)