// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movsd %xmm0, 0x40(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
andq $0x0, (%r9)
movq 0xa0(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x4831a
movq %rdi, %rbp
xorl %ebx, %ebx
pushq $0x12
popq %r14
movq %r9, 0x8(%rsp)
cmpq 0x10(%rbp), %rbx
jae 0x47fe0
movq (%rbp), %r15
imulq $0xa8, %rbx, %r13
leaq (%r15,%r13), %r12
movq %r12, %rdi
movq 0x98(%rsp), %rsi
callq 0x1cf34
movq 0x48(%r15,%r13), %rax
orl $-0x1, 0x28(%r15,%r13)
movq %r14, %rcx
subq $0x1, %rax
jb 0x47fd6
movq 0x58(%r12), %rdx
cmpw $0x0, (%rdx,%rcx)
jns 0x47fd0
andw $0x0, (%rdx,%rcx)
addq $0x20, %rcx
jmp 0x47fb9
incq %rbx
movq 0x8(%rsp), %r9
jmp 0x47f86
movq 0xa0(%rsp), %rbx
movq 0x8(%rbx), %rax
leaq (%rax,%rax), %rcx
cmpq %rcx, 0x8(%r9)
jae 0x48050
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(%rbx), %rax
movq 0x80(%rsp), %rcx
cmpq %rax, 0x8(%rcx)
jae 0x480bc
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 0x80(%rsp), %r14
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r14)
pushq $0xc
popq %rax
movabsq $0x100000000, %r10      # imm = 0x100000000
xorl %r11d, %r11d
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpq 0x8(%rbx), %rcx
jae 0x48131
movq (%rbp), %rdx
movq (%rbx), %rsi
movl -0x18(%rsi,%rax,2), %edi
imulq $0xa8, %rdi, %rdi
orl $-0x1, 0x28(%rdx,%rdi)
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)
orb $0x40, 0x3(%rsi,%rax,2)
addq $0x2, %r15
incq %rcx
addq $-0x10, %r11
addq $0xe, %rax
jmp 0x480d1
movq %rbp, 0x10(%rsp)
movq 0x10(%r9), %rdi
movq %rdi, %rsi
subq %r11, %rsi
negq %r11
movq %r11, 0x28(%rsp)
callq 0x1905d
movq 0x8(%rsp), %rax
movq %r15, (%rax)
movq 0x98(%rsp), %rax
leaq (%rax,%rax), %rsi
movq 0x18(%rsp), %rdi
callq 0x562fa
movabsq $0x100000000, %rdi      # imm = 0x100000000
movq 0x8(%rsp), %r8
movq %r15, %rax
negq %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x482cf
movq 0x10(%r8), %rax
movq (%rax,%rbp,8), %rcx
movq %rcx, %r12
shrq $0x21, %r12
testq %rdi, %rcx
jne 0x481fc
movq (%r8), %rdx
cmpq 0x8(%r8), %rdx
jne 0x481e9
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movabsq $0x100000000, %rdi      # imm = 0x100000000
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movl (%rax,%rbp,8), %ecx
movq (%r8), %rdx
leaq 0x1(%rbx), %r13
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r8)
movq %rcx, (%rax,%rdx,8)
jmp 0x48203
leaq -0x1(%rbx), %r13
incq %r12
movq %r12, %rcx
subq %r14, %rcx
jbe 0x482c1
cmpq 0x98(%rsp), %rcx
setg %cl
cmpq $0x2, %rbx
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x482c1
addq 0x28(%rsp), %rax
movq (%r8), %r8
addq 0x20(%rsp), %r8
movq 0x18(%rsp), %rcx
movq (%rcx), %r10
movq 0x98(%rsp), %rcx
leaq (%r10,%rcx), %r11
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
movq %r14, %r9
movsd 0x48(%rsp), %xmm0
pushq $0x10
pushq 0xc0(%rsp)
pushq 0xa0(%rsp)
pushq 0xc0(%rsp)
pushq %r11
pushq %r10
pushq %rbx
movq 0xc0(%rsp), %rax
pushq 0x10(%rax)
pushq 0xe0(%rsp)
pushq 0xe0(%rsp)
pushq %r12
callq 0x478c8
movabsq $0x100000000, %rdi      # imm = 0x100000000
movq 0x68(%rsp), %r8
addq $0x60, %rsp
addq %r15, %rax
movq %rax, (%r8)
incq %rbp
movq %r13, %rbx
movq %r12, %r14
jmp 0x4818c
movq 0xa0(%rsp), %r10
movq 0x8(%r10), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x10(%rsp), %r9
cmpq %rdx, %rax
je 0x48317
movq (%r10), %rdi
testq %rdx, %rdx
je 0x482fc
movl (%rdi,%rcx), %esi
cmpl -0x1c(%rdi,%rcx), %esi
jne 0x482fe
jmp 0x4830e
movl (%rdi), %esi
movq (%r9), %rdi
movl %esi, %esi
imulq $0xa8, %rsi, %rsi
movl %edx, 0x28(%rdi,%rsi)
incq %rdx
addq $0x1c, %rcx
jmp 0x482e4
movq %r15, (%r8)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq