// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x1(%rax), %rax
addq $0x22, %rax
cmpq -0x20(%rbp), %rax
jae 0x17357
movq $0x0, -0x8(%rbp)
jmp 0x175ab
movq -0x18(%rbp), %rax
addq $-0x8, %rax
addq $-0x8, %rax
addq $-0x8, %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rsi
callq 0x170c0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x30(%rbp), %rsi
callq 0x17120
movq -0x30(%rbp), %rax
movq 0x1(%rax), %rax
movq -0x20(%rbp), %rcx
addq $0x23, %rcx
cmpq %rcx, %rax
jae 0x1743a
movq -0x30(%rbp), %rax
movb $0x1, (%rax)
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
movb $0x1, 0x21(%rax,%rcx)
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
movq -0x30(%rbp), %rsi
callq 0x171c0
movq -0x30(%rbp), %rdi
addq $0x1, %rdi
addq $0x8, %rdi
addq $0x8, %rdi
addq $0x8, %rdi
movq -0x28(%rbp), %rsi
addq $0x1, %rsi
addq $0x8, %rsi
addq $0x8, %rsi
addq $0x8, %rsi
movq -0x28(%rbp), %rax
movq 0x1(%rax), %rdx
callq 0x1120
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x175c0
movq -0x30(%rbp), %rax
addq $0x1, %rax
addq $0x8, %rax
addq $0x8, %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
jmp 0x175ab
movq -0x30(%rbp), %rax
movq 0x1(%rax), %rax
subq -0x20(%rbp), %rax
subq $0x22, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movb $0x1, (%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x1(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x9(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x11(%rax)
movq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
movq %rdx, 0x19(%rax,%rcx)
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
movb $0x1, 0x21(%rax,%rcx)
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
movq -0x30(%rbp), %rsi
callq 0x171c0
movq -0x30(%rbp), %rdi
addq $0x1, %rdi
addq $0x8, %rdi
addq $0x8, %rdi
addq $0x8, %rdi
movq -0x28(%rbp), %rsi
addq $0x1, %rsi
addq $0x8, %rsi
addq $0x8, %rsi
addq $0x8, %rsi
movq -0x28(%rbp), %rax
movq 0x1(%rax), %rdx
callq 0x1120
movq -0x30(%rbp), %rax
addq $0x1, %rax
addq $0x8, %rax
addq $0x8, %rax
addq $0x8, %rax
movq -0x30(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
addq %rcx, %rax
addq $0x8, %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movb $0x0, (%rax)
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x1(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x9(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x11(%rax)
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
movq %rdx, 0x19(%rax,%rcx)
movq -0x40(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
movb $0x0, 0x21(%rax,%rcx)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x40(%rbp), %rsi
callq 0x17250
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x175c0
movq -0x30(%rbp), %rax
addq $0x1, %rax
addq $0x8, %rax
addq $0x8, %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)