// File: /workspace/github_cpp_makefile_star2_O3/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 $0x168, %rsp            # imm = 0x168
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r12
testq %r9, %r9
je 0x163338
movq $0x0, (%r9)
movq %r9, 0x18(%rsp)
leaq 0x108(%rsp), %r14
movq %rbp, -0x38(%r14)
movq 0x10(%rbp), %rax
movq %rax, 0x58(%r14)
movq $0x1312d00, -0x20(%r14) # imm = 0x1312D00
leaq 0x1323f8(%rip), %r13    # 0x295758
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 0x146d08
testl %eax, %eax
jne 0x1633cd
leaq 0xd0(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x15d602
movq (%rbp), %rsi
movq 0x10(%r13), %rdx
movq %r14, %rdi
callq 0x1461bb
movq 0x18(%rbp), %rax
movq %rax, 0x150(%rsp)
movq 0x30(%rbp), %rax
movq %rax, 0x158(%rsp)
cmpq $0x0, 0x120(%rsp)
jne 0x1633fd
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x14547c
movq (%rbp), %rdi
movq 0x8(%rbp), %rsi
movq 0x8(%rbx), %rdx
callq 0x162618
movq (%rbp), %rdi
movq 0x10(%rbp), %rdx
movq 0x10(%rdx), %rsi
movl $0x138, %r12d           # imm = 0x138
addq %r12, %rdx
movq %rax, 0x8(%rsp)
movq %rax, %rcx
callq 0x162904
movq %rax, %r13
movq (%rbp), %rax
movq 0x10(%rbp), %rdx
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
movq %r14, %rbx
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andq %r14, %rsi
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
callq 0xbf70f
movq (%rbp), %rsi
addq 0x10(%rbp), %r12
movq %rbx, %rdx
movq %r15, %rcx
movq %r13, %rbx
movq %r13, %r8
movq %r12, %r9
callq 0x1456f7
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movq (%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %r13d
andq %r14, %r13
movl $0x1, %edi
movl $0x18, %esi
callq 0x2120
movq %rax, %r14
movq $0xb, 0x8(%rax)
movl $0x1, %esi
movq %r13, %rdi
callq 0x2120
movq %rax, 0x10(%rsp)
movq %rax, (%r14)
movq %r13, 0x10(%r14)
leaq 0x13227d(%rip), %rax    # 0x295758
movslq 0x1bc(%rax), %r12
movl $0x1, %esi
movq %r13, %rdi
callq 0x2120
movq %rax, %r15
shlq $0x2, %r13
movq %r13, %rdi
callq 0x2280
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
testq %r12, %r12
movq %rax, (%rsp)
je 0x163583
decq %r12
xorl %r13d, %r13d
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
leaq 0xa8(%rsp), %r8
leaq 0x110(%rsp), %r9
leaq 0x28(%rsp), %r10
pushq %r10
leaq 0x50(%rsp), %r10
pushq %r10
pushq %rax
pushq %r15
leaq 0x98(%rsp), %rax
pushq %rax
pushq %r14
pushq %rbx
callq 0x162eca
movq 0x40(%rsp), %rax
addq $0x40, %rsp
incq %r13
addq $-0x1, %r12
jb 0x16352d
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x163595
movq %rcx, (%rax)
movq 0x118(%rsp), %rdi
callq 0x2220
movq 0x130(%rsp), %rdi
callq 0x2220
movq 0x148(%rsp), %rdi
callq 0x2220
leaq 0xa0(%rsp), %rdi
callq 0xbf835
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x98(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x13dea7
movq %rbx, %rdi
callq 0x2220
movq 0x30(%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq (%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x68(%rsp), %rdi
callq 0x2220
movl $0x1, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq