// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_genome_sequence_tools.cpp
// Function: binarize_fasta_file(char*, char*)
void binarize_fasta_file(char* fasta_fp, char* bin_dir)
{
	printf("Binarizing %s.\n", fasta_fp);
	FILE* f_fasta = open_f(fasta_fp, "r");

	char* cur_line = NULL;
	char* cur_entry_buffer = new char[250 * 1000 * 1000];
	int cur_entry_i = 0;
	char cur_entry_id[1000];

	while(1)
	{
		// Process the current buffer.
		cur_line = getline(f_fasta);

		if(cur_line == NULL)
		{
			// File ended, dump the last entry if there are values in it.
			if(cur_entry_i > 1)
			{
				char cur_entry_bin_fp[1000];
				normalize_chr_id(cur_entry_id);
				sprintf(cur_entry_bin_fp, "%s/%s.bin", bin_dir, cur_entry_id);
				FILE* f_bin = open_f(cur_entry_bin_fp, "wb");

				// Open the new binary file.
				fprintf(stderr, "Dumping %s (%d)\n", cur_entry_id, cur_entry_i);

				// Dump the sequence buffer.
				fwrite(&cur_entry_i, sizeof(int), 1, f_bin);
				fwrite(cur_entry_buffer, sizeof(char), cur_entry_i, f_bin);

				// Close current file.
				fclose(f_bin);
			}
			break;
		}
		else if(cur_line[0] == '>')
		{
			if(cur_entry_i > 1)
			{
				char cur_entry_bin_fp[1000];
				normalize_chr_id(cur_entry_id);
				sprintf(cur_entry_bin_fp, "%s/%s.bin", bin_dir, cur_entry_id);
				FILE* f_bin = open_f(cur_entry_bin_fp, "wb");

				// Open the new binary file.
				fprintf(stderr, "Dumping %s (%d)\n", cur_entry_id, cur_entry_i);

				// Dump the sequence buffer.
				fwrite(&cur_entry_i, sizeof(int), 1, f_bin);
				fwrite(cur_entry_buffer, sizeof(char), cur_entry_i, f_bin);

				// Close current file.
				fclose(f_bin);
			}

			// Update the id, reset the counter.
			strcpy(cur_entry_id, &(cur_line[1]));
			cur_entry_i = 0;
		}
		else
		{
			// Concatenate the current sequence line.
			int l_cur_line = t_string::string_length(cur_line);
			for(int i = 0; i < l_cur_line; i++)
			{
				cur_entry_buffer[cur_entry_i] = cur_line[i];
				cur_entry_i++;
			} // i loop.
		}

		delete [] cur_line;
	} // file reading loop.

	fclose(f_fasta);

	delete [] cur_entry_buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x7e8, %rsp            # imm = 0x7E8
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
leaq 0xb5b9(%rip), %rdi      # 0x2d590
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
leaq 0xbd20(%rip), %rsi      # 0x2dd08
movq %rbx, %rdi
callq 0x26387
movq %rax, %rbx
movl $0xee6b280, %edi        # imm = 0xEE6B280
callq 0x2040
movq %rax, %r14
movl $0x0, 0x4(%rsp)
movq %rbx, %rdi
callq 0x26258
testq %rax, %rax
je 0x22124
movq %rax, %rbp
leaq 0x400(%rsp), %r12
cmpb $0x3e, (%rbp)
jne 0x22045
movl 0x4(%rsp), %r13d
cmpl $0x2, %r13d
jge 0x22095
leaq 0x1(%rbp), %rsi
movq %r12, %rdi
callq 0x2150
xorl %ecx, %ecx
jmp 0x22074
movq %rbp, %rdi
callq 0x26e82
movl 0x4(%rsp), %ecx
testl %eax, %eax
jle 0x22074
movslq %ecx, %rdx
movl %eax, %esi
addq %r14, %rdx
xorl %eax, %eax
movb (%rbp,%rax), %dil
movb %dil, (%rdx,%rax)
incq %rax
cmpq %rax, %rsi
jne 0x2205f
addl %ecx, %eax
movl %eax, %ecx
movl %ecx, 0x4(%rsp)
movq %rbp, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x26258
movq %rax, %rbp
testq %rax, %rax
jne 0x22024
jmp 0x22124
movq %r12, %rdi
callq 0x12f1c
leaq 0x10(%rsp), %r15
movq %r15, %rdi
leaq 0x9eed(%rip), %rsi      # 0x2bf99
movq 0x8(%rsp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2080
movq %r15, %rdi
leaq 0xb4db(%rip), %rsi      # 0x2d5a0
callq 0x26387
movq %rax, %r15
movq 0x17f0c(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0xb4c5(%rip), %rsi      # 0x2d5a3
movq %r12, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2250
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %r15, %rcx
callq 0x2340
movslq 0x4(%rsp), %rdx
movl $0x1, %esi
movq %r14, %rdi
movq %r15, %rcx
callq 0x2340
movq %r15, %rdi
callq 0x2170
jmp 0x22035
movl 0x4(%rsp), %ebp
cmpl $0x2, %ebp
jge 0x2214f
movq %rbx, %rdi
callq 0x2170
movq %r14, %rdi
callq 0x2220
addq $0x7e8, %rsp            # imm = 0x7E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x400(%rsp), %r12
movq %r12, %rdi
callq 0x12f1c
leaq 0x9e33(%rip), %rsi      # 0x2bf99
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2080
leaq 0xb41c(%rip), %rsi      # 0x2d5a0
movq %r13, %rdi
callq 0x26387
movq %rax, %r15
movq 0x17e4a(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0xb403(%rip), %rsi      # 0x2d5a3
movq %r12, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2250
leaq 0x4(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x2340
movslq (%r12), %rdx
movl $0x1, %esi
movq %r14, %rdi
movq %r15, %rcx
callq 0x2340
movq %r15, %rdi
callq 0x2170
jmp 0x2212d