// File: 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:
subq $0x6f8, %rsp            # imm = 0x6F8
movl $0x0, 0x6f4(%rsp)
movl %edi, 0x6f0(%rsp)
movq %rsi, 0x6e8(%rsp)
callq 0x60b0
movq %rax, 0x6e0(%rsp)
leaq 0x600(%rsp), %rdi
movq %rdi, 0xc8(%rsp)
callq 0x79c0
movq 0xc8(%rsp), %rdi
movl 0x6f0(%rsp), %esi
movq 0x6e8(%rsp), %rdx
callq 0x1fe50
movl %eax, 0xd4(%rsp)
jmp 0x6c98
movl 0xd4(%rsp), %eax
cmpl $0x0, %eax
je 0x6cc7
movl $0x1, %edi
callq 0x65e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
jmp 0x775b
leaq 0x628(%rsp), %rsi
leaq 0x5d0(%rsp), %rdi
callq 0x6260
jmp 0x6cde
movq 0x792db(%rip), %rdi     # 0x7ffc0
leaq 0x5d0(%rsp), %rsi
callq 0x6490
movq %rax, 0xc0(%rsp)
jmp 0x6cfc
movq 0xc0(%rsp), %rdi
movq 0x7929d(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0x6d12
movl $0x0, 0x5cc(%rsp)
leaq 0x648(%rsp), %rsi
leaq 0x5a8(%rsp), %rdi
callq 0x6260
jmp 0x6d34
leaq 0x668(%rsp), %rsi
leaq 0x588(%rsp), %rdi
callq 0x6260
jmp 0x6d4b
leaq 0x570(%rsp), %rdi
callq 0x7a20
leaq 0x558(%rsp), %rdi
callq 0x7a20
movl $0x0, 0x554(%rsp)
movl $0x0, 0x550(%rsp)
callq 0x60b0
movq %rax, 0x548(%rsp)
leaq 0x528(%rsp), %rdi
leaq 0x588(%rsp), %rsi
callq 0x6260
jmp 0x6d9f
leaq 0x528(%rsp), %rdi
leaq 0x558(%rsp), %rsi
leaq 0x550(%rsp), %rdx
callq 0x7780
jmp 0x6dbe
leaq 0x528(%rsp), %rdi
callq 0x6340
callq 0x60b0
movq %rax, 0x520(%rsp)
leaq 0x520(%rsp), %rdi
leaq 0x548(%rsp), %rsi
callq 0x7a60
movq %rax, 0xb8(%rsp)
jmp 0x6df7
movq 0xb8(%rsp), %rax
movq %rax, 0x508(%rsp)
leaq 0x508(%rsp), %rdi
callq 0x7a30
movq %rax, 0xb0(%rsp)
jmp 0x6e1e
movq 0xb0(%rsp), %rax
movq %rax, 0x510(%rsp)
leaq 0x51c(%rsp), %rdi
leaq 0x510(%rsp), %rsi
callq 0x7ab0
jmp 0x6e45
leaq 0x7970c(%rip), %rdi     # 0x80558
leaq 0x51c(%rsp), %rsi
callq 0x7af0
jmp 0x6e5b
xorl %eax, %eax
movl %eax, %esi
leaq 0x558(%rsp), %rdi
callq 0x7b30
movq %rax, 0xa8(%rsp)
jmp 0x6e76
movq 0xa8(%rsp), %rdi
callq 0x6390
movl %eax, 0x698(%rsp)
leaq 0x4f0(%rsp), %rdi
callq 0x7b70
leaq 0x1d8(%rsp), %rdi
leaq 0x600(%rsp), %rsi
callq 0x7b80
jmp 0x6eae
leaq 0x2b8(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0xad90
jmp 0x6ec5
leaq 0x1d8(%rsp), %rdi
callq 0x7d00
movb $0x0, 0x1d7(%rsp)
movb $0x0, 0x1d6(%rsp)
movb 0x620(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7064
leaq 0x2b8(%rsp), %rdi
callq 0xb910
movl %eax, 0xa4(%rsp)
jmp 0x6f0d
movl 0xa4(%rsp), %eax
movl %eax, 0x1d0(%rsp)
movq 0x7909e(%rip), %rdi     # 0x7ffc0
leaq 0x5d0df(%rip), %rsi     # 0x64008
callq 0x6500
movq %rax, 0x98(%rsp)
jmp 0x6f38
movq 0x98(%rsp), %rdi
movl 0x1d0(%rsp), %esi
callq 0x6830
movq %rax, 0x90(%rsp)
jmp 0x6f56
movq 0x90(%rsp), %rdi
movq 0x79043(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0x6f6c
movl 0x1d0(%rsp), %eax
subl $0x1, %eax
sete 0x1d7(%rsp)
movl 0x1d0(%rsp), %eax
subl $-0x1, %eax
sete %al
negb %al
andb $0x1, %al
movb %al, 0x1d6(%rsp)
jmp 0x7064
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
jmp 0x774e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
jmp 0x7741
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
jmp 0x771a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
leaq 0x528(%rsp), %rdi
callq 0x6340
jmp 0x771a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
jmp 0x770d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
leaq 0x1d8(%rsp), %rdi
callq 0x7d00
jmp 0x770d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
jmp 0x7700
testb $0x1, 0x620(%rsp)
jne 0x7086
testb $0x1, 0x1d6(%rsp)
jne 0x7086
testb $0x1, 0x1d7(%rsp)
je 0x71c9
leaq 0x1b0(%rsp), %rdi
leaq 0x5a8(%rsp), %rsi
callq 0x6260
jmp 0x709d
leaq 0x1b0(%rsp), %rdi
leaq 0x570(%rsp), %rsi
leaq 0x554(%rsp), %rdx
callq 0x7780
jmp 0x70bc
leaq 0x1b0(%rsp), %rdi
callq 0x6340
leaq 0x198(%rsp), %rdi
leaq 0x570(%rsp), %rsi
callq 0x7d60
jmp 0x70e0
leaq 0x2b8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
leaq 0x4f0(%rsp), %rdx
callq 0xe680
jmp 0x70ff
leaq 0x198(%rsp), %rdi
callq 0x7e60
leaq 0x180(%rsp), %rdi
leaq 0x4f0(%rsp), %rsi
callq 0x7ec0
jmp 0x7123
movzbl 0x1d7(%rsp), %edx
andl $0x1, %edx
leaq 0x2b8(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0xc090
jmp 0x7145
leaq 0x180(%rsp), %rdi
callq 0x7fc0
jmp 0x721a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x6340
jmp 0x7700
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x7e60
jmp 0x7700
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x7fc0
jmp 0x7700
leaq 0x2b8(%rsp), %rdi
callq 0xf270
jmp 0x71d8
leaq 0x2b8(%rsp), %rdi
callq 0xf5a0
movss %xmm0, 0x8c(%rsp)
jmp 0x71f0
movss 0x8c(%rsp), %xmm0
movss %xmm0, 0x17c(%rsp)
leaq 0x7934f(%rip), %rdi     # 0x80558
leaq 0x17c(%rsp), %rsi
callq 0x7af0
jmp 0x7218
jmp 0x721a
movq 0x78d9f(%rip), %rdi     # 0x7ffc0
leaq 0x5cdef(%rip), %rsi     # 0x64017
callq 0x6500
movq %rax, 0x80(%rsp)
jmp 0x7237
movq 0x80(%rsp), %rdi
movq 0x78d62(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0x724d
leaq 0x558(%rsp), %rdi
callq 0x8020
movq %rax, 0x170(%rsp)
movl $0x0, 0x16c(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x6700
xorl %eax, %eax
movl %eax, %edi
callq 0x64a0
movl %eax, %edi
callq 0x64f0
movl $0x0, 0x140(%rsp)
movl 0x140(%rsp), %eax
cmpl 0x4e8(%rsp), %eax
jge 0x759b
movq 0x78d10(%rip), %rdi     # 0x7ffc0
leaq 0x5cd73(%rip), %rsi     # 0x6402a
callq 0x6500
movq %rax, 0x78(%rsp)
jmp 0x72c3
movq 0x78(%rsp), %rdi
movl 0x140(%rsp), %esi
callq 0x6830
movq %rax, 0x70(%rsp)
jmp 0x72db
movq 0x70(%rsp), %rdi
leaq 0x5cd48(%rip), %rsi     # 0x6402f
callq 0x6500
movq %rax, 0x68(%rsp)
jmp 0x72f3
movq 0x68(%rsp), %rdi
movq 0x78ca9(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0x7306
movq $0x0, 0x138(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x558(%rsp), %rdi
callq 0x8020
movq %rax, %rcx
movq 0x60(%rsp), %rax
cmpq %rcx, %rax
jae 0x7556
movq 0x138(%rsp), %rsi
leaq 0x558(%rsp), %rdi
callq 0x8040
movq %rax, %rsi
leaq 0x148(%rsp), %rdi
callq 0x6080
jmp 0x7364
movb $0x0, 0x147(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x6260
jmp 0x7383
leaq 0x118(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
leaq 0x147(%rsp), %rcx
callq 0xe1a0
jmp 0x73aa
leaq 0x118(%rsp), %rdi
callq 0x6340
leaq 0xf8(%rsp), %rdi
callq 0x6340
leaq 0x148(%rsp), %rdi
callq 0x6680
movq 0x138(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x558(%rsp), %rdi
callq 0x8020
movl $0xa, %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq 0x58(%rsp), %rax
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
jne 0x74ca
movq 0x138(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x558(%rsp), %rdi
callq 0x8020
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rcx
movq 0x48(%rsp), %rax
movq %rdx, %rcx
shrq $0x3, %rcx
xorl %edx, %edx
divq %rcx
addq %rax, %rax
leaq (%rax,%rax,4), %rsi
movq 0x78b6e(%rip), %rdi     # 0x7ffc0
callq 0x6150
movq %rax, 0x50(%rsp)
jmp 0x745e
movq 0x50(%rsp), %rdi
leaq 0x5cbce(%rip), %rsi     # 0x64038
callq 0x6500
movq %rax, 0x40(%rsp)
jmp 0x7476
movq 0x40(%rsp), %rdi
movq 0x78b46(%rip), %rsi     # 0x7ffc8
callq 0x6540
jmp 0x7489
jmp 0x74ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
jmp 0x76f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5f8(%rsp)
movl %eax, 0x5f4(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x6340
jmp 0x76f3
movq 0x138(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x558(%rsp), %rdi
callq 0x8020
movl $0x64, %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq 0x38(%rsp), %rax
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
jne 0x753b
cmpq $0x0, 0x138(%rsp)
je 0x753b
movq 0x78aad(%rip), %rdi     # 0x7ffc0
leaq 0x5cb19(%rip), %rsi     # 0x64033
callq 0x6500
movq %rax, 0x30(%rsp)
jmp 0x7526
movq 0x30(%rsp), %rdi
movq 0x78a96(%rip), %rsi     # 0x7ffc8
callq 0x6540
jmp 0x7539
jmp 0x753b
jmp 0x753d
movq 0x138(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x138(%rsp)
jmp 0x7312
movq 0x78a63(%rip), %rdi     # 0x7ffc0
leaq 0x5cad1(%rip), %rsi     # 0x64035
callq 0x6500
movq %rax, 0x28(%rsp)
jmp 0x7570
movq 0x28(%rsp), %rdi
movq 0x78a2c(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0x7583
jmp 0x7585
movl 0x140(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x140(%rsp)
jmp 0x7295
callq 0x60b0
movq %rax, 0xf0(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0x6e0(%rsp), %rsi
callq 0x7a60
movq %rax, 0x20(%rsp)
jmp 0x75c4
movq 0x20(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x7a30
movq %rax, 0x18(%rsp)
jmp 0x75e5
movq 0x18(%rsp), %rax
movq %rax, 0xe0(%rsp)
leaq 0xec(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x7ab0
jmp 0x7609
leaq 0xec(%rsp), %rdi
callq 0x8060
movss %xmm0, 0x14(%rsp)
jmp 0x761e
movss 0x14(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x78f23(%rip), %rdi     # 0x80558
callq 0x8060
movss %xmm0, 0x10(%rsp)
jmp 0x7642
movsd 0x8(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x2b8(%rsp), %rdi
callq 0x105d0
jmp 0x7661
movl 0x16c(%rsp), %eax
movl %eax, 0x6f4(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x6340
leaq 0x2b8(%rsp), %rdi
callq 0x8070
leaq 0x4f0(%rsp), %rdi
callq 0x7fc0
leaq 0x558(%rsp), %rdi
callq 0x7e60
leaq 0x570(%rsp), %rdi
callq 0x7e60
leaq 0x588(%rsp), %rdi
callq 0x6340
leaq 0x5a8(%rsp), %rdi
callq 0x6340
leaq 0x5d0(%rsp), %rdi
callq 0x6340
leaq 0x600(%rsp), %rdi
callq 0x7d00
movl 0x6f4(%rsp), %eax
addq $0x6f8, %rsp            # imm = 0x6F8
retq
leaq 0x148(%rsp), %rdi
callq 0x6340
leaq 0x2b8(%rsp), %rdi
callq 0x8070
leaq 0x4f0(%rsp), %rdi
callq 0x7fc0
leaq 0x558(%rsp), %rdi
callq 0x7e60
leaq 0x570(%rsp), %rdi
callq 0x7e60
leaq 0x588(%rsp), %rdi
callq 0x6340
leaq 0x5a8(%rsp), %rdi
callq 0x6340
leaq 0x5d0(%rsp), %rdi
callq 0x6340
leaq 0x600(%rsp), %rdi
callq 0x7d00
movq 0x5f8(%rsp), %rdi
callq 0x6850
nopw %cs:(%rax,%rax)