// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: update_sketch_trace(overlap_region_alloc*, ul_idx_t const*, ug_opt_t const*, All_reads*, UC_Read*, asg64_v*, asg64_v*, asg64_v*, long, long, kv_ul_ov_t*, unsigned long, long, double)
void update_sketch_trace(overlap_region_alloc* ol, const ul_idx_t *uref, const ug_opt_t *uopt, 
All_reads *rref, UC_Read* tu, asg64_v* idx, asg64_v *b0, asg64_v *b1, int64_t ql, int64_t wl, 
kv_ul_ov_t *aln, uint64_t rid, int64_t max_lgap, double sgap_rate)
{
    idx->n = 0;
	if(!aln->n) return;
	uint64_t i, k, own, srt_n; int64_t dp, old_dp, beg, end; overlap_region *z;
    for (i = 0; i < ol->length; i++) {
		z = &(ol->list[i]); append_unmatched_wins(z, wl); 
		own = z->w_list.n; z->align_length = (uint32_t)-1;
		for (k = 0; k < own; k++) {
			if(z->w_list.a[k].extra_end < 0) z->w_list.a[k].extra_end = 0;
		}		
    }

    kv_resize(uint64_t, *idx, (aln->n<<1)); kv_resize(uint64_t, *b0, aln->n);
    for (i = srt_n = 0; i < aln->n; i++) {
		// if(i == 0 || aln->a[i].qn != aln->a[i-1].qn) ol->list[aln->a[i].qn].align_length = i;
        ol->list[aln->a[i].qn].align_length = (uint32_t)-1;///for co-linear
        idx->a[srt_n] = aln->a[i].qs<<1; idx->a[srt_n] <<= 32; idx->a[srt_n] += i; srt_n++;
        idx->a[srt_n] = ((aln->a[i].qe-1)<<1)+1; idx->a[srt_n] <<= 32; idx->a[srt_n] += i; srt_n++;
		aln->a[i].el = 1;
    }

    radix_sort_bc64(idx->a, idx->a+srt_n); idx->n = srt_n; resize_UC_Read(tu, (wl<<1));
    for (i = 0, dp = 0, beg = 0, end = -1; i < srt_n; ++i) {///[beg, end), but the idx saves [qs, qe]
		old_dp = dp;
        ///if idx->a.a[] is qe
        if ((idx->a[i]>>32)&1) {
			--dp; end = (idx->a[i]>>33)+1;
		}else {
			//meet a new overlap; the overlaps are pushed by the x_pos_s
			++dp; end = (idx->a[i]>>33);
			kv_push(uint64_t, *idx, ((uint32_t)idx->a[i]));
		}
		
		if((end > beg) && (end - beg > wl) && (old_dp >= 2) ) {
            // fprintf(stderr, "+++[M::%s::] beg::%ld, end::%ld\n", __func__, beg, end);
			idx->n = srt_n + 
					gen_commen_sketch(rref, uref, ol, idx->a+srt_n, idx->n-srt_n, beg, end, ql, wl, b0->a, 
                    old_dp, tu->seq, tu->seq+wl, aln, b1, max_lgap, sgap_rate, SGAP);
		}
        beg = end;
	}

    for (i = 0; i < aln->n; i++) {
		if(i == 0 || aln->a[i].qn != aln->a[i-1].qn) ol->list[aln->a[i].qn].align_length = i;
    }
    idx->n = srt_n;
    // fprintf(stderr, "+++[M::%s::] idx->n::%ld\n", __func__, idx->n);
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, 0x30(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq 0x90(%rsp), %rbp
movq $0x0, (%r9)
movq 0x8(%rbp), %rax
testq %rax, %rax
je 0x7b37d
movq %rdi, %r13
cmpq $0x0, 0x10(%rdi)
movq %r9, 0x8(%rsp)
je 0x7b04c
xorl %ebx, %ebx
movq (%r13), %r14
imulq $0xa8, %rbx, %r15
leaq (%r14,%r15), %r12
movq %r12, %rdi
movq 0x88(%rsp), %rsi
callq 0x49760
movq 0x48(%r14,%r15), %rax
movl $0xffffffff, 0x28(%r14,%r15) # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x7b03a
movq 0x58(%r12), %rcx
addq $0x12, %rcx
cmpw $0x0, (%rcx)
jns 0x7b031
movw $0x0, (%rcx)
addq $0x20, %rcx
decq %rax
jne 0x7b026
incq %rbx
cmpq 0x10(%r13), %rbx
jb 0x7afeb
movq 0x8(%rbp), %rax
movq 0x8(%rsp), %r9
movq 0x70(%rsp), %rbx
leaq (%rax,%rax), %rcx
cmpq %rcx, 0x8(%r9)
jae 0x7b0b5
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r9)
movq 0x10(%r9), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r9)
movq 0x8(%rbp), %rax
cmpq %rax, 0x8(%rbx)
jae 0x7b115
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq %rax, 0x10(%rbx)
movq 0x8(%rbp), %rax
movabsq $0x100000000, %r10      # imm = 0x100000000
testq %rax, %rax
je 0x7b18e
movl $0xc, %eax
xorl %ecx, %ecx
xorl %r15d, %r15d
movq (%r13), %rdx
movq (%rbp), %rsi
movl -0x18(%rsi,%rax,2), %edi
imulq $0xa8, %rdi, %rdi
movl $0xffffffff, 0x28(%rdx,%rdi) # imm = 0xFFFFFFFF
movq 0x10(%r9), %rdx
movl -0x14(%rsi,%rax,2), %edi
shlq $0x21, %rdi
addq %rcx, %rdi
movq %rdi, (%rdx,%r15,8)
movl -0x10(%rsi,%rax,2), %edi
leal -0x2(,%rdi,2), %edi
shlq $0x20, %rdi
addq %rcx, %rdi
addq %r10, %rdi
movq %rdi, 0x8(%rdx,%r15,8)
addq $0x2, %r15
orb $0x40, 0x3(%rsi,%rax,2)
incq %rcx
addq $0xe, %rax
cmpq 0x8(%rbp), %rcx
jb 0x7b12e
jmp 0x7b191
xorl %r15d, %r15d
movq %r13, 0x18(%rsp)
movq 0x10(%r9), %rdi
leaq (%rdi,%r15,8), %rsi
callq 0x45373
movq 0x8(%rsp), %r9
movq %r15, (%r9)
movq 0x88(%rsp), %rax
leaq (%rax,%rax), %r12
movq 0x10(%rsp), %rax
cmpq %r12, 0x10(%rax)
jge 0x7b1de
movq 0x10(%rsp), %rbx
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq %rax, (%rbx)
movq %r12, 0x10(%rbx)
testq %r15, %r15
movabsq $0x100000000, %rdi      # imm = 0x100000000
je 0x7b32f
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebx, %ebx
movq %rbx, %r14
movq %r12, %rbp
movq 0x10(%r9), %rax
movq (%rax,%r13,8), %rcx
movq %rcx, %r12
shrq $0x21, %r12
testq %rdi, %rcx
jne 0x7b269
movq (%r9), %rdx
cmpq 0x8(%r9), %rdx
jne 0x7b256
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movabsq $0x100000000, %rdi      # imm = 0x100000000
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r9)
movl (%rax,%r13,8), %ecx
movq (%r9), %rdx
leaq 0x1(%r14), %rbx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r9)
movq %rcx, (%rax,%rdx,8)
jmp 0x7b270
leaq -0x1(%r14), %rbx
incq %r12
movq %r12, %rcx
subq %rbp, %rcx
jbe 0x7b323
cmpq 0x88(%rsp), %rcx
setg %cl
cmpq $0x2, %r14
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x7b323
leaq (%rax,%r15,8), %rcx
movq (%r9), %r8
subq %r15, %r8
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x88(%rsp), %r11
leaq (%rax,%r11), %r10
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %rbp, %r9
movsd 0x38(%rsp), %xmm0
pushq $0x10
pushq 0xb0(%rsp)
pushq 0x90(%rsp)
pushq 0xb0(%rsp)
pushq %r10
pushq %rax
pushq %r14
movq 0xb0(%rsp), %rax
pushq 0x10(%rax)
pushq %r11
pushq 0xd0(%rsp)
pushq %r12
callq 0x7a489
movabsq $0x100000000, %rdi      # imm = 0x100000000
movq 0x68(%rsp), %r9
addq $0x60, %rsp
addq %r15, %rax
movq %rax, (%r9)
incq %r13
cmpq %r13, %r15
jne 0x7b1f9
movq 0x90(%rsp), %rcx
movq 0x8(%rcx), %rax
testq %rax, %rax
movq 0x18(%rsp), %r10
je 0x7b37a
movq (%rcx), %rcx
xorl %edx, %edx
movq %rcx, %rsi
testq %rdx, %rdx
je 0x7b35b
movl (%rsi), %edi
cmpl -0x1c(%rsi), %edi
jne 0x7b35d
jmp 0x7b36e
movl (%rcx), %edi
movq (%r10), %r8
movl %edi, %edi
imulq $0xa8, %rdi, %rdi
movl %edx, 0x28(%r8,%rdi)
incq %rdx
addq $0x1c, %rsi
cmpq %rdx, %rax
jne 0x7b34d
movq %r15, (%r9)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq