// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r12
testq %r9, %r9
je 0x15c875
movq $0x0, (%r9)
movq %r9, 0x28(%rsp)
leaq 0x118(%rsp), %r14
movq %rbp, -0x38(%r14)
movq 0x10(%rbp), %rax
movq %rax, 0x58(%r14)
movq $0x1312d00, -0x20(%r14) # imm = 0x1312D00
leaq 0x12debb(%rip), %r13    # 0x28a758
movslq 0x7c(%r13), %rax
movq %rax, -0x18(%r14)
xorps %xmm0, %xmm0
movups %xmm0, -0x10(%r14)
movslq 0x1b8(%r13), %rax
movq %rax, 0x48(%rbp)
movups %xmm0, 0x30(%r14)
movups %xmm0, 0x20(%r14)
movups %xmm0, 0x10(%r14)
movups %xmm0, (%r14)
movq $0x0, 0x40(%r14)
movq (%rbp), %rsi
movq 0x10(%r13), %rdx
movq %r14, %rdi
callq 0x14107b
testl %eax, %eax
jne 0x15c90a
leaq 0xe0(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x156cbb
movq (%rbp), %rsi
movq 0x10(%r13), %rdx
movq %r14, %rdi
callq 0x14050c
movq 0x18(%rbp), %rax
movq %rax, 0x160(%rsp)
movq 0x30(%rbp), %rax
movq %rax, 0x168(%rsp)
cmpq $0x0, 0x130(%rsp)
jne 0x15c93a
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x13f81d
movq (%rbp), %rdi
movq 0x8(%rbp), %rsi
movq 0x8(%rbx), %rdx
callq 0x15bbd8
movq (%rbp), %rdi
movq 0x10(%rbp), %rdx
movq 0x10(%rdx), %rsi
movl $0x138, %r15d           # imm = 0x138
addq %r15, %rdx
movq %rax, 0x18(%rsp)
movq %rax, %rcx
callq 0x15be3b
movq %rax, %r12
movq (%rbp), %rax
movq 0x10(%rbp), %rdx
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
andq %r13, %rsi
movq %r14, %rbx
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
callq 0xbe7d2
movq (%rbp), %rsi
addq 0x10(%rbp), %r15
movq %rbx, %rdx
movq %r14, %rcx
movq %r12, 0x10(%rsp)
movq %r12, %r8
movq %r15, %r9
callq 0x13fa9b
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movq (%rbp), %rax
movq 0x18(%rax), %r14
movl 0x14(%r14), %r12d
andq %r13, %r12
movl $0x1, %edi
movl $0x18, %esi
callq 0x2120
movq %rax, %r15
movq $0xb, 0x8(%rax)
movl $0x1, %esi
movq %r12, %rdi
callq 0x2120
movq %rax, 0x20(%rsp)
movq %rax, (%r15)
movq %r12, 0x10(%r15)
leaq 0x12dd3b(%rip), %rax    # 0x28a758
movslq 0x1bc(%rax), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movl 0x14(%r14), %ebx
andq %r13, %rbx
movl $0x1, %esi
movq %rbx, %rdi
callq 0x2120
movq %rax, %r14
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x2280
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
testq %r12, %r12
movq %rax, 0x8(%rsp)
je 0x15cad6
decq %r12
xorl %r13d, %r13d
leaq 0xb0(%rsp), %rbx
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
movq %rbx, %r8
leaq 0x120(%rsp), %r9
leaq 0x38(%rsp), %r10
pushq %r10
leaq 0x60(%rsp), %r10
pushq %r10
pushq %rax
pushq %r14
leaq 0xa8(%rsp), %rax
pushq %rax
pushq %r15
pushq 0x48(%rsp)
callq 0x15c426
movq 0x48(%rsp), %rax
addq $0x40, %rsp
incq %r13
addq $-0x1, %r12
jb 0x15ca82
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x15caed
movq %rcx, (%rax)
movq 0x128(%rsp), %rdi
callq 0x2220
movq 0x140(%rsp), %rdi
callq 0x2220
movq 0x158(%rsp), %rdi
callq 0x2220
leaq 0xb0(%rsp), %rdi
callq 0xbe8f4
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0xa8(%rsp), %rdi
callq 0x2220
movq 0x20(%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x1380d4
movq %rbx, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
movl $0x1, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq