// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
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 0x7791
movq -0x20(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %ecx
movl -0xc(%rbp), %r8d
xorl %edi, %edi
leaq 0xe132(%rip), %rsi      # 0x158b5
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 0x77e5
movq -0x20(%rbp), %rax
movl 0xc(%rax), %edx
xorl %edi, %edi
leaq 0xe130(%rip), %rsi      # 0x15907
movb $0x0, %al
callq 0x123f0
xorl %edi, %edi
callq 0x10180
movq $0x0, -0x38(%rbp)
leaq 0x13d7c(%rip), %rdi     # 0x1b570
callq 0x24e0
movq 0x13d38(%rip), %rax     # 0x1b538
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x782c
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x7817
jmp 0x782c
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x7804
cmpq $0x0, -0x30(%rbp)
jne 0x7860
movq -0x20(%rbp), %rax
movl 0xc(%rax), %edx
movl -0xc(%rbp), %ecx
xorl %edi, %edi
leaq 0xe0ff(%rip), %rsi      # 0x15945
movb $0x0, %al
callq 0x123f0
leaq 0x13d1c(%rip), %rdi     # 0x1b570
callq 0x2300
xorl %edi, %edi
callq 0x10180
cmpq $0x0, -0x38(%rbp)
jne 0x7877
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, 0x13cc3(%rip)     # 0x1b538
jmp 0x7885
movq -0x30(%rbp), %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
leaq 0x13ce4(%rip), %rdi     # 0x1b570
callq 0x2300
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 0x78f3
movq -0x28(%rbp), %rax
movl (%rax), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %ecx
movl -0xc(%rbp), %r8d
xorl %edi, %edi
leaq 0xe096(%rip), %rsi      # 0x1597b
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 0x7942
movq -0x20(%rbp), %rax
movl 0xc(%rax), %edx
xorl %edi, %edi
leaq 0xe09a(%rip), %rsi      # 0x159ce
movb $0x0, %al
callq 0x123f0
xorl %edi, %edi
callq 0x10180
movq -0x20(%rbp), %rax
movl 0xc(%rax), %edi
movl -0xc(%rbp), %esi
callq 0x7960
movq -0x20(%rbp), %rdi
callq 0x2050
addq $0x60, %rsp
popq %rbp
retq