// File: /workspace/github_cpp_makefile_star2_O2/auton1[P]viterbi/entry_filters.cpp
// Function: entry::filter_sites_by_BED_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool)
void entry::filter_sites_by_BED_file(const string &bed_file, bool BED_exclude)
{
	// Filter sites depending on positions in a BED file.
	if (passed_filters == false)
		return;

	if (bed_file == "")
		return;

	int pos1, pos2, idx;
	if (lims.empty())
	{
		ifstream BED(bed_file.c_str());
		if (!BED.is_open())
			LOG.error("Could not open BED file: " + bed_file);

		string chr;
		unsigned int N_chr=chr_to_idx.size();
		BED.ignore(numeric_limits<streamsize>::max(), '\n');	// Ignore header
		unsigned int N_BED_entries=0;
		while (!BED.eof())
		{
			BED >> chr >> pos1 >> pos2;
			BED.ignore(numeric_limits<streamsize>::max(), '\n');

			if (chr_to_idx.find(chr) == chr_to_idx.end())
			{
				N_chr++;
				chr_to_idx[chr] = (N_chr-1);
				lims.resize(N_chr);
			}

			idx = chr_to_idx[chr];
			lims[idx].push_back(make_pair(pos1,pos2));
			N_BED_entries++;
		}
		BED.close();

		LOG.printLOG("\tRead " + output_log::int2str(N_BED_entries) + " BED file entries.\n");

		for (unsigned int ui=0; ui<lims.size(); ui++)
			sort(lims[ui].begin(), lims[ui].end());
	}
	vector<unsigned int> min_ui(lims.size(), 0);
	parse_basic_entry(true);

	pos1 = POS;
	pos2 = pos1;
	unsigned int N_alleles = get_N_alleles();
	for (int i=0; i<(int)N_alleles; i++)
		pos2 = max(pos2, (int)(pos1 + get_allele(i).length() - 1));

	if (BED_exclude == false)
	{	// Exclude sites not in BED file
		if (chr_to_idx.find(CHROM) == chr_to_idx.end())
			passed_filters = false;
		else
		{
			idx = chr_to_idx[CHROM];
			bool found=false;
			unsigned int max_ui = lims[idx].size();
			for (unsigned int ui=min_ui[idx]; ui<max_ui; ui++)
			{	// No need to start this loop at zero every time...
				if (((pos1 > lims[idx][ui].first) && (pos1 <= lims[idx][ui].second)) ||	// Start pos inside bin
					((pos2 > lims[idx][ui].first) && (pos2 <= lims[idx][ui].second)) ||	// End pos inside bin
					((pos1 <= lims[idx][ui].first) && (pos2 >= lims[idx][ui].second)))	// Variant spans bin
				{
					found=true;
					break;
				}
				else if (pos1 > lims[idx][ui].second)
					min_ui[idx] = ui+1;
			}
			if (found == false)
				passed_filters = false;
		}
	}
	else
	{	// Exclude sites in BED file
		if (chr_to_idx.find(CHROM) != chr_to_idx.end())
		{
			idx = chr_to_idx[CHROM];
			bool found=false;
			unsigned int max_ui = lims[idx].size();
			for (unsigned int ui=min_ui[idx]; ui<max_ui; ui++)
			{	// No need to start this loop at zero every time...
				if (((pos1 > lims[idx][ui].first) && (pos1 <= lims[idx][ui].second)) ||	// Start pos inside bin
					((pos2 > lims[idx][ui].first) && (pos2 <= lims[idx][ui].second)) ||	// End pos inside bin
					((pos1 <= lims[idx][ui].first) && (pos2 >= lims[idx][ui].second)))	// Variant spans bin
				{
					found=true;
					break;
				}
				else if (pos1 > lims[idx][ui].second)
					min_ui[idx] = ui+1;
			}
			if (found == true)
				passed_filters = false;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
cmpb $0x0, 0xc(%rdi)
je 0x22fbe
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xc499(%rip), %rsi      # 0x2ee37
movq %r14, %rdi
callq 0x96f3
testb %al, %al
jne 0x22fbe
movq 0x1f9fb(%rip), %rcx     # 0x423b0
movq 0x1f9fc(%rip), %rax     # 0x423b8
pushq $0x50
popq %rsi
cmpq %rax, %rcx
jne 0x22c5b
movq (%r14), %rsi
leaq 0xf0(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x5860
leaq 0x168(%rsp), %rdi
callq 0x50d0
testb %al, %al
jne 0x22a27
leaq 0xc3f2(%rip), %rsi      # 0x2ede5
leaq 0x90(%rsp), %rdi
movq %r14, %rdx
callq 0x967d
leaq 0x1eac6(%rip), %rdi     # 0x414d0
leaq 0x90(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2bf18
leaq 0x90(%rsp), %rdi
callq 0x5908
movl %ebp, 0xc(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x1f965(%rip), %r14     # 0x423a8
leaq 0xf0(%rsp), %rdi
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %rdx
callq 0x5280
xorl %r15d, %r15d
leaq 0xf0(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0x1f90c(%rip), %rbp     # 0x42380
movq 0xf0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x110(%rsp,%rax)
jne 0x22b3e
movq %r12, %rdi
movq %r13, %rsi
callq 0x55c0
movq %rax, %rdi
leaq 0x4(%rsp), %rsi
callq 0x5250
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x5250
movq %r12, %rdi
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %rdx
callq 0x5280
movq %rbp, %rdi
movq %r13, %rsi
callq 0x13a1a
leaq 0x1f8ae(%rip), %rcx     # 0x42388
cmpq %rcx, %rax
jne 0x22aff
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1316c
movl %r14d, (%rax)
incl %r14d
leaq 0x1f8b9(%rip), %rdi     # 0x423b0
movq %r14, %rsi
callq 0x24424
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1316c
movslq (%rax), %rax
imulq $0x50, %rax, %rdi
addq 0x1f898(%rip), %rdi     # 0x423b0
movl 0x4(%rsp), %eax
movl 0x8(%rsp), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, 0x30(%rsp)
leaq 0x30(%rsp), %rsi
callq 0x25214
incl %r15d
jmp 0x22a74
leaq 0xf0(%rsp), %rdi
callq 0x50a0
leaq 0x50(%rsp), %rdi
movl %r15d, %esi
callq 0x2c1fc
leaq 0xc2a0(%rip), %rsi      # 0x2edff
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x98d5
leaq 0xc291(%rip), %rdx      # 0x2ee06
leaq 0x70(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x98b6
leaq 0x1e945(%rip), %rdi     # 0x414d0
leaq 0x70(%rsp), %rsi
callq 0x2beca
leaq 0x70(%rsp), %rdi
callq 0x5908
leaq 0x30(%rsp), %rdi
callq 0x5908
leaq 0x50(%rsp), %rdi
callq 0x5908
xorl %r12d, %r12d
leaq 0xd0(%rsp), %r14
leaq 0xb0(%rsp), %r15
movl %r12d, %r12d
movq 0x1f7e8(%rip), %rax     # 0x423b8
movq 0x1f7d9(%rip), %rcx     # 0x423b0
subq %rcx, %rax
cqto
pushq $0x50
popq %rsi
idivq %rsi
cmpq %r12, %rax
jbe 0x22c2f
imulq $0x50, %r12, %rax
movups 0x10(%rcx,%rax), %xmm0
movaps %xmm0, 0xd0(%rsp)
movups 0x20(%rcx,%rax), %xmm0
movaps %xmm0, 0xe0(%rsp)
movups 0x30(%rcx,%rax), %xmm0
movaps %xmm0, 0xb0(%rsp)
movups 0x40(%rcx,%rax), %xmm0
movaps %xmm0, 0xc0(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x24459
incl %r12d
jmp 0x22bc6
leaq 0x10(%rsp), %rdi
callq 0x5908
leaq 0xf0(%rsp), %rdi
callq 0x58f8
movq 0x1f76b(%rip), %rax     # 0x423b8
movq 0x1f75c(%rip), %rcx     # 0x423b0
movl 0xc(%rsp), %ebp
pushq $0x50
popq %rsi
subq %rcx, %rax
cqto
idivq %rsi
leaq 0xf0(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rcx
movq %rax, %rsi
callq 0x1dbae
movq (%rbx), %rax
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq *0x10(%rax)
movl %ebp, %r13d
movl 0x400(%rbx), %eax
movl %eax, 0x4(%rsp)
movl %eax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x1ce64
movl %eax, %ebp
xorl %r14d, %r14d
testl %eax, %eax
cmovlel %r14d, %ebp
leaq 0xf0(%rsp), %r15
cmpl %r14d, %ebp
je 0x22cfa
movl 0x4(%rsp), %r12d
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x1c034
movl 0xf8(%rsp), %eax
addl %r12d, %eax
decl %eax
movl 0x8(%rsp), %ecx
cmpl %eax, %ecx
cmovgl %ecx, %eax
movl %eax, 0x8(%rsp)
movq %r15, %rdi
callq 0x5908
incl %r14d
jmp 0x22cbc
leaq 0x3e0(%rbx), %r14
testb %r13b, %r13b
je 0x22e61
leaq 0x1f66f(%rip), %rdi     # 0x42380
movq %r14, %rsi
callq 0x13a1a
leaq 0x1f668(%rip), %rcx     # 0x42388
cmpq %rcx, %rax
je 0x22fb4
leaq 0x1f650(%rip), %rdi     # 0x42380
movq %r14, %rsi
callq 0x1316c
movslq (%rax), %r15
imulq $0x50, %r15, %r12
movq 0x1f66a(%rip), %rdi     # 0x423b0
addq %r12, %rdi
callq 0x24492
movq 0x10(%rsp), %rcx
movl (%rcx,%r15,4), %r14d
movl %eax, %r13d
cmpq %r13, %r14
jae 0x22fb4
movl 0x4(%rsp), %ebp
movq 0x1f642(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl (%rax), %ebp
jle 0x22da6
movl 0x4(%rsp), %ebp
movq 0x1f623(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl 0x4(%rax), %ebp
jle 0x22fb0
movl 0x8(%rsp), %ebp
movq 0x1f5ff(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl (%rax), %ebp
jle 0x22de9
movl 0x8(%rsp), %ebp
movq 0x1f5e0(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl 0x4(%rax), %ebp
jle 0x22fb0
movl 0x4(%rsp), %ebp
movq 0x1f5bc(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl (%rax), %ebp
jg 0x22e2c
movl 0x8(%rsp), %ebp
movq 0x1f59d(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl 0x4(%rax), %ebp
jge 0x22fb0
movl 0x4(%rsp), %ebp
movq 0x1f579(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl 0x4(%rax), %ebp
jle 0x22e59
movq 0x10(%rsp), %rax
leal 0x1(%r14), %ecx
movl %ecx, (%rax,%r15,4)
incq %r14
jmp 0x22d5a
leaq 0x1f518(%rip), %rdi     # 0x42380
movq %r14, %rsi
callq 0x13a1a
leaq 0x1f511(%rip), %rcx     # 0x42388
cmpq %rcx, %rax
je 0x22fb0
leaq 0x1f4f9(%rip), %rdi     # 0x42380
movq %r14, %rsi
callq 0x1316c
movslq (%rax), %r15
imulq $0x50, %r15, %r12
movq 0x1f513(%rip), %rdi     # 0x423b0
addq %r12, %rdi
callq 0x24492
movq 0x10(%rsp), %rcx
movl (%rcx,%r15,4), %r14d
movl %eax, %r13d
cmpq %r13, %r14
jae 0x22fb0
movl 0x4(%rsp), %ebp
movq 0x1f4eb(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl (%rax), %ebp
jle 0x22efd
movl 0x4(%rsp), %ebp
movq 0x1f4cc(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl 0x4(%rax), %ebp
jle 0x22fb4
movl 0x8(%rsp), %ebp
movq 0x1f4a8(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl (%rax), %ebp
jle 0x22f3c
movl 0x8(%rsp), %ebp
movq 0x1f489(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl 0x4(%rax), %ebp
jle 0x22fb4
movl 0x4(%rsp), %ebp
movq 0x1f469(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl (%rax), %ebp
jg 0x22f7b
movl 0x8(%rsp), %ebp
movq 0x1f44a(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl 0x4(%rax), %ebp
jge 0x22fb4
movl 0x4(%rsp), %ebp
movq 0x1f42a(%rip), %rax     # 0x423b0
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x26512
cmpl 0x4(%rax), %ebp
jle 0x22fa8
movq 0x10(%rsp), %rax
leal 0x1(%r14), %ecx
movl %ecx, (%rax,%r15,4)
incq %r14
jmp 0x22eb1
movb $0x0, 0xc(%rbx)
leaq 0x10(%rsp), %rdi
callq 0xa14c
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x2303f
movq %rax, %rbx
jmp 0x23044
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x5908
jmp 0x22ff4
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5908
jmp 0x23003
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x5908
jmp 0x2303a
jmp 0x23037
jmp 0x23037
jmp 0x23037
jmp 0x23028
jmp 0x23028
jmp 0x23028
movq %rax, %rbx
jmp 0x23051
jmp 0x23028
jmp 0x23037
jmp 0x23037
jmp 0x23037
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xa14c
jmp 0x23051
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5908
leaq 0xf0(%rsp), %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x5800
nop