// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: recalu_minimizer_non_retrieve(unsigned long, anchor1_t*, asg16_v*, long*, long*, long*, unsigned long, unsigned long, tiny_queue_t*, char*, long, unsigned long)
uint64_t recalu_minimizer_non_retrieve(uint64_t rid, anchor1_t *z, asg16_v *sc, int64_t *iok, int64_t *ink, int64_t *ick, uint64_t mz_k, uint64_t mz_h, tiny_queue_t *tq, char *tstr, int64_t tl, uint64_t trev/** , char *qstr, uint64_t qs, uint64_t qe**/)
{
	char *str = NULL; uint64_t rpos, rspan; char c;
	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, k; 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;


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

	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;
    }

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

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

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

	rpos = rspan = ((uint64_t)-1);
	if(weo[0] == s0 && weo[1] == e0) {
		rpos = wen[1]-1; rspan = e0 - s0;
		// 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;
	} else {
		if(e1 <= s1) return 0;
		// if(s1 >= tl || e1 >= tl) return 0;
		
		if(trev) str = tstr + tl - e1;
		else str = tstr + s1; 

		if(recalu_minimizer0_adv(str, e1 - s1, !(asm_opt.flag & HA_F_NO_HPC), mz_k, mz_h, trev, 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;
		}
	}

	if(rpos != ((uint64_t)-1) && rspan != ((uint64_t)-1)) {
		e0 = rpos + 1; s0 = e0 - rspan;
		if(!(asm_opt.flag & HA_F_NO_HPC)) {
			if(!trev) {
				s1 = s0; e1 = e0; 
			} else {
				s1 = tl - e0; e1 = tl - s0;
			}

			c = tstr[s1];
			for (k = s1 - 1; k >= 0 && tstr[k] == c; k--); s1 = k + 1;

			c = tstr[e1-1];
			for (k = e1; k < tl && tstr[k] == c; k++); e1 = k;

			if(!trev) {
				s0 = s1; e0 = e1; 
			} else {
				s0 = tl - e1; e0 = tl - s1;
			}
		}
		
		rpos = e0 - 1; rspan = e0 - s0;
		if(rspan < 256) {
			z->srt >>= 32; z->srt <<= 32; z->srt |= rpos; z->other_off = rspan;
			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), %r10d
testq %r10, %r10
je 0xec59e
movq %rdx, %rbp
movq %r9, 0x78(%rsp)
movq (%r9), %r14
movq (%rdx), %rdx
movq %rsi, 0x58(%rsp)
movl (%rsi), %r12d
incl %r12d
movq %r12, 0x28(%rsp)
subq %r10, %r12
testq %r14, %r14
sets %al
movq %rdx, 0x50(%rsp)
cmpq %rdx, %r14
setg %dl
xorl %r9d, %r9d
orb %al, %dl
movq (%r8), %rbx
cmovneq %r9, %rbx
cmovneq %r9, %r14
movq (%rcx), %rax
cmovneq %r9, %rax
testq %r14, %r14
jle 0xec1c5
cmpq %r12, %rax
jl 0xec1c5
movq 0x10(%rbp), %rsi
movzwl -0x2(%rsi,%r14,2), %edi
movl %edi, %edx
shrl $0xe, %edx
movw %dx, 0xe(%rsp)
testw %di, %di
js 0xec190
movzwl -0x2(%rsi,%r14,2), %esi
cmpw $0x1, %dx
jne 0xec19e
andl $0x3ff, %esi            # imm = 0x3FF
jmp 0xec1a4
movzwl -0x2(%rsi,%r14,2), %esi
andl $0xfff, %esi            # imm = 0xFFF
jmp 0xec1a4
andl $0x3fff, %esi           # imm = 0x3FFF
movzwl %si, %esi
cmpw $0x2, %dx
movq %rsi, %rdi
cmoveq %r9, %rdi
subq %rdi, %rax
cmpw $0x3, %dx
cmoveq %r9, %rsi
subq %rsi, %rbx
decq %r14
jmp 0xec159
movq %rcx, 0x68(%rsp)
movq %r8, 0x70(%rsp)
movq %r10, 0x60(%rsp)
pushq $-0x1
popq %rcx
movq %rcx, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %rcx, 0x20(%rsp)
movq %rcx, 0x40(%rsp)
movq %rcx, 0x18(%rsp)
movq %rax, %r13
movq %rbx, %r15
cmpq 0x50(%rsp), %r14
jge 0xec388
cmpq 0x28(%rsp), %r13
jge 0xec388
movq %rbp, %rdi
movl %r14d, %esi
leaq 0xe(%rsp), %rdx
leaq 0x12(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x14(%rsp), %r9
callq 0xeeaa9
movl %eax, %r14d
movzwl 0xe(%rsp), %edx
movl 0x14(%rsp), %eax
cmpw $0x3, %dx
movq %rax, %rcx
movl $0x0, %esi
cmoveq %rsi, %rcx
addq %r15, %rcx
cmpw $0x2, %dx
jne 0xec2fb
movq %rcx, %r15
cmpq %r12, %r13
jl 0xec1f6
movq %r13, %rdi
movq %rbx, %r15
movq %r13, %rax
movq %r13, %rsi
xorl %r8d, %r8d
movq %rdi, %r9
subq %r13, %r9
movq %rsi, %r10
subq %r13, %r10
addq %r15, %r9
cmpw $0x2, %dx
movl $0x0, %r11d
cmovaeq %r11, %r10
movq %rcx, %r11
cmovbq %r9, %r11
movq %r11, 0x20(%rsp)
addq %r15, %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 0x30(%rsp), %rbx
movq %rbx, %r11
movq 0x38(%rsp), %r15
subq %r15, %r11
cmpq %r11, %r8
setg %r8b
andb %r10b, %r8b
testb %dl, %r8b
cmovneq %rdi, %rbx
movq %rbx, 0x30(%rsp)
cmovneq %rsi, %r15
movq %r15, 0x38(%rsp)
movq 0x40(%rsp), %rdx
cmovneq %r9, %rdx
movq %rdx, 0x40(%rsp)
movq %rcx, %rbx
jmp 0xec1f0
addq %r13, %rax
cmpq %r13, %r12
movq %r13, %rsi
cmovgq %r12, %rsi
movq 0x28(%rsp), %r8
cmpq %rax, %r8
movq %rax, %rdi
cmovlq %r8, %rdi
movq %rdi, %r8
movq %rcx, %rbx
subq %rsi, %r8
jle 0xec1f0
movq %rcx, %rbx
je 0xec1f0
jmp 0xec26b
leaq 0xe(%rsp), %rdx
leaq 0x12(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x14(%rsp), %rbx
movq %rbp, %rdi
movl %r14d, %esi
movq %rbx, %r9
callq 0xeeaa9
movl (%rbx), %ecx
addq %r15, %rcx
movq 0x18(%rsp), %rdx
cmpq $-0x1, %rdx
cmovneq %rdx, %r15
cmpq %r12, %r13
cmovgeq %r15, %rdx
movq %rdx, 0x18(%rsp)
movl %eax, %r14d
movq 0x20(%rsp), %rax
cmovgeq %rcx, %rax
movq %rax, 0x20(%rsp)
movq %rcx, %r15
cmpq 0x50(%rsp), %r14
jge 0xec3c2
cmpq 0x28(%rsp), %r13
jg 0xec3c2
leaq 0xe(%rsp), %rbx
leaq 0x12(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x14(%rsp), %r9
movq %rbp, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0xeeaa9
cmpw $0x2, (%rbx)
je 0xec334
movq 0xe0(%rsp), %rbp
movq 0x40(%rsp), %rsi
cmpq %rbp, %rsi
jg 0xec5b2
movq 0x20(%rsp), %rdx
cmpq %rbp, %rdx
jg 0xec5d1
movq 0x68(%rsp), %rax
movq %r13, (%rax)
movq 0x70(%rsp), %rax
movq %r15, (%rax)
movq 0x78(%rsp), %rax
movq %r14, (%rax)
orq $-0x1, 0x48(%rsp)
orq $-0x1, 0x80(%rsp)
cmpq %r12, 0x38(%rsp)
jne 0xec432
movq 0x30(%rsp), %rax
cmpq 0x28(%rsp), %rax
jne 0xec432
decq %rsi
movq 0x60(%rsp), %rax
movq %rax, 0x48(%rsp)
jmp 0xec4b3
movq %rdx, %rsi
movq 0x18(%rsp), %r14
subq %r14, %rsi
movq 0xd8(%rsp), %rcx
jle 0xec59e
leaq (%rcx,%rbp), %rax
subq %rdx, %rax
leaq (%rcx,%r14), %rdi
movq 0xe8(%rsp), %r9
testq %r9, %r9
cmovneq %rax, %rdi
leaq 0x1672ec(%rip), %rax    # 0x253758
movl (%rax), %edx
notl %edx
andl $0x1, %edx
subq $0x8, %rsp
leaq 0x50(%rsp), %rax
leaq 0x88(%rsp), %rbx
movq 0xc8(%rsp), %rcx
movq 0xd0(%rsp), %r8
pushq %rax
pushq %rbx
pushq 0xe8(%rsp)
callq 0xeaa14
addq $0x20, %rsp
testq %rax, %rax
cmoveq %rax, %r14
addq (%rbx), %r14
movq %r14, %rsi
cmpq $-0x1, %rsi
setne %al
movq 0x48(%rsp), %rcx
cmpq $-0x1, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0xec59e
incq %rsi
movq %rsi, %rax
subq %rcx, %rax
leaq 0x167277(%rip), %rcx    # 0x253758
testb $0x1, (%rcx)
jne 0xec572
movq %rbp, %rdx
subq %rsi, %rdx
movq %rbp, %rcx
subq %rax, %rcx
cmpq $0x0, 0xe8(%rsp)
cmoveq %rax, %rdx
cmoveq %rsi, %rcx
movq 0xd8(%rsp), %r8
movb (%r8,%rdx), %sil
movq %rdx, %rax
sarq $0x3f, %rax
andq %rdx, %rax
testq %rdx, %rdx
jle 0xec536
leaq -0x1(%rdx), %rdi
cmpb %sil, -0x1(%r8,%rdx)
movq %rdi, %rdx
je 0xec51d
incq %rdi
movq %rdi, %rax
movb -0x1(%r8,%rcx), %dl
cmpq %rbp, %rcx
movq %rbp, %rsi
cmovgq %rcx, %rsi
cmpq %rbp, %rcx
jge 0xec558
cmpb %dl, (%r8,%rcx)
jne 0xec555
incq %rcx
jmp 0xec545
movq %rcx, %rsi
cmpq $0x0, 0xe8(%rsp)
je 0xec572
movq %rbp, %rcx
subq %rsi, %rcx
subq %rax, %rbp
movq %rcx, %rax
movq %rbp, %rsi
movq %rsi, %rcx
subq %rax, %rcx
cmpq $0xff, %rcx
ja 0xec59e
decq %rsi
movq 0x58(%rsp), %rdx
movl 0x4(%rdx), %eax
shlq $0x20, %rax
orq %rsi, %rax
movq %rax, (%rdx)
movl %ecx, 0xc(%rdx)
pushq $0x1
popq %rax
jmp 0xec5a0
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1009f1(%rip), %rdi    # 0x1ecfaa
leaq 0x100533(%rip), %rsi    # 0x1ecaf3
leaq 0x1009f0(%rip), %rcx    # 0x1ecfb7
movl $0xbbc, %edx            # imm = 0xBBC
callq 0x2110
leaq 0x100a88(%rip), %rdi    # 0x1ed060
leaq 0x100514(%rip), %rsi    # 0x1ecaf3
leaq 0x1009d1(%rip), %rcx    # 0x1ecfb7
movl $0xbbd, %edx            # imm = 0xBBD
callq 0x2110