// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: linear_ctg_trans_chain_dp(unsigned int, ul_ov_t*, long, ul_idx_t const*, ug_opt_t const*, long, double, double, double, long, long, long, long, long, Chain_Data*, ul_ov_t*, asg64_v*)
uint64_t linear_ctg_trans_chain_dp(uint32_t qid, ul_ov_t *ch, int64_t ch_n, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, 
double diff_ec_ul, double ovlp_max, double unmatch_max, int64_t qlen, int64_t tlen, int64_t max_skip, int64_t max_iter, int64_t max_dis, Chain_Data* dp, ul_ov_t *res, asg64_v *ea)
{
	memset(res, 0, sizeof((*res)));
	if(ch_n == 0) return 0;
    int64_t i, j, k, l, sc, csc, se, cse, mm_sc, mm_se, mm_idx, max, max_e, peak_sc, peak_e, peak_i, rts, rte, rqs, rqe;
    ul_ov_t *li = NULL, *lj = NULL; int64_t *f, *e, st, plus, max_ii, n_skip, end_j; int32_t *p, *t;
    resize_Chain_Data(dp, ch_n, NULL);
    e = dp->tmp; f = dp->pre; p = dp->score; t = dp->indels;

	i = 0;
	if(i < ch_n) {
		ch[i].tn >>= 1;
		if(ch[i].rev) {
			adjust_rev_tse(&(ch[i]), tlen, &rts, &rte);
			ch[i].ts = rts; ch[i].te = rte; 
		}
	}

	for (i = 1, j = 0; i <= ch_n; i++) {
		if(i < ch_n) {
			ch[i].tn >>= 1;
			if(ch[i].rev) {
				adjust_rev_tse(&(ch[i]), tlen, &rts, &rte);
				ch[i].ts = rts; ch[i].te = rte; 
			}
		}
        if (i == ch_n || ch[i].rev != ch[j].rev) {
            if(i - j > 1) {
				radix_sort_ul_ov_srt_qe(ch+j, ch+i);
				for (k = j + 1, l = j; k <= i; k++) {
					if(k == i || ch[k].qe != ch[l].qe) {
						if(k - l > 1) radix_sort_ul_ov_srt_qs(ch+l, ch+k);
						l = k;
					}
				}
			}
            j = i;
        }
    }

    // fprintf(stderr, "[M::%s::] ch_n:%ld\n", __func__, ch_n);
    memset(t, 0, (ch_n*sizeof((*t)))); peak_sc = peak_e = peak_i = INT32_MIN;
    for (i = st = plus = 0, max_ii = -1; i < ch_n; ++i) {
        li = &(ch[i]); csc = li->sec; cse = 0; 
		if(li->el || li->qn == RC_0 || li->qn == RC_1) cse = li->qe - li->qs;

        mm_sc = csc; mm_se = cse; mm_idx = -1; n_skip = 0; end_j = -1; 
        st = (i<max_iter)?(0):(i-max_iter);
		while (ch[i].rev != ch[st].rev) ++st;

		// if(qid == 2 && qlen == 112183461 && ch[i].tn == 30 && tlen == 24761445) {
		// 	 fprintf(stderr, "+(%ld)[M::%s] h2tg%.6u%c\t%ld\t%u\t%u\t%c\th1tg%.6u%c\t%ld\t%u\t%u\tsec(%u)\tel(%u)\tf(%u)\n", i, __func__,
        //     	qid+1, "lc"[0], qlen, ch[i].qs, ch[i].qe, "+-"[ch[i].rev], ch[i].tn+1, "lc"[0], tlen, ch[i].ts, ch[i].te, ch[i].sec, ch[i].el, ch[i].qn);
		// } else if(ch[i].tn == 2 && tlen == 112183461 && ch[i].qn == 30 && qlen == 24761445) {
		// 	fprintf(stderr, "+(%ld)[M::%s] h1tg%.6u%c\t%ld\t%u\t%u\t%c\th2tg%.6u%c\t%ld\t%u\t%u\tsec(%u)\tel(%u)\tf(%u)\n", i, __func__,
        //     	qid+1, "lc"[0], qlen, ch[i].qs, ch[i].qe, "+-"[ch[i].rev], ch[i].tn+1, "lc"[0], tlen, ch[i].ts, ch[i].te, ch[i].sec, ch[i].el, ch[i].qn);
		// }

        for (j = i-1; j >= st; --j) {
            lj = &(ch[j]); 
            sc = comput_linear_ctg_trans_sc(li, lj, diff_ec_ul, ovlp_max, bw); ///should allow contain
            if(sc == INT32_MIN) continue;
            sc += f[j]; se = cse + e[j];
            if((sc > mm_sc) || (sc == mm_sc && se > mm_se)) {
                mm_sc = sc, mm_se = se, mm_idx = j;
                if (n_skip > 0) --n_skip;
            } else if (t[j] == i) {
                if (++n_skip > max_skip)
                    break;
            }
            if (p[j] >= 0) t[p[j]] = i;
        }


        end_j = j;
        if (max_ii < 0 || (ch[i].qe>(ch[max_ii].qe+max_dis))) {//too long
            max = INT32_MIN; max_e = INT32_MIN; max_ii = -1;
            for (j = i - 1; (j >= st) && (ch[i].qe<=(max_dis+ch[j].qe)); --j) {
                if ((max < f[j]) || (max == f[j] && max_e < e[j])) {
                    max = f[j], max_e = e[j], max_ii = j;
                }
            }
        }
        
        if (max_ii >= 0 && max_ii < end_j) {///just have a try with a[i]<->a[max_ii]
            lj = &(ch[max_ii]); 
            sc = comput_linear_ctg_trans_sc(li, lj, diff_ec_ul, ovlp_max, bw); ///should allow contain
            if(sc != INT32_MIN) {
                sc += f[max_ii];
				se = cse + e[max_ii];
                if((sc > mm_sc) || (sc == mm_sc && se > mm_se)) {
                    mm_sc = sc; mm_se = se; mm_idx = max_ii;
                }
            }
        }
		
        f[i] = mm_sc; e[i] = mm_se; p[i] = mm_idx; 
        if ((max_ii < 0) || ((ch[i].qe<=max_dis+ch[max_ii].qe) && ((f[max_ii]<f[i]) || (f[max_ii] == f[i] && e[max_ii] < e[i])))) {
            max_ii = i;
        }

		if((f[i] > peak_sc) || (f[i] == peak_sc || e[i] > peak_e)) {
			peak_sc = f[i]; peak_e = e[i]; peak_i = i;
		}
        // li->sec = (mm_idx<0?0x3FFFFFFF:i-mm_idx); 
        // sv[i] = *li;
        // fprintf(stderr, "##(%ld) %u\t%u\t%c\tutg%.6d%c(%u)\t%u\t%u\tmm_idx:%ld\tmm_sc:%ld\n", i, li->qs, li->qe, "+-"[li->rev], 
        // (int32_t)(li->tn)+1, "lc"[uref->ug->u.a[li->tn].circ], uref->ug->u.a[li->tn].len, li->ts, li->te, mm_idx, mm_sc);
        // track[i] = push_sc_pre(mm_sc, mm_idx); 
        // li->sec = (mm_idx<0?0x3FFFFFFF:i-mm_idx); sv[i] = *li;
    }

	if(peak_i < 0) return 0;

	uint64_t qs, qgap = 0, ts, tgap = 0, eqs = (uint64_t)-1, eqe = (uint64_t)-1, ea_n = ea->n;
	i = peak_i; *res = ch[i]; 
	res->qn = ((peak_sc>UINT32_MAX)?(UINT32_MAX):(peak_sc));
	res->sec = 0; qs = res->qs; ts = res->ts; 

	// memset(er, 0, sizeof((*er))); er->qs = er->qe = er->ts = er->te = (uint32_t)-1;
	// er->qn = ((peak_e>UINT32_MAX)?(UINT32_MAX):(peak_e));
    
	///has been sorted by qe && te
	while (i >= 0) {
		if(ch[i].qe >= qs) {
			if(ch[i].qs < qs) qs = ch[i].qs;
		} else {
			qgap += qs - ch[i].qe;
			qs = ch[i].qs; 
		}

		if(ch[i].te >= ts) {
			if(ch[i].ts < ts) ts = ch[i].ts;
		} else {
			tgap += ts - ch[i].te;
			ts = ch[i].ts; 
		} 

		if(ch[i].el || ch[i].qn == RC_0 || ch[i].qn == RC_1) {///reliable
			if ((ea->n > ea_n) && (ch[i].qe >= (ea->a[ea->n-1]>>32))) {
				if(ch[i].qs < (ea->a[ea->n-1]>>32)) {
					eqs = ch[i].qs; eqe = (uint32_t)(ea->a[ea->n-1]);
					ea->a[ea->n-1] = eqs << 32; ea->a[ea->n-1] |= eqe;
				}
			} else {
				eqs = ch[i].qs; eqe = ch[i].qe; eqs <<= 32; eqs |= eqe;
				kv_push(uint64_t, *ea, eqs);
				// ea->a[ea->n-1] = eqs << 32; ea->a[ea->n-1] |= eqe;
			}
		}

		if(ch[i].qs < res->qs) res->qs = ch[i].qs;
		if(ch[i].qe > res->qe) res->qe = ch[i].qe;
		if(ch[i].ts < res->ts) res->ts = ch[i].ts;
		if(ch[i].te > res->te) res->te = ch[i].te;
		// if(qid == 2 && qlen == 112183461 && ch[i].tn == 30 && tlen == 24761445) {
		// 	 fprintf(stderr, "-(%ld)[M::%s] h2tg%.6u%c\t%ld\t%u\t%u\t%c\th1tg%.6u%c\t%ld\t%u\t%u\tsec(%u)\tel(%u)\tf(%u)\n", i, __func__,
        //     	qid+1, "lc"[0], qlen, ch[i].qs, ch[i].qe, "+-"[ch[i].rev], ch[i].tn+1, "lc"[0], tlen, ch[i].ts, ch[i].te, ch[i].sec, ch[i].el, ch[i].qn);
		// } else if(ch[i].tn == 2 && tlen == 112183461 && ch[i].qn == 30 && qlen == 24761445) {
		// 	fprintf(stderr, "-(%ld)[M::%s] h1tg%.6u%c\t%ld\t%u\t%u\t%c\th2tg%.6u%c\t%ld\t%u\t%u\tsec(%u)\tel(%u)\tf(%u)\n", i, __func__,
        //     	qid+1, "lc"[0], qlen, ch[i].qs, ch[i].qe, "+-"[ch[i].rev], ch[i].tn+1, "lc"[0], tlen, ch[i].ts, ch[i].te, ch[i].sec, ch[i].el, ch[i].qn);
		// }
        i = p[i];
    }
	////0x3fffffff
	// if(qgap > 0x3fffffff || tgap > 0x3fffffff) {
	// 	ea->n = ea_n; return 0;
	// }

	adjust_rev_tse(res, tlen, &rts, &rte);
	res->ts = rts; res->te = rte; 
	extend_end_coord(NULL, res, qlen, tlen, &rqs, &rqe, &rts, &rte);
	qgap += (rqe - rqs) - (res->qe - res->qs);
	tgap += (rte - rts) - (res->te - res->ts);

	// if(qid == 2 && qlen == 112183461 && ch[0].tn == 30 && tlen == 24761445) {
	// 	fprintf(stderr, "+(%ld)[M::%s] h2tg%.6u%c\t%ld\t%ld(%u)\t%ld(%u)\t%c\th1tg%.6u%c\t%ld\t%ld(%u)\t%ld(%u)\tqgap(%ld)\ttgap(%ld)\n", i, __func__,
	// 	qid+1, "lc"[0], qlen, rqs, res->qs, rqe, res->qe, "+-"[ch[0].rev], ch[0].tn+1, "lc"[0], tlen, rts, res->ts, rte, res->te, qgap, tgap);
	// } else if(ch[i].tn == 2 && tlen == 112183461 && ch[0].qn == 30 && qlen == 24761445) {
	// 	fprintf(stderr, "+(%ld)[M::%s] h2tg%.6u%c\t%ld\t%ld(%u)\t%ld(%u)\t%c\th1tg%.6u%c\t%ld\t%ld(%u)\t%ld(%u)\tqgap(%ld)\ttgap(%ld)\n", i, __func__,
	// 	qid+1, "lc"[0], qlen, rqs, res->qs, rqe, res->qe, "+-"[ch[0].rev], ch[0].tn+1, "lc"[0], tlen, rts, res->ts, rte, res->te, qgap, tgap);
	// }

	// if(qgap > 0x3fffffff || tgap > 0x3fffffff) {
	// 	ea->n = ea_n; return 0;
	// }
	if((qgap > ((rqe-rqs)*unmatch_max)) || (tgap > ((rte-rts)*unmatch_max))) {
		ea->n = ea_n; return 0;
	}

	double qrate = ((double)((rqe-rqs)-qgap))/((double)(rqe-rqs));
	double trate = ((double)((rte-rts)-tgap))/((double)(rte-rts));
	double mrate = MIN(qrate, trate); if(mrate < 0) mrate = 0;
	res->qn *= mrate; 
	if(res->qn <= 0) {
		ea->n = ea_n; return 0;
	}

	res->qs = rqs; res->qe = rqe; res->ts = rts; res->te = rte;
	res->sec = (0x3fffffff);
	if(ea->n > ea_n) {
		uint64_t *a = ea->a + ea_n, an = ea->n - ea_n, ai, an1 = an>>1;
		for(ai = 0; ai < an1; ai++) {
			qs = a[ai]; a[ai] = a[an - ai - 1]; a[an - ai - 1] = qs;
		}
		res->sec = ea->n; kv_push(uint64_t, *ea, an);
	}

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq 0x160(%rsp), %rax
xorpd %xmm3, %xmm3
movupd %xmm3, 0xc(%rax)
movupd %xmm3, (%rax)
testq %rdx, %rdx
je 0x1d8ff9
movq %rdx, %r15
movq %rsi, %rbp
movq %r9, 0x18(%rsp)
movsd %xmm0, 0xb0(%rsp)
movsd %xmm1, 0xb8(%rsp)
movsd %xmm2, 0xa8(%rsp)
movq 0x158(%rsp), %r14
movq %r14, %rdi
movq %rdx, %rsi
xorl %edx, %edx
callq 0x3ecb0
movq 0x10(%r14), %rdi
testq %r15, %r15
jle 0x1d8e93
movq %rdi, 0x90(%rsp)
movq 0x28(%r14), %rax
movq %rax, 0x38(%rsp)
movq (%r14), %rax
movq %rax, 0x68(%rsp)
movq 0x8(%r14), %rdi
shrl 0xc(%rbp)
movq 0x138(%rsp), %rdx
cmpl $0x0, 0x18(%rbp)
jns 0x1d831b
movl 0x10(%rbp), %eax
movl 0x14(%rbp), %ecx
movq %rdx, %rsi
subq %rcx, %rsi
movq %rsi, 0x80(%rsp)
subq %rax, %rdx
movq %rdx, 0x78(%rsp)
movq %rsi, 0x10(%rsp)
movl %esi, 0x10(%rbp)
movq %rdx, 0x8(%rsp)
movl %edx, 0x14(%rbp)
leaq 0x1c(%rbp), %rax
movq %rax, 0x40(%rsp)
movl $0x1, %r13d
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r15, 0x60(%rsp)
movq %rdi, 0x48(%rsp)
cmpq %r15, %r13
jge 0x1d8386
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rax
shrl 0xc(%rbp,%rax)
cmpl $0x0, 0x18(%rbp,%rax)
jns 0x1d8386
addq %rbp, %rax
movl 0x10(%rax), %ecx
movl 0x14(%rax), %edx
movq 0x138(%rsp), %rsi
movq %rsi, %r8
subq %rdx, %r8
subq %rcx, %rsi
movq %r8, 0x10(%rsp)
movl %r8d, 0x10(%rax)
movq %rsi, 0x8(%rsp)
movl %esi, 0x14(%rax)
cmpq %r15, %r13
je 0x1d83ba
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rax
leaq (%r14,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r14, %rcx
movl 0x18(%rbp,%rcx), %ecx
xorl 0x18(%rbp,%rax), %ecx
js 0x1d83ba
movq %r14, %rax
jmp 0x1d8477
movq %r13, %rcx
subq %r14, %rcx
movq %r13, %rax
cmpq $0x2, %rcx
jl 0x1d8477
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %r12
leaq (%r14,%rbp), %rdi
addq %r12, %rdi
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rsi
addq %rbp, %rsi
addq %r13, %rsi
callq 0x19e2b4
movq %r13, %rax
cmpq %r14, %r13
jle 0x1d8477
leaq 0x1(%r14), %r15
movq 0x40(%rsp), %rax
addq %r14, %rax
addq %rax, %r12
leaq (%rbx,%r15), %rax
cmpq $0x1, %rax
je 0x1d8432
movl 0x8(%r12), %eax
leaq (%r14,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r14, %rcx
cmpl 0x8(%rbp,%rcx), %eax
jne 0x1d8432
movq %r14, %rax
jmp 0x1d845d
movq %r15, %rcx
subq %r14, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1d845d
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %rax
addq %rbp, %r14
addq %rax, %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x19e55c
movq %r15, %rax
leaq (%rbx,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %r12
movq %rax, %r14
cmpq $0x2, %rcx
jne 0x1d840d
movq %r13, %rax
leaq 0x1(%r13), %rcx
decq %rbx
movq %rax, %r14
movq 0x60(%rsp), %r15
cmpq %r15, %r13
movq %rcx, %r13
jne 0x1d8339
movq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq (,%r15,4), %rdx
xorl %r14d, %r14d
movq 0x90(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
testq %r15, %r15
jle 0x1d8ffc
movq 0x148(%rsp), %rcx
movq %rcx, %rax
notq %rax
movq %rax, 0xc8(%rsp)
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
negq %rax
addq %rbp, %rax
addq $0x18, %rax
movq %rax, 0xc0(%rsp)
leaq -0x4(%rbp), %rdi
leaq -0x14(%rbp), %rax
movq %rax, 0x98(%rsp)
movq $-0x80000000, %r9       # imm = 0x80000000
movq $-0x1, %rsi
xorl %r11d, %r11d
movq $-0x80000000, %rbx      # imm = 0x80000000
movq $-0x80000000, %r10      # imm = 0x80000000
movq %rbp, 0x70(%rsp)
movsd 0xb8(%rsp), %xmm1
movsd 0xb0(%rsp), %xmm2
movq %rsi, 0x8(%rsp)
movq %r11, 0x20(%rsp)
movq 0x148(%rsp), %rax
cmpq %r11, %rax
movq %r11, %rcx
cmovgq %rax, %rcx
leaq (%r11,%r11,8), %rax
leaq (%rax,%rax,2), %rax
addq %r11, %rax
leaq (%rax,%rbp), %r14
movl 0x18(%rbp,%rax), %edx
btl $0x1e, %edx
jb 0x1d8580
cmpl $0x1, (%r14)
ja 0x1d858a
movl 0x8(%r14), %r8d
subl 0x4(%r14), %r8d
jmp 0x1d858d
xorl %r8d, %r8d
movq 0xc8(%rsp), %rax
leaq (%rax,%rcx), %r13
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq 0xc0(%rsp), %rcx
addq %rax, %rcx
movl %edx, %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
incq %r13
movl (%rcx), %esi
addq $0x1c, %rcx
xorl %edx, %esi
js 0x1d85b3
movq %rbx, 0x50(%rsp)
movq %r10, 0x28(%rsp)
movq %r9, 0x30(%rsp)
movq 0x20(%rsp), %rcx
leaq -0x1(%rcx), %r10
cmpq %r13, %rcx
movq %r10, 0xd0(%rsp)
movq %rdi, 0xd8(%rsp)
movq %r14, 0x40(%rsp)
movq %r8, 0xa0(%rsp)
jle 0x1d8844
movq $-0x1, 0x58(%rsp)
movq %rdi, %rsi
movq %r8, %rdx
movq %r10, %r12
movq $0x0, 0x88(%rsp)
movl 0x18(%r14), %r8d
movl (%rsi), %ecx
xorl %r8d, %ecx
js 0x1d8632
movl 0x14(%r14), %edi
movl -0x4(%rsi), %ecx
cmpl %edi, %ecx
jbe 0x1d8654
movq %rdx, %rcx
movq %rax, %r8
leaq -0x1(%r12), %r10
addq $-0x1c, %rsi
movq %r8, %rax
movq %rcx, %rdx
cmpq %r13, %r12
movq %r10, %r12
jg 0x1d861c
jmp 0x1d8853
movq %r8, 0x10(%rsp)
movl 0x4(%r14), %r11d
movl 0x8(%r14), %r8d
movl -0x14(%rsi), %r15d
movl -0x10(%rsi), %r9d
movl 0x10(%r14), %r10d
movl -0x8(%rsi), %ebp
movq %r10, 0xe0(%rsp)
movq %r11, %r14
subq %r9, %r14
subq %r10, %r14
addq %rcx, %r14
movq %r14, %rbx
negq %rbx
cmovsq %r14, %rbx
movq %r8, %r14
subq %r15, %r14
movq %rdi, %r10
subq %rbp, %r10
cmpq %r10, %r14
cmovgq %r14, %r10
xorps %xmm0, %xmm0
cvtsi2sd %r10, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r14
movq 0x18(%rsp), %r10
cmpq %r10, %r14
jg 0x1d86bf
movq %r10, %r14
cmpq %r14, %rbx
jle 0x1d86d9
movq %rdx, %rcx
movq %rax, %r8
movq 0x70(%rsp), %rbp
movq 0x40(%rsp), %r14
jmp 0x1d8638
movq %r8, %r10
subq %r11, %r10
movq %r9, %rbx
subq %r15, %rbx
cmpq %rbx, %r10
cmovlq %r10, %rbx
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
movq 0x18(%rsp), %r10
cmpq %r10, %rbx
jg 0x1d870a
movq %r10, %rbx
cmpq %r15, %r11
cmovaq %r11, %r15
cmpq %r9, %r8
cmovbq %r8, %r9
movl $0x0, %r8d
subq %r15, %r9
cmovaeq %r9, %r8
movq 0x40(%rsp), %r14
jbe 0x1d8731
cmpq %rbx, %r8
jg 0x1d877a
movq 0xe0(%rsp), %r9
subq %r9, %rdi
movq %rcx, %r8
subq %rbp, %r8
cmpq %r8, %rdi
cmovlq %rdi, %r8
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r8
cmpq %r10, %r8
jg 0x1d8762
movq %r10, %r8
cmpq %rbp, %r9
cmovaq %r9, %rbp
subq %rbp, %rcx
movl $0x0, %edi
cmovaeq %rcx, %rdi
cmpq %r8, %rdi
jle 0x1d878a
movq %rdx, %rcx
movq %rax, %r8
movq 0x70(%rsp), %rbp
jmp 0x1d8638
movq 0x10(%rsp), %r8
andl $0x3fffffff, %r8d       # imm = 0x3FFFFFFF
movq 0x48(%rsp), %rcx
addq (%rcx,%r12,8), %r8
movq 0x38(%rsp), %rcx
movq (%rcx,%r12,8), %rcx
addq 0xa0(%rsp), %rcx
cmpq %rax, %r8
movq 0x70(%rsp), %rbp
jg 0x1d87c1
jne 0x1d87e3
cmpq %rdx, %rcx
jle 0x1d87e3
xorl %eax, %eax
movq 0x88(%rsp), %rdx
testq %rdx, %rdx
setg %al
subq %rax, %rdx
movq %rdx, 0x88(%rsp)
movq %r12, 0x58(%rsp)
jmp 0x1d881d
movq 0x90(%rsp), %rcx
movslq (%rcx,%r12,4), %rcx
cmpq %rcx, 0x20(%rsp)
jne 0x1d8817
movq 0x88(%rsp), %rcx
cmpq 0x140(%rsp), %rcx
jge 0x1d8c7b
incq %rcx
movq %rcx, 0x88(%rsp)
movq %rdx, %rcx
movq %rax, %r8
movq 0x68(%rsp), %rax
movslq (%rax,%r12,4), %rax
testq %rax, %rax
js 0x1d8638
movq 0x90(%rsp), %rdx
movq 0x20(%rsp), %rdi
movl %edi, (%rdx,%rax,4)
jmp 0x1d8638
movq $-0x1, 0x58(%rsp)
movq %r8, %rcx
movq %rax, %r8
movq %r8, 0x10(%rsp)
cmpq $0x0, 0x8(%rsp)
movq 0x60(%rsp), %rbx
movq 0x48(%rsp), %r15
movq 0x150(%rsp), %r12
js 0x1d8896
movl 0x8(%r14), %eax
movq 0x8(%rsp), %rsi
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
movl 0x8(%rbp,%rdx), %edx
addq %r12, %rdx
cmpq %rax, %rdx
jge 0x1d891d
movq 0x20(%rsp), %r11
cmpq %r13, %r11
jle 0x1d8983
movl 0x8(%r14), %eax
movq $-0x80000000, %rsi      # imm = 0x80000000
movq $-0x1, 0x8(%rsp)
movq 0x98(%rsp), %rdx
movq $-0x80000000, %r8       # imm = 0x80000000
movq 0xd0(%rsp), %r11
movl (%rdx), %edi
addq %r12, %rdi
cmpq %rax, %rdi
jl 0x1d891d
movq (%r15,%r11,8), %r9
cmpq %r9, %r8
jge 0x1d88f5
movq 0x38(%rsp), %rsi
movq (%rsi,%r11,8), %rsi
movq %r9, %r8
movq %r11, 0x8(%rsp)
jmp 0x1d890d
jne 0x1d890d
movq 0x38(%rsp), %rdi
movq (%rdi,%r11,8), %rdi
cmpq %rdi, %rsi
jge 0x1d890d
movq %r9, %r8
movq %rdi, %rsi
jmp 0x1d88ee
leaq -0x1(%r11), %rdi
addq $-0x1c, %rdx
cmpq %r13, %r11
movq %rdi, %r11
jg 0x1d88cf
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
sets %al
cmpq %r10, %rdi
setge %dl
orb %al, %dl
leaq (%rdi,%rdi,8), %rax
jne 0x1d89c0
leaq (%rax,%rax,2), %r14
addq %rdi, %r14
movq 0x40(%rsp), %r13
movl 0x18(%r13), %edx
movl 0x18(%rbp,%r14), %esi
xorl %edx, %esi
movq 0x20(%rsp), %r11
js 0x1d896c
addq %rbp, %r14
movl 0x14(%r13), %r10d
movl 0x14(%r14), %esi
cmpl %r10d, %esi
jbe 0x1d8a10
movq 0x160(%rsp), %r8
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %r10
jmp 0x1d8af3
leaq (%r15,%r11,8), %rdx
movq 0x10(%rsp), %rax
movq %rax, (%rdx)
movq 0x38(%rsp), %rax
movq %rcx, (%rax,%r11,8)
movq 0x68(%rsp), %rax
movq 0x58(%rsp), %rsi
movl %esi, (%rax,%r11,4)
movq %r11, %rsi
movq 0x160(%rsp), %r8
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %r10
jmp 0x1d8b42
movq 0x20(%rsp), %r11
leaq (%r15,%r11,8), %rdx
movq 0x10(%rsp), %rsi
movq %rsi, (%rdx)
movq 0x38(%rsp), %rsi
movq %rcx, (%rsi,%r11,8)
movq 0x68(%rsp), %rsi
movq 0x58(%rsp), %r8
movl %r8d, (%rsi,%r11,4)
movq %r11, %rsi
testq %rdi, %rdi
movq 0x160(%rsp), %r8
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %r10
movq 0x50(%rsp), %r14
jns 0x1d8b16
jmp 0x1d8b47
movl 0x4(%r13), %r15d
movl 0x8(%r13), %r11d
movl 0x4(%r14), %r8d
movl 0x8(%r14), %r9d
movl 0x10(%r13), %ebx
movl 0x10(%r14), %r14d
movq %r15, %r12
subq %r9, %r12
subq %rbx, %r12
addq %rsi, %r12
movq %r12, %rdi
negq %rdi
cmovsq %r12, %rdi
movq %r11, %r12
subq %r8, %r12
movq %r10, %r13
subq %r14, %r13
cmpq %r13, %r12
cmovgq %r12, %r13
cvtsi2sd %r13, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r12
movq 0x18(%rsp), %r13
cmpq %r13, %r12
cmovleq %r13, %r12
cmpq %r12, %rdi
jg 0x1d8ac5
movq %r11, %rdi
subq %r15, %rdi
movq %r9, %r12
subq %r8, %r12
cmpq %r12, %rdi
cmovlq %rdi, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r12
cmpq %r13, %r12
cmovleq %r13, %r12
cmpq %r8, %r15
cmovaq %r15, %r8
cmpq %r9, %r11
cmovbq %r11, %r9
movl $0x0, %r11d
subq %r8, %r9
cmovaeq %r9, %r11
jbe 0x1d8ba0
cmpq %r12, %r11
jle 0x1d8ba0
movq 0x160(%rsp), %r8
movq 0x60(%rsp), %rbx
movq 0x30(%rsp), %r9
movq 0x48(%rsp), %r15
movq 0x150(%rsp), %r12
movq 0x28(%rsp), %r10
movq 0x20(%rsp), %r11
movq 0x8(%rsp), %rdi
leaq (%r15,%r11,8), %rdx
movq 0x10(%rsp), %rsi
movq %rsi, (%rdx)
movq 0x38(%rsp), %rsi
movq %rcx, (%rsi,%r11,8)
movq 0x68(%rsp), %rsi
movq 0x58(%rsp), %r14
movl %r14d, (%rsi,%r11,4)
movq 0x40(%rsp), %rsi
movl 0x8(%rsi), %esi
leaq (%rax,%rax,2), %rax
addq %rdi, %rax
movl 0x8(%rbp,%rax), %eax
addq %r12, %rax
cmpq %rsi, %rax
jge 0x1d8b36
movq %rdi, %rsi
jmp 0x1d8b42
movq (%rdx), %rax
cmpq %rax, (%r15,%rdi,8)
jge 0x1d8b8f
movq %r11, %rsi
movq 0x50(%rsp), %r14
movq (%rdx), %rax
cmpq %r10, %rax
setge %dl
cmpq %r14, %rcx
setg %dil
orb %dl, %dil
cmovneq %rax, %r10
cmovneq %rcx, %r14
cmovneq %r11, %r9
incq %r11
movq 0xd8(%rsp), %rdi
addq $0x1c, %rdi
addq $0x1c, 0x98(%rsp)
cmpq %rbx, %r11
movq %r14, %rbx
jne 0x1d8545
jmp 0x1d8c89
jne 0x1d8b31
movq 0x38(%rsp), %rax
cmpq %rcx, (%rax,%rdi,8)
cmovlq %r11, %rdi
jmp 0x1d8b31
subq %rbx, %r10
movq %rsi, %rdi
subq %r14, %rdi
cmpq %rdi, %r10
cmovlq %r10, %rdi
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdi
movq 0x18(%rsp), %r8
cmpq %r8, %rdi
cmovleq %r8, %rdi
cmpq %r14, %rbx
cmovaq %rbx, %r14
subq %r14, %rsi
movl $0x0, %r8d
cmovaeq %rsi, %r8
cmpq %rdi, %r8
movq 0x20(%rsp), %r11
movq 0x8(%rsp), %rdi
jle 0x1d8c14
movq 0x160(%rsp), %r8
movq 0x60(%rsp), %rbx
movq 0x30(%rsp), %r9
movq 0x48(%rsp), %r15
movq 0x150(%rsp), %r12
jmp 0x1d8979
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movq 0x48(%rsp), %r15
addq (%r15,%rdi,8), %rdx
movq 0x38(%rsp), %rsi
movq 0xa0(%rsp), %r14
addq (%rsi,%rdi,8), %r14
cmpq 0x10(%rsp), %rdx
movq 0x160(%rsp), %r8
movq 0x60(%rsp), %rbx
movq 0x30(%rsp), %r9
movq 0x150(%rsp), %r12
movq 0x28(%rsp), %r10
jg 0x1d8c69
jne 0x1d8af3
cmpq %rcx, %r14
jle 0x1d8af3
movq %rdi, 0x58(%rsp)
movq %r14, %rcx
movq %rdx, 0x10(%rsp)
jmp 0x1d8af3
movq %rdx, %rcx
movq %rax, %r8
movq %r12, %r10
jmp 0x1d8853
testq %r9, %r9
js 0x1d8ff9
movq 0x168(%rsp), %rax
movq (%rax), %r11
leaq (%r9,%r9,8), %rax
leaq (%rax,%rax,2), %rax
addq %r9, %rax
movups (%rbp,%rax), %xmm0
movups 0xc(%rbp,%rax), %xmm1
movups %xmm0, (%r8)
movups %xmm1, 0xc(%r8)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r10
cmovlq %r10, %rax
movl %eax, (%r8)
andl $0xc0000000, 0x18(%r8)  # imm = 0xC0000000
movl 0x4(%r8), %ecx
movl 0x10(%r8), %r14d
xorl %r10d, %r10d
movl %ecx, %ebp
xorl %r15d, %r15d
movq %rcx, %rdi
movq %r11, 0x8(%rsp)
leaq (%r9,%r9,8), %rax
leaq (%rax,%rax,2), %rax
addq %r9, %rax
movq 0x70(%rsp), %rdx
leaq (%rdx,%rax), %r13
movl 0x8(%rdx,%rax), %ebx
cmpq %rbx, %rdi
jbe 0x1d8d16
addq %rdi, %r15
subq %rbx, %r15
movl 0x4(%r13), %r12d
movq %r12, %rdi
jmp 0x1d8d21
movl 0x4(%r13), %r12d
cmpq %r12, %rdi
cmovaeq %r12, %rdi
movl 0x14(%r13), %eax
cmpq %rax, %r14
jbe 0x1d8d36
addq %r14, %r10
subq %rax, %r10
movl 0x10(%r13), %r14d
jmp 0x1d8d41
movl 0x10(%r13), %eax
cmpq %rax, %r14
cmovaeq %rax, %r14
testb $0x40, 0x1b(%r13)
jne 0x1d8d53
cmpl $0x1, (%r13)
ja 0x1d8e10
movq %rdi, 0x18(%rsp)
movq 0x168(%rsp), %rax
movq (%rax), %rdx
movq 0x10(%rax), %rax
cmpq %r11, %rdx
jbe 0x1d8d81
movq -0x8(%rax,%rdx,8), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
cmpq %rbx, %rdi
jbe 0x1d8e6b
movq %r14, 0x10(%rsp)
movq 0x168(%rsp), %r14
cmpq 0x8(%r14), %rdx
jne 0x1d8dec
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r15, %r14
movq %r9, %r15
movq %r10, %rbp
callq 0x2370
movq %rbp, %r10
movq 0x8(%rsp), %r11
movq %r15, %r9
movq %r14, %r15
movq 0x168(%rsp), %r14
movq 0x160(%rsp), %r8
movq %rax, 0x10(%r14)
movq (%r14), %rdx
movl 0x4(%r13), %esi
movl 0x4(%r8), %ecx
jmp 0x1d8def
movl %r12d, %esi
shlq $0x20, %r12
orq %rbx, %r12
leaq 0x1(%rdx), %rdi
movq %rdi, (%r14)
movq %r12, (%rax,%rdx,8)
movl %ecx, %ebp
movl %esi, %r12d
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rdi
cmpl %ebp, %r12d
jae 0x1d8e1f
movl %r12d, 0x4(%r8)
movl %r12d, %ecx
movl %r12d, %ebp
movl 0x8(%r13), %eax
movl 0x8(%r8), %r12d
cmpl %r12d, %eax
jbe 0x1d8e33
movl %eax, 0x8(%r8)
movl %eax, %r12d
movl 0x10(%r13), %edx
movl 0x10(%r8), %eax
cmpl %eax, %edx
jae 0x1d8e45
movl %edx, 0x10(%r8)
movl %edx, %eax
movl 0x14(%r13), %esi
movl 0x14(%r8), %edx
cmpl %edx, %esi
jbe 0x1d8e57
movl %esi, 0x14(%r8)
movl %esi, %edx
movq 0x68(%rsp), %rsi
movslq (%rsi,%r9,4), %r9
testq %r9, %r9
jns 0x1d8cea
jmp 0x1d8ea9
cmpl %r12d, %edi
jbe 0x1d8e0b
movl %r12d, %edi
movl %esi, %esi
shlq $0x20, %rdi
movq %rdi, -0x8(%rax,%rdx,8)
movq 0x168(%rsp), %rdx
movq (%rdx), %rdx
orq %rsi, -0x8(%rax,%rdx,8)
jmp 0x1d8e0b
shlq $0x2, %r15
xorl %r14d, %r14d
xorl %esi, %esi
movq %r15, %rdx
callq 0x20e0
jmp 0x1d8ffc
movl %ebp, 0x18(%rsp)
movl %eax, %ebp
movq %rbp, 0x80(%rsp)
movl %edx, %r13d
movq %r13, 0x78(%rsp)
movl 0x18(%r8), %eax
testl %eax, %eax
movq %r10, 0x40(%rsp)
movl %eax, 0x10(%rsp)
movq %r15, %rbx
movq 0x138(%rsp), %rcx
jns 0x1d8ef7
movq %rcx, %rax
subq %r13, %rax
movq %rax, 0x80(%rsp)
movq %rcx, %r13
subq %rbp, %r13
movq %r13, 0x78(%rsp)
movq %rax, %rbp
movl %ebp, 0x10(%r8)
movl %r13d, 0x14(%r8)
leaq 0x78(%rsp), %rax
movq %r8, %rsi
leaq 0x80(%rsp), %r10
leaq 0xf0(%rsp), %r14
leaq 0xe8(%rsp), %r15
xorl %edi, %edi
movq 0x130(%rsp), %rdx
movq %r14, %r8
movq %r15, %r9
pushq %rax
pushq %r10
callq 0x1d0301
addq $0x10, %rsp
movq (%r15), %rax
movq (%r14), %rcx
movq %rax, %rdi
subq %rcx, %rdi
subl 0x18(%rsp), %r12d
movq %rdi, %r8
subq %r12, %r8
addq %rbx, %r8
movq %r8, %xmm0
punpckldq 0x5831e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x58326(%rip), %xmm0    # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
movapd %xmm0, %xmm2
movsd 0xa8(%rsp), %xmm4
mulsd %xmm4, %xmm2
ucomisd %xmm2, %xmm1
ja 0x1d8fe9
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rsi
movq %rdx, %r9
subq %rsi, %r9
subl %ebp, %r13d
movq %r9, %r10
subq %r13, %r10
addq 0x40(%rsp), %r10
movq %r10, %xmm2
punpckldq 0x582bd(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x582c5(%rip), %xmm2    # 0x231290
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
addsd %xmm2, %xmm3
mulsd %xmm1, %xmm4
ucomisd %xmm4, %xmm3
jbe 0x1d9011
movq 0x168(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
xorl %r14d, %r14d
movq %r14, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subq %r8, %rdi
movq %rdi, %xmm2
movq 0x5825f(%rip), %xmm3    # 0x231280
punpckldq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
movapd 0x58263(%rip), %xmm4    # 0x231290
subpd %xmm4, %xmm2
movapd %xmm2, %xmm5
unpckhpd %xmm2, %xmm5            # xmm5 = xmm5[1],xmm2[1]
addsd %xmm2, %xmm5
divsd %xmm0, %xmm5
subq %r10, %r9
movq %r9, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm1, %xmm2
movapd %xmm5, %xmm0
cmplesd %xmm2, %xmm0
andpd %xmm0, %xmm5
andnpd %xmm2, %xmm0
orpd %xmm5, %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movq 0x160(%rsp), %r9
movl (%r9), %edi
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdi
movl %edi, (%r9)
testl %edi, %edi
movq 0x8(%rsp), %rdi
je 0x1d9184
movl %ecx, 0x4(%r9)
movl %eax, 0x8(%r9)
movl %esi, 0x10(%r9)
movl %edx, 0x14(%r9)
movl 0x10(%rsp), %edx
orl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movl %edx, 0x18(%r9)
movq 0x168(%rsp), %rcx
movq (%rcx), %rax
movl $0x1, %r14d
movq %rax, %r15
subq %rdi, %r15
jbe 0x1d8ffc
cmpq $0x2, %r15
jb 0x1d912c
movq 0x10(%rcx), %rsi
leaq (%rsi,%rdi,8), %rcx
movq %r15, %rdx
shrq %rdx
leaq (%rsi,%rax,8), %rax
addq $-0x8, %rax
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
movq (%rax), %r8
movq %r8, (%rcx,%rsi,8)
movq %rdi, (%rax)
incq %rsi
addq $-0x8, %rax
cmpq %rsi, %rdx
jne 0x1d9103
movq 0x168(%rsp), %rcx
movq (%rcx), %rax
movl 0x18(%r9), %edx
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
andl $0xc0000000, %edx       # imm = 0xC0000000
orl %eax, %edx
movl %edx, 0x18(%r9)
movq %rcx, %rbx
movq (%rcx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x1d9174
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r15, (%rax,%rcx,8)
jmp 0x1d8ffc
movq 0x168(%rsp), %rax
movq %rdi, (%rax)
jmp 0x1d8ff9