// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: hic_short_align_poy(enzyme const*, enzyme const*, ha_ug_index*, ug_opt_t*)
int hic_short_align_poy(const enzyme *fn1, const enzyme *fn2, ha_ug_index* idx, ug_opt_t *opt)
{
    double index_time = yak_realtime();
    sldat_t sl;
    kvec_hc_edge back_hc_edge;
    kv_init(back_hc_edge.a);
    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);
    }

    hc_links link;
    init_hc_links(&link, idx->ug->g->n_seq, idx->t_ch);
    ///H_partition hap;
    bubble_type bub; 
    kv_u_trans_t k_trans; 
    kv_init(k_trans); kv_init(k_trans.idx);
    psg_t *s = NULL;
    mb_nodes_t u; 
    kv_init(u.bid); kv_init(u.idx); kv_init(u.u);
    memset(&bub, 0, sizeof(bubble_type));
    bub.round_id = 0; bub.n_round = asm_opt.n_weight;

    resolve_tangles_hic(idx, &bub, &sl.hits, &k_trans);
    measure_distance(idx, idx->ug, &sl.hits, &link, &bub, &(idx->t_ch->k_trans));
    if((asm_opt.flag & HA_F_VERBOSE_GFA) && load_psg_t(&s, asm_opt.output_file_name))
    {
        bub.round_id = bub.n_round;
        goto skip_flipping;
    }
    s = init_psg_t(11, idx->ug, idx->read_g, opt);
    for (bub.round_id = 0; bub.round_id < bub.n_round; bub.round_id++)
    {
        // identify_bubbles(idx->ug, &bub, idx->t_ch->is_r_het, &(idx->t_ch->k_trans));
        renew_kv_u_trans(&k_trans, &link, &sl.hits, &(idx->t_ch->k_trans), idx, &bub, NULL, s->sg.a, 0);
        // if(bub.round_id == 0) init_phase(idx, &k_trans, &bub, s); 
        // update_trans_g(idx, &k_trans, &bub);
        /*******************************for debug************************************/
        mc_solve_general(&k_trans, idx->ug->u.n, &(s->sg), asm_opt.polyploidy, 0, 1);
        /*******************************for debug************************************/

        /*******************************for debug************************************/
        // if(bub.round_id == bub.n_round - 1)
        // {
        //     debug_output_disconnected_hits(idx, &k_trans, &sl.hits, &link, &bub, s->s);
        // }
        /*******************************for debug************************************/
    }
    if((asm_opt.flag & HA_F_VERBOSE_GFA)) write_psg_t(s, asm_opt.output_file_name);

    skip_flipping:
    verbose_het_stat(&bub);
    
    if(asm_opt.polyploidy == 2) label_unitigs_sm(NULL, s->sg.a, idx->ug);

    // print_kv_weight(&k_trans);

    // horder_t *ho = init_horder_t(&sl.hits, idx->uID_bits, idx->pos_mode, idx->read_g, idx->ug, &bub, &(idx->t_ch->k_trans), opt, 3);

    ///print_hc_links(&link, 0, &hap);
    // print_kv_u_trans(&k_trans, &link, s->s);


    ///print_bubbles(idx->ug, &bub, sl.hits.a.n?&sl.hits:NULL, idx->link, idx);
    ///print_hits(idx, &sl.hits, fn1);
    

    ///print_debug_bubble_graph(&bub, idx->ug, asm_opt.output_file_name);
    // print_bubble_chain(&bub);
    // destory_contig_partition(&hap);
    // destory_horder_t(&ho);
    kv_destroy(back_hc_edge.a);
    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_psg_t(&s);
    kv_destroy(u.bid); kv_destroy(u.idx); kv_destroy(u.u);
    fprintf(stderr, "[M::%s::%.3f] processed %lu pairs; %lu bases\n", __func__, yak_realtime()-index_time, sl.total_pair, sl.total_base);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r13
callq 0x1303c7
movsd %xmm0, 0x18(%rsp)
leaq 0xb8(%rsp), %r14
movq %rbx, -0x38(%r14)
movq 0x10(%rbx), %rax
movq %rax, 0x58(%r14)
movq $0x1312d00, -0x20(%r14) # imm = 0x1312D00
leaq 0x26e745(%rip), %rbp    # 0x28a758
movslq 0x7c(%rbp), %rax
movq %rax, -0x18(%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x10(%r14)
movslq 0x1b8(%rbp), %rax
movq %rax, 0x48(%rbx)
movupd %xmm0, 0x30(%r14)
movupd %xmm0, 0x20(%r14)
andq $0x0, 0x40(%r14)
movupd %xmm0, 0x10(%r14)
movupd %xmm0, (%r14)
movq (%rbx), %rsi
movq 0x10(%rbp), %rdx
movq %r14, %rdi
callq 0x14107b
testl %eax, %eax
jne 0x1c081
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x156cbb
movq (%rbx), %rsi
movq 0x10(%rbp), %rdx
movq %r14, %rdi
callq 0x14050c
movq (%rbx), %rax
movq 0x10(%rbx), %rdx
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leaq 0x50(%rsp), %r15
movq %r15, %rdi
callq 0xbe7d2
leaq 0x20(%rsp), %r12
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%r12)
movapd %xmm0, 0x10(%r12)
movapd %xmm0, (%r12)
andq $0x0, 0x8(%rsp)
leaq 0x118(%rsp), %r13
movl $0x110, %edx            # imm = 0x110
movq %r13, %rdi
xorl %esi, %esi
callq 0x20e0
movl 0x1e0(%rbp), %eax
movl %eax, 0xc(%r13)
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x15983d
movq (%rbx), %rsi
movl $0x138, %r9d            # imm = 0x138
addq 0x10(%rbx), %r9
movq %r14, %r12
movq %r14, %rdx
movq %r15, %rcx
movq %r13, %r8
callq 0x13fa9b
testb $0x4, (%rbp)
je 0x1c131
movq 0x10(%rbp), %rsi
leaq 0x8(%rsp), %rdi
callq 0x15b97f
testl %eax, %eax
jne 0x1c1eb
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
pushq $0xb
popq %rdi
movq 0x10(%rsp), %rcx
callq 0x15bb08
andl $0x0, 0x120(%rsp)
movq %rax, %r15
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x124(%rsp)
je 0x1c1d7
movq %r12, %r13
leaq 0x20(%rsp), %r12
pushq $0x1
popq %r14
movq 0x10(%rbx), %rcx
movl $0x138, %eax            # imm = 0x138
addq %rax, %rcx
subq $0x8, %rsp
movq %r12, %rdi
leaq 0x58(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %r8
leaq 0x120(%rsp), %r9
pushq $0x0
pushq (%r15)
pushq $0x0
callq 0x1aa50
addq $0x20, %rsp
movq (%rbx), %rax
movl (%rax), %esi
movzwl 0x1bc(%rbp), %ecx
movq %r12, %rdi
movq %r15, %rdx
xorl %r8d, %r8d
movl %r14d, %r9d
callq 0x16ae05
movl 0x120(%rsp), %eax
incl %eax
movl %eax, 0x120(%rsp)
cmpl 0x124(%rsp), %eax
jb 0x1c16b
testb $0x4, (%rbp)
je 0x1c1f9
movq 0x10(%rbp), %rsi
movq %r15, %rdi
callq 0x15ba69
jmp 0x1c1f9
movl 0x124(%rsp), %eax
movl %eax, 0x120(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x1abf0
cmpl $0x2, 0x1bc(%rbp)
jne 0x1c221
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq (%rbx), %rdx
xorl %edi, %edi
callq 0x155c54
movq 0xc8(%rsp), %rdi
callq 0x2220
movq 0xe0(%rsp), %rdi
callq 0x2220
movq 0xf8(%rsp), %rdi
callq 0x2220
leaq 0x50(%rsp), %rdi
callq 0xbe8f4
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq 0x26dd5e(%rip), %rax    # 0x289fe0
movq (%rax), %rbx
callq 0x1303c7
subsd 0x18(%rsp), %xmm0
movq 0xa8(%rsp), %r8
movq 0xb0(%rsp), %rcx
leaq 0x214526(%rip), %rsi    # 0x2307cd
leaq 0x21454d(%rip), %rdx    # 0x2307fb
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
pushq $0x1
popq %rax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq