// File: /workspace/github_c_makefile_star2_O2/lh3[P]minigraph/algo.c
// Function: mg_intv_overlap
int32_t mg_intv_overlap(void *km, int32_t n_a, const mg_intv_t *a, int32_t st, int32_t en, int32_t **b_, int32_t *m_b_)
{
	int32_t t = 0, h, *b = *b_, m_b = *m_b_, n = 0;
	istack_t stack[64], *p;

	for (h = 0; 1<<h <= n_a; ++h);
	--h;
	p = &stack[t++];
	p->k = h, p->x = (1LL<<p->k) - 1, p->w = 0; // push the root into the stack
	while (t) { // stack is not empyt
		istack_t z = stack[--t];
		if (z.k <= 3) { // the subtree is no larger than (1<<(z.k+1))-1; do a linear scan
			int32_t i, i0 = z.x >> z.k << z.k, i1 = i0 + (1LL<<(z.k+1)) - 1;
			if (i1 >= n_a) i1 = n_a;
			for (i = i0; i < i1 && a[i].st < en; ++i)
				if (st < a[i].en) {
					if (n == m_b) KEXPAND(km, b, m_b);
					b[n++] = i;
				}
		} else if (z.w == 0) { // if left child not processed
			int32_t y = z.x - (1LL<<(z.k-1));
			p = &stack[t++];
			p->k = z.k, p->x = z.x, p->w = 1;
			if (y >= n_a || a[y].far > st) {
				p = &stack[t++];
				p->k = z.k - 1, p->x = y, p->w = 0; // push the left child to the stack
			}
		} else if (z.x < n_a && a[z.x].st < en) {
			if (st < a[z.x].en) { // then z.x overlaps the query; write to the output array
				if (n == m_b) KEXPAND(km, b, m_b);
				b[n++] = z.x;
			}
			p = &stack[t++];
			p->k = z.k - 1, p->x = z.x + (1LL<<(z.k-1)), p->w = 0; // push the right child
		}
	}
	*b_ = b, *m_b_ = m_b;
	return n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movl %r8d, 0x8(%rsp)
movl %ecx, %r8d
movq %rdx, %r10
movl %esi, 0x4(%rsp)
movq 0x480(%rsp), %rcx
movq %r9, 0x30(%rsp)
movq (%r9), %rax
movl (%rcx), %r9d
pushq $-0x2
popq %rdx
pushq $0x1
popq %r11
leal 0x2(%rdx), %ecx
movl %r11d, %esi
shll %cl, %esi
incl %edx
cmpl 0x4(%rsp), %esi
jle 0x343b
movl %r11d, (%rsp)
movq %rdi, 0x10(%rsp)
movl %edx, 0x48(%rsp)
pushq $-0x1
popq %rdi
movq %rdi, %rsi
movl %edx, %ecx
shlq %cl, %rsi
notq %rsi
movq %rsi, 0x40(%rsp)
andl $0x0, 0x4c(%rsp)
movslq 0x4(%rsp), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x4(%r10), %rcx
movq %rcx, 0x28(%rsp)
xorl %r11d, %r11d
movl %r8d, 0xc(%rsp)
movq %r10, 0x18(%rsp)
movl (%rsp), %ecx
movl %ecx, %ebp
leal -0x1(%rbp), %ecx
movl %ecx, (%rsp)
movslq %ebp, %rdx
shlq $0x4, %rdx
leaq 0x40(%rsp), %rcx
leaq (%rdx,%rcx), %r15
addq $-0x10, %r15
leaq (%rdx,%rcx), %r14
addq $-0x4, %r14
testl %ebp, %ebp
je 0x3687
movq (%r15), %rbx
movslq 0x8(%r15), %r13
cmpq $0x3, %r13
jle 0x3529
cmpl $0x0, (%r14)
jne 0x35ec
decl %r13d
movq %rdi, %rsi
movl %r13d, %ecx
shlq %cl, %rsi
addq %rbx, %rsi
movl $0x1, (%r14)
movq %rsi, %rcx
shlq $0x20, %rcx
cmpl 0x4(%rsp), %esi
jge 0x3507
movq %rcx, %rsi
sarq $0x1c, %rsi
cmpl %r8d, 0x8(%r10,%rsi)
jle 0x34b7
incl %ebp
leaq (%rsp,%rdx), %r14
addq $0x4c, %r14
movl %r13d, -0x4(%r14)
sarq $0x20, %rcx
movq %rcx, -0xc(%r14)
andl $0x0, (%r14)
movl %ebp, (%rsp)
jmp 0x3490
movl %r13d, %ecx
shrq %cl, %rbx
shlq %cl, %rbx
incb %r13b
pushq $0x1
popq %rdx
movl %r13d, %ecx
shlq %cl, %rdx
leal (%rbx,%rdx), %ecx
decl %ecx
movl 0x4(%rsp), %edx
cmpl %edx, %ecx
cmovgel %edx, %ecx
movslq %ebx, %rbx
movslq %ecx, %rsi
movq %rbx, %r15
shlq $0x4, %r15
addq 0x28(%rsp), %r15
movl 0x8(%rsp), %r12d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rsi, 0x38(%rsp)
cmpq %rsi, %rbx
jge 0x3490
cmpl %r12d, -0x4(%r15)
jae 0x3490
movl (%r15), %ecx
andl %edx, %ecx
cmpl %r8d, %ecx
jle 0x35e3
cmpl %r9d, %r11d
jne 0x35da
movl %r11d, %r13d
sarl %r13d
addl %r11d, %r13d
cmpl $0x4, %r11d
pushq $0x10
popq %rcx
cmovll %ecx, %r13d
movslq %r13d, %rdx
shlq $0x2, %rdx
movq %rdi, %r14
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movl %r11d, %ebp
callq 0x2774
movq 0x38(%rsp), %rsi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %ebp, %r11d
movq %r14, %rdi
movl %r13d, %r9d
movq 0x18(%rsp), %r10
movl 0xc(%rsp), %r8d
movslq %r11d, %rcx
incl %r11d
movl %ebx, (%rax,%rcx,4)
incq %rbx
addq $0x10, %r15
jmp 0x356d
cmpq 0x20(%rsp), %rbx
jge 0x3490
movq %rbx, %rcx
shlq $0x4, %rcx
movl 0x8(%rsp), %edx
cmpl %edx, (%r10,%rcx)
jae 0x3490
addq %r10, %rcx
movl 0x4(%rcx), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
cmpl %r8d, %ecx
jle 0x366c
cmpl %r9d, %r11d
jne 0x3663
movl %r11d, %r12d
sarl %r12d
addl %r11d, %r12d
cmpl $0x4, %r11d
pushq $0x10
popq %rcx
cmovll %ecx, %r12d
movslq %r12d, %rdx
shlq $0x2, %rdx
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movl %r11d, (%rsp)
callq 0x2774
movl (%rsp), %r11d
pushq $-0x1
popq %rdi
movl %r12d, %r9d
movq 0x18(%rsp), %r10
movl 0xc(%rsp), %r8d
movslq %r11d, %rcx
incl %r11d
movl %ebx, (%rax,%rcx,4)
decl %r13d
pushq $0x1
popq %rdx
movl %r13d, %ecx
shlq %cl, %rdx
movl %r13d, 0x8(%r15)
addq %rbx, %rdx
movq %rdx, (%r15)
jmp 0x351d
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
movq 0x480(%rsp), %rax
movl %r9d, (%rax)
movl %r11d, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq