// File: chhylp123[P]hifiasm/hic.cpp
// Function: build_unitig_index(ma_ug_t*, int, unsigned long, unsigned long, unsigned long)
ha_ug_index* build_unitig_index(ma_ug_t *ug, int k, uint64_t up_occ, uint64_t low_occ, uint64_t thread_num)
{
    ha_ug_index* idx = NULL; CALLOC(idx, 1);
    pldat_t pl; pl.h = idx; pl.is_cnt = 1;
    double index_time = yak_realtime(), beg_time;
    init_ha_ug_index_opt(idx, ug, k, &pl, up_occ, low_occ, thread_num);

    beg_time = yak_realtime();
    pl.is_cnt = 1;
    parallel_count_hc_pt1(&pl);
    fprintf(stderr, "[M::%s::%.3f] ==> Counting\n", __func__, yak_realtime()-beg_time);

    beg_time = yak_realtime();
    hc_pt_t_gen(pl.h, NULL);
    fprintf(stderr, "[M::%s::%.3f] ==> Memory allocating\n", __func__, yak_realtime()-beg_time);

    beg_time = yak_realtime();
    pl.is_cnt = 0;
    parallel_count_hc_pt1(&pl);
    fprintf(stderr, "[M::%s::%.3f] ==> Filling pos\n", __func__, yak_realtime()-beg_time);

    beg_time = yak_realtime();
    hc_pt_t_gen(pl.h, &pl);
    fprintf(stderr, "[M::%s::%.3f] ==> Sorting pos\n", __func__, yak_realtime()-beg_time);

    fprintf(stderr, "[M::%s::%.3f] ==> HiC index has been built\n", __func__, yak_realtime()-index_time);
    
    uint64_t i;
    for (i = 0; i < idx->tot; i++)
    {
        kv_destroy(pl.cnt[i].a);
        kv_destroy(pl.buf[i].a);
    }
    free(pl.cnt); free(pl.buf);

    return idx;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x1, %edi
movl $0xc0, %esi
callq 0x2140
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x1, -0x60(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x68(%rbp)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0xc(%rbp), %edx
movq -0x18(%rbp), %r8
movq -0x20(%rbp), %r9
movq -0x28(%rbp), %rax
leaq -0x60(%rbp), %rcx
movq %rax, (%rsp)
callq 0x1bf390
callq 0x1b7ca0
movsd %xmm0, -0x70(%rbp)
movl $0x1, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x1c0860
movq 0x202519(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
callq 0x1b7ca0
movq -0xa0(%rbp), %rdi
subsd -0x70(%rbp), %xmm0
leaq 0x1ca48a(%rip), %rsi    # 0x38bf73
leaq 0x1ca49f(%rip), %rdx    # 0x38bf8f
movb $0x1, %al
callq 0x22a0
callq 0x1b7ca0
movsd %xmm0, -0x70(%rbp)
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1c0590
movq 0x2024cb(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x98(%rbp)
callq 0x1b7ca0
movq -0x98(%rbp), %rdi
subsd -0x70(%rbp), %xmm0
leaq 0x1ca46b(%rip), %rsi    # 0x38bfa2
leaq 0x1ca451(%rip), %rdx    # 0x38bf8f
movb $0x1, %al
callq 0x22a0
callq 0x1b7ca0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x1c0860
movq 0x20247a(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x90(%rbp)
callq 0x1b7ca0
movq -0x90(%rbp), %rdi
subsd -0x70(%rbp), %xmm0
leaq 0x1ca43f(%rip), %rsi    # 0x38bfc7
leaq 0x1ca400(%rip), %rdx    # 0x38bf8f
movb $0x1, %al
callq 0x22a0
callq 0x1b7ca0
movsd %xmm0, -0x70(%rbp)
movq -0x50(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1c0590
movq 0x20242c(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x88(%rbp)
callq 0x1b7ca0
movq -0x88(%rbp), %rdi
subsd -0x70(%rbp), %xmm0
leaq 0x1ca410(%rip), %rsi    # 0x38bfe6
leaq 0x1ca3b2(%rip), %rdx    # 0x38bf8f
movb $0x1, %al
callq 0x22a0
movq 0x2023f5(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x80(%rbp)
callq 0x1b7ca0
movq -0x80(%rbp), %rdi
subsd -0x68(%rbp), %xmm0
leaq 0x1ca3fe(%rip), %rsi    # 0x38c005
leaq 0x1ca381(%rip), %rdx    # 0x38bf8f
movb $0x1, %al
callq 0x22a0
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x58(%rcx), %rax
jae 0x1c1c63
movq -0x48(%rbp), %rax
imulq $0x18, -0x78(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
callq 0x2210
movq -0x40(%rbp), %rax
imulq $0x18, -0x78(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
callq 0x2210
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x1c1c1d
movq -0x48(%rbp), %rdi
callq 0x2210
movq -0x40(%rbp), %rdi
callq 0x2210
movq -0x30(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)