// File: /workspace/github_cpp_makefile_star2_O3/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), %eax
testq %rax, %rax
je 0x1328f1
movq %r9, 0x68(%rsp)
movq (%r9), %r14
movq (%rdx), %rbp
movq %rsi, 0x58(%rsp)
movl (%rsi), %r15d
incl %r15d
movq %r15, %rsi
movq %rax, 0x60(%rsp)
subq %rax, %rsi
testq %r14, %r14
sets %al
cmpq %rbp, %r14
setg %dil
xorl %r13d, %r13d
orb %al, %dil
movq (%r8), %rax
cmovneq %r13, %rax
movq %r14, %r9
cmovneq %r13, %r9
cmoveq (%rcx), %r13
testq %r9, %r9
movq %r8, 0x78(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x50(%rsp)
jle 0x1322ca
cmpq %rsi, %r13
jl 0x1322ca
movq 0x10(%rdx), %rdx
movl $0xfff, %edi            # imm = 0xFFF
xorl %r8d, %r8d
movzwl -0x2(%rdx,%r14,2), %ebx
movl %ebx, %r10d
shrl $0xe, %r10d
movl $0x3ff, %r11d           # imm = 0x3FF
cmpw $0x1, %r10w
je 0x132293
movl $0x3fff, %r11d          # imm = 0x3FFF
testw %bx, %bx
cmovsl %edi, %r11d
leaq -0x1(%r14), %r9
andl %ebx, %r11d
cmpw $0x2, %r10w
movq %r11, %rbx
cmoveq %r8, %rbx
subq %rbx, %r13
cmpw $0x3, %r10w
cmoveq %r8, %r11
subq %r11, %rax
cmpq $0x2, %r14
jb 0x1322ca
movq %r9, %r14
cmpq %rsi, %r13
jge 0x132273
movq %rsi, 0x10(%rsp)
movq $-0x1, 0x38(%rsp)
movw $0xffff, %r10w          # imm = 0xFFFF
movq $-0x1, 0x30(%rsp)
movq $-0x1, 0x28(%rsp)
movq $-0x1, 0x18(%rsp)
movq $-0x1, 0x20(%rsp)
movq %r13, 0x8(%rsp)
movq %rax, %rbx
cmpq %rbp, %r9
jge 0x132544
cmpq %r15, 0x8(%rsp)
jge 0x132544
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %r12
movl %r9d, %ecx
movzwl (%r12,%rcx,2), %r14d
movl %r14d, %r11d
shrl $0xe, %r11d
movw %r10w, 0x4(%rsp)
movw %r10w, 0x6(%rsp)
leal -0x2(%r11), %ecx
cmpl $0x2, %ecx
jae 0x132359
movw $0xfff, %dx             # imm = 0xFFF
movw $0xc, %cx
leaq 0x4(%rsp), %rsi
jmp 0x13237d
movw $0x3fff, %dx            # imm = 0x3FFF
testl %r11d, %r11d
je 0x132389
movl %r14d, %ecx
shrl $0xc, %ecx
andl $0x3, %ecx
movw %cx, 0x4(%rsp)
movw $0x3ff, %dx             # imm = 0x3FF
movw $0xa, %cx
leaq 0x6(%rsp), %rsi
movzwl %r14w, %edi
shrl %cl, %edi
andl $0x3, %edi
movw %di, (%rsi)
movzwl %dx, %ecx
andl %r14d, %ecx
incl %r9d
cmpq %r9, %rbp
jbe 0x132413
movzwl 0x6(%rsp), %edi
movzwl 0x4(%rsp), %r13d
movzwl (%r12,%r9,2), %edx
movl %edx, %esi
shrl $0xe, %esi
cmpl %r11d, %esi
jne 0x132413
movzwl %r11w, %esi
leal -0x2(%rsi), %r8d
cmpl $0x2, %r8d
jae 0x1323cf
movl %edx, %esi
shrl $0xc, %esi
andl $0x3, %esi
andl $0xfff, %edx            # imm = 0xFFF
jmp 0x1323dd
testl %esi, %esi
jne 0x1323e5
andl $0x3fff, %edx           # imm = 0x3FFF
movw $0xffff, %si            # imm = 0xFFFF
movl $0xffff, %r8d           # imm = 0xFFFF
jmp 0x1323fe
movl %edx, %esi
shrl $0xc, %esi
andl $0x3, %esi
movl %edx, %r8d
shrl $0xa, %r8d
andl $0x3, %r8d
andl $0x3ff, %edx            # imm = 0x3FF
cmpl %edi, %r8d
jne 0x132413
cmpw %si, %r13w
jne 0x132413
addl %edx, %ecx
incl %r9d
cmpq %r9, %rbp
ja 0x1323a2
cmpw $0x3, %r11w
je 0x1324ed
movl %r9d, %r9d
movl %ecx, %r13d
leaq (%rbx,%r13), %r12
cmpw $0x2, %r11w
jne 0x132509
movq %r12, %rbx
movq 0x8(%rsp), %rcx
cmpq 0x10(%rsp), %rcx
jl 0x132309
movq 0x8(%rsp), %rdi
movq %rdi, %r11
movq %rax, %rbx
movq %rdi, %r13
xorl %edx, %edx
movq %r12, %rax
movq %r11, %rsi
movq 0x8(%rsp), %rcx
subq %rcx, %rsi
movq %rdi, %r8
subq %rcx, %r8
addq %rbx, %rsi
testw %r14w, %r14w
movl $0x0, %ecx
cmovsq %rcx, %r8
cmovnsq %rsi, %r12
movq %r12, 0x18(%rsp)
addq %rbx, %r8
movq 0x20(%rsp), %rcx
cmpq $-0x1, %rcx
cmoveq %r8, %rcx
movq %rcx, 0x20(%rsp)
movzwl %r14w, %r8d
cmpl $0x4000, %r8d           # imm = 0x4000
setb %r8b
testq %rdx, %rdx
setg %bl
movq 0x28(%rsp), %rcx
movq %rcx, %r14
movq 0x30(%rsp), %r12
subq %r12, %r14
cmpq %r14, %rdx
setg %dl
andb %bl, %dl
testb %r8b, %dl
cmovneq %r11, %rcx
movq %rcx, 0x28(%rsp)
cmovneq %rdi, %r12
movq %r12, 0x30(%rsp)
movq 0x38(%rsp), %rcx
cmovneq %rsi, %rcx
movq %rcx, 0x38(%rsp)
jmp 0x132301
movzwl 0x4(%rsp), %eax
movw %ax, 0x6(%rsp)
movw $0xffff, 0x4(%rsp)      # imm = 0xFFFF
movl %r9d, %r9d
movl %ecx, %r13d
movq %rbx, %rax
jmp 0x13250c
movq %r12, %rax
movq 0x8(%rsp), %rdi
addq %rdi, %r13
movq 0x10(%rsp), %rcx
cmpq %rdi, %rcx
cmovgq %rcx, %rdi
cmpq %r13, %r15
movq %r13, %r11
cmovlq %r15, %r11
movq %r11, %rdx
subq %rdi, %rdx
jle 0x132301
movq %rax, %r12
je 0x132301
jmp 0x132459
cmpq %rbp, %r9
jge 0x132701
cmpq %r15, 0x8(%rsp)
movq 0x10(%rsp), %r8
jg 0x1326f9
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
movq %r15, 0x48(%rsp)
movl %r9d, %ecx
movzwl (%rax,%rcx,2), %r14d
movl %r14d, %r12d
shrl $0xe, %r12d
movw $0xffff, %cx            # imm = 0xFFFF
movw %cx, 0x4(%rsp)
movw %cx, 0x6(%rsp)
movw $0xc, %cx
leal -0x2(%r12), %esi
leaq 0x4(%rsp), %rdx
cmpl $0x2, %esi
jb 0x1325b7
testl %r12d, %r12d
je 0x1325c2
movl %r14d, %ecx
shrl $0xc, %ecx
andl $0x3, %ecx
movw %cx, 0x4(%rsp)
movw $0xa, %cx
leaq 0x6(%rsp), %rdx
movl %r14d, %esi
shrl %cl, %esi
andl $0x3, %esi
movw %si, (%rdx)
leal 0x1(%r9), %ecx
cmpq %rcx, %rbp
jbe 0x13263f
movzwl 0x6(%rsp), %r13d
movzwl 0x4(%rsp), %r11d
leal 0x2(%r9), %r10d
movzwl %r12w, %edi
leal -0x2(%rdi), %edx
movq %rcx, %rsi
movzwl (%rax,%rsi,2), %esi
movl %esi, %r8d
shrl $0xe, %r8d
cmpw %r12w, %r8w
jne 0x13263f
movl $0xffff, %r8d           # imm = 0xFFFF
cmpl $0x2, %edx
jae 0x13260c
shrl $0xc, %esi
andl $0x3, %esi
movl %esi, %r15d
jmp 0x132629
movw $0xffff, %r15w          # imm = 0xFFFF
testl %edi, %edi
je 0x132629
movl %esi, %r15d
shrl $0xc, %r15d
andl $0x3, %r15d
shrl $0xa, %esi
andl $0x3, %esi
movl %esi, %r8d
cmpl %r13d, %r8d
jne 0x13263f
cmpw %r15w, %r11w
jne 0x13263f
movl %r10d, %esi
incl %r10d
cmpq %rsi, %rbp
ja 0x1325e5
cmpw $0x2, %r12w
jne 0x1326ec
movl %r14d, %edx
shrl $0xc, %edx
andl $0x3, %edx
andl $0xfff, %r14d           # imm = 0xFFF
movw $0xffff, 0x6(%rsp)      # imm = 0xFFFF
movw %dx, 0x4(%rsp)
cmpq %rcx, %rbp
movq 0x48(%rsp), %r15
movq 0x10(%rsp), %r8
jbe 0x1326ac
movq 0x18(%rsp), %r9
movzwl (%rax,%rcx,2), %esi
movl %esi, %edi
andl $0xc000, %edi           # imm = 0xC000
negw %di
jno 0x1326a8
movl %esi, %edi
shrl $0xc, %edi
andl $0x3, %edi
cmpw %di, %dx
jne 0x1326a8
andl $0xfff, %esi            # imm = 0xFFF
addl %esi, %r14d
incl %ecx
cmpq %rcx, %rbp
ja 0x13267a
movl %ecx, %ecx
jmp 0x1326b1
movq 0x18(%rsp), %r9
movl %r14d, %edx
addq %rbx, %rdx
movq 0x20(%rsp), %rsi
cmpq $-0x1, %rsi
cmovneq %rsi, %rbx
cmpq %r8, 0x8(%rsp)
cmovgeq %rbx, %rsi
movq %rsi, 0x20(%rsp)
cmovgeq %rdx, %r9
movq %r9, 0x18(%rsp)
movq %rcx, %r9
movq %rdx, %rbx
cmpq %rcx, %rbp
jg 0x13256b
jmp 0x13270c
movq %rbx, %rdx
movq %r9, %rcx
movq 0x48(%rsp), %r15
jmp 0x132707
movq %rbx, %rdx
movq %r9, %rcx
jmp 0x13270c
movq %rbx, %rdx
movq %r9, %rcx
movq 0x10(%rsp), %r8
movq 0xe0(%rsp), %r14
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
jg 0x132905
movq 0x18(%rsp), %r9
cmpq %r14, %r9
movq 0x78(%rsp), %rax
movq 0x70(%rsp), %rsi
jg 0x132924
movq 0x8(%rsp), %r10
movq %r10, (%rsi)
movq %rdx, (%rax)
movq 0x68(%rsp), %rax
movq %rcx, (%rax)
movq $-0x1, %rax
movq %rax, 0x40(%rsp)
movq %rax, 0x80(%rsp)
cmpq %r8, 0x30(%rsp)
jne 0x132781
cmpq %r15, 0x28(%rsp)
jne 0x132781
decq %rdi
movq 0x60(%rsp), %rax
movq %rax, 0x40(%rsp)
jmp 0x132806
movq %r9, %rax
movq 0x20(%rsp), %r15
subq %r15, %rax
movq 0xd8(%rsp), %rdx
jle 0x1328f1
leaq (%rdx,%r14), %rcx
subq %r9, %rcx
leaq (%rdx,%r15), %rdi
movq 0xe8(%rsp), %r9
testq %r9, %r9
cmovneq %rcx, %rdi
leaq 0x162f9d(%rip), %rcx    # 0x295758
movl (%rcx), %edx
notl %edx
andl $0x1, %edx
subq $0x8, %rsp
leaq 0x48(%rsp), %r10
leaq 0x88(%rsp), %rbx
movq %rax, %rsi
movq 0xc8(%rsp), %rcx
movq 0xd0(%rsp), %r8
pushq %r10
pushq %rbx
pushq 0xe8(%rsp)
callq 0x1304b7
addq $0x20, %rsp
testq %rax, %rax
cmoveq %rax, %r15
addq (%rbx), %r15
movq %r15, %rdi
cmpq $-0x1, %rdi
setne %al
movq 0x40(%rsp), %rcx
cmpq $-0x1, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x1328f1
incq %rdi
movq %rdi, %rax
subq %rcx, %rax
leaq 0x162f24(%rip), %rcx    # 0x295758
testb $0x1, (%rcx)
jne 0x1328c3
movq %r14, %rdx
subq %rdi, %rdx
movq %r14, %rcx
subq %rax, %rcx
cmpq $0x0, 0xe8(%rsp)
cmoveq %rax, %rdx
cmoveq %rdi, %rcx
movq 0xd8(%rsp), %r8
movb (%r8,%rdx), %sil
movq %rdx, %rax
sarq $0x3f, %rax
andq %rdx, %rax
testq %rdx, %rdx
jle 0x132889
leaq -0x1(%rdx), %rdi
cmpb %sil, -0x1(%r8,%rdx)
movq %rdi, %rdx
je 0x132870
incq %rdi
movq %rdi, %rax
cmpq %r14, %rcx
jge 0x1328a4
movb -0x1(%r8,%rcx), %dl
cmpb %dl, (%r8,%rcx)
jne 0x1328a4
incq %rcx
cmpq %rcx, %r14
jne 0x132893
movq %r14, %rcx
cmpq $0x0, 0xe8(%rsp)
je 0x1328c0
movq %r14, %rdx
subq %rcx, %rdx
subq %rax, %r14
movq %rdx, %rax
movq %r14, %rdi
jmp 0x1328c3
movq %rcx, %rdi
movq %rdi, %rcx
subq %rax, %rcx
cmpq $0xff, %rcx
ja 0x1328f1
decq %rdi
movq 0x58(%rsp), %rdx
movl 0x4(%rdx), %eax
shlq $0x20, %rax
orq %rdi, %rax
movq %rax, (%rdx)
movl %ecx, 0xc(%rdx)
movl $0x1, %eax
jmp 0x1328f3
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1087ae(%rip), %rdi    # 0x23b0ba
leaq 0x1082f0(%rip), %rsi    # 0x23ac03
leaq 0x1087ad(%rip), %rcx    # 0x23b0c7
movl $0xbbc, %edx            # imm = 0xBBC
callq 0x2110
leaq 0x108845(%rip), %rdi    # 0x23b170
leaq 0x1082d1(%rip), %rsi    # 0x23ac03
leaq 0x10878e(%rip), %rcx    # 0x23b0c7
movl $0xbbd, %edx            # imm = 0xBBD
callq 0x2110