// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x15ebb(%rip), %rsi     # 0x2dd08
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x26387
movq %rax, %r14
xorl %r13d, %r13d
leaq 0x16f55(%rip), %rbp     # 0x2edbc
movq %rsp, %r12
movq %r15, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x26258
testq %rax, %rax
je 0x181a8
movq %rax, %rbx
movq %rax, %rdi
callq 0x17dac
testb %al, %al
jne 0x18197
movl %r13d, (%rsp)
movw $0x2e, 0x560(%rsp)
movl %r13d, 0x80(%rsp)
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)
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
movl $0x64, %edx
movq %rbp, %rcx
movq %r12, %r8
callq 0x283ee
testb %al, %al
je 0x181d1
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 %rbx, %rdi
leaq 0x100(%rsp), %r14
movq %r14, %rsi
movl $0x64, %edx
movq %rbp, %rcx
movq %r12, %r8
callq 0x283ee
testb %al, %al
je 0x181d9
movq %r14, %rdi
callq 0x2290
movl %eax, %r13d
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)
movl $0x0, 0xf0(%rsp)
movq %rbx, %rdi
leaq 0x90(%rsp), %r14
movq %r14, %rsi
movl $0x64, %edx
movq %rbp, %rcx
movq %r12, %r8
callq 0x283ee
testb %al, %al
je 0x181e1
movq %r14, %rdi
callq 0x2290
movl %eax, %r15d
movl $0x3e8, %edx            # imm = 0x3E8
leaq 0x950(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x20f0
movq %rbx, %rdi
movq %r14, %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movq %rbp, %rcx
movq %r12, %r8
callq 0x283ee
movb $0x1, %r14b
testb %al, %al
je 0x18019
leaq 0x560(%rsp), %rdi
leaq 0x950(%rsp), %rsi
callq 0x2150
xorl %r14d, %r14d
xorl %r12d, %r12d
testb %r14b, %r14b
jne 0x18077
movl %r15d, 0x4(%rsp)
xorl %r12d, %r12d
movl $0x3e8, %edx            # imm = 0x3E8
leaq 0x170(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x20f0
movq %rbx, %rdi
movq %r15, %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movq %rbp, %rcx
movq %rsp, %r8
callq 0x283ee
movb $0x1, %cl
testb %al, %al
je 0x1806f
leaq 0x170(%rsp), %rdi
callq 0x2290
movl %eax, %r12d
movl %r14d, %ecx
movl %ecx, %r14d
movl 0x4(%rsp), %r15d
movb $0x2b, %bpl
testb %r14b, %r14b
jne 0x180c4
movw $0x0, 0x178(%rsp)
movq $0x0, 0x170(%rsp)
movq %rbx, %rdi
leaq 0x170(%rsp), %rsi
movl $0xa, %edx
leaq 0x16d10(%rip), %rcx     # 0x2edbc
movq %rsp, %r8
callq 0x283ee
movl $0x2b, %ebp
testb %al, %al
je 0x180c4
movl 0x170(%rsp), %ebp
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 %r13d
movl %r13d, 0xc(%rax)
movl %r15d, 0x20(%rax)
leaq 0x20(%rsp), %rdi
callq 0x273e0
movq 0x170(%rsp), %rcx
movq %rax, (%rcx)
movq 0x170(%rsp), %rax
movq (%rax), %rdi
callq 0x12f1c
leaq 0x560(%rsp), %r15
movq %r15, %rdi
callq 0x20d0
leaq 0x2(%rax), %rdi
callq 0x2040
movq 0x170(%rsp), %r14
movq %rax, 0x18(%r14)
movq %rax, %rdi
movq %r15, %rsi
callq 0x2150
movb %bpl, 0x8(%r14)
xorl %r13d, %r13d
movq %r13, 0x48(%r14)
movl %r12d, 0x10(%r14)
movq %r13, 0x38(%r14)
movq 0x18(%rsp), %r15
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x18178
movq %r14, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x18188
movq %r15, %rdi
leaq 0x170(%rsp), %rdx
callq 0xb976
movq 0x8(%rsp), %r14
movq %rsp, %r12
leaq 0x16c25(%rip), %rbp     # 0x2edbc
movq %rbx, %rdi
callq 0x2220
testq %rbx, %rbx
jne 0x17e74
movq %r14, %rdi
callq 0x2170
movq %r15, %rdi
callq 0x181f3
testb %al, %al
je 0x181e9
movq %r15, %rax
addq $0xd38, %rsp            # imm = 0xD38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x4d07
movq %rbx, %rdi
callq 0x4d2a
movq %rbx, %rdi
callq 0x4d4d
movq 0x10(%rsp), %rdi
callq 0x4d70