// File: /workspace/github_cpp_makefile_star2_O2/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 0x9b9d(%rip), %rdi      # 0x2658a
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
leaq 0xa30a(%rip), %rsi      # 0x26d08
movq %rbx, %rdi
callq 0x2027c
movq %rax, %rbx
movl $0xee6b280, %edi        # imm = 0xEE6B280
callq 0x2040
movq %rax, %r14
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
leaq 0x400(%rsp), %r13
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x2017f
testq %rax, %rax
je 0x1cb1f
movq %rax, %r12
cmpb $0x3e, (%rax)
jne 0x1ca58
cmpl $0x2, %ebp
jge 0x1ca94
movq %r12, %rsi
incq %rsi
movq %r13, %rdi
callq 0x2140
xorl %ebp, %ebp
jmp 0x1ca86
movq %r12, %rdi
callq 0x20d90
movslq %ebp, %rcx
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
addq %r14, %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x1ca84
movb (%r12,%rdx), %sil
movb %sil, (%rcx,%rdx)
incq %rdx
jmp 0x1ca72
addl %edx, %ebp
movl %ebp, 0x4(%rsp)
movq %r12, %rdi
callq 0x2210
jmp 0x1ca28
movq %r13, %rdi
callq 0xf63c
leaq 0x10(%rsp), %r15
movq %r15, %rdi
leaq 0x84e8(%rip), %rsi      # 0x24f93
movq 0x8(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2080
movq %r15, %rdi
leaq 0x9ad6(%rip), %rsi      # 0x2659a
callq 0x2027c
movq %rax, %r15
movq 0x1850d(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x9ac0(%rip), %rsi      # 0x2659d
movq %r13, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2240
leaq 0x4(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rbp
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2330
movslq 0x4(%rsp), %rdx
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rcx
callq 0x2330
movq %r15, %rdi
callq 0x2160
jmp 0x1ca46
cmpl $0x2, %ebp
jge 0x1cb46
movq %rbx, %rdi
callq 0x2160
movq %r14, %rdi
callq 0x2210
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 0xf63c
leaq 0x8436(%rip), %rsi      # 0x24f93
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2080
leaq 0x9a1f(%rip), %rsi      # 0x2659a
movq %r13, %rdi
callq 0x2027c
movq %rax, %r15
movq 0x18453(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x9a06(%rip), %rsi      # 0x2659d
movq %r12, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2240
leaq 0x4(%rsp), %r12
pushq $0x4
popq %rsi
pushq $0x1
popq %r13
movq %r12, %rdi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2330
movslq (%r12), %rdx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x2330
movq %r15, %rdi
callq 0x2160
jmp 0x1cb24