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

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

    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);
        
        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);
    
    /* 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);
    
    return BLOCK_DATA(cur);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x1(%rax), %rax
addq $0x22, %rax
cmpq -0x18(%rbp), %rax
jae 0x16ef3
movq $0x0, -0x8(%rbp)
jmp 0x170a7
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x18(%rbp), %rsi
callq 0x170c0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rsi
callq 0x17120
movq -0x20(%rbp), %rax
movq 0x1(%rax), %rax
movq -0x18(%rbp), %rcx
addq $0x23, %rcx
cmpq %rcx, %rax
jae 0x16f78
movq -0x20(%rbp), %rax
movb $0x1, (%rax)
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
movb $0x1, 0x21(%rax,%rcx)
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
movq -0x20(%rbp), %rsi
callq 0x171c0
movq -0x20(%rbp), %rax
addq $0x1, %rax
addq $0x8, %rax
addq $0x8, %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
jmp 0x170a7
movq -0x20(%rbp), %rax
movq 0x1(%rax), %rax
subq -0x18(%rbp), %rax
subq $0x22, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movb $0x1, (%rax)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x1(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x9(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x11(%rax)
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
movq %rdx, 0x19(%rax,%rcx)
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
movb $0x1, 0x21(%rax,%rcx)
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
movq -0x20(%rbp), %rsi
callq 0x171c0
movq -0x20(%rbp), %rax
addq $0x1, %rax
addq $0x8, %rax
addq $0x8, %rax
addq $0x8, %rax
movq -0x20(%rbp), %rcx
movq 0x1(%rcx), %rcx
shlq $0x0, %rcx
addq %rcx, %rax
addq $0x8, %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movb $0x0, (%rax)
movq -0x28(%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 -0x28(%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 $0x0, 0x21(%rax,%rcx)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x30(%rbp), %rsi
callq 0x17250
movq -0x20(%rbp), %rax
addq $0x1, %rax
addq $0x8, %rax
addq $0x8, %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)