// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: overlap_statistics(overlap_region_alloc*, ma_ug_t*, long*, unsigned char)
uint32_t overlap_statistics(overlap_region_alloc* olist, ma_ug_t *ug, int64_t *tt, uint8_t mm)
{
	uint32_t k, sp = (uint32_t)-1, ep = (uint32_t)-1, l = 0;
	for (k = 0; k < olist->length; k++) {
		/**
		if(b->olist.list[k].y_id != 38) continue;
		**/		
		/**
		
		for (z = 0, te = ta = tua = 0; z < b->olist.list[k].w_list_length; z++) {
			if(b->olist.list[k].w_list[z].y_end != -1) {
				te += b->olist.list[k].w_list[z].error;
				ta += b->olist.list[k].w_list[z].x_end + 1 - b->olist.list[k].w_list[z].x_start;
				fprintf(stderr, "x->[%lu, %lu), y->[%d, %d), e->%d\n", b->olist.list[k].w_list[z].x_start, b->olist.list[k].w_list[z].x_end+1, 
					b->olist.list[k].w_list[z].y_start, b->olist.list[k].w_list[z].y_end+1, b->olist.list[k].w_list[z].error);
			}
			else {
				tua += b->olist.list[k].w_list[z].x_end + 1 - b->olist.list[k].w_list[z].x_start;
			}
		}
		fprintf(stderr, "[M::utg%.6d%c::is_match:%u] x->[%u, %u); y->[%u, %u), ualigned->%u, e_rate->%f\n", b->olist.list[k].y_id+1, "lc"[s->ug->u.a[b->olist.list[k].y_id].circ], 
		b->olist.list[k].is_match == 1, b->olist.list[k].x_pos_s, b->olist.list[k].x_pos_e+1, b->olist.list[k].y_pos_s, b->olist.list[k].y_pos_e+1, tua, (float)te/(float)ta);
		**/
		if(tt){
			uint32_t z;
			for (z = 0; z < olist->list[k].w_list.n; z++) {
				if(olist->list[k].w_list.a[z].y_end != -1) {
					if(tt) *tt += olist->list[k].w_list.a[z].x_end+1-olist->list[k].w_list.a[z].x_start;
				}
			}
		}
		if(olist->list[k].is_match == mm) {
			if(sp == (uint32_t)-1 || ep < olist->list[k].x_pos_s) {
				if(sp != (uint32_t)-1) l += ep + 1 - sp;
				sp = olist->list[k].x_pos_s;
				ep = olist->list[k].x_pos_e;
			} else {
				ep = MAX(ep, olist->list[k].x_pos_e);
			}
			if(ug) {
				fprintf(stderr, "[M::utg%.6d%c::is_match->%u] rev->%u, x->[%u, %u), y->[%u, %u)\n", (int)olist->list[k].y_id+1, "lc"[ug->u.a[olist->list[k].y_id].circ], olist->list[k].is_match, 
				olist->list[k].y_pos_strand, olist->list[k].x_pos_s, olist->list[k].x_pos_e+1, olist->list[k].y_pos_s, olist->list[k].y_pos_e+1);
			}
		}
	}
	if(sp != (uint32_t)-1) l += ep + 1 - sp;
	return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %ebx
movq %rdx, %r14
movq %rsi, %r8
movq %rdi, %r12
pushq $-0x1
popq %r13
xorl %ebp, %ebp
movl %r13d, %r15d
xorl %r9d, %r9d
movl %ebp, %ebp
cmpq %rbp, 0x10(%r12)
jbe 0x1602e7
movq (%r12), %rax
testq %r14, %r14
je 0x160223
imulq $0xa8, %rbp, %rcx
addq %rax, %rcx
addq $0x48, %rcx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, (%rcx)
jbe 0x160223
movq 0x10(%rcx), %rsi
movq %rdx, %rdi
shlq $0x5, %rdi
cmpl $-0x1, 0xc(%rsi,%rdi)
je 0x16021f
addq %rdi, %rsi
movl 0x4(%rsi), %edi
subl (%rsi), %edi
incl %edi
movslq %edi, %rsi
addq %rsi, (%r14)
incl %edx
jmp 0x1601f6
imulq $0xa8, %rbp, %rcx
cmpb %bl, 0x2c(%rax,%rcx)
jne 0x160267
addq %rcx, %rax
movl 0x4(%rax), %r10d
cmpl $-0x1, %r13d
je 0x16024b
cmpl %r10d, %r15d
jae 0x160257
subl %r13d, %r15d
addl %r15d, %r9d
incl %r9d
movl 0x8(%rax), %r11d
movl %r10d, %r13d
movl %r11d, %r15d
jmp 0x160262
movl 0x8(%rax), %r11d
cmpl %r11d, %r15d
cmovbel %r11d, %r15d
testq %r8, %r8
jne 0x16026e
incl %ebp
jmp 0x1601d0
movq 0xf2d6b(%rip), %rcx     # 0x252fe0
movq (%rcx), %rdi
movl 0x10(%rax), %ecx
movl 0x14(%rax), %edx
movq %rdx, 0x10(%rsp)
leal 0x1(%rcx), %edx
movq 0x10(%r8), %rsi
imulq $0x28, %rcx, %rcx
movl (%rsi,%rcx), %ecx
shrl $0x1f, %ecx
leaq 0x863f0(%rip), %rsi     # 0x1e668b
movsbl (%rcx,%rsi), %ecx
movq %r9, 0x18(%rsp)
movl 0x1c(%rax), %r9d
incl %r11d
movl 0x18(%rax), %eax
incl %eax
movq %rax, 0x8(%rsp)
leaq 0x91e41(%rip), %rsi     # 0x1f20fd
movq %r8, 0x20(%rsp)
movl %ebx, %r8d
xorl %eax, %eax
pushq 0x8(%rsp)
pushq 0x18(%rsp)
pushq %r11
pushq %r10
callq 0x22b0
movq 0x38(%rsp), %r9
movq 0x40(%rsp), %r8
addq $0x20, %rsp
jmp 0x160267
subl %r13d, %r15d
incl %r15d
xorl %eax, %eax
cmpl $-0x1, %r13d
cmovnel %r15d, %eax
addl %r9d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq