// File: /workspace/github_cpp_makefile_star2_O3/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 0xab07(%rip), %rdi      # 0x2c58a
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
leaq 0xb274(%rip), %rsi      # 0x2cd08
movq %rbx, %rdi
callq 0x25d5a
movq %rax, %rbx
movl $0xee6b280, %edi        # imm = 0xEE6B280
callq 0x2040
movq %rax, %r14
movl $0x0, 0x4(%rsp)
movq %rbx, %rdi
callq 0x25c1e
testq %rax, %rax
je 0x21bd1
movq %rax, %rbp
leaq 0x400(%rsp), %r12
xorl %r13d, %r13d
cmpb $0x3e, (%rbp)
jne 0x21af0
cmpl $0x2, %r13d
jge 0x21b3c
leaq 0x1(%rbp), %rsi
movq %r12, %rdi
callq 0x2150
xorl %r13d, %r13d
jmp 0x21b1a
movq %rbp, %rdi
callq 0x26856
testl %eax, %eax
jle 0x21b1a
movslq %r13d, %rcx
movl %eax, %eax
addq %r14, %rcx
xorl %edx, %edx
movb (%rbp,%rdx), %sil
movb %sil, (%rcx,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x21b06
addl %edx, %r13d
movl %r13d, 0x4(%rsp)
movq %rbp, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x25c1e
movq %rax, %rbp
testq %rax, %rax
jne 0x21ad3
jmp 0x21bcb
movq %r12, %rdi
callq 0x12c04
leaq 0x10(%rsp), %r15
movq %r15, %rdi
leaq 0x9440(%rip), %rsi      # 0x2af93
movq 0x8(%rsp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2080
movq %r15, %rdi
leaq 0xaa2e(%rip), %rsi      # 0x2c59a
callq 0x25d5a
movq %rax, %r15
movq 0x17465(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0xaa18(%rip), %rsi      # 0x2c59d
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 0x21adf
cmpl $0x2, %r13d
jge 0x21bf3
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 0x12c04
leaq 0x9389(%rip), %rsi      # 0x2af93
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2080
leaq 0xa972(%rip), %rsi      # 0x2c59a
movq %rbp, %rdi
callq 0x25d5a
movq %rax, %r15
movq 0x173a6(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0xa959(%rip), %rsi      # 0x2c59d
movq %r12, %rdx
movl %r13d, %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 0x21bd1