// File: /workspace/github_c_makefile_star2_O3/masyagin1998[P]GCI/src/garbage-collector/allocator.c
// Function: allocator_realloc_block
void*allocator_realloc_block(allocator_type_t a, void*ptrmem, size_t sizemem)
{
    void*orig;
    void*cur;

    if ((BLOCK_L_DATA_LEN(a->free_list.last) + BLOCK_OVERHEAD) < sizemem) {
        return NULL;
    }

    orig = BLOCK_PTR_FROM_DATA(ptrmem);

    cur = allocator_list_search_by_sizemem(&(a->free_list), sizemem);

    /* remove block from list. */
    allocator_list_remove_elem(&(a->free_list), cur);

    if (BLOCK_L_DATA_LEN(cur) < (sizemem + MIN_BLOCK_LEN)) {
        /* don't need to divide block. */
        BLOCK_L_FLAG(cur) = BUSY_BLOCK;
        BLOCK_R_FLAG(cur) = BUSY_BLOCK;
        allocator_list_push_back(&(a->busy_list), cur);
        memcpy(BLOCK_DATA(cur), BLOCK_DATA(orig), BLOCK_L_DATA_LEN(orig));
        allocator_free_block(a, ptrmem);
        return BLOCK_DATA(cur);
    }

    size_t len = BLOCK_L_DATA_LEN(cur) - sizemem - BLOCK_OVERHEAD;
    void*tmp_block;
    /* have to divide block. */
    
    /* split block into two smaller blocks (one of them will contain data). */
    
    /* put busy block to busy list. */
    BLOCK_L_FLAG(cur) = BUSY_BLOCK;
    BLOCK_L_DATA_LEN(cur) = sizemem;
    BLOCK_LIST_PREV(cur) = NULL;
    BLOCK_LIST_NEXT(cur) = NULL;
    BLOCK_R_DATA_LEN(cur) = sizemem;
    BLOCK_R_FLAG(cur) = BUSY_BLOCK;
    allocator_list_push_back(&(a->busy_list), cur);
    memcpy(BLOCK_DATA(cur), BLOCK_DATA(orig), BLOCK_L_DATA_LEN(orig));
    
    /* put free block to free list. */
    tmp_block = BLOCK_ARR_NEXT(cur);
    BLOCK_L_FLAG(tmp_block) = FREE_BLOCK;
    BLOCK_L_DATA_LEN(tmp_block) = len;
    BLOCK_LIST_PREV(tmp_block) = NULL;
    BLOCK_LIST_NEXT(tmp_block) = NULL;
    BLOCK_R_DATA_LEN(tmp_block) = len;
    BLOCK_R_FLAG(tmp_block) = FREE_BLOCK;
    allocator_list_insert_elem(&(a->free_list), tmp_block);
    allocator_free_block(a, ptrmem);

    return BLOCK_DATA(cur);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x18(%rdi), %rax
movq 0x1(%rax), %rax
addq $0x22, %rax
cmpq %rdx, %rax
jae 0xe964
xorl %r13d, %r13d
jmp 0xea72
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x10(%rdi), %r13
movq (%r13), %r12
testq %r12, %r12
je 0xe988
cmpq %rbp, 0x1(%r12)
jae 0xe98b
movq 0x11(%r12), %r12
jmp 0xe975
xorl %r12d, %r12d
movq %r13, %rdi
movq %r12, %rsi
callq 0xe860
movq 0x1(%r12), %r15
leaq 0x23(%rbp), %rax
cmpq %rax, %r15
jae 0xe9d4
movb $0x1, %al
movb %al, (%r12)
leaq 0x19(%r12), %r13
movb %al, 0x21(%r12,%r15)
leaq 0x30(%r14), %rdi
movq %r12, %rsi
callq 0xe898
movq -0x18(%rbx), %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1120
jmp 0xea67
subq %rbp, %r15
leaq -0x22(%r15), %rax
movq %rax, 0x8(%rsp)
movb $0x1, %al
movb %al, (%r12)
movq %rbp, 0x1(%r12)
movq %r13, 0x10(%rsp)
leaq 0x19(%r12), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x9(%r12)
movq %rbp, 0x19(%r12,%rbp)
movq 0x1(%r12), %rcx
movb %al, 0x21(%r12,%rcx)
leaq 0x30(%r14), %rdi
movq %r12, %rsi
callq 0xe898
movq -0x18(%rbx), %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1120
movq 0x1(%r12), %rax
leaq (%r12,%rax), %rcx
addq $0x19, %rcx
leaq (%r12,%rax), %rsi
addq $0x22, %rsi
xorl %eax, %eax
movb %al, (%rsi)
movq 0x8(%rsp), %rdx
movq %rdx, 0x1(%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x9(%rsi)
movq %rdx, (%r15,%rcx)
movq 0x1(%rsi), %rdx
movb %al, 0x2a(%rdx,%rcx)
movq 0x10(%rsp), %rdi
callq 0xe8cb
movq %r14, %rdi
movq %rbx, %rsi
callq 0xea84
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq