// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rsi, %rbp
movq %rdi, %rbx
movq 0x100(%rsp), %r15
callq 0x7d780
movslq 0x24(%rbx), %rax
movl 0x10(%rbx), %ecx
movq %rax, %rsi
shlq $0x4, %rsi
addq (%rbp), %rsi
movq %r14, 0x60(%rsp)
movq (%r14), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,4), %rdi
movl (%rdx,%rdi,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, 0x58(%rsp)
movq 0x8(%rbp), %rdi
movq %rax, %rdx
cmpq %rax, %rdi
jle 0x83144
movl (%rsi), %r8d
movq %rsi, %r9
movq %rax, %rdx
movl (%r9), %r10d
xorl %r8d, %r10d
testl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jne 0x83144
incq %rdx
addq $0x10, %r9
cmpq %rdx, %rdi
jne 0x83126
movq %rdi, %rdx
xorl %edi, %edi
movq %rdi, 0x60(%r15)
movq %rdi, 0x48(%r15)
movl %ecx, 0x10(%r15)
movl 0x1c(%rbx), %ecx
movl %ecx, 0x1c(%r15)
movl 0x4(%rbx), %ecx
movl %ecx, 0x4(%r15)
movl 0x8(%rbx), %ecx
movl %ecx, 0x8(%r15)
movl 0x14(%rbx), %ecx
movl %ecx, 0x14(%r15)
movl 0x18(%rbx), %ecx
movl %ecx, 0x18(%r15)
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rcx
movups %xmm0, 0xc(%rcx)
movaps %xmm0, (%rcx)
movl $0xc0000000, %edi       # imm = 0xC0000000
andl 0x18(%rcx), %edi
subq %rax, %rdx
orl $0x3, %edi
movl %edi, 0x18(%rcx)
movq $-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 %r15, 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)
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq 0xe0(%rsp), %r8
movq 0x60(%rsp), %r9
movsd 0x50(%rsp), %xmm0
callq 0x69f3e
movq 0x48(%r15), %r13
testq %r13, %r13
jle 0x832ba
movl $0x12, %r12d
xorl %r14d, %r14d
movq 0x58(%r15), %rax
cmpw $0x7fff, 0x2(%rax,%r12) # imm = 0x7FFF
jne 0x832aa
cmpl $0x0, 0xa(%rax,%r12)
jne 0x832aa
cmpw $0x0, (%rax,%r12)
jns 0x832aa
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 %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq 0xe0(%rsp), %r8
movq 0x60(%rsp), %r9
movsd 0x50(%rsp), %xmm0
callq 0x6947b
incq %r14
addq $0x20, %r12
cmpq %r14, %r13
jne 0x8321d
cmpq %r13, 0x48(%r15)
jle 0x83338
cmpq $0x0, 0x48(%r15)
jle 0x83322
xorl %eax, %eax
xorl %ecx, %ecx
xorl %esi, %esi
cmpq %r13, %rcx
jge 0x832ee
movq 0x58(%r15), %rdx
cmpw $0x7fff, 0x14(%rdx,%rax) # imm = 0x7FFF
jne 0x832ee
cmpl $0x0, 0x1c(%rdx,%rax)
jne 0x832ee
cmpw $0x0, 0x12(%rdx,%rax)
js 0x83313
movq 0x58(%r15), %rdx
leaq 0x1(%rsi), %rdi
shlq $0x5, %rsi
movupd (%rdx,%rax), %xmm0
movupd 0x10(%rdx,%rax), %xmm1
movupd %xmm1, 0x10(%rdx,%rsi)
movupd %xmm0, (%rdx,%rsi)
movq %rdi, %rsi
incq %rcx
addq $0x20, %rax
cmpq 0x48(%r15), %rcx
jl 0x832cd
jmp 0x83324
xorl %esi, %esi
movq %rsi, 0x48(%r15)
movq 0x58(%r15), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x46b34
movq %rbx, %rdi
movq %r15, %rsi
movq 0xe8(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x69a6f
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
cmpl %eax, %ecx
jb 0x8341c
movl 0x18(%rbx), %edx
cmpl 0x14(%rbx), %edx
jb 0x8341c
movq 0x48(%rbx), %rdi
incl %ecx
movl %ecx, %edx
subl %eax, %edx
testq %rdi, %rdi
jle 0x833cf
movq 0x58(%rbx), %r8
addq $0x1c, %r8
xorl %esi, %esi
cmpw $0x7fff, -0x8(%r8)      # imm = 0x7FFF
jne 0x8339a
cmpl $0x0, (%r8)
jne 0x8339a
cmpw $0x0, -0xa(%r8)
js 0x833ad
movswq -0x8(%r8), %r9
addq %r9, %rsi
addq $0x20, %r8
decq %rdi
jne 0x83383
jmp 0x833d1
movl -0x18(%r8), %r9d
movl -0x10(%r8), %r10d
subl -0x1c(%r8), %r9d
incl %r9d
subl -0x14(%r8), %r10d
incl %r10d
cmpl %r10d, %r9d
cmovgl %r9d, %r10d
movslq %r10d, %r9
jmp 0x8339f
xorl %esi, %esi
movl %esi, 0x30(%rbx)
cmpq %rdx, %rsi
jge 0x83408
cmpl %eax, %ecx
je 0x833e7
movl $0x1, %eax
testq %rsi, %rsi
jle 0x8340a
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd 0x50(%rsp), %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
setbe %al
jmp 0x8340a
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a61c6(%rip), %rdi    # 0x2295e9
leaq 0x1a2219(%rip), %rsi    # 0x225643
leaq 0x1a61ed(%rip), %rcx    # 0x22961e
movl $0x61ad, %edx           # imm = 0x61AD
callq 0x2110