// File: /workspace/github_c_makefile_star2_O1/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 0x609c
leaq -0x18(%r15), %r14
leaq 0x1154f(%rip), %rdi     # 0x174d0
callq 0x24c0
leaq 0x1150b(%rip), %rax     # 0x17498
xorl %edx, %edx
movq %rdx, %rcx
movq (%rax), %rax
testq %rax, %rax
je 0x5fa2
movq %rax, %rdx
cmpq %r14, %rax
jne 0x5f8f
testq %rax, %rax
je 0x60a9
testq %rcx, %rcx
movq (%rax), %rax
leaq 0x114e0(%rip), %rdx     # 0x17498
cmovneq %rcx, %rdx
movq %rax, (%rdx)
leaq 0x1150a(%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 0x60c8
testq %rsi, %rsi
je 0x600d
xorl %edx, %edx
xorl %esi, %esi
addb $-0x59, %dl
cmpb (%rax,%rsi), %dl
jne 0x6081
incq %rsi
cmpq %rsi, %rcx
jne 0x5ffd
movl -0xc(%r15), %ebp
leaq 0x11490(%rip), %rdi     # 0x174a8
callq 0x24c0
leaq 0x1147c(%rip), %rax     # 0x174a0
movq (%rax), %rax
testq %rax, %rax
je 0x605f
cmpl %ebp, 0x10(%rax)
jne 0x6024
movl 0xc(%rax), %ecx
testl %ecx, %ecx
je 0x60e1
decl %ecx
movl %ecx, 0xc(%rax)
leaq 0x11460(%rip), %rdi     # 0x174a8
callq 0x22d0
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x2050
leaq 0xa96f(%rip), %rsi      # 0x109d5
xorl %edi, %edi
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xd5b0
leaq 0x1142e(%rip), %rdi     # 0x174a8
callq 0x22d0
jmp 0x6095
movl -0xc(%r15), %edx
leaq 0xa8c2(%rip), %rsi      # 0x1094e
xorl %edi, %edi
xorl %eax, %eax
callq 0xd5b0
xorl %edi, %edi
callq 0xbd8c
movl -0xc(%r15), %ecx
leaq 0xa7cc(%rip), %rsi      # 0x10873
jmp 0x60d3
movl -0xc(%r15), %edx
leaq 0xa811(%rip), %rsi      # 0x108c5
xorl %edi, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0xd5b0
leaq 0x1140a(%rip), %rdi     # 0x174d0
jmp 0x607a
movl -0xc(%r15), %ecx
leaq 0xa828(%rip), %rsi      # 0x108fb
xorl %edi, %edi
movl %ebx, %r8d
xorl %eax, %eax
callq 0xd5b0
jmp 0x6095
leaq 0xa8a5(%rip), %rsi      # 0x1098d
jmp 0x6066
nopl (%rax)