// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movl 0xc(%rsi), %edi
testq %rdi, %rdi
je 0x12c884
movq (%rdx), %r10
movq %rsi, 0x50(%rsp)
movl (%rsi), %eax
incl %eax
movq %rax, 0x10(%rsp)
movq (%r9), %rax
testq %rax, %rax
sets %sil
movq %r10, (%rsp)
cmpq %r10, %rax
setg %r10b
orb %sil, %r10b
movq %r8, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
jne 0x12c1f5
movq (%r8), %r14
movq (%rcx), %rbx
jmp 0x12c1fc
xorl %r14d, %r14d
xorl %eax, %eax
xorl %ebx, %ebx
movq 0x10(%rsp), %r10
subq %rdi, %r10
testq %rax, %rax
movq %rdi, 0x58(%rsp)
movq %r9, 0x70(%rsp)
jle 0x12c27a
cmpq %r10, %rbx
jl 0x12c27a
movq 0x10(%rdx), %rcx
movl $0xfff, %r11d           # imm = 0xFFF
xorl %esi, %esi
movzwl -0x2(%rcx,%rax,2), %r9d
movl %r9d, %edi
shrl $0xe, %edi
movl $0x3ff, %r8d            # imm = 0x3FF
cmpw $0x1, %di
je 0x12c242
movl $0x3fff, %r8d           # imm = 0x3FFF
testw %r9w, %r9w
cmovsl %r11d, %r8d
leaq -0x1(%rax), %rbp
andl %r9d, %r8d
cmpw $0x2, %di
movq %r8, %r9
cmoveq %rsi, %r9
subq %r9, %rbx
cmpw $0x3, %di
cmoveq %rsi, %r8
subq %r8, %r14
cmpq $0x2, %rax
jl 0x12c27d
movq %rbp, %rax
cmpq %r10, %rbx
jge 0x12c224
jmp 0x12c27d
movq %rax, %rbp
movq $-0x1, %r15
cmpq (%rsp), %rbp
movq %r10, 0x18(%rsp)
movq %rdx, 0x40(%rsp)
jge 0x12c4d2
cmpq 0x10(%rsp), %rbx
jge 0x12c4d2
movq 0x10(%rdx), %r13
movq $-0x1, 0x30(%rsp)
xorl %ecx, %ecx
movq $-0x1, 0x28(%rsp)
movq $-0x1, 0x20(%rsp)
movq $-0x1, %rdi
movq %rdi, 0x8(%rsp)
movq %r15, %r9
movq %rbx, %r8
movq %r14, %rax
movl %ebp, %edx
movzwl (%r13,%rdx,2), %r11d
movl %r11d, %r12d
shrl $0xe, %r12d
leal -0x2(%r12), %edx
cmpl $0x2, %edx
jae 0x12c306
movl %r11d, %ebx
shrl $0xc, %ebx
andl $0x3, %ebx
movl %r11d, %edx
andl $0xfff, %edx            # imm = 0xFFF
jmp 0x12c318
testl %r12d, %r12d
jne 0x12c320
movl %r11d, %edx
andl $0x3fff, %edx           # imm = 0x3FFF
movw $0xffff, %bx            # imm = 0xFFFF
movl $0xffff, %r14d          # imm = 0xFFFF
jmp 0x12c33d
movl %r11d, %ebx
shrl $0xc, %ebx
andl $0x3, %ebx
movl %r11d, %r14d
shrl $0xa, %r14d
andl $0x3, %r14d
movl %r11d, %edx
andl $0x3ff, %edx            # imm = 0x3FF
movzwl %dx, %edx
incl %ebp
cmpq %rbp, (%rsp)
jbe 0x12c3b7
movzwl (%r13,%rbp,2), %esi
movl %esi, %edi
shrl $0xe, %edi
cmpl %r12d, %edi
jne 0x12c3b7
movzwl %r12w, %edi
leal -0x2(%rdi), %r10d
cmpl $0x2, %r10d
jae 0x12c379
movl %esi, %r15d
shrl $0xc, %r15d
andl $0x3, %r15d
andl $0xfff, %esi            # imm = 0xFFF
jmp 0x12c388
testl %edi, %edi
jne 0x12c38f
andl $0x3fff, %esi           # imm = 0x3FFF
movw $0xffff, %r15w          # imm = 0xFFFF
movl $0xffff, %edi           # imm = 0xFFFF
jmp 0x12c3a8
movl %esi, %r15d
shrl $0xc, %r15d
andl $0x3, %r15d
movl %esi, %edi
shrl $0xa, %edi
andl $0x3, %edi
andl $0x3ff, %esi            # imm = 0x3FF
cmpl %r14d, %edi
jne 0x12c3b7
cmpw %r15w, %bx
jne 0x12c3b7
addl %esi, %edx
jmp 0x12c340
cmpw $0x2, %r12w
movl %edx, %r14d
movq %r14, %rbx
cmoveq %rcx, %rbx
addq %r8, %rbx
cmpw $0x3, %r12w
cmoveq %rcx, %r14
addq %rax, %r14
movq 0x18(%rsp), %r10
cmpq %r8, %r10
movq %r8, %rdx
cmovgq %r10, %rdx
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
movq %rbx, %rsi
cmovlq %rdi, %rsi
movq %rsi, %r15
subq %rdx, %r15
cmovleq %rcx, %r15
movl %ebp, %ebp
cmpw $0x2, %r12w
jne 0x12c40d
cmpq %r10, %r8
jl 0x12c412
jmp 0x12c432
testq %r15, %r15
jne 0x12c432
cmpq %rbp, (%rsp)
jle 0x12c4f6
movq %rbx, %r8
movq %r14, %rax
cmpq 0x10(%rsp), %rbx
jl 0x12c2d9
jmp 0x12c4f6
movq %rdx, %r12
subq %r8, %r12
movq %rsi, %rdi
subq %r8, %rdi
addq %rax, %rdi
testw %r11w, %r11w
cmovsq %rcx, %r12
movq %r14, %r8
cmovnsq %rdi, %r8
movq %r8, 0x8(%rsp)
addq %rax, %r12
cmpq $-0x1, %r9
cmovneq %r9, %r12
cmpl $0x4000, %r11d          # imm = 0x4000
setb %al
testq %r15, %r15
setg %r8b
movq 0x20(%rsp), %r10
movq %r10, %r9
movq 0x28(%rsp), %r11
subq %r11, %r9
cmpq %r9, %r15
setg %r9b
andb %r8b, %r9b
testb %al, %r9b
cmovneq %rsi, %r10
movq %r10, 0x20(%rsp)
cmovneq %rdx, %r11
movq %r11, 0x28(%rsp)
movq 0x30(%rsp), %rax
cmovneq %rdi, %rax
movq %rax, 0x30(%rsp)
cmpq %rbp, (%rsp)
jle 0x12c895
cmpq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rdi
movq %r12, %r15
jl 0x12c2cb
jmp 0x12c503
movq $-0x1, %rdi
movq $-0x1, 0x20(%rsp)
movq $-0x1, 0x28(%rsp)
movq $-0x1, 0x30(%rsp)
jmp 0x12c503
movq %r9, %r15
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rdi
cmpq (%rsp), %rbp
jge 0x12c6a1
cmpq 0x10(%rsp), %rbx
jg 0x12c6a1
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rcx
movl %ebp, %eax
movzwl (%rcx,%rax,2), %r13d
movl %r13d, %r8d
shrl $0xe, %r8d
movl $0xffff, %edx           # imm = 0xFFFF
leal -0x2(%r8), %eax
cmpl $0x2, %eax
movq %rdi, 0x8(%rsp)
movq %r15, 0x40(%rsp)
jae 0x12c552
movl %r13d, %eax
shrl $0xc, %eax
andl $0x3, %eax
jmp 0x12c56d
movw $0xffff, %ax            # imm = 0xFFFF
testl %r8d, %r8d
je 0x12c56d
movl %r13d, %eax
shrl $0xc, %eax
andl $0x3, %eax
movl %r13d, %edx
shrl $0xa, %edx
andl $0x3, %edx
leal 0x1(%rbp), %r9d
movzwl %r8w, %r11d
cmpq %r9, (%rsp)
jbe 0x12c5e0
leal 0x2(%rbp), %esi
leal -0x2(%r11), %r15d
movq %r9, %rdi
movzwl (%rcx,%rdi,2), %edi
movl %edi, %r10d
shrl $0xe, %r10d
cmpw %r8w, %r10w
jne 0x12c5e0
movl $0xffff, %r10d          # imm = 0xFFFF
cmpl $0x2, %r15d
jae 0x12c5ad
shrl $0xc, %edi
andl $0x3, %edi
movl %edi, %r12d
jmp 0x12c5cb
movw $0xffff, %r12w          # imm = 0xFFFF
testl %r11d, %r11d
je 0x12c5cb
movl %edi, %r12d
shrl $0xc, %r12d
andl $0x3, %r12d
shrl $0xa, %edi
andl $0x3, %edi
movl %edi, %r10d
cmpl %edx, %r10d
jne 0x12c5e0
cmpw %r12w, %ax
jne 0x12c5e0
movl %esi, %edi
incl %esi
cmpq %rdi, (%rsp)
ja 0x12c585
cmpw $0x2, %r8w
jne 0x12c6a6
movl %r13d, %edx
andl $0xfff, %edx            # imm = 0xFFF
cmpq %r9, (%rsp)
movq 0x40(%rsp), %r15
jbe 0x12c666
shrl $0xc, %r13d
andl $0x3, %r13d
movzwl (%rcx,%r9,2), %eax
movl %eax, %esi
shrl $0xe, %esi
cmpl %r8d, %esi
jne 0x12c666
leal -0x2(%r11), %esi
cmpl $0x2, %esi
jae 0x12c62e
movl %eax, %esi
shrl $0xc, %esi
andl $0x3, %esi
andl $0xfff, %eax            # imm = 0xFFF
jmp 0x12c63c
testl %r11d, %r11d
jne 0x12c641
andl $0x3fff, %eax           # imm = 0x3FFF
movw $0xffff, %si            # imm = 0xFFFF
movb $0x1, %dil
jmp 0x12c650
movl %eax, %esi
shrl $0xc, %esi
andl $0x3, %esi
andl $0x3ff, %eax            # imm = 0x3FF
xorl %edi, %edi
testb %dil, %dil
je 0x12c666
cmpw %si, %r13w
jne 0x12c666
addl %eax, %edx
incl %r9d
cmpq %r9, (%rsp)
ja 0x12c607
movl %edx, %eax
addq %r14, %rax
cmpq $-0x1, %r15
cmovneq %r15, %r14
movq 0x18(%rsp), %r10
cmpq %r10, %rbx
cmovgeq %r14, %r15
movl %r9d, %ebp
movq 0x8(%rsp), %rdi
cmovgeq %rax, %rdi
cmpq %rbp, (%rsp)
jle 0x12c6b8
movq %rax, %r14
cmpq 0x10(%rsp), %rbx
jle 0x12c521
jmp 0x12c6b8
movq %r14, %rax
jmp 0x12c6b8
movq %r14, %rax
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %r15
movq 0x30(%rsp), %rdx
movq 0xd0(%rsp), %r12
cmpq %r12, %rdx
jg 0x12c8a7
cmpq %r12, %rdi
jg 0x12c8c6
movq 0xd8(%rsp), %r13
movq 0xc8(%rsp), %r14
movq 0x60(%rsp), %rcx
movq %rbx, (%rcx)
movq 0x68(%rsp), %rcx
movq %rax, (%rcx)
movq 0x70(%rsp), %rax
movq %rbp, (%rax)
movq $-0x1, %rax
movq %rax, 0x48(%rsp)
movq %rax, 0x38(%rsp)
cmpq %r10, 0x28(%rsp)
jne 0x12c737
movq 0x20(%rsp), %rax
cmpq 0x10(%rsp), %rax
jne 0x12c737
decq %rdx
movq %rdx, 0x38(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
jmp 0x12c7a1
movq %rdi, %rsi
subq %r15, %rsi
jle 0x12c884
movq 0xb8(%rsp), %r8
movq 0xb0(%rsp), %rcx
leaq (%r14,%r12), %rax
subq %rdi, %rax
leaq (%r14,%r15), %rdi
testq %r13, %r13
cmovneq %rax, %rdi
leaq 0x15dfec(%rip), %rax    # 0x28a758
movl (%rax), %edx
notl %edx
andl $0x1, %edx
subq $0x8, %rsp
leaq 0x50(%rsp), %rax
leaq 0x40(%rsp), %r10
movq %r13, %r9
pushq %rax
pushq %r10
pushq 0xd8(%rsp)
callq 0x12a3f4
addq $0x20, %rsp
testq %rax, %rax
je 0x12c7a1
addq %r15, 0x38(%rsp)
movq 0x38(%rsp), %rdx
cmpq $-0x1, %rdx
setne %al
movq 0x48(%rsp), %rcx
cmpq $-0x1, %rcx
setne %sil
andb %al, %sil
cmpb $0x1, %sil
jne 0x12c884
incq %rdx
movq %rdx, %rax
subq %rcx, %rax
leaq 0x15df81(%rip), %rcx    # 0x28a758
testb $0x1, (%rcx)
jne 0x12c84e
movq %r12, %rsi
subq %rdx, %rsi
movq %r12, %rcx
subq %rax, %rcx
testq %r13, %r13
cmoveq %rax, %rsi
cmoveq %rdx, %rcx
movb (%r14,%rsi), %dl
movq %rsi, %rax
sarq $0x3f, %rax
andq %rsi, %rax
testq %rsi, %rsi
jle 0x12c81a
leaq -0x1(%rsi), %rdi
cmpb %dl, -0x1(%r14,%rsi)
movq %rdi, %rsi
je 0x12c801
incq %rdi
movq %rdi, %rax
cmpq %r12, %rcx
jge 0x12c835
movb -0x1(%r14,%rcx), %dl
cmpb %dl, (%r14,%rcx)
jne 0x12c835
incq %rcx
cmpq %rcx, %r12
jne 0x12c824
movq %r12, %rcx
testq %r13, %r13
je 0x12c84b
movq %r12, %rdx
subq %rcx, %rdx
subq %rax, %r12
movq %rdx, %rax
movq %r12, %rdx
jmp 0x12c84e
movq %rcx, %rdx
leaq -0x1(%rdx), %rcx
movq %rcx, 0x38(%rsp)
subq %rax, %rdx
movq %rdx, 0x48(%rsp)
cmpq $0xff, %rdx
ja 0x12c884
movq 0x50(%rsp), %rsi
movl 0x4(%rsi), %eax
shlq $0x20, %rax
orq %rcx, %rax
movq %rax, (%rsi)
movl %edx, 0xc(%rsi)
movl $0x1, %eax
jmp 0x12c886
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rdi
movq %r12, %r15
jmp 0x12c503
leaq 0x1025fc(%rip), %rdi    # 0x22eeaa
leaq 0x10213e(%rip), %rsi    # 0x22e9f3
leaq 0x1025fb(%rip), %rcx    # 0x22eeb7
movl $0xbbc, %edx            # imm = 0xBBC
callq 0x2110
leaq 0x102693(%rip), %rdi    # 0x22ef60
leaq 0x10211f(%rip), %rsi    # 0x22e9f3
leaq 0x1025dc(%rip), %rcx    # 0x22eeb7
movl $0xbbd, %edx            # imm = 0xBBD
callq 0x2110