// File: chhylp123[P]hifiasm/hic.cpp
// Function: hic_short_align_mmhap(enzyme const*, enzyme const*, ha_ug_index*, ug_opt_t*, kvec_pe_hit**, mmhap_t**)
int hic_short_align_mmhap(const enzyme *fn1, const enzyme *fn2, ha_ug_index* idx, ug_opt_t *opt, kvec_pe_hit **rhits, mmhap_t **rh)
{
    if(rh) (*rh) = NULL;
    sldat_t sl; 
    sl.idx = idx;
    sl.t_ch = idx->t_ch;
    sl.chunk_size = 20000000;
    sl.n_thread = asm_opt.thread_num;
    sl.total_base = sl.total_pair = 0;
    idx->hap_cnt = asm_opt.hap_occ;
    kv_init(sl.hits.a); kv_init(sl.hits.idx); kv_init(sl.hits.occ);


    if(!load_hc_hits(&sl.hits, idx->ug, asm_opt.output_file_name)) {
        alignment_worker_pipeline(&sl, fn1, fn2);
        write_hc_hits(&sl.hits, idx->ug, asm_opt.output_file_name);
    }
    sl.hits.uID_bits = idx->uID_bits; sl.hits.pos_mode = idx->pos_mode;

    if(sl.hits.idx.n == 0) idx_hc_links(&(sl.hits), idx, NULL);

    mmhap_t *hh = gen_mmhap_t(idx->ug, idx->read_g, opt->sources);
    bubble_type *bub = gen_mmhap_bub(idx->ug, idx->t_ch->ir_het, &(idx->t_ch->k_trans), hh);
    hc_links link; init_hc_links(&link, idx->ug->g->n_seq, idx->t_ch);
    measure_distance(idx, idx->ug, &sl.hits, &link, bub, &(idx->t_ch->k_trans));
    kv_u_trans_t k_trans; kv_init(k_trans); kv_init(k_trans.idx);
    ps_t *s = init_ps_t(11, idx->ug->g->n_seq); ///H_partition hap;
    uint64_t k, n_hap = asm_opt.polyploidy; asg64_v srt; kv_init(srt);
    uint8_t *ff; CALLOC(ff, idx->ug->g->n_seq);
    uint32_t *bidx; MALLOC(bidx, idx->ug->g->n_seq);
    kv_u_trans_t r_trans_buf; kv_init(r_trans_buf); kv_init(r_trans_buf.idx);

    for (k = 0; k < n_hap; k++) {
        purge_phase(idx, hh, k, ((n_hap>k)?(n_hap-k-1):(0)), &link, &sl.hits, bub, s, &k_trans, ff, bidx, &r_trans_buf, &srt);
    }
    if(rh) (*rh) = hh;

    // if(rhits) (*rhits) = get_r_hits_order(&sl.hits, idx->uID_bits, idx->pos_mode, idx->read_g, idx->ug, &bub);

    kv_destroy(sl.hits.a); kv_destroy(sl.hits.idx); kv_destroy(sl.hits.occ);
    destory_hc_links(&link);
    kv_destroy(k_trans); kv_destroy(k_trans.idx);
    destory_ps_t(&s); destory_bubbles(bub); free(bub);
    kv_destroy(srt); free(ff); free(bidx); 
    kv_destroy(r_trans_buf); kv_destroy(r_trans_buf.idx);
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %r8, -0x48(%rbp)
movq %r9, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x20d17c
movq -0x50(%rbp), %rax
movq $0x0, (%rax)
movq -0x38(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x1312d00, -0xd0(%rbp) # imm = 0x1312D00
leaq 0x1b7653(%rip), %rax    # 0x3c47f8
movslq 0x7c(%rax), %rax
movq %rax, -0xc8(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
leaq 0x1b762b(%rip), %rax    # 0x3c47f8
movslq 0x1b8(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x48(%rax)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
leaq -0xe8(%rbp), %rdi
addq $0x38, %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
leaq 0x1b75a9(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rdx
callq 0x1d5990
cmpl $0x0, %eax
jne 0x20d293
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0xe8(%rbp), %rdi
callq 0x200e50
leaq -0xe8(%rbp), %rdi
addq $0x38, %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
leaq 0x1b756e(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rdx
callq 0x1d44d0
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0x20d2cd
leaq -0xe8(%rbp), %rdi
addq $0x38, %rdi
movq -0x38(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1d2bc0
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x20b6a0
movq %rax, -0xf0(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
addq $0x138, %rdx            # imm = 0x138
movq -0xf0(%rbp), %rcx
callq 0x20bab0
movq %rax, -0xf8(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %esi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
leaq -0x128(%rbp), %rdi
callq 0xfca90
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
leaq -0xe8(%rbp), %rdx
addq $0x38, %rdx
movq -0xf8(%rbp), %r8
movq -0x38(%rbp), %rax
movq 0x10(%rax), %r9
addq $0x138, %r9             # imm = 0x138
leaq -0x128(%rbp), %rcx
callq 0x1d30a0
movq $0x0, -0x150(%rbp)
movq $0x0, -0x158(%rbp)
movq $0x0, -0x148(%rbp)
movq $0x0, -0x138(%rbp)
movq $0x0, -0x140(%rbp)
movq $0x0, -0x130(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %esi
movl $0xb, %edi
callq 0x204880
movq %rax, -0x160(%rbp)
leaq 0x1b73ff(%rip), %rax    # 0x3c47f8
movslq 0x1bc(%rax), %rax
movq %rax, -0x170(%rbp)
movq $0x0, -0x180(%rbp)
movq $0x0, -0x188(%rbp)
movq $0x0, -0x178(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x190(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x2270
movq %rax, -0x198(%rbp)
movq $0x0, -0x1c0(%rbp)
movq $0x0, -0x1c8(%rbp)
movq $0x0, -0x1b8(%rbp)
movq $0x0, -0x1a8(%rbp)
movq $0x0, -0x1b0(%rbp)
movq $0x0, -0x1a0(%rbp)
movq $0x0, -0x168(%rbp)
movq -0x168(%rbp), %rax
cmpq -0x170(%rbp), %rax
jae 0x20d5d2
movq -0x38(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x168(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x170(%rbp), %rax
cmpq -0x168(%rbp), %rax
jbe 0x20d52a
movq -0x170(%rbp), %rax
subq -0x168(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x1e8(%rbp)
jmp 0x20d535
xorl %eax, %eax
movq %rax, -0x1e8(%rbp)
jmp 0x20d535
movq -0x1d0(%rbp), %rdx
movq -0x1d8(%rbp), %rsi
movq -0x1e0(%rbp), %rdi
movq -0x1e8(%rbp), %rcx
leaq -0xe8(%rbp), %r9
addq $0x38, %r9
movq -0xf8(%rbp), %r12
movq -0x160(%rbp), %r15
movq -0x190(%rbp), %rbx
movq -0x198(%rbp), %r11
leaq -0x128(%rbp), %r8
leaq -0x158(%rbp), %r14
leaq -0x1c8(%rbp), %r10
leaq -0x188(%rbp), %rax
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x20ca10
movq -0x168(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x168(%rbp)
jmp 0x20d4c4
cmpq $0x0, -0x50(%rbp)
je 0x20d5e7
movq -0xf0(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0xa0(%rbp), %rdi
callq 0x2210
movq -0x88(%rbp), %rdi
callq 0x2210
movq -0x70(%rbp), %rdi
callq 0x2210
leaq -0x128(%rbp), %rdi
callq 0xfcce0
movq -0x148(%rbp), %rdi
callq 0x2210
movq -0x130(%rbp), %rdi
callq 0x2210
leaq -0x160(%rbp), %rdi
callq 0x2048f0
movq -0xf8(%rbp), %rdi
callq 0x1c4a90
movq -0xf8(%rbp), %rdi
callq 0x2210
movq -0x178(%rbp), %rdi
callq 0x2210
movq -0x190(%rbp), %rdi
callq 0x2210
movq -0x198(%rbp), %rdi
callq 0x2210
movq -0x1b8(%rbp), %rdi
callq 0x2210
movq -0x1a0(%rbp), %rdi
callq 0x2210
movl $0x1, %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)