// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: ul_raw_aln(overlap_region*, Candidates_list*, ul_idx_t const*, char*, UC_Read*, bit_extz_t*, double, long, unsigned long, unsigned long, unsigned long, overlap_region*)
int64_t ul_raw_aln(overlap_region *z, Candidates_list *cl, const ul_idx_t *udb, char* qstr, UC_Read *tu, 
bit_extz_t *exz, double e_rate, int64_t w_l, uint64_t ql, uint64_t rid, uint64_t khit, overlap_region *aux_o)
{   
    return_t_chain(z, cl);
    int64_t ch_idx = z->shared_seed, ch_n;
    int64_t i, tl, id = z->y_id, m; ul_ov_t ov;
    k_mer_hit *ch_a = cl->list + ch_idx;
    tl = udb->ug->u.a[id].len;
    for (i = ch_idx; i < cl->length && cl->list[i].readID == cl->list[ch_idx].readID; i++); ch_n = i-ch_idx;

    // if(z->x_id == 57 && z->y_id == 2175) {
    //     fprintf(stderr, "\n-*-[M::%s]\tutg%.6u%c\txl::%u\tx::[%u,\t%u)\t%c\tutg%.6u%c\tyl::%u\ty::[%u,\t%u)\tch_idx::%ld\tch_n::%ld\n", 
    //         __func__, 
	// 		z->x_id+1, "lc"[udb->ug->u.a[z->x_id].circ], udb->ug->u.a[z->x_id].len, z->x_pos_s, z->x_pos_e+1,
	// 		"+-"[z->y_pos_strand],
	// 		z->y_id+1, "lc"[udb->ug->u.a[z->y_id].circ], udb->ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1,
    //         ch_idx, ch_n);
    //     for (i = ch_idx; i < cl->length && cl->list[i].readID == cl->list[ch_idx].readID; i++) {
    //         fprintf(stderr, "i::%ld[M::%s]\treadID::%u\tself_offset::%u\toffset::%u\t%c\n", 
    //             i, __func__, cl->list[i].readID, cl->list[i].self_offset, cl->list[i].offset, 
    //             "+-"[cl->list[i].strand]);
    //     }
    // }

    // on = fusion_chain_ovlp(z, ch_a, ch_n, ov, on, wl, ql, tl);
    aux_o->w_list.n = aux_o->w_list.c.n = 0; 
    aux_o->y_id = z->y_id; aux_o->y_pos_strand = z->y_pos_strand;
    aux_o->x_pos_s = z->x_pos_s; aux_o->x_pos_e = z->x_pos_e;
    aux_o->y_pos_s = z->y_pos_s; aux_o->y_pos_e = z->y_pos_e;

    memset(&ov, 0, sizeof(ov)); ov.sec = 3;
    ov.ts = ov.te = (uint32_t)-1;
    ov.qs = 0; ov.qn = (uint32_t)-1; //extension to left
    ov.qe = ql; ov.tn = ch_n; //extension to right

    ovlp_base_direct(z, ch_a, ch_n, &ov, w_l, udb, NULL, NULL, qstr, tu, exz, aux_o, e_rate, ql, tl, rid);

    int64_t aux_n = aux_o->w_list.n;


    // if(z->x_id == 57 && z->y_id == 2175) {
    //     fprintf(stderr, "\n-0-[M::%s]\tutg%.6u%c\txl::%u\tx::[%u,\t%u)\t%c\tutg%.6u%c\tyl::%u\ty::[%u,\t%u)\n", 
    //         __func__, 
	// 		z->x_id+1, "lc"[udb->ug->u.a[z->x_id].circ], udb->ug->u.a[z->x_id].len, z->x_pos_s, z->x_pos_e+1,
	// 		"+-"[z->y_pos_strand],
	// 		z->y_id+1, "lc"[udb->ug->u.a[z->y_id].circ], udb->ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1);
    //     for (i = 0; i < aux_n; i++) {
    //         window_list *m = &(aux_o->w_list.a[i]);
    //         fprintf(stderr, "i::%ld[M::%s]\tutg%.6u%c\twx::[%u,\t%u)\t%c\tutg%.6u%c\twy::[%u,\t%u)\terr::%d\tualn::%u\test::%u\n", i, __func__, 
	// 			z->x_id+1, "lc"[udb->ug->u.a[z->x_id].circ], m->x_start, m->x_end+1,
	// 			"+-"[z->y_pos_strand], 
	// 			z->y_id+1, "lc"[udb->ug->u.a[z->y_id].circ], m->y_start, m->y_end+1, m->error, 
	// 			(is_ualn_win((*m))), (is_est_aln((*m))));
    //     }
    //     fprintf(stderr, "\n");
    // }
    

    for (i = 0; i < aux_n; i++) {
        if(!(is_ualn_win(aux_o->w_list.a[i]))) continue;
        //will overwrite ch_a; does not matter
        rechain_aln(z, cl, aux_o, i, w_l, udb, NULL, NULL, qstr, tu, exz, e_rate, ql, tl, khit, rid);
    }

    // if(z->x_id == 57 && z->y_id == 2175) {
    //     fprintf(stderr, "\n-1-[M::%s]\tutg%.6u%c\txl::%u\tx::[%u,\t%u)\t%c\tutg%.6u%c\tyl::%u\ty::[%u,\t%u)\n", 
    //         __func__, 
	// 		z->x_id+1, "lc"[udb->ug->u.a[z->x_id].circ], udb->ug->u.a[z->x_id].len, z->x_pos_s, z->x_pos_e+1,
	// 		"+-"[z->y_pos_strand],
	// 		z->y_id+1, "lc"[udb->ug->u.a[z->y_id].circ], udb->ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1);
    //     for (i = 0; i < ((int64_t)aux_o->w_list.n); i++) {
    //         window_list *m = &(aux_o->w_list.a[i]);
    //         fprintf(stderr, "i::%ld[M::%s]\tutg%.6u%c\twx::[%u,\t%u)\t%c\tutg%.6u%c\twy::[%u,\t%u)\terr::%d\tualn::%u\test::%u\n", i, __func__, 
	// 			z->x_id+1, "lc"[udb->ug->u.a[z->x_id].circ], m->x_start, m->x_end+1,
	// 			"+-"[z->y_pos_strand], 
	// 			z->y_id+1, "lc"[udb->ug->u.a[z->y_id].circ], m->y_start, m->y_end+1, m->error, 
	// 			(is_ualn_win((*m))), (is_est_aln((*m))));
    //     }
    //     fprintf(stderr, "\n");
    // }
    
    if(((int64_t)aux_o->w_list.n) > aux_n) {
        for (i = m = 0; i < ((int64_t)aux_o->w_list.n); i++) {
            if((i < aux_n) && (is_ualn_win(aux_o->w_list.a[i]))) continue;
            aux_o->w_list.a[m++] = aux_o->w_list.a[i];
        }
        aux_o->w_list.n = m;
        radix_sort_window_list_xs_srt(aux_o->w_list.a, aux_o->w_list.a+aux_o->w_list.n);
    }

    ///update z by aux_o
    update_overlap_region(z, aux_o, ql, tl);
    assert((z->x_pos_e>=z->x_pos_s) && (z->y_pos_e>=z->y_pos_s));

    int64_t zwn = z->w_list.n, zerr = 0, zlen = z->x_pos_e+1-z->x_pos_s;
    for (i = 0; i < zwn; i++) {
        if(is_ualn_win(z->w_list.a[i])) {
            zerr += MAX((z->w_list.a[i].x_end+1-z->w_list.a[i].x_start), 
                                (z->w_list.a[i].y_end+1-z->w_list.a[i].y_start));
        } else {
            zerr += z->w_list.a[i].error;
        }
    }
    z->non_homopolymer_errors = zerr;

    // if(z->x_id == 57 && z->y_id == 2175) {
    //     fprintf(stderr, "\n-2-[M::%s]\tutg%.6u%c\txl::%u\tx::[%u,\t%u)\t%c\tutg%.6u%c\tyl::%u\ty::[%u,\t%u)\n", 
    //         __func__, 
	// 		z->x_id+1, "lc"[udb->ug->u.a[z->x_id].circ], udb->ug->u.a[z->x_id].len, z->x_pos_s, z->x_pos_e+1,
	// 		"+-"[z->y_pos_strand],
	// 		z->y_id+1, "lc"[udb->ug->u.a[z->y_id].circ], udb->ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1);
    //     for (i = 0; i < zwn; i++) {
    //         window_list *m = &(z->w_list.a[i]);
    //         fprintf(stderr, "i::%ld[M::%s]\tutg%.6u%c\twx::[%u,\t%u)\t%c\tutg%.6u%c\twy::[%u,\t%u)\terr::%d\tualn::%u\test::%u\n", i, __func__, 
	// 			z->x_id+1, "lc"[udb->ug->u.a[z->x_id].circ], m->x_start, m->x_end+1,
	// 			"+-"[z->y_pos_strand], 
	// 			z->y_id+1, "lc"[udb->ug->u.a[z->y_id].circ], m->y_start, m->y_end+1, m->error, 
	// 			(is_ualn_win((*m))), (is_est_aln((*m))));
    //     }
    //     fprintf(stderr, "\n");
    // }

    // if(z->x_id == 57 && z->y_id == 2175) {
    //     fprintf(stderr, "\n-3-[M::%s]\tutg%.6u%c\txl::%u\tx::[%u,\t%u)\t%c\tutg%.6u%c\tyl::%u\ty::[%u,\t%u)\tzerr::%ld\tzlen::%ld\te_rate::%f\n", 
    //         __func__, 
	// 		z->x_id+1, "lc"[udb->ug->u.a[z->x_id].circ], udb->ug->u.a[z->x_id].len, z->x_pos_s, z->x_pos_e+1,
	// 		"+-"[z->y_pos_strand],
	// 		z->y_id+1, "lc"[udb->ug->u.a[z->y_id].circ], udb->ug->u.a[z->y_id].len, z->y_pos_s, z->y_pos_e+1,
    //         zerr, zlen, e_rate);
    // }

    if(zerr >= zlen) return 0;
    if(zerr <= 0 && zlen > 0) return 1;
    if(zerr > (zlen*e_rate)) return 0;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movsd %xmm0, 0x48(%rsp)
movq %r9, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movq 0xf0(%rsp), %r15
movq 0xd8(%rsp), %r14
callq 0x7d1f5
movslq 0x24(%rbx), %rcx
movl 0x10(%rbx), %eax
movq %rcx, %rsi
shlq $0x4, %rsi
addq (%r13), %rsi
movq %r12, 0x50(%rsp)
movq (%r12), %rdx
movq 0x10(%rdx), %rdx
leaq (%rax,%rax,4), %rdi
movl (%rdx,%rdi,8), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq %r13, 0x70(%rsp)
movq 0x8(%r13), %rdi
movq %rcx, %rdx
cmpq %rcx, %rdi
jle 0x8294b
movl (%rsi), %r8d
movq %rsi, %r9
movq %rcx, %rdx
movl (%r9), %r10d
xorl %r8d, %r10d
testl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jne 0x8294b
incq %rdx
addq $0x10, %r9
cmpq %rdx, %rdi
jne 0x8292d
movq %rdi, %rdx
subq %rcx, %rdx
xorl %ecx, %ecx
movq %rcx, 0x60(%r15)
movq %rcx, 0x48(%r15)
movl %eax, 0x10(%r15)
movl 0x1c(%rbx), %eax
movl %eax, 0x1c(%r15)
movq 0x4(%rbx), %rax
movq %rax, 0x4(%r15)
movq 0x14(%rbx), %rax
movq %rax, 0x14(%r15)
leaq 0x7c(%rsp), %rcx
movl $0x3, 0x18(%rcx)
movq $-0x1, 0x10(%rcx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, (%rcx)
movl %r14d, 0x8(%rcx)
movl %edx, 0xc(%rcx)
movq %rbp, 0x38(%rsp)
movq %r14, 0x30(%rsp)
movq %r15, 0x28(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq 0xd0(%rsp), %r8
movq 0x50(%rsp), %r9
movsd 0x48(%rsp), %xmm0
callq 0x69be8
movq 0x48(%r15), %r13
testq %r13, %r13
jle 0x82b1e
movl $0x12, %r12d
xorl %r14d, %r14d
movq 0x58(%r15), %rax
cmpw $0x7fff, 0x2(%rax,%r12) # imm = 0x7FFF
jne 0x82a85
cmpl $0x0, 0xa(%rax,%r12)
jne 0x82a85
cmpw $0x0, (%rax,%r12)
jns 0x82a85
movq 0xe8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq 0x70(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movq 0xd0(%rsp), %r8
movq 0x50(%rsp), %r9
movsd 0x48(%rsp), %xmm0
callq 0x69160
incq %r14
addq $0x20, %r12
cmpq %r14, %r13
jne 0x829fb
movq 0x48(%r15), %rcx
cmpq %r13, %rcx
movq 0xd8(%rsp), %r14
jle 0x82b1e
testq %rcx, %rcx
jle 0x82b08
movl $0x1c, %eax
xorl %edx, %edx
xorl %esi, %esi
movq 0x58(%r15), %rdi
cmpq %r13, %rdx
jge 0x82ad4
cmpw $0x7fff, -0x8(%rdi,%rax) # imm = 0x7FFF
jne 0x82ad4
cmpl $0x0, (%rdi,%rax)
jne 0x82ad4
cmpw $0x0, -0xa(%rdi,%rax)
js 0x82afa
leaq 0x1(%rsi), %r8
shlq $0x5, %rsi
movupd -0x1c(%rdi,%rax), %xmm0
movupd -0xc(%rdi,%rax), %xmm1
movupd %xmm1, 0x10(%rdi,%rsi)
movupd %xmm0, (%rdi,%rsi)
movq 0x48(%r15), %rcx
movq %r8, %rsi
incq %rdx
addq $0x20, %rax
cmpq %rcx, %rdx
jl 0x82ab4
jmp 0x82b0a
xorl %esi, %esi
movq %rsi, 0x48(%r15)
movq 0x58(%r15), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x46146
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x69732
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
cmpl %eax, %ecx
jb 0x82bf5
movl 0x18(%rbx), %edx
cmpl 0x14(%rbx), %edx
jb 0x82bf5
movq 0x48(%rbx), %rdi
incl %ecx
movl %ecx, %edx
subl %eax, %edx
testq %rdi, %rdi
jle 0x82ba8
movq 0x58(%rbx), %r8
xorl %esi, %esi
movswq 0x14(%r8), %r9
cmpq $0x7fff, %r9            # imm = 0x7FFF
jne 0x82b9a
cmpl $0x0, 0x1c(%r8)
jne 0x82b9a
cmpw $0x0, 0x12(%r8)
jns 0x82b9a
movl 0x4(%r8), %r9d
movl 0xc(%r8), %r10d
subl (%r8), %r9d
incl %r9d
subl 0x8(%r8), %r10d
incl %r10d
cmpl %r10d, %r9d
cmovgl %r9d, %r10d
movslq %r10d, %r9
addq %r9, %rsi
addq $0x20, %r8
decq %rdi
jne 0x82b5e
jmp 0x82baa
xorl %esi, %esi
movl %esi, 0x30(%rbx)
cmpq %rdx, %rsi
jge 0x82be1
cmpl %eax, %ecx
je 0x82bc0
movl $0x1, %eax
testq %rsi, %rsi
jle 0x82be3
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd 0x48(%rsp), %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
setbe %al
jmp 0x82be3
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b2afd(%rip), %rdi    # 0x2356f9
leaq 0x1aeb50(%rip), %rsi    # 0x231753
leaq 0x1b2b24(%rip), %rcx    # 0x23572e
movl $0x61ad, %edx           # imm = 0x61AD
callq 0x2110