// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd48, %rsp            # imm = 0xD48
movq %rdi, %rbx
pushq $0x18
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x18(%rsp)
andq $0x0, 0x10(%rax)
leaq 0x12dcb(%rip), %rsi     # 0x26d08
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
callq 0x2027c
movq %rax, %r14
leaq 0x13e68(%rip), %r13     # 0x27dbc
pushq $0x64
popq %r15
leaq 0x10(%rsp), %rbp
movq %rax, 0x28(%rsp)
movq %r14, %rdi
callq 0x2017f
testq %rax, %rax
je 0x14244
movq %rax, %r12
movq %rax, %rdi
callq 0x13e9f
testb %al, %al
jne 0x14237
andl $0x0, 0x10(%rsp)
movw $0x2e, 0x570(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
andl $0x0, 0x90(%rsp)
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
movl %r15d, %edx
movq %r13, %rcx
movq %rbp, %r8
callq 0x21c5c
testb %al, %al
je 0x14285
xorps %xmm0, %xmm0
movaps %xmm0, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
andl $0x0, 0x170(%rsp)
movq %r12, %rdi
leaq 0x110(%rsp), %rbx
movq %rbx, %rsi
movl %r15d, %edx
movq %r13, %rcx
movq %rbp, %r8
callq 0x21c5c
testb %al, %al
je 0x14298
movq %rbx, %rdi
callq 0x2280
movl %eax, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
andl $0x0, 0x100(%rsp)
movq %r12, %rdi
leaq 0xa0(%rsp), %r14
movq %r14, %rsi
movl %r15d, %edx
movq %r13, %rcx
movq %rbp, %r8
callq 0x21c5c
testb %al, %al
je 0x14272
movq %r14, %rdi
callq 0x2280
movl %eax, 0xc(%rsp)
movl $0x3e8, %edx            # imm = 0x3E8
leaq 0x960(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x20f0
movq %r12, %rdi
movq %r14, %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movq %r13, %rcx
movq %rbp, %r8
callq 0x21c5c
movb $0x2b, %r15b
testb %al, %al
je 0x14174
leaq 0x570(%rsp), %rdi
movq %r14, %rsi
callq 0x2140
movl $0x3e8, %edx            # imm = 0x3E8
leaq 0x180(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x20f0
movq %r12, %rdi
movq %r14, %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movq %r13, %rcx
movq %rbp, %r8
callq 0x21c5c
testb %al, %al
je 0x14174
movq %r14, %rdi
callq 0x2280
andw $0x0, 0x188(%rsp)
movl %eax, 0x14(%rsp)
andq $0x0, 0x180(%rsp)
movq %r12, %rdi
movq %r14, %rsi
pushq $0xa
popq %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0x21c5c
pushq $0x2b
popq %r15
testb %al, %al
je 0x1416e
movl 0x180(%rsp), %r15d
movl 0x14(%rsp), %ebp
jmp 0x14176
xorl %ebp, %ebp
movl 0xc(%rsp), %r14d
pushq $0x50
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, 0x180(%rsp)
incl %ebx
movl %ebx, 0xc(%rax)
movl %r14d, 0x20(%rax)
leaq 0x30(%rsp), %rdi
callq 0x21062
movq 0x180(%rsp), %rcx
movq %rax, (%rcx)
movq 0x180(%rsp), %rax
movq (%rax), %rdi
callq 0xf63c
leaq 0x570(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
leaq 0x2(%rax), %rdi
callq 0x2040
movq 0x180(%rsp), %rbx
movq %rax, 0x18(%rbx)
movq %rax, %rdi
movq %r14, %rsi
callq 0x2140
movb %r15b, 0x8(%rbx)
andq $0x0, 0x48(%rbx)
movl %ebp, 0x10(%rbx)
andq $0x0, 0x38(%rbx)
movq 0x18(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x7448
movq 0x28(%rsp), %r14
leaq 0x13b8e(%rip), %r13     # 0x27dbc
pushq $0x64
popq %r15
leaq 0x10(%rsp), %rbp
movq %r12, %rdi
callq 0x2210
jmp 0x13f62
movq %r14, %rdi
callq 0x2160
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x142d2
testb %al, %al
je 0x142ba
movq %rbx, %rax
addq $0xd48, %rsp            # imm = 0xD48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20d67(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x13869(%rip), %rsi     # 0x27aec
jmp 0x142a9
movq 0x20d54(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x13819(%rip), %rsi     # 0x27aaf
jmp 0x142a9
movq 0x20d41(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x13827(%rip), %rsi     # 0x27ad0
movq %r12, %rdx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0
movq 0x20d1f(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x1383b(%rip), %rsi     # 0x27b06
movq 0x20(%rsp), %rdx
jmp 0x142ac