// File: 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
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x28(%rbp)
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xd8(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x1312d00, -0xa8(%rbp) # imm = 0x1312D00
leaq 0x1b95f9(%rip), %rax    # 0x3c47f8
movslq 0x7c(%rax), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
leaq 0x1b95d1(%rip), %rax    # 0x3c47f8
movslq 0x1b8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x48(%rbp)
leaq -0xc0(%rbp), %rdi
addq $0x38, %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
leaq 0x1b955e(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rdx
callq 0x1d5990
cmpl $0x0, %eax
jne 0x20b2de
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0x200e50
leaq -0xc0(%rbp), %rdi
addq $0x38, %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
leaq 0x1b9523(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rdx
callq 0x1d44d0
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %esi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
leaq -0x108(%rbp), %rdi
callq 0xfca90
movq $0x0, -0x240(%rbp)
movq $0x0, -0x248(%rbp)
movq $0x0, -0x238(%rbp)
movq $0x0, -0x228(%rbp)
movq $0x0, -0x230(%rbp)
movq $0x0, -0x220(%rbp)
movq $0x0, -0x250(%rbp)
movq $0x0, -0x290(%rbp)
movq $0x0, -0x298(%rbp)
movq $0x0, -0x288(%rbp)
movq $0x0, -0x278(%rbp)
movq $0x0, -0x280(%rbp)
movq $0x0, -0x270(%rbp)
movq $0x0, -0x260(%rbp)
movq $0x0, -0x268(%rbp)
movq $0x0, -0x258(%rbp)
leaq -0x218(%rbp), %rdi
xorl %esi, %esi
movl $0x110, %edx            # imm = 0x110
callq 0x2100
movl $0x0, -0x210(%rbp)
leaq 0x1b941b(%rip), %rax    # 0x3c47f8
movl 0x1e0(%rax), %eax
movl %eax, -0x20c(%rbp)
movq -0x18(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
addq $0x38, %rdx
leaq -0x218(%rbp), %rsi
leaq -0x248(%rbp), %rcx
callq 0x205720
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
leaq -0xc0(%rbp), %rdx
addq $0x38, %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %r9
addq $0x138, %r9             # imm = 0x138
leaq -0x108(%rbp), %rcx
leaq -0x218(%rbp), %r8
callq 0x1d30a0
leaq 0x1b93ae(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x20b481
leaq 0x1b939d(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rsi
leaq -0x250(%rbp), %rdi
callq 0x20add0
cmpl $0x0, %eax
je 0x20b481
movl -0x20c(%rbp), %eax
movl %eax, -0x210(%rbp)
jmp 0x20b594
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x20(%rbp), %rcx
movl $0xb, %edi
callq 0x20b010
movq %rax, -0x250(%rbp)
movl $0x0, -0x210(%rbp)
movl -0x210(%rbp), %eax
cmpl -0x20c(%rbp), %eax
jae 0x20b56a
leaq -0xc0(%rbp), %rdx
addq $0x38, %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x138, %rcx            # imm = 0x138
movq -0x18(%rbp), %r8
movq -0x250(%rbp), %rax
movq (%rax), %rax
leaq -0x248(%rbp), %rdi
leaq -0x108(%rbp), %rsi
leaq -0x218(%rbp), %r9
xorl %r10d, %r10d
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x2043f0
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movl %eax, %esi
movq -0x250(%rbp), %rdx
leaq 0x1b92c0(%rip), %rax    # 0x3c47f8
movl 0x1bc(%rax), %eax
leaq -0x248(%rbp), %rdi
xorl %r8d, %r8d
movl $0x1, %r9d
movzwl %ax, %ecx
callq 0x22ad50
movl -0x210(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x210(%rbp)
jmp 0x20b4af
leaq 0x1b9287(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x20b592
movq -0x250(%rbp), %rdi
leaq 0x1b926f(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rsi
callq 0x20af40
jmp 0x20b594
leaq -0x218(%rbp), %rdi
callq 0x204f60
leaq 0x1b9251(%rip), %rax    # 0x3c47f8
cmpl $0x2, 0x1bc(%rax)
jne 0x20b5ca
movq -0x250(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
xorl %eax, %eax
movl %eax, %edi
callq 0x1ff3d0
movq -0xc8(%rbp), %rdi
callq 0x2210
movq -0x78(%rbp), %rdi
callq 0x2210
movq -0x60(%rbp), %rdi
callq 0x2210
movq -0x48(%rbp), %rdi
callq 0x2210
leaq -0x108(%rbp), %rdi
callq 0xfcce0
movq -0x238(%rbp), %rdi
callq 0x2210
movq -0x220(%rbp), %rdi
callq 0x2210
leaq -0x250(%rbp), %rdi
callq 0x20b160
movq -0x288(%rbp), %rdi
callq 0x2210
movq -0x270(%rbp), %rdi
callq 0x2210
movq -0x258(%rbp), %rdi
callq 0x2210
movq 0x1b8994(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x2a0(%rbp)
callq 0x1b7ca0
movq -0x2a0(%rbp), %rdi
subsd -0x28(%rbp), %xmm0
movq -0x90(%rbp), %rcx
movq -0x98(%rbp), %r8
leaq 0x181fbe(%rip), %rsi    # 0x38d63a
leaq 0x181fe5(%rip), %rdx    # 0x38d668
movb $0x1, %al
callq 0x22a0
movl $0x1, %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
nopl (%rax,%rax)