// File: /workspace/github_c_makefile_star2_O2/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 0xd1a5
xorl %r15d, %r15d
jmp 0xd2b5
movq %rdx, %rbp
movq %rdi, %r14
movq %rsi, 0x8(%rsp)
leaq 0x10(%rdi), %r13
movq %r13, %rdi
movq %rdx, %rsi
callq 0xd07e
movq %rax, %r12
movq %r13, %rdi
movq %rax, %rsi
callq 0xd096
movq 0x1(%r12), %rbx
leaq 0x23(%rbp), %rax
cmpq %rax, %rbx
jae 0xd212
movb $0x1, %al
movb %al, (%r12)
movq %r12, %r15
addq $0x19, %r15
movb %al, 0x21(%r12,%rbx)
leaq 0x30(%r14), %rdi
movq %r12, %rsi
callq 0xd0ce
movq 0x8(%rsp), %rbp
movq -0x18(%rbp), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1120
jmp 0xd2aa
subq %rbp, %rbx
leaq -0x22(%rbx), %rax
movq %rax, 0x10(%rsp)
movb $0x1, %al
movb %al, (%r12)
movq %rbp, 0x1(%r12)
leaq 0x19(%r12), %r15
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 0xd0ce
movq 0x8(%rsp), %rbp
movq -0x18(%rbp), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1120
movq 0x1(%r12), %rax
leaq (%r12,%rax), %rcx
addq $0x19, %rcx
leaq (%r12,%rax), %rsi
addq $0x22, %rsi
xorl %edx, %edx
movb %dl, 0x22(%r12,%rax)
movq 0x10(%rsp), %rdi
movq %rdi, 0x23(%r12,%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x2b(%r12,%rax)
movq %rdi, (%rbx,%rcx)
movq 0x23(%r12,%rax), %rax
movb %dl, 0x2a(%rax,%rcx)
movq %r13, %rdi
callq 0xd0fe
movq %r14, %rdi
movq %rbp, %rsi
callq 0xd2c7
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq