// File: /workspace/github_cpp_makefile_star2_O3/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: load_BED(char*)
vector<t_annot_region*>* load_BED(char* bed_fp)
{
	vector<t_annot_region*>* bed_regions = new vector<t_annot_region*>();

	FILE* f_bed = open_f(bed_fp, "r");
	
	while(1)
	{
		char* cur_line = getline(f_bed);
		if(cur_line == NULL)
		{
			break;
		}

		bool skip_line = check_line_skip(cur_line);

		if(!skip_line)
		{
			// Read the mandatory fields.
			int i_cur_char = 0;
			char strand_char = '+';
			char region_name[1000];
			strcpy(region_name, ".");
			int region_score = 0;

			char chrom[100];
			memset(chrom, 0, 100);
			if(!t_string::get_next_token(cur_line, chrom, 100, "\t", i_cur_char))
			{
				fprintf(stderr, "Could not read chromosome in %s\n", cur_line);
				exit(0);
			}
			

			int start = 0;
			char start_str[100];
			memset(start_str, 0, 100);
			if(!t_string::get_next_token(cur_line, start_str, 100, "\t", i_cur_char))
			{
				fprintf(stderr, "Could not read start in %s\n", cur_line);
				exit(0);
			}
			else
			{
				start = atoi(start_str);
			}

			int end = 0;
			char end_str[100];
			memset(end_str, 0, 100);
			if(!t_string::get_next_token(cur_line, end_str, 100, "\t", i_cur_char))
			{
				fprintf(stderr, "Could not read end in %s\n", cur_line);
				exit(0);
			}
			else
			{
				end = atoi(end_str);
			}

			
			// Keep on reading and load until line is finished.
			bool line_finished = false;
			char name_str[1000];
			memset(name_str, 0, 1000);
			if(!t_string::get_next_token(cur_line, name_str, 1000, "\t", i_cur_char))
			{
				// Could not get the next token.
				line_finished = true;
			}
			else
			{
				// Region name is already initialized to '.'.
				strcpy(region_name, name_str);
			}

			if(!line_finished)
			{
				char score_str[1000];
				memset(score_str, 0, 1000);
				if(!t_string::get_next_token(cur_line, score_str, 1000, "\t", i_cur_char))
				{
					line_finished = true;
				}
				else
				{
					//strcpy(region_score, score_str);
					region_score = atoi(score_str);
				}
			}

			if(!line_finished)
			{
				char strand_str[10];
				memset(strand_str, 0, 10);
				if(!t_string::get_next_token(cur_line, strand_str, 10, "\t", i_cur_char))
				{
					line_finished = true;
				}
				else
				{
					strand_char = strand_str[0];
				}
			}

			/*if(sscanf(cur_line, "%s %d %d", chrom, &start, &end) != 3)
			{
				printf("Could not read the mandatory fields from BED file line:\n%s\n", cur_line);
				exit(0);
			}*/

			//char strand = '+'; 
			//if(sscanf(cur_line, "%*s %*s %*s %*s %*s %c", &strand) != 1)
			//{
			//	// Could not read the strand from file, set it to '+' by default.
			//	strand = '+';
			//}

			//int region_score = 0;
			//if(sscanf(cur_line, "%*s %*s %*s %*s %d", &region_score) != 1)
			//{
			//	// Could not read the score from the file.
			//}

			//char region_name[1000];
			//if(sscanf(cur_line, "%*s %*s %*s %s", region_name) != 1)
			//{
			//	// Could not read the strand from file, set it to '+' by default.
			//	strcpy(region_name, ".");
			//}

			t_annot_region* new_region = new t_annot_region();

			// Translate the start and end of the BED coordinates to the codebase coordinates.
			//new_region->start = start - BED_START_BASE + CODEBASE_START_BASE;
			//new_region->end = end - BED_END_BASE + CODEBASE_END_BASE;
			new_region->start = translate_coord(start, BED_COORDS::start_base, CODEBASE_COORDS::start_base);
			new_region->end = translate_coord(end, BED_COORDS::end_base, CODEBASE_COORDS::end_base);
			new_region->chrom = t_string::copy_me_str(chrom);
			normalize_chr_id(new_region->chrom);
			new_region->name = new char[strlen(region_name) + 2];
			strcpy(new_region->name, region_name);
			new_region->strand = strand_char;
			new_region->data = NULL;
			new_region->score = region_score;
			new_region->sort_info = NULL;
		
			bed_regions->push_back(new_region);
		} // line skip check.

		delete [] cur_line;
	} // file reading loop.

	fclose(f_bed);

	//printf("Loaded %d regions from %s\n", bed_regions->size(), bed_fp);

	if(!validate_region_coords(bed_regions))
	{
		fprintf(stderr, "The coordinates are not valid for %s\n", bed_fp);
		exit(0);
	}

	return(bed_regions);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd38, %rsp            # imm = 0xD38
movq %rdi, %rbx
movl $0x18, %edi
callq 0x2180
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x15216(%rip), %rsi     # 0x2cd08
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
callq 0x25d5a
movq %rax, %rbx
movq %rax, %rdi
callq 0x25c1e
testq %rax, %rax
je 0x17e31
movq %rax, %r12
xorl %r13d, %r13d
leaq 0x1629c(%rip), %rbp     # 0x2ddbc
leaq 0x20(%rsp), %r15
movq %rbx, 0x10(%rsp)
movq %r12, %rdi
callq 0x17a51
testb %al, %al
jne 0x17e15
movl %r13d, 0x8(%rsp)
movw $0x2e, 0x560(%rsp)
movl %r13d, 0xf0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %r12, %rdi
leaq 0x90(%rsp), %rsi
movl $0x64, %edx
movq %rbp, %rcx
leaq 0x8(%rsp), %r13
movq %r13, %r8
callq 0x27d40
testb %al, %al
je 0x17e62
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl $0x0, 0x80(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movl $0x64, %edx
movq %rbp, %rcx
movq %r13, %r8
callq 0x27d40
testb %al, %al
je 0x17e6a
movq %r15, %rdi
callq 0x2290
movl %eax, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movl $0x0, 0x160(%rsp)
movq %r12, %rdi
leaq 0x100(%rsp), %r15
movq %r15, %rsi
movl $0x64, %edx
movq %rbp, %rcx
movq %r13, %r8
callq 0x27d40
testb %al, %al
je 0x17e5a
movq %r15, %rdi
callq 0x2290
movl %eax, 0x4(%rsp)
movl $0x3e8, %edx            # imm = 0x3E8
leaq 0x950(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x20f0
movq %r12, %rdi
movq %r15, %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movq %rbp, %rcx
movq %r13, %r8
callq 0x27d40
movb $0x2b, %r13b
testb %al, %al
je 0x17d3d
leaq 0x560(%rsp), %rdi
movq %r15, %rsi
callq 0x2150
movl $0x3e8, %edx            # imm = 0x3E8
leaq 0x170(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x20f0
movq %r12, %rdi
movq %r15, %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movq %rbp, %rcx
leaq 0x8(%rsp), %r8
callq 0x27d40
testb %al, %al
je 0x17d3d
movq %r15, %rdi
callq 0x2290
movl %eax, 0xc(%rsp)
movw $0x0, 0x178(%rsp)
movq $0x0, 0x170(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movl $0xa, %edx
movq %rbp, %rcx
leaq 0x8(%rsp), %r8
callq 0x27d40
movl $0x2b, %r13d
testb %al, %al
je 0x17d37
movl 0x170(%rsp), %r13d
movl 0xc(%rsp), %ebp
jmp 0x17d3f
xorl %ebp, %ebp
movl 0x4(%rsp), %r15d
movl $0x50, %edi
callq 0x2180
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, 0x170(%rsp)
incl %ebx
movl %ebx, 0xc(%rax)
movl %r15d, 0x20(%rax)
leaq 0x90(%rsp), %rdi
callq 0x26db4
movq 0x170(%rsp), %rcx
movq %rax, (%rcx)
movq 0x170(%rsp), %rax
movq (%rax), %rdi
callq 0x12c04
leaq 0x560(%rsp), %rbx
movq %rbx, %rdi
callq 0x20d0
leaq 0x2(%rax), %rdi
callq 0x2040
movq 0x170(%rsp), %r15
movq %rax, 0x18(%r15)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2150
movb %r13b, 0x8(%r15)
xorl %r13d, %r13d
movq %r13, 0x48(%r15)
movl %ebp, 0x10(%r15)
movq %r13, 0x38(%r15)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x17df4
movq %r15, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x17e04
movq %r14, %rdi
leaq 0x170(%rsp), %rdx
callq 0xb74c
movq 0x10(%rsp), %rbx
leaq 0x15fac(%rip), %rbp     # 0x2ddbc
leaq 0x20(%rsp), %r15
movq %r12, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x25c1e
movq %rax, %r12
testq %rax, %rax
jne 0x17b2a
movq %rbx, %rdi
callq 0x2170
movq %r14, %rdi
callq 0x17e7c
testb %al, %al
je 0x17e72
movq %r14, %rax
addq $0xd38, %rsp            # imm = 0xD38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x4be2
movq %r12, %rdi
callq 0x4b9c
movq %r12, %rdi
callq 0x4bbf
movq 0x18(%rsp), %rdi
callq 0x4c05