// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gen_trans_aln(overlap_region_alloc*, All_reads*, ul_idx_t const*, ug_opt_t const*, long, double, long, unsigned long*, long, ul_ov_t*, long, unsigned long, unsigned long, long*, int*, int*, int*, int*, int*, long*, long*, long, long, long, bit_extz_t*, long, char*, UC_Read*, double, kv_rtrace_t*, unsigned int, ul_ov_t*, ul_ov_t*)
int64_t gen_trans_aln(overlap_region_alloc* ol, All_reads *ridx, const ul_idx_t *uref, 
const ug_opt_t *uopt, int64_t bw, double diff_ec_ul, int64_t qlen, uint64_t *id, int64_t idn, 
ul_ov_t *a, int64_t a_n, uint64_t qs, uint64_t qe, int64_t *vis, int32_t *f0, int32_t *c_n0, 
int32_t *c_sc0, int32_t *rch0, int32_t *ssc0, int64_t *p0, int64_t *t0, int64_t max_skip, 
int64_t max_iter, int64_t max_dis, bit_extz_t *exz, int64_t trans_sc, char* qstr, UC_Read *tu, 
double e_rate, kv_rtrace_t *trace, uint32_t rechain_w, ul_ov_t *res, ul_ov_t *rr)
{
	if(idn == 0) return 0;
	int64_t k, l, i, j, z, nv = 0, qo, is_c, nf; 
	ul_ov_t *li, *lj; uint32_t li_v, lj_v, update, tipl = 0, mm = ((uint32_t)(0x20000000U));
	// for (i = 0; i < idn; i++) {
	// 	li = &(a[id[i]]);
	// 	fprintf(stderr, "[i::%ld] (id::%u)%.*s\t%c\tq::[%u, %u)\tt::[%u, %u)\tc::%u\n", 
	// 			i, li->tn,
    //                 (int)Get_NAME_LENGTH(R_INF, li->tn), Get_NAME(R_INF, li->tn), 
	// 				"+-"[li->rev], li->qs, li->qe, li->ts, li->te, !li->el);
	// }
	for (i = nf = 0; i < idn; i++) {
		if(a[id[i]].el) break;
		if((a[id[i]].ts == 0) && (a[id[i]].te == Get_READ_LENGTH(R_INF, a[id[i]].tn))) nf++;
	}
	if(i < idn && nf > 0) {
		if((a[id[i]].qs > qs) && (qs <= rechain_w)) {
			tipl = qs; ///there are contained chains beforehand
		}
	}

	for (i = 0, k = l = -1; i < idn; i++) {
		k = id[i]; 
		if(k - l > 1) {
			if(l < 0) {
				for (l = k-1; l >= 0 && a[l].qe > qs; l--);
			}

			for (z = l + 1; z < k; z++) {
				li = &(a[z]); li_v = (li->tn<<1)|li->rev;
				if((!(li->el))||(vis[z]&1)) continue;
				if(!(((li->qs + tipl) >= qs) && (li->qe < qe))) continue;
				for (j = i-1, update = 1; j >= 0; --j) {
					lj = &(a[id[j]]); lj_v = (lj->tn<<1)|lj->rev;
            		if(lj->qe <= li->qs) break;
					qo = infer_rovlp(li, lj, NULL, NULL, ridx, NULL); ///overlap length in query (UL read)
            		if((li_v == lj_v) || (!get_ecov_contain_adv(uref, uopt, li_v^1, lj_v^1, 
																			bw, diff_ec_ul, qo, &is_c))) {
						update = 0; break;
					}
				}
				// if(li->tn == 6312) {
				// 	fprintf(stderr, "[z::%ld] (id::%u)%.*s\t%c\tq::[%u, %u)\tt::[%u, %u)\tc::%u\tnf::%ld\ttipl::%u\tl::%ld\tk::%ld\tupdate::%u\n", z, li->tn,
                //     (int)Get_NAME_LENGTH(R_INF, li->tn), Get_NAME(R_INF, li->tn), 
				// 	"+-"[li->rev], li->qs, li->qe, li->ts, li->te, !li->el, nf, tipl, l, k, update);
				// }
				if(update) {
					res[nv] = a[z]; res[nv].sec = (z<<1) + ((li->qs>=qs)?0:1); nv++;
				}
			}
		}
		l = k; 

		li = &(a[k]); li_v = (li->tn<<1)|li->rev;
		for (j = nv-1; j >= 0; --j) {
			lj = &(res[j]); lj_v = (lj->tn<<1)|lj->rev;
			if(lj->qe <= li->qs) break;
			if(lj->qn == (uint32_t)-1) continue;//deleted
			if((lj->sec&mm)) continue;//could not be deleted
			qo = infer_rovlp(li, lj, NULL, NULL, ridx, NULL); ///overlap length in query (UL read)
			if((li_v == lj_v) || (!get_ecov_contain_adv(uref, uopt, li_v^1, lj_v^1, 
																	bw, diff_ec_ul, qo, &is_c))) {
				// if(lj->tn == 6312) {
				// 	fprintf(stderr, "delete::(id::%u)%.*s\t%c\tq::[%u, %u)\tt::[%u, %u)\tc::%u\n", 
				// 	li->tn, (int)Get_NAME_LENGTH(R_INF, li->tn), Get_NAME(R_INF, li->tn), 
				// 	"+-"[li->rev], li->qs, li->qe, li->ts, li->te, !li->el);
				// }
				res[j].qn = res[j].tn = (uint32_t)-1;
			}
		}
		res[nv] = a[k]; res[nv].sec = k; res[nv].sec<<=1; res[nv].sec |= mm; nv++;
	}

	k = a_n; i = idn;
	if(k - l > 1) {
		for (z = l + 1; z < k; z++) {
			li = &(a[z]); li_v = (li->tn<<1)|li->rev;
			if((!(li->el))||(vis[z]&1)) continue;
			if(!(((li->qs + tipl) >= qs) && (li->qe < qe))) continue;
			for (j = i-1, update = 1; j >= 0; --j) {
				lj = &(a[id[j]]); lj_v = (lj->tn<<1)|lj->rev;
				if(lj->qe <= li->qs) break;
				qo = infer_rovlp(li, lj, NULL, NULL, ridx, NULL); ///overlap length in query (UL read)
				if((li_v == lj_v) || (!get_ecov_contain_adv(uref, uopt, li_v^1, lj_v^1, 
																		bw, diff_ec_ul, qo, &is_c))) {
					update = 0; break;
				}
			}
			if(update) {
				res[nv] = a[z]; res[nv].sec = (z<<1) + ((li->qs>=qs)?0:1); nv++;
			}
		}
	}
	// fprintf(stderr, "\n[M::%s::] old_n::%ld, new_n::%ld\n", __func__, idn, nv);
	for (i = k = 0; i < nv; i++) {
		if(res[i].qn == (uint32_t)-1) continue;
		res[k] = res[i]; 
		if(res[k].sec&mm) res[k].sec -= mm; k++;
		// res[k] = res[i]; 
		// id[k] = res[k].sec; 
		// if(id[k]&mm) id[k] -= mm; 
		// id[k] >>= 1;
		// if(res[k].sec&1) id[k] |= ((uint64_t)(0x100000000U));///low prority
		// // if(res[k].sec&mm) id[k] |= ((uint64_t)(0x200000000U));//orignal alignments
		// k++;

		// fprintf(stderr, "[k::%ld] (id::%u)%.*s\t%c\tq::[%u, %u)\tt::[%u, %u)\tc::%u\tnew::%u\n", k, 
		// 			res[k].tn,(int)Get_NAME_LENGTH(R_INF, res[k].tn), Get_NAME(R_INF, res[k].tn), 
		// 			"+-"[res[k].rev], res[k].qs, res[k].qe, res[k].ts, res[k].te, !res[k].el, 
		// 			(res[k].sec != SEC_MODE));
		// if(res[k].sec != SEC_MODE) vis[res[k].sec>>1] |= 1;
		// res[k].sec = SEC_MODE; k++;
	}
	nv = idn = k;
	nv = flat_contain_adv(ol, ridx, uref, uopt, bw, diff_ec_ul, qlen, max_skip, max_iter, max_dis, 
        exz, trans_sc, qstr, tu, e_rate, trace, res, nv, f0, c_n0, c_sc0, rch0, ssc0, p0, t0, a, id, 
		rechain_w, vis, rr);
		// f, ssc, p, t, &(res->a[k]));
	// for (k = 0; k < idn; k++) {
	// 	if(((uint32_t)id[k])&1) continue;
	// 	vis[((uint32_t)id[k])>>1] |= 1;
	// }
	return nv;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movsd %xmm1, 0x128(%rsp)
movsd %xmm0, 0xe0(%rsp)
movq %r8, 0xd0(%rsp)
movq %rcx, 0xd8(%rsp)
movq %rsi, 0xe8(%rsp)
movq 0x188(%rsp), %rsi
testq %rsi, %rsi
je 0x176688
movq %rdi, 0x110(%rsp)
movq %rdx, 0x118(%rsp)
movq %r9, 0x120(%rsp)
movq 0x238(%rsp), %rbp
movl 0x230(%rsp), %eax
leaq 0xddc23(%rip), %rcx     # 0x254230
movq 0x18(%rcx), %rcx
xorl %ebx, %ebx
testq %rsi, %rsi
movl $0x0, %edx
cmovgq %rsi, %rdx
movq %rdx, 0xf0(%rsp)
movq 0x1b0(%rsp), %r10
movq 0x1a0(%rsp), %r13
movq 0x190(%rsp), %r11
xorl %esi, %esi
xorl %edx, %edx
movl %eax, %r8d
cmpq %rsi, 0xf0(%rsp)
je 0x1766aa
movq 0x180(%rsp), %rdi
imulq $0x1c, (%rdi,%rsi,8), %rdi
testb $0x40, 0x1b(%r11,%rdi)
jne 0x17668f
addq %r11, %rdi
cmpl $0x0, 0x10(%rdi)
jne 0x176683
movl 0xc(%rdi), %r8d
movl 0x14(%rdi), %edi
xorl %r9d, %r9d
cmpq %rdi, (%rcx,%r8,8)
sete %r9b
addq %r9, %rdx
incq %rsi
jmp 0x176643
xorl %eax, %eax
jmp 0x176dbf
testq %rdx, %rdx
jle 0x1766aa
movl 0x4(%r11,%rdi), %eax
xorl %ecx, %ecx
cmpq %r13, %rax
movl %r13d, %ebx
cmovbel %ecx, %ebx
cmpq %r13, %r8
cmovbl %ecx, %ebx
movq %r8, 0x108(%rsp)
leaq -0x14(%r11), %rax
movq %rax, 0x130(%rsp)
leaq -0x1c(%rbp), %rax
movq %rax, 0x138(%rsp)
pushq $-0x1
popq %r15
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbx, 0xc0(%rsp)
cmpq 0xf0(%rsp), %rcx
je 0x176a7c
movq 0x180(%rsp), %rax
movq (%rax,%rcx,8), %rsi
movq %rsi, %rax
subq %r15, %rax
cmpq $0x2, %rax
movq %rcx, 0xf8(%rsp)
movq %rsi, 0xc8(%rsp)
jl 0x17690f
testq %r15, %r15
jns 0x176900
movq %rsi, %rax
sarq $0x3f, %rax
andq %rsi, %rax
decq %rax
imulq $0x1c, %rsi, %rcx
addq 0x130(%rsp), %rcx
movq %rsi, %r15
testq %r15, %r15
jle 0x176752
decq %r15
movl (%rcx), %edx
addq $-0x1c, %rcx
cmpq %r13, %rdx
ja 0x17673a
jmp 0x176900
movq %rax, %r15
jmp 0x176900
imulq $0x1c, %r15, %rcx
movl 0x18(%r11,%rcx), %eax
btl $0x1e, %eax
jae 0x176900
testb $0x1, 0x8(%r10,%rdx,8)
jne 0x176900
leaq (%r11,%rcx), %r14
movl 0x4(%r14), %edx
addl %ebx, %edx
cmpq %r13, %rdx
jb 0x176900
movl 0x8(%r14), %edx
cmpq 0x1a8(%rsp), %rdx
jae 0x176900
movq %r8, 0xb8(%rsp)
movl 0xc(%r11,%rcx), %r13d
shldl $0x1, %eax, %r13d
movl %r13d, %ebp
xorl $0x1, %ebp
movq 0xf8(%rsp), %r12
testq %r12, %r12
movq 0x190(%rsp), %r11
jle 0x17688c
movq 0x180(%rsp), %rax
imulq $0x1c, -0x8(%rax,%r12,8), %rax
movl 0x8(%r11,%rax), %ecx
cmpl 0x4(%r14), %ecx
jbe 0x17688c
leaq (%r11,%rax), %rsi
movl 0xc(%r11,%rax), %ecx
movl 0x18(%r11,%rax), %ebx
shrdl $0x1f, %ecx, %ebx
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0xe8(%rsp), %r8
xorl %r9d, %r9d
callq 0x3d261
cmpl %ebx, %r13d
je 0x17685a
xorl $0x1, %ebx
leaq 0x100(%rsp), %rcx
movq %rcx, (%rsp)
movq 0xd8(%rsp), %rsi
movl %ebp, %edx
movl %ebx, %ecx
movq 0xd0(%rsp), %r8
movsd 0xe0(%rsp), %xmm0
movq %rax, %r9
callq 0x1728e0
decq %r12
testq %rax, %rax
jne 0x1767be
movq 0x1b0(%rsp), %r10
movq 0x190(%rsp), %r11
movq 0xc0(%rsp), %rbx
movq 0x1a0(%rsp), %r13
movq 0xb8(%rsp), %r8
movq 0xc8(%rsp), %rsi
jmp 0x176900
movq 0xb8(%rsp), %r8
imulq $0x1c, %r8, %rax
movups (%r14), %xmm0
movups 0xc(%r14), %xmm1
movq 0x238(%rsp), %r12
movups %xmm1, 0xc(%r12,%rax)
movups %xmm0, (%r12,%rax)
movl 0x4(%r14), %ecx
xorl %edx, %edx
movq 0x1a0(%rsp), %r13
cmpq %r13, %rcx
setb %dl
leal (%rdx,%r15,2), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl 0x18(%r12,%rax), %edx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %edx
orl %ecx, %edx
movl %edx, 0x18(%r12,%rax)
incq %r8
movq 0x1b0(%rsp), %r10
movq 0xc0(%rsp), %rbx
movq 0xc8(%rsp), %rsi
movq %r15, %rdx
incq %r15
cmpq %rsi, %r15
jl 0x17675a
imulq $0x1c, %rsi, %rax
leaq (%r11,%rax), %r14
movl 0xc(%r11,%rax), %ecx
movl 0x18(%r11,%rax), %r15d
shrdl $0x1f, %ecx, %r15d
movl %r15d, %r12d
xorl $0x1, %r12d
imulq $0x1c, %r8, %rcx
movq %r8, %r13
movq 0x138(%rsp), %rax
movq %rcx, 0x140(%rsp)
leaq (%rax,%rcx), %rbp
movq %r8, 0xb8(%rsp)
testq %r13, %r13
jle 0x1769f2
movl 0x8(%rbp), %eax
cmpl 0x4(%r14), %eax
jbe 0x1769f2
movl 0x18(%rbp), %eax
cmpl $-0x1, (%rbp)
setne %cl
btl $0x1d, %eax
setae %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1769e6
movl 0xc(%rbp), %ebx
shldl $0x1, %eax, %ebx
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0xe8(%rsp), %r8
xorl %r9d, %r9d
callq 0x3d261
cmpl %ebx, %r15d
je 0x1769de
xorl $0x1, %ebx
leaq 0x100(%rsp), %rcx
movq %rcx, (%rsp)
movq 0xd8(%rsp), %rsi
movl %r12d, %edx
movl %ebx, %ecx
movq 0xd0(%rsp), %r8
movsd 0xe0(%rsp), %xmm0
movq %rax, %r9
callq 0x1728e0
testq %rax, %rax
jne 0x1769e6
orl $-0x1, 0xc(%rbp)
orl $-0x1, (%rbp)
addq $-0x1c, %rbp
decq %r13
jmp 0x176950
movups (%r14), %xmm0
movups 0xc(%r14), %xmm1
movq 0x140(%rsp), %r12
movq 0x238(%rsp), %rbp
movups %xmm1, 0xc(%rbp,%r12)
movups %xmm0, (%rbp,%r12)
movl 0x18(%rbp,%r12), %eax
movl $0xc0000000, %ecx       # imm = 0xC0000000
andl %ecx, %eax
movq 0xc8(%rsp), %r15
movl %r15d, %ecx
andl $0xfffffff, %ecx        # imm = 0xFFFFFFF
leal (%rax,%rcx,2), %eax
addl $0x20000000, %eax       # imm = 0x20000000
movl %eax, 0x18(%rbp,%r12)
movq 0xb8(%rsp), %r8
incq %r8
movq 0xf8(%rsp), %rcx
incq %rcx
movq 0x1b0(%rsp), %r10
movq 0x190(%rsp), %r11
movq 0xc0(%rsp), %rbx
movq 0x1a0(%rsp), %r13
jmp 0x1766db
movq 0x198(%rsp), %rax
subq %r15, %rax
cmpq $0x2, %rax
jl 0x176c38
movq %r15, %rdx
incq %r15
cmpq 0x198(%rsp), %r15
jge 0x176c38
imulq $0x1c, %r15, %rcx
movl 0x18(%r11,%rcx), %eax
btl $0x1e, %eax
jae 0x176a91
testb $0x1, 0x8(%r10,%rdx,8)
jne 0x176a91
leaq (%r11,%rcx), %r14
movl 0x4(%r14), %edx
addl %ebx, %edx
cmpq %r13, %rdx
jb 0x176a91
movl 0x8(%r14), %edx
cmpq 0x1a8(%rsp), %rdx
jae 0x176a91
movq %r8, 0xb8(%rsp)
movl 0xc(%r11,%rcx), %r13d
shldl $0x1, %eax, %r13d
movl %r13d, %ebp
xorl $0x1, %ebp
movq 0x188(%rsp), %r12
testq %r12, %r12
movq 0x190(%rsp), %r11
jle 0x176bca
movq 0x180(%rsp), %rax
imulq $0x1c, -0x8(%rax,%r12,8), %rax
movl 0x8(%r11,%rax), %ecx
cmpl 0x4(%r14), %ecx
jbe 0x176bca
leaq (%r11,%rax), %rsi
movl 0xc(%r11,%rax), %ecx
movl 0x18(%r11,%rax), %ebx
shrdl $0x1f, %ecx, %ebx
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0xe8(%rsp), %r8
xorl %r9d, %r9d
callq 0x3d261
cmpl %ebx, %r13d
je 0x176b95
xorl $0x1, %ebx
leaq 0x100(%rsp), %rcx
movq %rcx, (%rsp)
movq 0xd8(%rsp), %rsi
movl %ebp, %edx
movl %ebx, %ecx
movq 0xd0(%rsp), %r8
movsd 0xe0(%rsp), %xmm0
movq %rax, %r9
callq 0x1728e0
decq %r12
testq %rax, %rax
jne 0x176af9
movq 0x238(%rsp), %rbp
movq 0x1b0(%rsp), %r10
movq 0x190(%rsp), %r11
movq 0xc0(%rsp), %rbx
movq 0x1a0(%rsp), %r13
movq 0xb8(%rsp), %r8
jmp 0x176a91
movq 0xb8(%rsp), %r8
imulq $0x1c, %r8, %rax
movups (%r14), %xmm0
movups 0xc(%r14), %xmm1
movq 0x238(%rsp), %rbp
movups %xmm1, 0xc(%rbp,%rax)
movups %xmm0, (%rbp,%rax)
movl 0x4(%r14), %ecx
xorl %edx, %edx
movq 0x1a0(%rsp), %r13
cmpq %r13, %rcx
setb %dl
leal (%rdx,%r15,2), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl 0x18(%rbp,%rax), %edx
movl $0xc0000000, %esi       # imm = 0xC0000000
andl %esi, %edx
orl %ecx, %edx
movl %edx, 0x18(%rbp,%rax)
incq %r8
movq 0x1b0(%rsp), %r10
movq 0xc0(%rsp), %rbx
jmp 0x176a91
xorl %eax, %eax
testq %r8, %r8
cmovleq %rax, %r8
movq %rbp, %rcx
movq 0x108(%rsp), %rdi
subq $0x1, %r8
jb 0x176c8b
cmpl $-0x1, (%rcx)
je 0x176c85
imulq $0x1c, %rax, %rdx
movups (%rcx), %xmm0
movups 0xc(%rcx), %xmm1
movups %xmm1, 0xc(%rbp,%rdx)
movups %xmm0, (%rbp,%rdx)
movl 0x18(%rbp,%rdx), %esi
btl $0x1d, %esi
jae 0x176c82
addq %rbp, %rdx
andl $0xdfffffff, %esi       # imm = 0xDFFFFFFF
movl %esi, 0x18(%rdx)
incq %rax
addq $0x1c, %rcx
jmp 0x176c4c
movq 0x240(%rsp), %rcx
movq %rcx, 0xa8(%rsp)
movq %r10, 0xa0(%rsp)
movq 0x180(%rsp), %rcx
movq %rcx, 0x90(%rsp)
movq %r11, 0x88(%rsp)
movq 0x1e8(%rsp), %rcx
movq %rcx, 0x80(%rsp)
movq 0x1e0(%rsp), %rcx
movq %rcx, 0x78(%rsp)
movq 0x1d0(%rsp), %rcx
movq %rcx, 0x68(%rsp)
movq 0x1c0(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq 0x1b8(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq %rbp, 0x40(%rsp)
movq 0x228(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x220(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x218(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x210(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x208(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x200(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x1f8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, (%rsp)
movq %rdi, 0x98(%rsp)
movq 0xd0(%rsp), %r8
movq 0x120(%rsp), %r9
movq 0x110(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq 0x118(%rsp), %rdx
movq 0xd8(%rsp), %rcx
movsd 0xe0(%rsp), %xmm0
movsd 0x128(%rsp), %xmm1
callq 0x1746df
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq