// File: /workspace/github_cpp_makefile_star2_O2/BU-DiSC[P]BF-Shared-Hashing/lsm-emulation.cpp
// Function: main
int main(int argc, char * argv[])
{
	auto t_start = high_resolution_clock::now();
	options op;
	// parse the command line arguments
	if ( op.parse(argc, argv) ) {
	  exit(1);
	}
	string filename = op.out_path;
	cout << filename << endl;

	int aloc = 0;

	// -- load workload --------------------------------------------------------------------
	string file_in_set  = op.insert_path;
	string file_out_set = op.query_path;

	vector<string> table_in;
	vector<string> table_out;

	int S_size = 0;
	int SC_size = 0;
	auto l_start = high_resolution_clock::now();
	loadfile( file_out_set, &table_out, &SC_size );
	auto l_end = high_resolution_clock::now();
	load_duration += duration_cast<microseconds>(l_end - l_start);

	op.key_size = table_out.at(0).size();
	// -------------------------------------------------------------------------------------
	
	vector<vector<vector<string> > > reallocated_keys;

        db database( op );

	// -- Program && Bloom Filters -------------------------------------------------------------------
	// log file
	bool bf_only = false;
	bool rebuild = false;
	if ( op.destroy_db==false ){
		int read_result  = database.ReadSettings();
		cout << "read db check " << read_result << endl;
		bf_only = ( read_result==1 )? true : false;
		rebuild = ( read_result==-1 )? true : false;
	}
	if ( op.destroy_db || rebuild || bf_only ) {
		loadfile( file_in_set, &table_in, &S_size );
		database.split_keys( table_in, reallocated_keys );
		database.Build( reallocated_keys, bf_only );
	}
	else {
		database.SetTreeParam();
		load_duration += database.loadBFAndIndex();
	}
	cout << "DB has been loaded" << endl; 
	// ------------------------------------------------------------------------------


	// -- Query ---------------------------------------------------------------------
	unsigned long total_query = table_out.size();

	int temp = 0;

	string input_str;
	srand(time(0));

	bool result;

	
	//for ( long i=0 ; i<100 ; i++ ) {
	for(int k = 0; k < database.tries; k++){
		cout << "Run " << k << " : " << endl;
		for ( long i=0 ; i<table_out.size() ; i++ ) {
			input_str = table_out[i];
			//cout << input_str << endl;
			result = false;
			database.Get( input_str, &result );

			input_str.clear();
			if ( i%(table_out.size()/10) == 0 ) cout << i/(table_out.size()/10)*10 << "%" << std::flush;
			if ( i%(table_out.size()/100) == 0 && i != 0 ) cout << "=" << std::flush;
		}
		cout << "100%" << endl;
	}
	
	// ------------------------------------------------------------------------------
		// log file
	auto t_end = high_resolution_clock::now();
	fsec t_duration = duration_cast<std::chrono::microseconds>(t_end-t_start);
	database.PrintStat(t_duration.count(), load_duration.count());
	return temp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5a8, %rsp            # imm = 0x5A8
movq %rsi, %rbx
movl %edi, %ebp
callq 0x6070
leaq 0x1a0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
xorl %edx, %edx
movb %dl, (%rcx)
leaq 0x1c8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %dl, (%rcx)
leaq 0x1e8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %dl, (%rcx)
leaq 0x208(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movq %rax, 0x70(%rsp)
movb %dl, (%rcx)
leaq 0x250(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %dl, (%rax)
leaq 0x190(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0xf26c
testl %eax, %eax
jne 0x6f58
leaq 0x1b8(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x6570
movq 0x2e575(%rip), %rdi     # 0x34fc8
leaq 0x150(%rsp), %rsi
callq 0x63b0
movq %rax, %rdi
callq 0x6210
leaq 0x1d8(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0x6570
leaq 0x1f8(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x6570
andq $0x0, 0x60(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x20(%rsp)
movaps %xmm0, 0x50(%rsp)
andl $0x0, 0x2c(%rsp)
movaps %xmm0, 0x10(%rsp)
andl $0x0, 0x28(%rsp)
callq 0x6070
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x6570
leaq 0xf0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x7076
leaq 0xf0(%rsp), %rdi
callq 0x61f0
callq 0x6070
subq %rbx, %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
cvtsi2ss %rax, %xmm0
divss 0x1b4ef(%rip), %xmm0    # 0x22004
addss 0x2e94b(%rip), %xmm0    # 0x35468
movss %xmm0, 0x2e943(%rip)    # 0x35468
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x720c
movl 0x8(%rax), %eax
leaq 0x190(%rsp), %rsi
movl %eax, 0x98(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x4c8(%rsp), %rdi
callq 0x722e
leaq 0x290(%rsp), %rdi
leaq 0x4c8(%rsp), %rsi
callq 0x7d4c
leaq 0x4c8(%rsp), %rdi
callq 0x7316
cmpb $0x0, 0x1b0(%rsp)
je 0x6b8d
xorl %ebp, %ebp
jmp 0x6be0
leaq 0x290(%rsp), %rdi
callq 0x8372
movl %eax, %ebx
movq 0x2e425(%rip), %rdi     # 0x34fc8
leaq 0x1b45e(%rip), %rsi     # 0x22008
callq 0x6410
movq %rax, %rdi
movl %ebx, %esi
callq 0x6690
movq %rax, %rdi
callq 0x6210
cmpl $0x1, %ebx
sete %bpl
testb $0x1, 0x1b0(%rsp)
jne 0x6be0
cmpl $0x1, %ebx
je 0x6be0
cmpl $-0x1, %ebx
jne 0x6f29
leaq 0xd0(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x6570
leaq 0xd0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
callq 0x7076
leaq 0xd0(%rsp), %rdi
callq 0x61f0
leaq 0xb8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x734a
leaq 0x290(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x9df6
leaq 0xb8(%rsp), %rdi
callq 0x739e
leaq 0xa0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x73c0
movzbl %bpl, %edx
leaq 0x290(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x8794
leaq 0xa0(%rsp), %rdi
callq 0x741a
movq 0x2e337(%rip), %rdi     # 0x34fc8
leaq 0x1b37f(%rip), %rsi     # 0x22017
callq 0x6410
movq %rax, %rdi
callq 0x6210
leaq 0x90(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x63d0
movl %eax, %edi
callq 0x6400
leaq 0x80(%rsp), %rbp
leaq 0x170(%rsp), %rbx
leaq 0x270(%rsp), %r12
pushq $0x64
popq %r13
cmpl 0x4c0(%rsp), %r14d
jge 0x6e6f
movq 0x2e2cd(%rip), %rdi     # 0x34fc8
leaq 0x1b328(%rip), %rsi     # 0x2202a
callq 0x6410
movq %rax, %rdi
movl %r14d, %esi
callq 0x6690
movq %rax, %rdi
leaq 0x1b313(%rip), %rsi     # 0x2202f
callq 0x6410
movq %rax, %rdi
callq 0x6210
movq %r14, 0x78(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %rax, %r15
jae 0x6e47
addq %r14, %rsi
movq %rbp, %rdi
callq 0x6250
movb $0x0, 0xf(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x6570
movq %r12, %rdi
leaq 0x290(%rsp), %rsi
movq %rbx, %rdx
leaq 0xf(%rsp), %rcx
callq 0x9b5c
movq %r12, %rdi
callq 0x61f0
movq %rbx, %rdi
callq 0x61f0
andq $0x0, 0x88(%rsp)
movq 0x80(%rsp), %rax
movb $0x0, (%rax)
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
sarq $0x5, %rcx
movq %rcx, %rax
xorl %edx, %edx
pushq $0xa
popq %rsi
divq %rsi
movq %rax, %rsi
movq %r15, %rax
xorl %edx, %edx
divq %rsi
testq %rdx, %rdx
jne 0x6e03
imulq $0xa, %rax, %rsi
movq 0x2e1ef(%rip), %rdi     # 0x34fc8
callq 0x62b0
movq %rax, %rdi
leaq 0x1b250(%rip), %rsi     # 0x22038
callq 0x6410
movq %rax, %rdi
callq 0x62c0
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
sarq $0x5, %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r13
movq %rax, %rcx
movq %r15, %rax
xorl %edx, %edx
divq %rcx
testq %r15, %r15
je 0x6e3b
testq %rdx, %rdx
jne 0x6e3b
movq 0x2e1a1(%rip), %rdi     # 0x34fc8
leaq 0x1b205(%rip), %rsi     # 0x22033
callq 0x6410
movq %rax, %rdi
callq 0x62c0
incq %r15
addq $0x20, %r14
jmp 0x6d34
movq 0x2e17a(%rip), %rdi     # 0x34fc8
leaq 0x1b1e0(%rip), %rsi     # 0x22035
callq 0x6410
movq 0x78(%rsp), %r14
movq %rax, %rdi
callq 0x6210
incl %r14d
jmp 0x6ce6
callq 0x6070
subq 0x70(%rsp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
cvtsi2ss %rax, %xmm0
divss 0x1b174(%rip), %xmm0    # 0x22004
cvtss2sd %xmm0, %xmm0
cvtss2sd 0x2e5cc(%rip), %xmm1    # 0x35468
leaq 0x290(%rsp), %rdi
callq 0xae30
leaq 0x80(%rsp), %rdi
callq 0x61f0
leaq 0x290(%rsp), %rdi
callq 0x743c
leaq 0x30(%rsp), %rdi
callq 0x741a
leaq 0x10(%rsp), %rdi
callq 0x739e
leaq 0x50(%rsp), %rdi
callq 0x739e
leaq 0x110(%rsp), %rdi
callq 0x61f0
leaq 0x130(%rsp), %rdi
callq 0x61f0
leaq 0x150(%rsp), %rdi
callq 0x61f0
leaq 0x190(%rsp), %rdi
callq 0x7316
xorl %eax, %eax
addq $0x5a8, %rsp            # imm = 0x5A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x290(%rsp), %rdi
callq 0xa31c
leaq 0x290(%rsp), %rdi
callq 0xa45e
addss 0x2e51d(%rip), %xmm0    # 0x35468
movss %xmm0, 0x2e515(%rip)    # 0x35468
jmp 0x6c8a
pushq $0x1
popq %rdi
callq 0x64b0
jmp 0x6fe6
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x741a
jmp 0x700f
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x739e
jmp 0x700f
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x700a
jmp 0x6fff
movq %rax, %rbx
leaq 0x4c8(%rsp), %rdi
callq 0x7316
jmp 0x701c
movq %rax, %rbx
jmp 0x701c
jmp 0x6fc9
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x61f0
jmp 0x7026
movq %rax, %rbx
jmp 0x7026
movq %rax, %rbx
jmp 0x7047
jmp 0x6fe1
jmp 0x6fd7
movq %rax, %rbx
jmp 0x7061
jmp 0x6fe6
movq %rax, %rbx
jmp 0x7054
movq %rax, %rbx
jmp 0x700f
jmp 0x6fff
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x61f0
jmp 0x7002
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x61f0
leaq 0x290(%rsp), %rdi
callq 0x743c
leaq 0x30(%rsp), %rdi
callq 0x741a
leaq 0x10(%rsp), %rdi
callq 0x739e
leaq 0x50(%rsp), %rdi
callq 0x739e
leaq 0x110(%rsp), %rdi
callq 0x61f0
leaq 0x130(%rsp), %rdi
callq 0x61f0
leaq 0x150(%rsp), %rdi
callq 0x61f0
leaq 0x190(%rsp), %rdi
callq 0x7316
movq %rbx, %rdi
callq 0x66b0