// File: /workspace/github_cpp_makefile_star2_O3/CK7[P]abawaca/src/SCGdb.cpp
// Function: SCGdb::load_scgs()
void SCGdb::load_scgs()
{
	FILE* fin = fopen(scg_list_file.c_str(), "r");
	if(fin == NULL) {
		char	s[2048];
		sprintf(s, "Failed to read file %s", scg_list_file.c_str());
		throw invalid_argument(s);
	}

	char	scg[1024], gene[1024];

	total_num_scgs = 0;
	while(fscanf(fin, "%s", scg) == 1) {
		total_num_scgs++;
	}
	fclose(fin);

	// An empty SCG database
	if(gene2scg_file.size() == 0)
		return;

	fin = fopen(gene2scg_file.c_str(), "r");
	if(fin == NULL) {
		char	s[2048];
		sprintf(s, "Failed to read file %s", gene2scg_file.c_str());
		throw invalid_argument(s);
	}

	while(fscanf(fin, "%s %s", gene, scg) == 2) {
		char* p = strrchr(gene, '_');
		if(p == NULL) {
			char	s[2048];
			sprintf(s, "Unexpected gene name %s, expected prodigal format: <name>_<integer>\nfile: %s", gene, scg_list_file.c_str());
			throw invalid_argument(s);
		}

		*p = 0;
		size_t scaf = scaf_db.scaf_name2id(gene);
		// The following can happen if a sequence is in the file that was used for the SCG analysis but was not in the file that was binned.
		// We are fine with that.
		if(scaf == 0) {
			continue;
		}

		int gene_id = atoi(p+1);

		if(gene_id == 0) {
			string scaf_name = gene;
			*p = '_';
			char	s[2048];
			sprintf(s, "Could not identify gene id in %s (recovered scaf=%s (%lu), gene_id=%s). Expecting prodigal format gene names: <name>_<integer>\nfile: %s",
				gene, scaf_name.c_str(), scaf, p+1, scg_list_file.c_str());
			throw invalid_argument(s);
		}
		auto it = scaf2scgs.find(scaf);
		if(it == scaf2scgs.end()) {
			scaf2scgs.insert(pair<size_t, set<string> >(scaf, set<string>()));
			it = scaf2scgs.find(scaf);
		}
		it->second.insert(scg);
	}
	fclose(fin);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1078, %rsp           # imm = 0x1078
movq %rdi, %rbx
movq 0x20(%rdi), %rdi
leaq 0x3c19(%rip), %rsi      # 0x19131
callq 0x5450
testq %rax, %rax
je 0x158e5
movq %rax, %r14
movl $0x0, 0x70(%rbx)
leaq 0x4c31(%rip), %rsi      # 0x1a168
leaq -0x1090(%rbp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x56f0
cmpl $0x1, %eax
jne 0x15573
leaq 0x4c14(%rip), %r15      # 0x1a168
leaq -0x1090(%rbp), %r12
incl 0x70(%rbx)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x56f0
cmpl $0x1, %eax
je 0x1555b
movq %r14, %rdi
callq 0x5390
cmpq $0x0, 0x8(%rbx)
je 0x157f0
movq (%rbx), %rdi
leaq 0x3ba1(%rip), %rsi      # 0x19131
callq 0x5450
testq %rax, %rax
je 0x1591e
movq %rax, %r14
leaq 0x4e80(%rip), %rsi      # 0x1a428
leaq -0xc90(%rbp), %rdx
leaq -0x1090(%rbp), %rcx
movq %rax, %rdi
xorl %eax, %eax
callq 0x56f0
cmpl $0x2, %eax
jne 0x157e8
leaq 0x40(%rbx), %rax
movq %rax, -0x68(%rbp)
leaq 0x48(%rbx), %rax
movq %rax, -0x30(%rbp)
leaq -0xc90(%rbp), %r15
movq %r14, -0x70(%rbp)
movq %r15, %rdi
movl $0x5f, %esi
callq 0x53f0
testq %rax, %rax
je 0x15802
movq %rax, %r12
movb $0x0, (%rax)
movq %rbx, %r13
movq 0x78(%rbx), %r14
leaq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq %r15, %rdi
callq 0x5180
leaq (%rax,%rbp), %rdx
addq $-0xc90, %rdx           # imm = 0xF370
leaq -0x90(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb226
movq %r14, %rdi
movq %rbx, %rsi
callq 0x159d6
movq %rax, %r15
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x1565a
callq 0x5340
testq %r15, %r15
je 0x157b6
movq %r12, %r14
incq %r14
movq %r14, %rdi
callq 0x55c0
testl %eax, %eax
je 0x15853
movq 0x50(%r13), %rax
testq %rax, %rax
leaq -0x880(%rbp), %r12
je 0x156b4
movq -0x30(%rbp), %r14
xorl %ecx, %ecx
cmpq %r15, 0x20(%rax)
setb %cl
cmovaeq %rax, %r14
movq 0x10(%rax,%rcx,8), %rax
testq %rax, %rax
jne 0x1568d
cmpq -0x30(%rbp), %r14
je 0x156b4
cmpq 0x20(%r14), %r15
jae 0x15763
xorl %ecx, %ecx
movq %rcx, -0x50(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
leaq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq %rax, -0x40(%rbp)
movq %rcx, -0x38(%rbp)
movq %r15, -0x890(%rbp)
movq %rcx, -0x878(%rbp)
movq %r12, -0x870(%rbp)
movq %r12, -0x868(%rbp)
movq %rcx, -0x860(%rbp)
movl $0x0, -0x880(%rbp)
movq -0x68(%rbp), %rdi
leaq -0x890(%rbp), %rsi
callq 0x15e08
leaq -0x888(%rbp), %rdi
callq 0x9852
leaq -0x60(%rbp), %rdi
callq 0x9852
movq 0x50(%r13), %rcx
movq -0x30(%rbp), %r14
testq %rcx, %rcx
je 0x15763
movq -0x30(%rbp), %rax
xorl %edx, %edx
cmpq %r15, 0x20(%rcx)
setb %dl
cmovaeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x15734
movq -0x30(%rbp), %rcx
movq %rcx, %r14
cmpq %rcx, %rax
je 0x15763
cmpq 0x20(%rax), %r15
cmovbq -0x30(%rbp), %rax
movq %rax, %r14
movq %r12, -0x890(%rbp)
leaq -0x1090(%rbp), %rbx
movq %rbx, %rdi
callq 0x5180
leaq (%rax,%rbp), %rdx
addq $-0x1090, %rdx          # imm = 0xEF70
leaq -0x890(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0xb226
addq $0x28, %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x15f76
movq -0x890(%rbp), %rdi
cmpq %r12, %rdi
je 0x157b6
callq 0x5340
movq -0x70(%rbp), %r14
movq %r14, %rdi
leaq 0x4c64(%rip), %rsi      # 0x1a428
leaq -0xc90(%rbp), %r15
movq %r15, %rdx
leaq -0x1090(%rbp), %rcx
xorl %eax, %eax
callq 0x56f0
cmpl $0x2, %eax
movq %r13, %rbx
je 0x155e4
movq %r14, %rdi
callq 0x5390
addq $0x1078, %rsp           # imm = 0x1078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rbx), %rcx
leaq 0x4c21(%rip), %rsi      # 0x1a42e
leaq -0x890(%rbp), %rbx
leaq -0xc90(%rbp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x5090
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5730
movq 0xc784(%rip), %rsi      # 0x21fc8
movq 0xc76d(%rip), %rdx      # 0x21fb8
movq %r14, %rdi
callq 0x5660
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0xc90(%rbp), %rbx
movq %rbx, %rdi
callq 0x5180
leaq (%rax,%rbp), %rdx
addq $-0xc90, %rdx           # imm = 0xF370
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0xb226
movb $0x5f, (%r12)
movq -0x60(%rbp), %rcx
movq 0x20(%r13), %rax
movq %rax, (%rsp)
leaq 0x4be2(%rip), %rsi      # 0x1a47b
leaq -0x890(%rbp), %rbx
leaq -0xc90(%rbp), %rdx
movq %rbx, %rdi
movq %r15, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x5090
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5730
movq 0xc6f2(%rip), %rsi      # 0x21fc8
movq 0xc6db(%rip), %rdx      # 0x21fb8
movq %r14, %rdi
callq 0x5660
movq 0x20(%rbx), %rdx
leaq 0x4b21(%rip), %rsi      # 0x1a411
leaq -0x890(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x5090
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5730
jmp 0x1583d
movq (%rbx), %rdx
leaq 0x4ae9(%rip), %rsi      # 0x1a411
leaq -0x890(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x5090
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5730
jmp 0x1583d
jmp 0x15976
jmp 0x15976
movq %rax, %rbx
jmp 0x1596a
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
jmp 0x159c3
jmp 0x159b0
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x159cd
movq %rax, %rbx
leaq -0x888(%rbp), %rdi
callq 0x9852
leaq -0x60(%rbp), %rdi
callq 0x9852
jmp 0x159cd
movq %rax, %rbx
movq -0x890(%rbp), %rdi
cmpq %r12, %rdi
jne 0x159c8
jmp 0x159cd
jmp 0x159b0
movq %rax, %rbx
jmp 0x159cd
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x159cd
callq 0x5340
movq %rbx, %rdi
callq 0x56a0
nop