// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdi, %r12
callq 0xefea7
movsd %xmm0, 0x18(%rsp)
leaq 0xb8(%rsp), %r15
movq %rbx, -0x38(%r15)
movq 0x10(%rbx), %rax
movq %rax, 0x58(%r15)
movq $0x1312d00, -0x20(%r15) # imm = 0x1312D00
leaq 0x1366c8(%rip), %r13    # 0x253758
movslq 0x7c(%r13), %rax
movq %rax, -0x18(%r15)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x10(%r15)
movslq 0x1b8(%r13), %rax
movq %rax, 0x48(%rbx)
movupd %xmm0, 0x30(%r15)
movupd %xmm0, 0x20(%r15)
andq $0x0, 0x40(%r15)
movupd %xmm0, 0x10(%r15)
movupd %xmm0, (%r15)
movq (%rbx), %rsi
movq 0x10(%r13), %rdx
movq %r15, %rdi
callq 0xff599
testl %eax, %eax
jne 0x11d0fe
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x11718b
movq (%rbx), %rsi
movq 0x10(%r13), %rdx
movq %r15, %rdi
callq 0xfeae5
movq (%rbx), %rax
movq 0x10(%rbx), %rdx
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x83ca2
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), %rbp
xorl %r13d, %r13d
movl $0x110, %edx            # imm = 0x110
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
leaq 0x1365f5(%rip), %rax    # 0x253758
movl 0x1e0(%rax), %eax
movl %eax, 0xc(%rbp)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x119c12
movq (%rbx), %rsi
movl $0x138, %eax            # imm = 0x138
movq 0x10(%rbx), %r9
addq %rax, %r9
movq %r15, %rdx
movq %r14, %rcx
movq %rbp, %r8
leaq 0x1365bc(%rip), %r14    # 0x253758
callq 0xfdf3d
testb $0x4, (%r14)
je 0x11d1bd
movq 0x10(%r14), %rsi
leaq 0x8(%rsp), %rdi
callq 0x11cdea
testl %eax, %eax
jne 0x11d27e
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
pushq $0xb
popq %rdi
movq 0x10(%rsp), %rcx
callq 0x11cf77
movq %rax, %r15
movq %rax, 0x8(%rsp)
leaq 0x20(%rsp), %r12
leaq 0x118(%rsp), %rbp
pushq $0x1
popq %r14
movl %r13d, 0x120(%rsp)
cmpl 0x124(%rsp), %r13d
jae 0x11d263
movq 0x10(%rbx), %rcx
movl $0x138, %eax            # imm = 0x138
addq %rax, %rcx
subq $0x8, %rsp
movq %r12, %rdi
leaq 0x58(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
movq %rbx, %r8
movq %rbp, %r9
pushq $0x0
pushq (%r15)
pushq $0x0
callq 0x1191a6
addq $0x20, %rsp
movq (%rbx), %rax
movl (%rax), %esi
leaq 0x13651a(%rip), %rax    # 0x253758
movzwl 0x1bc(%rax), %ecx
movq %r12, %rdi
movq %r15, %rdx
xorl %r8d, %r8d
movl %r14d, %r9d
callq 0x12d800
movl 0x120(%rsp), %r13d
incl %r13d
jmp 0x11d1ea
leaq 0x1364ee(%rip), %r14    # 0x253758
testb $0x4, (%r14)
je 0x11d28c
movq 0x10(%r14), %rsi
movq %r15, %rdi
callq 0x11ced4
jmp 0x11d28c
movl 0x124(%rsp), %eax
movl %eax, 0x120(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x1197d6
cmpl $0x2, 0x1bc(%r14)
jne 0x11d2b5
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq (%rbx), %rdx
xorl %edi, %edi
callq 0x116189
movq 0xc8(%rsp), %rdi
callq 0x2220
movq 0xe0(%rsp), %rdi
callq 0x2220
movq 0xf8(%rsp), %rdi
callq 0x2220
leaq 0x50(%rsp), %rdi
callq 0x83da5
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
callq 0x2220
leaq 0x8(%rsp), %rdi
callq 0x11d02b
movq 0x135cd5(%rip), %rax    # 0x252fe0
movq (%rax), %rbx
callq 0xefea7
subsd 0x18(%rsp), %xmm0
movq 0xa8(%rsp), %r8
movq 0xb0(%rsp), %rcx
leaq 0xd1599(%rip), %rsi     # 0x1ee8c9
leaq 0xd15c0(%rip), %rdx     # 0x1ee8f7
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