// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movsd %xmm0, 0x50(%rsp)
movq %r9, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movq 0x100(%rsp), %r14
callq 0x4a008
movslq 0x24(%rbx), %rcx
movl 0x10(%rbx), %eax
movq %rcx, %rsi
shlq $0x4, %rsi
addq (%r12), %rsi
movq %r15, 0x60(%rsp)
movq (%r15), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rax, %rdi
movl (%rdx,%rdi), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, 0x58(%rsp)
movq %r12, 0x80(%rsp)
movq 0x8(%r12), %rdi
cmpq %rcx, %rdi
movq %rcx, %rdx
cmovgq %rdi, %rdx
movq %rsi, %r9
movq %rcx, %r8
cmpq %rdi, %r8
jge 0x4f46c
movl (%rsi), %r10d
xorl (%r9), %r10d
testl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jne 0x4f469
incq %r8
addq $0x10, %r9
jmp 0x4f44c
movq %r8, %rdx
andq $0x0, 0x60(%r14)
subq %rcx, %rdx
andq $0x0, 0x48(%r14)
movl %eax, 0x10(%r14)
movl 0x1c(%rbx), %eax
movl %eax, 0x1c(%r14)
movq 0x4(%rbx), %rax
movq %rax, 0x4(%r14)
movq 0x14(%rbx), %rax
movq %rax, 0x14(%r14)
leaq 0x8c(%rsp), %rcx
movl $0x3, 0x18(%rcx)
orq $-0x1, 0x10(%rcx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, (%rcx)
movq 0xe8(%rsp), %rax
movl %eax, 0x8(%rcx)
movl %edx, 0xc(%rcx)
movq 0x58(%rsp), %rdi
movq %rdi, 0x38(%rsp)
movq %rax, 0x30(%rsp)
movq %r14, 0x28(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq 0xe0(%rsp), %r8
movq 0x60(%rsp), %r9
movsd 0x50(%rsp), %xmm0
callq 0x38748
movq 0x48(%r14), %r13
xorl %r15d, %r15d
testq %r13, %r13
movl $0x0, %r12d
cmovgq %r13, %r12
pushq $0x12
popq %rbp
cmpq %r15, %r12
je 0x4f5d3
movq 0x58(%r14), %rax
cmpw $0x7fff, 0x2(%rax,%rbp) # imm = 0x7FFF
jne 0x4f5c7
cmpl $0x0, 0xa(%rax,%rbp)
jne 0x4f5c7
cmpw $0x0, (%rax,%rbp)
jns 0x4f5c7
movq 0xf8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq 0x80(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movq 0xe0(%rsp), %r8
movq 0x60(%rsp), %r9
movsd 0x50(%rsp), %xmm0
callq 0x37ce8
incq %r15
addq $0x20, %rbp
jmp 0x4f52b
movq 0x48(%r14), %rcx
cmpq %r13, %rcx
jle 0x4f649
pushq $0x1c
popq %rax
xorl %esi, %esi
xorl %edx, %edx
cmpq %rcx, %rdx
jge 0x4f635
movq 0x58(%r14), %rdi
cmpq %r13, %rdx
jge 0x4f608
cmpw $0x7fff, -0x8(%rdi,%rax) # imm = 0x7FFF
jne 0x4f608
cmpl $0x0, (%rdi,%rax)
jne 0x4f608
cmpw $0x0, -0xa(%rdi,%rax)
js 0x4f62c
leaq 0x1(%rsi), %r8
shlq $0x5, %rsi
movupd -0x1c(%rdi,%rax), %xmm0
movups -0xc(%rdi,%rax), %xmm1
movups %xmm1, 0x10(%rdi,%rsi)
movupd %xmm0, (%rdi,%rsi)
movq 0x48(%r14), %rcx
movq %r8, %rsi
incq %rdx
addq $0x20, %rax
jmp 0x4f5e3
movq %rsi, 0x48(%r14)
movq 0x58(%r14), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x19dc0
movq %rbx, %rdi
movq %r14, %rsi
movq 0xe8(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x382a0
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
cmpl %eax, %ecx
jb 0x4f72e
movl 0x18(%rbx), %edx
cmpl 0x14(%rbx), %edx
jb 0x4f72e
movq 0x48(%rbx), %rsi
incl %ecx
movl %ecx, %edi
subl %eax, %edi
xorl %r8d, %r8d
testq %rsi, %rsi
cmovleq %r8, %rsi
xorl %edx, %edx
subq $0x1, %rsi
jb 0x4f6e7
movq 0x58(%rbx), %r9
movswq 0x14(%r9,%r8), %r10
cmpq $0x7fff, %r10           # imm = 0x7FFF
jne 0x4f6de
cmpl $0x0, 0x1c(%r9,%r8)
jne 0x4f6de
cmpw $0x0, 0x12(%r9,%r8)
jns 0x4f6de
movl 0x4(%r9,%r8), %r10d
movl 0xc(%r9,%r8), %r11d
subl (%r9,%r8), %r10d
incl %r10d
subl 0x8(%r9,%r8), %r11d
incl %r11d
cmpl %r11d, %r10d
cmovgl %r10d, %r11d
movslq %r11d, %r10
addq %r10, %rdx
addq $0x20, %r8
jmp 0x4f691
movl %edi, %esi
movl %edx, 0x30(%rbx)
cmpq %rsi, %rdx
jge 0x4f6ff
cmpl %eax, %ecx
je 0x4f703
testq %rdx, %rdx
jg 0x4f703
pushq $0x1
popq %rax
jmp 0x4f71c
xorl %eax, %eax
jmp 0x4f71c
cvtsi2sd %rdx, %xmm0
cvtsi2sd %rsi, %xmm1
mulsd 0x50(%rsp), %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
setbe %al
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x197f14(%rip), %rdi    # 0x1e7649
leaq 0x193f67(%rip), %rsi    # 0x1e36a3
leaq 0x197f3b(%rip), %rcx    # 0x1e767e
movl $0x61ad, %edx           # imm = 0x61AD
callq 0x2110