// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: recalu_minimizer_bd(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_bd(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, si, ei, 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 = si = ei = -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;
			// if((op == 0) && (wn[1] > ots) && ((wn[0] < ots) || (ots == 0))) si = 1;
			if((op == 0) && (wo[1] > s0) && ((wo[0] < s0) || (s1 == 0))) si = 1;
		}
		
		e1 = ote;
		if((op == 0) && (wo[0] < e0) && ((wo[1] > e0) || (e1 == len))) ei = 1;

        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(e1 <= s1) return 0;
	rpos = rspan = ((uint64_t)-1);

	if(weo[0] == s0 && weo[1] == e0) {
		rpos = wen[1]-1; rspan = e0 - s0;
		/** 
		if(si == 1 && ei == 1) {
			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(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;
		}
	}

	if(rpos != ((uint64_t)-1) && rspan != ((uint64_t)-1)) {
        e0 = rpos + 1; s0 = e0 - rspan;
        if(!(asm_opt.flag & HA_F_NO_HPC)) {
			if(si == -1) {
				s0 -= hpc_minimizer_test(id, rref, tu, *str_s, *str_e, s0, len, 1, 8);
				assert(s0 >= 0 && s0 < len && s0 < e0);
			}

			if(ei == -1) {
				e0 += hpc_minimizer_test(id, rref, tu, *str_s, *str_e, e0-1, len, 0, 8);
				assert(e0 >= 0 && e0 < len && s0 < e0);
			}
        }
        
        rpos = e0 - 1; rspan = e0 - s0;
        if(rspan < 256) {
            z->srt >>= 32; z->srt <<= 32; z->srt |= rpos; z->other_off = rspan;
            return 1;
        }
    }
	/**
	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 $0xa8, %rsp
movq %rdx, %r12
movl 0xc(%rsi), %edx
testq %rdx, %rdx
je 0xec022
movq 0x108(%rsp), %rax
movq %rsi, 0x78(%rsp)
movq (%rsi), %r13
movq %r13, %rdi
shrq $0x21, %rdi
movq %r9, 0x98(%rsp)
movq (%r9), %r14
movq (%r12), %rsi
movq 0x18(%rax), %rax
incl %r13d
movq %r13, 0x28(%rsp)
movq %rdx, 0x80(%rsp)
subq %rdx, %r13
testq %r14, %r14
sets %dl
movq %rsi, 0x70(%rsp)
cmpq %rsi, %r14
setg %sil
xorl %r10d, %r10d
orb %dl, %sil
cmovneq %r10, %r14
movq %rdi, 0x30(%rsp)
movq (%rax,%rdi,8), %r9
movq (%r8), %rbx
cmovneq %r10, %rbx
movq (%rcx), %rax
cmovneq %r10, %rax
testq %r14, %r14
jle 0xebb30
cmpq %r13, %rax
jl 0xebb30
movq 0x10(%r12), %rsi
movzwl -0x2(%rsi,%r14,2), %edi
movl %edi, %edx
shrl $0xe, %edx
movw %dx, 0xa(%rsp)
testw %di, %di
js 0xebafb
movzwl -0x2(%rsi,%r14,2), %esi
cmpw $0x1, %dx
jne 0xebb09
andl $0x3ff, %esi            # imm = 0x3FF
jmp 0xebb0f
movzwl -0x2(%rsi,%r14,2), %esi
andl $0xfff, %esi            # imm = 0xFFF
jmp 0xebb0f
andl $0x3fff, %esi           # imm = 0x3FFF
movzwl %si, %esi
cmpw $0x2, %dx
movq %rsi, %rdi
cmoveq %r10, %rdi
subq %rdi, %rax
cmpw $0x3, %dx
cmoveq %r10, %rsi
subq %rsi, %rbx
decq %r14
jmp 0xebac3
movq %rcx, 0x88(%rsp)
movq %r8, 0x90(%rsp)
movq %r9, 0x40(%rsp)
pushq $-0x1
popq %rcx
movq %rcx, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %rcx, 0x38(%rsp)
movq %rcx, 0x48(%rsp)
movq %rcx, 0x10(%rsp)
movq %rcx, 0x60(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, %r15
movq %rbx, %rbp
cmpq 0x70(%rsp), %r14
jge 0xebd50
cmpq 0x28(%rsp), %r15
jge 0xebd50
movq %r12, %rdi
movl %r14d, %esi
leaq 0xa(%rsp), %rdx
leaq 0xe(%rsp), %rcx
leaq 0xc(%rsp), %r8
leaq 0x1c(%rsp), %r9
callq 0xeeaa9
movl %eax, %r14d
movzwl 0xa(%rsp), %edx
movl 0x1c(%rsp), %eax
cmpw $0x3, %dx
movq %rax, %rcx
movl $0x0, %esi
cmoveq %rsi, %rcx
addq %rbp, %rcx
cmpw $0x2, %dx
jne 0xebc21
movq %rcx, %rbp
cmpq %r13, %r15
jl 0xebb71
movq %r15, %rdi
movq %rbx, %rbp
movq %r15, %rax
movq %r15, %rsi
xorl %r8d, %r8d
movq %rdi, %r9
subq %r15, %r9
movq %rsi, %r10
subq %r15, %r10
addq %rbp, %r10
addq %rbp, %r9
cmpw $0x2, %dx
cmovbq %r10, %rbp
movq %rcx, %r11
cmovbq %r9, %r11
movq %r11, 0x10(%rsp)
movq 0x20(%rsp), %r11
cmpq $-0x1, %r11
je 0xebc57
movq %r11, %rbp
pushq $0x1
popq %r11
movq 0x38(%rsp), %rbx
jmp 0xebc82
addq %r15, %rax
cmpq %r15, %r13
movq %r15, %rsi
cmovgq %r13, %rsi
movq 0x28(%rsp), %r8
cmpq %rax, %r8
movq %rax, %rdi
cmovlq %r8, %rdi
movq %rdi, %r8
movq %rcx, %rbx
subq %rsi, %r8
jle 0xebb6b
movq %rcx, %rbx
je 0xebb6b
jmp 0xebbe2
testw %dx, %dx
pushq $0x1
popq %r11
movq 0x38(%rsp), %rbx
jne 0xebc82
cmpq %r13, %rax
jle 0xebc82
testq %r10, %r10
movq 0x48(%rsp), %r10
cmoveq %r11, %r10
cmpq %r13, %r15
cmovlq %r11, %r10
movq %r10, 0x48(%rsp)
movq 0x10(%rsp), %r10
cmpq 0x40(%rsp), %r10
movq %rbx, %r10
cmoveq %r11, %r10
cmpq 0x28(%rsp), %rax
cmovgq %r11, %r10
testw %dx, %dx
sete %dl
cmoveq %r10, %rbx
movq %rbx, 0x38(%rsp)
testq %r8, %r8
setg %r10b
movq 0x50(%rsp), %rbx
movq %rbx, %r11
movq 0x58(%rsp), %r15
subq %r15, %r11
cmpq %r11, %r8
setg %r8b
andb %r10b, %r8b
testb %r8b, %dl
cmovneq %rdi, %rbx
movq %rbx, 0x50(%rsp)
cmovneq %rsi, %r15
movq %r15, 0x58(%rsp)
movq 0x60(%rsp), %rdx
cmovneq %r9, %rdx
movq %rdx, 0x60(%rsp)
movq %rbp, 0x20(%rsp)
movq %rcx, %rbx
jmp 0xebb6b
leaq 0xa(%rsp), %rdx
leaq 0xe(%rsp), %rcx
leaq 0xc(%rsp), %r8
leaq 0x1c(%rsp), %rbx
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %r9
callq 0xeeaa9
movl (%rbx), %ecx
addq %rbp, %rcx
movq 0x20(%rsp), %rdx
cmpq $-0x1, %rdx
cmovneq %rdx, %rbp
cmpq %r13, %r15
cmovgeq %rbp, %rdx
movq %rdx, 0x20(%rsp)
movl %eax, %r14d
movq 0x10(%rsp), %rax
cmovgeq %rcx, %rax
movq %rax, 0x10(%rsp)
movq %rcx, %rbp
cmpq 0x70(%rsp), %r14
jge 0xebd8a
cmpq 0x28(%rsp), %r15
jg 0xebd8a
leaq 0xa(%rsp), %rbx
leaq 0xe(%rsp), %rcx
leaq 0xc(%rsp), %r8
leaq 0x1c(%rsp), %r9
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0xeeaa9
cmpw $0x2, (%rbx)
je 0xebcfc
movq 0x40(%rsp), %r10
movq 0x60(%rsp), %rcx
cmpq %r10, %rcx
jg 0xec095
movq 0x10(%rsp), %rdx
cmpq %r10, %rdx
jg 0xec0b4
movq 0x88(%rsp), %rax
movq %r15, (%rax)
movq 0x90(%rsp), %rax
movq %rbp, (%rax)
movq 0x98(%rsp), %rax
movq %r14, (%rax)
movq %rdx, %rbx
movq 0x20(%rsp), %rbp
subq %rbp, %rbx
jle 0xec022
orq $-0x1, 0x68(%rsp)
orq $-0x1, 0xa0(%rsp)
cmpq %r13, 0x58(%rsp)
jne 0xebe2f
movq 0x50(%rsp), %rax
cmpq 0x28(%rsp), %rax
jne 0xebe2f
decq %rcx
movq 0x80(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %rcx, %rbp
movq 0x110(%rsp), %r15
movq 0xe8(%rsp), %r12
movq 0xe0(%rsp), %r13
jmp 0xebf61
movq 0xe0(%rsp), %r13
movq (%r13), %rcx
cmpq %rcx, %rbp
movq 0x110(%rsp), %r15
movq 0xe8(%rsp), %r12
jge 0xebe97
movq (%r15), %rdi
cmpq %rbx, 0x10(%r15)
jge 0xebe6b
movq %rbx, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, (%r15)
movq %rbx, 0x10(%r15)
movq %rbp, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
movq 0x108(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0x6c9e
movq (%r15), %rdi
movq %rbp, (%r13)
movq 0x10(%rsp), %rax
movq %rax, (%r12)
jmp 0xebf12
movq (%r12), %rsi
cmpq %rsi, %rdx
jle 0xebf09
cmpq %rsi, %rbp
jge 0xebe50
movq (%r15), %rdi
movq %rdx, %rax
subq %rcx, %rax
cmpq %rax, 0x10(%r15)
jge 0xebed9
movq %rax, %rsi
callq 0x2370
movq 0x10(%rsp), %rdx
movq %rax, %rdi
movq %rax, (%r15)
movq (%r13), %rcx
movq %rdx, %rax
subq %rcx, %rax
movq %rax, 0x10(%r15)
movq (%r12), %rsi
addq %rsi, %rdi
subq %rcx, %rdi
movq %rdx, %r14
subq %rsi, %rdx
xorl %ecx, %ecx
movq 0x108(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0x6c9e
movq (%r15), %rdi
addq %rbp, %rdi
subq (%r13), %rdi
movq %r14, (%r12)
jmp 0xebf12
movq (%r15), %rdi
addq %rbp, %rdi
subq %rcx, %rdi
leaq 0x16783f(%rip), %rax    # 0x253758
movl (%rax), %edx
notl %edx
andl $0x1, %edx
leaq 0x68(%rsp), %rax
leaq 0xa0(%rsp), %r14
movq %rbx, %rsi
movq 0xf0(%rsp), %rcx
movq 0xf8(%rsp), %r8
movq 0x100(%rsp), %r9
pushq %rax
pushq %r14
callq 0xea65e
popq %rcx
popq %rdx
testq %rax, %rax
cmoveq %rax, %rbp
addq (%r14), %rbp
movq 0x40(%rsp), %r10
cmpq $-0x1, %rbp
setne %cl
movq 0x68(%rsp), %rax
cmpq $-0x1, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xec022
leaq 0x1(%rbp), %r14
movq %r14, %rbx
subq %rax, %rbx
leaq 0x1677c8(%rip), %rax    # 0x253758
testb $0x1, (%rax)
jne 0xebff3
cmpq $-0x1, 0x48(%rsp)
jne 0xebfeb
movq (%r13), %rcx
movq (%r12), %r8
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %r9
pushq $0x8
pushq $0x1
pushq %r10
callq 0xeb5e8
movq 0x60(%rsp), %r10
addq $0x20, %rsp
subq %rax, %rbx
js 0xec0d3
cmpq %r10, %rbx
jge 0xec0d3
cmpq %r14, %rbx
jge 0xec0d3
cmpq $-0x1, 0x38(%rsp)
je 0xec036
movq %r14, %rax
movq %rax, %rcx
subq %rbx, %rcx
cmpq $0xff, %rcx
ja 0xec022
decq %rax
movq 0x78(%rsp), %rsi
movl 0x4(%rsi), %edx
shlq $0x20, %rdx
orq %rax, %rdx
movq %rdx, (%rsi)
movl %ecx, 0xc(%rsi)
pushq $0x1
popq %rax
jmp 0xec024
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rcx
movq (%r12), %r8
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq %r15, %rdx
movq %rbp, %r9
pushq $0x8
pushq $0x0
pushq %r10
movq %r10, %r15
callq 0xeb5e8
addq $0x20, %rsp
addq %r14, %rax
js 0xec076
cmpq %r15, %rax
jge 0xec076
cmpq %rax, %rbx
jl 0xebff6
leaq 0x100f0e(%rip), %rdi    # 0x1ecf8b
leaq 0x100a6f(%rip), %rsi    # 0x1ecaf3
leaq 0x100e13(%rip), %rcx    # 0x1ece9e
movl $0xb52, %edx            # imm = 0xB52
callq 0x2110
leaq 0x100d1f(%rip), %rdi    # 0x1ecdbb
leaq 0x100a50(%rip), %rsi    # 0x1ecaf3
leaq 0x100df4(%rip), %rcx    # 0x1ece9e
movl $0xb18, %edx            # imm = 0xB18
callq 0x2110
leaq 0x100dd9(%rip), %rdi    # 0x1ece94
leaq 0x100a31(%rip), %rsi    # 0x1ecaf3
leaq 0x100dd5(%rip), %rcx    # 0x1ece9e
movl $0xb19, %edx            # imm = 0xB19
callq 0x2110
leaq 0x100e92(%rip), %rdi    # 0x1ecf6c
leaq 0x100a12(%rip), %rsi    # 0x1ecaf3
leaq 0x100db6(%rip), %rcx    # 0x1ece9e
movl $0xb4d, %edx            # imm = 0xB4D
callq 0x2110