// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq 0x90(%rsp), %rbp
movq $0x0, (%r9)
cmpq $0x0, 0x8(%rbp)
je 0x7b7f1
movq %rdi, %r13
movq %r9, (%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x7b4ac
xorl %ebx, %ebx
movq (%r13), %r14
imulq $0xa8, %rbx, %r15
leaq (%r14,%r15), %r12
movq %r12, %rdi
movq 0x88(%rsp), %rsi
callq 0x4a082
movq 0x48(%r14,%r15), %rax
movl $0xffffffff, 0x28(%r14,%r15) # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x7b4a3
movq 0x58(%r12), %rcx
addq $0x12, %rcx
cmpw $0x0, (%rcx)
jns 0x7b49a
movw $0x0, (%rcx)
addq $0x20, %rcx
decq %rax
jne 0x7b48f
incq %rbx
cmpq 0x10(%r13), %rbx
jb 0x7b454
movq 0x8(%rbp), %rax
addq %rax, %rax
movq (%rsp), %r9
cmpq %rax, 0x8(%r9)
jae 0x7b512
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(%r9)
movq 0x10(%r9), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq 0x8(%rbp), %rax
movq 0x70(%rsp), %rcx
cmpq %rax, 0x8(%rcx)
jae 0x7b57b
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 0x70(%rsp), %rbx
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%rbx)
movabsq $0x100000000, %r10      # imm = 0x100000000
cmpq $0x0, 0x8(%rbp)
je 0x7b5f6
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 0x7b596
jmp 0x7b5f9
xorl %r15d, %r15d
movq %r13, 0x10(%rsp)
movq 0x10(%r9), %rdi
leaq (%rdi,%r15,8), %rsi
callq 0x45d47
movq (%rsp), %r8
movq %r15, (%r8)
movq 0x88(%rsp), %rax
leaq (%rax,%rax), %r12
movq 0x8(%rsp), %rax
cmpq %r12, 0x10(%rax)
jge 0x7b644
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, (%rbx)
movq %r12, 0x10(%rbx)
testq %r15, %r15
movabsq $0x100000000, %r9       # imm = 0x100000000
je 0x7b7a3
xorl %ebp, %ebp
leaq (,%r15,8), %rax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
movq %rbx, %r14
movq %r12, %r13
movq 0x10(%r8), %rdi
movq (%rdi,%rbp,8), %rax
movq %rax, %r12
shrq $0x21, %r12
testq %r9, %rax
jne 0x7b6d8
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x7b6bd
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
callq 0x2370
movabsq $0x100000000, %r9       # imm = 0x100000000
movq (%rsp), %r8
movq %rax, 0x10(%r8)
leaq 0x1(%r14), %rbx
movq (%r8), %rax
movq 0x10(%r8), %rcx
movl (%rcx,%rbp,8), %edx
leaq 0x1(%rax), %rsi
movq %rsi, (%r8)
movq %rdx, (%rcx,%rax,8)
jmp 0x7b6df
leaq -0x1(%r14), %rbx
incq %r12
movq %r12, %rax
subq %r13, %rax
jbe 0x7b797
cmpq 0x88(%rsp), %rax
setg %al
cmpq $0x2, %r14
setge %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x7b797
movq 0x10(%r8), %rcx
addq 0x18(%rsp), %rcx
movq (%r8), %r8
subq %r15, %r8
movq 0x8(%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 0x18(%rsp), %rdx
movq %r13, %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 0x7a819
movabsq $0x100000000, %r9       # imm = 0x100000000
movq 0x60(%rsp), %r8
addq $0x60, %rsp
addq %r15, %rax
movq %rax, (%r8)
incq %rbp
cmpq %rbp, %r15
jne 0x7b66b
movq 0x90(%rsp), %r10
movq 0x8(%r10), %rax
testq %rax, %rax
movq 0x10(%rsp), %r9
je 0x7b7ee
xorl %ecx, %ecx
xorl %edx, %edx
testq %rdx, %rdx
je 0x7b7ce
movq (%r10), %rsi
movl (%rsi,%rcx), %edi
cmpl -0x1c(%rsi,%rcx), %edi
je 0x7b7e2
movq (%r9), %rsi
movq (%r10), %rdi
movl (%rdi,%rcx), %edi
imulq $0xa8, %rdi, %rdi
movl %edx, 0x28(%rsi,%rdi)
incq %rdx
addq $0x1c, %rcx
cmpq %rdx, %rax
jne 0x7b7bd
movq %r15, (%r8)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq