// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: recalu_minimizer(unsigned long, anchor1_t*, asg16_v*, long*, long*, long*, long*, long*, unsigned long, unsigned long, tiny_queue_t*, All_reads*, UC_Read*, char*, unsigned long, unsigned long)
uint64_t recalu_minimizer(uint64_t rid, anchor1_t *z, asg16_v *sc, int64_t *iok, int64_t *ink, int64_t *ick, int64_t *str_s, int64_t *str_e, uint64_t mz_k, uint64_t mz_h, tiny_queue_t *tq, All_reads *rref, UC_Read *tu, char *qstr, uint64_t qs, uint64_t qe)
{
	int64_t id = z->srt>>33; char *str = NULL; uint64_t rpos, rspan;
	int64_t ok = *iok, nk = *ink, ck = *ick, cn = sc->n, s0, e0, s1, e1, os, oe, ots, ote, ol, wo[2], wn[2], weo[2], wen[2], ovlp, len = Get_READ_LENGTH((*rref), id); uint16_t op, bq, bt; uint32_t cl;
	e0 = ((uint32_t)z->srt) + 1; s0 = e0 - z->other_off; s1 = e1 = -1; weo[0] = weo[1] = wen[0] = wen[1] = -1;
	if(e0 <= s0) return 0;

	///debug
	// ok = nk = ck = 0;

	if((ck < 0) || (ck > cn)) {//(*ck) == cn is allowed
        ck = ok = nk = 0;
    }

	// if(id == 1/**s0 == 10260 && e0 == 10334**/) {
	// 	fprintf(stderr, "\n\n-0-qk::%ld,\ttk::%ld,\tck::%ld,\ts0::%ld,\te0::%ld\n", ok, nk, ck, s0, e0);
	// }

	while (ck > 0 && ok >= s0) {///x -> t; y -> p; first insertion and then match/mismatch
        --ck;
        op = sc->a[ck]>>14;
        // ol = (((op == 1) || (op == 2))?(sc->a[ck]&(0xfff)):(sc->a[ck]&(0x3fff)));
		if((op == 2) || (op == 3)) {
            ol = sc->a[ck]&(0xfff);
        } else if(op == 1) {
            ol = sc->a[ck]&(0x3ff);
        } else {
            ol = sc->a[ck]&(0x3fff);
        }
        if(op != 2) ok -= ol;
        if(op != 3) nk -= ol;
    }

	// char cm[4]; cm[0] = 'M'; cm[1] = 'S'; cm[2] = 'I'; cm[3] = 'D';  
	// if(id == 1/**s0 == 10260 && e0 == 10334**/) {
	// 	fprintf(stderr, "-1-qk::%ld,\ttk::%ld,\tck::%ld\n", ok, nk, ck);
	// }

	while (ck < cn && ok < e0) { ///[s0, e0)
        wo[0] = ok; wn[0] = nk; 
        // ck = pop_trace_bp(sc, ck, &op, &b, &cl);
		ck = pop_trace_bp_f(sc, ck, &op, &bq, &bt, &cl);
        if(op != 2) ok += cl;
        if(op != 3) nk += cl;
        wo[1] = ok; wn[1] = nk;  

		os = ((s0 >= wo[0])? s0 : wo[0]);
		oe = ((e0 <= wo[1])? e0 : wo[1]);
		// os = MAX(s0, wo[0]); oe = MIN(e0, wo[1]);
        ovlp = ((oe>os)? (oe-os):0);
		// if(id == 1/**s0 == 10260 && e0 == 10334**/) {
		// 	fprintf(stderr, "%u%c(q::[%ld,%ld))(t::[%ld,%ld))(ck::%ld)\n", cl, cm[op], wo[0], wo[1], wn[0], wn[1], ck);
		// }

		if(op != 2) {
			if(!ovlp) continue;
		} else {///wo[0] == wo[1]
			if(wo[0] < s0 || wo[0] >= e0) continue;
		}

		if(op < 2) {
            ots = os - wo[0] + wn[0]; ote = oe - wo[0] + wn[0];
        } else {///op == 2: more y; p == 3: more x
            ots = wn[0]; ote = wn[1];
        } 

		if(s1 == -1) s1 = ots;
		e1 = ote;

        if((op == 0) && (ovlp > 0) && (ovlp > weo[1] - weo[0])) {
			weo[0] = os; weo[1] = oe;
			wen[0] = ots; wen[1] = ote;
        }
    }

	while (ck < cn && ok <= e0) { ///[s0, e0)
        wo[0] = ok; wn[0] = nk; 
        // pop_trace_bp(sc, ck, &op, &b, &cl);
		pop_trace_bp_f(sc, ck, &op, &bq, &bt, &cl);
		if(op != 2) break;
		// ck = pop_trace_bp(sc, ck, &op, &b, &cl);
		ck = pop_trace_bp_f(sc, ck, &op, &bq, &bt, &cl);
        // if(op != 2) ok += cl;
        // if(op != 3) nk += cl;
		nk += cl;
        wo[1] = ok; wn[1] = nk;  

		// if(id == 1/**s0 == 10260 && e0 == 10334**/) {
		// 	fprintf(stderr, "%u%c(q::[%ld,%ld))(t::[%ld,%ld))(ck::%ld)\n", cl, cm[op], wo[0], wo[1], wn[0], wn[1], ck);
		// }

		if(wo[0] >= s0 && wo[0] <= e0) {
			ots = wn[0]; ote = wn[1];
			if(s1 == -1) s1 = ots;
			e1 = ote;
		}
    }

	assert(wen[1] <= len);
	assert(e1 <= len);

	*iok = ok; *ink = nk; *ick = ck; 

	if(weo[0] == s0 && weo[1] == e0) {
		z->srt >>= 32; z->srt <<= 32; z->srt |= ((uint64_t)(wen[1]-1));
		///debug
		// char sstr[256]; recover_UC_Read_sub_region(sstr, wen[0], wen[1] - wen[0], 0, rref, id);
		// if(recalu_minimizer0(sstr, wen[1] - wen[0], !(asm_opt.flag & HA_F_NO_HPC), mz_k, mz_h, tq, &rpos, &rspan) && (rpos + 1 == ((uint64_t)(wen[1] - wen[0]))) && (((uint64_t)(wen[1] - wen[0])) == rspan)) {
		// 	// fprintf(stderr, "-0-[M::%s]\n", __func__);
		// } else {
		// 	// if(((z->srt>>32)&1) == 0) {
		// 		fprintf(stderr, "-1-[M::%s]\trid::%lu\ttid::%ld\t%c\trg1::[%ld,%ld)\trg0::[%ld,%ld)\tspan::%ld\n", __func__, rid, id, "+-"[(z->srt>>32)&1], wen[0], wen[1], s0, e0, e0 - s0);
		// 		fprintf(stderr, "tstr::%.*s\n", ((uint32_t)(wen[1] - wen[0])), sstr);
		// 		fprintf(stderr, "qstr::%.*s\n", ((uint32_t)(qe - qs)), qstr + qs);
		// 		exit(1);
		// 	// }
		// }
		// if(rid == 3196 && id == 3199) fprintf(stderr, "-full-[M::%s]\n", __func__);
		return 1;
	}

	if(e1 <= s1) return 0;

	

	if(s1 >= (*str_s) && e1 <= (*str_e)) {
		str = tu->seq + s1 - (*str_s);
	} else {
		if(s1 >= (*str_s) && (s1 < (*str_e)) && (e1 > (*str_e))) {
			UC_Read_resize((*tu), (e1 - (*str_s))); 
			recover_UC_Read_sub_region(tu->seq + (*str_e) - (*str_s), (*str_e), e1 - (*str_e), 0, rref, id);
			str = tu->seq + s1 - (*str_s); (*str_e) = e1;
		} else {
			UC_Read_resize((*tu), (e1 - s1)); 
			recover_UC_Read_sub_region(tu->seq, s1, e1 - s1, 0, rref, id);
			str = tu->seq; (*str_s) = s1; (*str_e) = e1;
		}
	}

	if(recalu_minimizer0(str, e1 - s1, !(asm_opt.flag & HA_F_NO_HPC), mz_k, mz_h, tq, &rpos, &rspan)) {
		rpos += s1;
		z->srt >>= 32; z->srt <<= 32; z->srt |= rpos; z->other_off = rspan;
		// fprintf(stderr, "-1-[M::%s]\trid::%lu\ttid::%ld\t%c\trg1::[%ld,%ld)\trg0::[%ld,%ld)\tspan::%ld\n", __func__, rid, id, "+-"[(z->srt>>32)&1], s1, e1, s0, e0, e0 - s0);
		// if(rid == 3196 && id == 3199) fprintf(stderr, "-part-[M::%s]\n", __func__);
		return 1;
	}
	// fprintf(stderr, "-0-[M::%s]\trid::%lu\ttid::%ld\t%c\trg1::[%ld,%ld)\trg0::[%ld,%ld)\tspan::%ld\n", __func__, rid, id, "+-"[(z->srt>>32)&1], s1, e1, s0, e0, e0 - s0);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl 0xc(%rsi), %eax
testq %rax, %rax
je 0xeb596
movq %rdx, %rbp
movq 0xe8(%rsp), %rdx
movq (%rsi), %r13
movq %r13, %r10
shrq $0x21, %r10
movq (%r9), %r15
movq (%rbp), %rdi
movq 0x18(%rdx), %rdx
incl %r13d
movq %r13, 0x20(%rsp)
subq %rax, %r13
testq %r15, %r15
sets %al
movq %rdi, 0x50(%rsp)
cmpq %rdi, %r15
setg %dil
xorl %r11d, %r11d
orb %al, %dil
cmovneq %r11, %r15
movq %r10, 0x40(%rsp)
movq (%rdx,%r10,8), %rax
movq %rax, 0x70(%rsp)
movq (%r8), %rbx
cmovneq %r11, %rbx
movq (%rcx), %rax
cmovneq %r11, %rax
testq %r15, %r15
jle 0xeb1c7
cmpq %r13, %rax
jl 0xeb1c7
movq 0x10(%rbp), %r10
movzwl -0x2(%r10,%r15,2), %edi
movl %edi, %edx
shrl $0xe, %edx
movw %dx, 0x2(%rsp)
testw %di, %di
js 0xeb191
movzwl -0x2(%r10,%r15,2), %edi
cmpw $0x1, %dx
jne 0xeb19f
andl $0x3ff, %edi            # imm = 0x3FF
jmp 0xeb1a5
movzwl -0x2(%r10,%r15,2), %edi
andl $0xfff, %edi            # imm = 0xFFF
jmp 0xeb1a5
andl $0x3fff, %edi           # imm = 0x3FFF
movzwl %di, %r10d
cmpw $0x2, %dx
movq %r10, %rdi
cmoveq %r11, %rdi
subq %rdi, %rax
cmpw $0x3, %dx
cmoveq %r11, %r10
subq %r10, %rbx
decq %r15
jmp 0xeb15a
movq %rcx, 0x58(%rsp)
movq %r8, 0x60(%rsp)
movq %r9, 0x68(%rsp)
movq %rsi, 0x48(%rsp)
pushq $-0x1
popq %rcx
movq %rcx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rcx, 0x8(%rsp)
movq %rcx, 0x38(%rsp)
movq %rcx, 0x18(%rsp)
movq %rax, %r12
movq %rbx, %r14
cmpq 0x50(%rsp), %r15
jge 0xeb38f
cmpq 0x20(%rsp), %r12
jge 0xeb38f
movq %rbp, %rdi
movl %r15d, %esi
leaq 0x2(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0x4(%rsp), %r8
leaq 0x14(%rsp), %r9
callq 0xeeaa9
movl %eax, %r15d
movzwl 0x2(%rsp), %edx
movl 0x14(%rsp), %eax
cmpw $0x3, %dx
movq %rax, %rcx
movl $0x0, %esi
cmoveq %rsi, %rcx
addq %r14, %rcx
cmpw $0x2, %dx
jne 0xeb302
movq %rcx, %r14
cmpq %r13, %r12
jl 0xeb1fd
movq %r12, %rdi
movq %rbx, %r14
movq %r12, %rax
movq %r12, %rsi
xorl %r8d, %r8d
movq %rdi, %r9
subq %r12, %r9
movq %rsi, %r10
subq %r12, %r10
addq %r14, %r9
cmpw $0x2, %dx
movl $0x0, %r11d
cmovaeq %r11, %r10
movq %rcx, %r11
cmovbq %r9, %r11
movq %r11, 0x8(%rsp)
addq %r14, %r10
movq 0x18(%rsp), %r11
cmpq $-0x1, %r11
cmoveq %r10, %r11
movq %r11, 0x18(%rsp)
testw %dx, %dx
sete %dl
testq %r8, %r8
setg %r10b
movq 0x28(%rsp), %rbx
movq %rbx, %r11
movq 0x30(%rsp), %r14
subq %r14, %r11
cmpq %r11, %r8
setg %r8b
andb %r10b, %r8b
testb %dl, %r8b
cmovneq %rdi, %rbx
movq %rbx, 0x28(%rsp)
cmovneq %rsi, %r14
movq %r14, 0x30(%rsp)
movq 0x38(%rsp), %rdx
cmovneq %r9, %rdx
movq %rdx, 0x38(%rsp)
movq %rcx, %rbx
jmp 0xeb1f7
addq %r12, %rax
cmpq %r12, %r13
movq %r12, %rsi
cmovgq %r13, %rsi
movq 0x20(%rsp), %r8
cmpq %rax, %r8
movq %rax, %rdi
cmovlq %r8, %rdi
movq %rdi, %r8
movq %rcx, %rbx
subq %rsi, %r8
jle 0xeb1f7
movq %rcx, %rbx
je 0xeb1f7
jmp 0xeb272
leaq 0x2(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0x4(%rsp), %r8
leaq 0x14(%rsp), %rbx
movq %rbp, %rdi
movl %r15d, %esi
movq %rbx, %r9
callq 0xeeaa9
movl (%rbx), %ecx
addq %r14, %rcx
movq 0x18(%rsp), %rdx
cmpq $-0x1, %rdx
cmovneq %rdx, %r14
cmpq %r13, %r12
cmovgeq %r14, %rdx
movq %rdx, 0x18(%rsp)
movl %eax, %r15d
movq 0x8(%rsp), %rax
cmovgeq %rcx, %rax
movq %rax, 0x8(%rsp)
movq %rcx, %r14
cmpq 0x50(%rsp), %r15
jge 0xeb3c9
cmpq 0x20(%rsp), %r12
jg 0xeb3c9
leaq 0x2(%rsp), %rbx
leaq 0x6(%rsp), %rcx
leaq 0x4(%rsp), %r8
leaq 0x14(%rsp), %r9
movq %rbp, %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0xeeaa9
cmpw $0x2, (%rbx)
je 0xeb33b
movq 0x70(%rsp), %rax
movq 0x38(%rsp), %rsi
cmpq %rax, %rsi
jg 0xeb5aa
movq 0x8(%rsp), %rdx
cmpq %rax, %rdx
movq 0x18(%rsp), %rbp
jg 0xeb5c9
movq 0x58(%rsp), %rax
movq %r12, (%rax)
movq 0x60(%rsp), %rax
movq %r14, (%rax)
movq 0x68(%rsp), %rax
movq %r15, (%rax)
cmpq %r13, 0x30(%rsp)
jne 0xeb434
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
jne 0xeb434
movq 0x48(%rsp), %rcx
movl 0x4(%rcx), %eax
shlq $0x20, %rax
decq %rsi
orq %rax, %rsi
movq %rsi, (%rcx)
jmp 0xeb591
movq %rdx, %rbx
subq %rbp, %rbx
movq 0x48(%rsp), %r14
jle 0xeb596
movq 0xc0(%rsp), %r13
movq (%r13), %rcx
cmpq %rcx, %rbp
movq 0xc8(%rsp), %r15
movq 0xf0(%rsp), %r12
jge 0xeb4b1
movq (%r12), %rdi
cmpq %rbx, 0x10(%r12)
jge 0xeb485
movq %rbx, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, (%r12)
movq %rbx, 0x10(%r12)
movq %rbp, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
movq 0xe8(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x6c9e
movq (%r12), %rdi
movq %rbp, (%r13)
movq 0x8(%rsp), %rax
movq %rax, (%r15)
jmp 0xeb52e
movq (%r15), %rsi
cmpq %rsi, %rdx
jle 0xeb524
cmpq %rsi, %rbp
jge 0xeb466
movq (%r12), %rdi
movq %rdx, %rax
subq %rcx, %rax
cmpq %rax, 0x10(%r12)
jge 0xeb4f5
movq %rax, %rsi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, %rdi
movq %rax, (%r12)
movq (%r13), %rcx
movq %rdx, %rax
subq %rcx, %rax
movq %rax, 0x10(%r12)
movq (%r15), %rsi
addq %rsi, %rdi
subq %rcx, %rdi
movq %rdx, 0x8(%rsp)
subq %rsi, %rdx
xorl %ecx, %ecx
movq 0xe8(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x6c9e
movq (%r12), %rdi
addq %rbp, %rdi
subq (%r13), %rdi
jmp 0xeb4a7
movq (%r12), %rdi
addq %rbp, %rdi
subq %rcx, %rdi
leaq 0x168223(%rip), %rax    # 0x253758
movl (%rax), %edx
notl %edx
andl $0x1, %edx
leaq 0x78(%rsp), %rax
leaq 0x80(%rsp), %r10
movq %rbx, %rsi
movq 0xd0(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0xe0(%rsp), %r9
pushq %rax
pushq %r10
callq 0xea65e
popq %rcx
popq %rdx
testq %rax, %rax
je 0xeb596
addq 0x80(%rsp), %rbp
movl 0x4(%r14), %eax
shlq $0x20, %rax
orq %rbp, %rax
movq %rax, (%r14)
movl 0x78(%rsp), %eax
movl %eax, 0xc(%r14)
pushq $0x1
popq %rax
jmp 0xeb598
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10180a(%rip), %rdi    # 0x1ecdbb
leaq 0x10153b(%rip), %rsi    # 0x1ecaf3
leaq 0x10180a(%rip), %rcx    # 0x1ecdc9
movl $0xa2c, %edx            # imm = 0xA2C
callq 0x2110
leaq 0x1018c4(%rip), %rdi    # 0x1ece94
leaq 0x10151c(%rip), %rsi    # 0x1ecaf3
leaq 0x1017eb(%rip), %rcx    # 0x1ecdc9
movl $0xa2d, %edx            # imm = 0xA2D
callq 0x2110