// File: /workspace/github_c_makefile_star2_O2/masyagin1998[P]GCI/src/garbage-collector/allocator.c
// Function: allocator_free_block
void allocator_free_block(allocator_type_t a, void*ptrmem)
{
    void*cur = BLOCK_PTR_FROM_DATA(ptrmem);
    void*arr_prev = NULL;
    void*arr_next = NULL;
    if ((cur == a->mem) ||
        (BLOCK_L_FLAG(BLOCK_ARR_PREV(cur)) == BUSY_BLOCK)) {
        arr_prev = NULL;
    } else {
        arr_prev = BLOCK_ARR_PREV(cur);
    }

    if (((cur + BLOCK_L_DATA_LEN(cur) + BLOCK_OVERHEAD) >= (void*) (a->mem + a->sizemem)) ||
        (BLOCK_L_FLAG(BLOCK_ARR_NEXT(cur)) == BUSY_BLOCK)) {
        arr_next = NULL;
    } else {
        arr_next = BLOCK_ARR_NEXT(cur);
    }

    allocator_list_remove_elem(&(a->busy_list), cur);
    
    if ((arr_prev != NULL) && (arr_next == NULL)) {
        /* concatenate previous free array block with current block. */
        size_t len = BLOCK_L_DATA_LEN(arr_prev) + BLOCK_R_OVERHEAD + BLOCK_L_OVERHEAD + BLOCK_L_DATA_LEN(cur);
        allocator_list_remove_elem(&(a->free_list), arr_prev);
        BLOCK_L_DATA_LEN(arr_prev) = len;
        BLOCK_R_DATA_LEN(cur) = len;
        allocator_list_insert_elem(&(a->free_list), arr_prev);
    } else if ((arr_prev == NULL) && (arr_next != NULL)) {
        /* concatenate current block with next free array block. */
        size_t len = BLOCK_L_DATA_LEN(cur) + BLOCK_R_OVERHEAD + BLOCK_L_OVERHEAD + BLOCK_L_DATA_LEN(arr_next);
        allocator_list_remove_elem(&(a->free_list), arr_next);
        BLOCK_L_DATA_LEN(cur) = len;
        BLOCK_R_DATA_LEN(arr_next) = len;
        allocator_list_insert_elem(&(a->free_list), cur);
    } else if ((arr_prev != NULL) && (arr_next != NULL)) {
        /* concatenate previous free array block with current block with next free array block. */
        size_t len =
            BLOCK_L_DATA_LEN(arr_prev) + BLOCK_R_OVERHEAD +
            BLOCK_L_OVERHEAD + BLOCK_L_DATA_LEN(cur) + BLOCK_R_OVERHEAD +
            BLOCK_L_OVERHEAD + BLOCK_L_DATA_LEN(arr_next);
        allocator_list_remove_elem(&(a->free_list), arr_prev);
        allocator_list_remove_elem(&(a->free_list), arr_next);
        BLOCK_L_DATA_LEN(arr_prev) = len;
        BLOCK_R_DATA_LEN(arr_next) = len;
        allocator_list_insert_elem(&(a->free_list), arr_prev);
    } else {
        BLOCK_L_FLAG(cur) = FREE_BLOCK;
        BLOCK_R_FLAG(cur) = FREE_BLOCK;
        allocator_list_insert_elem(&(a->free_list), cur);
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %r14
movq %rdi, %rbx
leaq -0x19(%rsi), %r12
movq (%rdi), %rax
cmpq %rax, %r12
je 0xd2fb
leaq -0x22(%r14), %rcx
subq -0x22(%r14), %rcx
leaq -0x19(%rcx), %rdx
xorl %r15d, %r15d
cmpb $0x1, -0x19(%rcx)
cmovneq %rdx, %r15
jmp 0xd2fe
xorl %r15d, %r15d
movq -0x18(%r14), %rcx
leaq (%r12,%rcx), %rdx
addq $0x22, %rdx
addq 0x8(%rbx), %rax
cmpq %rax, %rdx
jae 0xd327
leaq (%r14,%rcx), %rax
addq $0x9, %rax
xorl %r13d, %r13d
cmpb $0x1, (%rax)
cmovneq %rax, %r13
jmp 0xd32a
xorl %r13d, %r13d
leaq 0x30(%rbx), %rdi
movq %r12, %rsi
callq 0xd096
testq %r15, %r15
je 0xd376
testq %r13, %r13
jne 0xd376
movq 0x1(%r15), %rax
movq -0x18(%r14), %rcx
leaq (%rax,%rcx), %r12
addq $0x22, %r12
addq $0x10, %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xd096
movq %r12, 0x1(%r15)
movq -0x18(%r14), %rax
movq %r12, (%r14,%rax)
movq %rbx, %rdi
movq %r15, %rsi
jmp 0xd411
testq %r15, %r15
jne 0xd3ae
testq %r13, %r13
je 0xd3ae
movq -0x18(%r14), %rax
movq 0x1(%r13), %rcx
leaq (%rax,%rcx), %r15
addq $0x22, %r15
addq $0x10, %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd096
movq %r15, -0x18(%r14)
movq 0x1(%r13), %rax
movq %r15, 0x19(%r13,%rax)
jmp 0xd40b
testq %r15, %r15
je 0xd3f8
testq %r13, %r13
je 0xd3f8
movq 0x1(%r15), %rax
movq 0x1(%r13), %rcx
addq -0x18(%r14), %rax
leaq (%rcx,%rax), %r14
addq $0x44, %r14
addq $0x10, %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xd096
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd096
movq %r14, 0x1(%r15)
movq 0x1(%r13), %rax
movq %r14, 0x19(%r13,%rax)
jmp 0xd36b
xorl %eax, %eax
movb %al, -0x19(%r14)
movq -0x18(%r14), %rcx
movb %al, 0x8(%r14,%rcx)
addq $0x10, %rbx
movq %rbx, %rdi
movq %r12, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xd0fe