// File: 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:
subq $0xc18, %rsp            # imm = 0xC18
movq %rdi, 0xc10(%rsp)
movq %rsi, 0xc08(%rsp)
movq 0xc10(%rsp), %rsi
leaq 0x18152(%rip), %rdi     # 0x54988
movb $0x0, %al
callq 0x2050
movq 0xc10(%rsp), %rdi
leaq 0x18882(%rip), %rsi     # 0x550ce
callq 0x455f0
movq %rax, 0xc00(%rsp)
movq $0x0, 0xbf8(%rsp)
movl $0xee6b280, %edi        # imm = 0xEE6B280
callq 0x2040
movq %rax, 0xbf0(%rsp)
movl $0x0, 0xbec(%rsp)
movq 0xc00(%rsp), %rdi
callq 0x45400
movq %rax, 0xbf8(%rsp)
cmpq $0x0, 0xbf8(%rsp)
jne 0x3c97d
cmpl $0x1, 0xbec(%rsp)
jle 0x3c978
leaq 0x800(%rsp), %rdi
callq 0x22350
leaq 0x410(%rsp), %rdi
movq 0xc08(%rsp), %rdx
leaq 0x800(%rsp), %rcx
leaq 0x166af(%rip), %rsi     # 0x52f8f
movb $0x0, %al
callq 0x2090
leaq 0x410(%rsp), %rdi
leaq 0x180a2(%rip), %rsi     # 0x54998
callq 0x455f0
movq %rax, 0x408(%rsp)
movq 0x286d6(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x800(%rsp), %rdx
movl 0xbec(%rsp), %ecx
leaq 0x18078(%rip), %rsi     # 0x5499b
movb $0x0, %al
callq 0x2250
movq 0x408(%rsp), %rcx
leaq 0xbec(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2360
movq 0xbf0(%rsp), %rdi
movslq 0xbec(%rsp), %rdx
movq 0x408(%rsp), %rcx
movl $0x1, %esi
callq 0x2360
movq 0x408(%rsp), %rdi
callq 0x2180
jmp 0x3cb01
movq 0xbf8(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x3e, %eax
jne 0x3ca77
cmpl $0x1, 0xbec(%rsp)
jle 0x3ca51
leaq 0x800(%rsp), %rdi
callq 0x22350
leaq 0x20(%rsp), %rdi
movq 0xc08(%rsp), %rdx
leaq 0x800(%rsp), %rcx
leaq 0x165c7(%rip), %rsi     # 0x52f8f
movb $0x0, %al
callq 0x2090
leaq 0x20(%rsp), %rdi
leaq 0x17fbd(%rip), %rsi     # 0x54998
callq 0x455f0
movq %rax, 0x18(%rsp)
movq 0x285f4(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x800(%rsp), %rdx
movl 0xbec(%rsp), %ecx
leaq 0x17f96(%rip), %rsi     # 0x5499b
movb $0x0, %al
callq 0x2250
movq 0x18(%rsp), %rcx
leaq 0xbec(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2360
movq 0xbf0(%rsp), %rdi
movslq 0xbec(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl $0x1, %esi
callq 0x2360
movq 0x18(%rsp), %rdi
callq 0x2180
leaq 0x800(%rsp), %rdi
movq 0xbf8(%rsp), %rsi
addq $0x1, %rsi
callq 0x2160
movl $0x0, 0xbec(%rsp)
jmp 0x3cadd
movq 0xbf8(%rsp), %rdi
callq 0x47640
movl %eax, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x14(%rsp), %eax
jge 0x3cadb
movq 0xbf8(%rsp), %rax
movslq 0x10(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0xbf0(%rsp), %rax
movslq 0xbec(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xbec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbec(%rsp)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x3ca90
jmp 0x3cadd
jmp 0x3cadf
movq 0xbf8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x3cafc
movq 0x8(%rsp), %rdi
callq 0x2220
jmp 0x3c882
movq 0xc00(%rsp), %rdi
callq 0x2180
movq 0xbf0(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x3cb29
movq (%rsp), %rdi
callq 0x2220
addq $0xc18, %rsp            # imm = 0xC18
retq
nopw %cs:(%rax,%rax)