// File: /workspace/github_cpp_makefile_star2_O2/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 $0x188, %rsp            # imm = 0x188
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r12
testq %r9, %r9
je 0x11e13a
andq $0x0, (%r9)
movq %r9, 0x20(%rsp)
leaq 0xc0(%rsp), %rdi
movq %rbp, -0x38(%rdi)
movq 0x10(%rbp), %rax
movq %rax, 0x58(%rdi)
movq $0x1312d00, -0x20(%rdi) # imm = 0x1312D00
leaq 0x1355f6(%rip), %r14    # 0x253758
movslq 0x7c(%r14), %rax
movq %rax, -0x18(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, -0x10(%rdi)
movslq 0x1b8(%r14), %rax
movq %rax, 0x48(%rbp)
movups %xmm0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
andq $0x0, 0x40(%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
movq (%rbp), %rsi
movq 0x10(%r14), %rdx
callq 0xff599
testl %eax, %eax
jne 0x11e1c9
leaq 0x88(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x11718b
movq (%rbp), %rsi
movq 0x10(%r14), %rdx
leaq 0xc0(%rsp), %rdi
callq 0xfeae5
movq 0x18(%rbp), %rax
movq %rax, 0x108(%rsp)
movq 0x30(%rbp), %rax
movq %rax, 0x110(%rsp)
cmpq $0x0, 0xd8(%rsp)
jne 0x11e1fe
leaq 0xc0(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0xfdcd5
movq (%rbp), %rdi
movq 0x8(%rbp), %rsi
movq 0x8(%rbx), %rdx
callq 0x11d356
movq (%rbp), %rdi
movq 0x10(%rbp), %rdx
movq 0x10(%rdx), %rsi
movl $0x138, %ebx            # imm = 0x138
addq %rbx, %rdx
movq %rax, 0x8(%rsp)
movq %rax, %rcx
callq 0x11d5ac
movq %rax, %r12
movq (%rbp), %rax
movq 0x10(%rbp), %rdx
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
andq %r15, %rsi
leaq 0x158(%rsp), %r13
movq %r13, %rdi
callq 0x83ca2
movq (%rbp), %rsi
addq 0x10(%rbp), %rbx
leaq 0xc0(%rsp), %rdx
movq %r13, %rcx
movq %r12, 0x18(%rsp)
movq %r12, %r8
movq %rbx, %r9
callq 0xfdf3d
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rax
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq (%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
andq %r15, %rsi
pushq $0xb
popq %rdi
callq 0x1193f3
movq %rax, %rbx
movq %rax, 0x28(%rsp)
movslq 0x1bc(%r14), %r12
leaq 0x30(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rbp, %r13
movq (%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %r14d
andq %r15, %r14
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, 0x10(%rsp)
shlq $0x2, %r14
movq %r14, %rdi
callq 0x2280
movq %rax, %r15
leaq 0x120(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
decq %r12
xorl %r14d, %r14d
cmpq $-0x1, %r12
je 0x11e365
subq $0x8, %rsp
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
leaq 0x160(%rsp), %r8
leaq 0xc8(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
pushq %rbp
pushq %r15
pushq 0x30(%rsp)
leaq 0x78(%rsp), %rax
pushq %rax
pushq %rbx
pushq 0x50(%rsp)
callq 0x11dd31
addq $0x40, %rsp
incq %r14
decq %r12
jmp 0x11e314
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x11e377
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
movq 0xd0(%rsp), %rdi
callq 0x2220
movq 0xe8(%rsp), %rdi
callq 0x2220
movq 0x100(%rsp), %rdi
callq 0x2220
leaq 0x158(%rsp), %rdi
callq 0x83da5
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
leaq 0x28(%rsp), %rdi
callq 0x11943a
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0xf6a60
movq %rbx, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq 0x130(%rsp), %rdi
callq 0x2220
movq 0x148(%rsp), %rdi
callq 0x2220
pushq $0x1
popq %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq