// File: nmmmnu[P]HM3/hm3/main/db_merge.cc
// Function: main
int main(int argc, char **argv){
	if (argc <= 1 + 1 + 1){
		printUsage(argv[0]);
		return 1;
	}

	bool keepTombstones	= argv[1][0] == 't' ? false : true;
	const char *output	= argv[2];

	const char **path	= (const char **) &argv[3];
	int const pathc		= argc - 3;

	if (fileExists(output)){
		printf("File %s exists. Please remove it and try again.\n", output);
		return 2;
	}

	using DiskTable = hm3::DiskTable;

	if (pathc == 1){
		const char *filename = path[0];

		using MyMergeTable = DiskTable;

		MyMergeTable table;
		table.open(filename);

		printf("Merging (cleanup) single table...\n");
		printf("\t%s\n", filename);

		return merge(table, output, keepTombstones);

	}else if (pathc == 2){
		const char *filename1 = path[0];
		const char *filename2 = path[1];

		DiskTable table1;
		table1.open(filename1);

		DiskTable table2;
		table2.open(filename2);

		using MyMergeTable = hm3::DualTable<DiskTable, DiskTable>;

		// table 2 have precedence
		MyMergeTable table(table2, table1);

		printf("Merging two tables...\n");
		printf("\t%s\n", filename1);
		printf("\t%s\n", filename2);

		return merge(table, output, keepTombstones);

	}else{
		using ArgTableLoader = hm3::tableloader::ArgTableLoader;

		ArgTableLoader al { pathc, path };

		using MyMergeTable = hm3::LSMTable<ArgTableLoader::container_type>;

		MyMergeTable table( *al );

		printf("Merging multiple tables...\n");

		return merge(table, output, keepTombstones);
	}
}

// Assembly:
subq $0x398, %rsp            # imm = 0x398
movl $0x0, 0x394(%rsp)
movl %edi, 0x390(%rsp)
movq %rsi, 0x388(%rsp)
cmpl $0x3, 0x390(%rsp)
jg 0x25db
movq 0x388(%rsp), %rax
movq (%rax), %rdi
callq 0x29d0
movl $0x1, 0x394(%rsp)
jmp 0x29b0
movq 0x388(%rsp), %rax
movq 0x8(%rax), %rax
movb (%rax), %al
subb $0x74, %al
setne %al
negb %al
andb $0x1, %al
movb %al, 0x387(%rsp)
movq 0x388(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x378(%rsp)
movq 0x388(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x370(%rsp)
movl 0x390(%rsp), %eax
subl $0x3, %eax
movl %eax, 0x36c(%rsp)
movq 0x378(%rsp), %rsi
leaq 0x358(%rsp), %rdi
callq 0x2b40
leaq 0x358(%rsp), %rdi
callq 0x2b10
testb $0x1, %al
jne 0x265a
jmp 0x2680
movq 0x378(%rsp), %rsi
leaq 0xc99b(%rip), %rdi      # 0xf004
movb $0x0, %al
callq 0x2030
movl $0x2, 0x394(%rsp)
jmp 0x29b0
cmpl $0x1, 0x36c(%rsp)
jne 0x2772
movq 0x370(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x350(%rsp)
leaq 0x270(%rsp), %rdi
movl $0x1, %esi
callq 0x2b90
movq 0x350(%rsp), %rsi
leaq 0x260(%rsp), %rdi
callq 0x2b40
jmp 0x26ca
leaq 0x270(%rsp), %rdi
leaq 0x260(%rsp), %rsi
callq 0xbae0
jmp 0x26e1
leaq 0xc94d(%rip), %rdi      # 0xf035
xorl %eax, %eax
callq 0x2030
jmp 0x26f1
movq 0x350(%rsp), %rsi
leaq 0xc958(%rip), %rdi      # 0xf058
xorl %eax, %eax
callq 0x2030
jmp 0x2709
movq 0x378(%rsp), %rsi
movzbl 0x387(%rsp), %edx
andl $0x1, %edx
leaq 0x270(%rsp), %rdi
callq 0x2cc0
movl %eax, 0x1c(%rsp)
jmp 0x272f
movl 0x1c(%rsp), %eax
movl %eax, 0x394(%rsp)
leaq 0x270(%rsp), %rdi
callq 0x2d10
jmp 0x29b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x258(%rsp)
movl %eax, 0x254(%rsp)
leaq 0x270(%rsp), %rdi
callq 0x2d10
jmp 0x29bf
cmpl $0x2, 0x36c(%rsp)
jne 0x2906
movq 0x370(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x248(%rsp)
movq 0x370(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x240(%rsp)
leaq 0x160(%rsp), %rdi
movl $0x1, %esi
callq 0x2b90
movq 0x248(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x2b40
jmp 0x27d0
leaq 0x160(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0xbae0
jmp 0x27e7
leaq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x2b90
jmp 0x27f8
movq 0x240(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x2b40
jmp 0x280c
leaq 0x70(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xbae0
jmp 0x281d
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x160(%rsp), %rdx
callq 0x2d60
jmp 0x2836
leaq 0xc820(%rip), %rdi      # 0xf05d
xorl %eax, %eax
callq 0x2030
jmp 0x2846
movq 0x248(%rsp), %rsi
leaq 0xc803(%rip), %rdi      # 0xf058
xorl %eax, %eax
callq 0x2030
jmp 0x285e
movq 0x240(%rsp), %rsi
leaq 0xc7eb(%rip), %rdi      # 0xf058
xorl %eax, %eax
callq 0x2030
jmp 0x2876
movq 0x378(%rsp), %rsi
movzbl 0x387(%rsp), %edx
andl $0x1, %edx
leaq 0x50(%rsp), %rdi
callq 0x2d90
movl %eax, 0x18(%rsp)
jmp 0x2899
movl 0x18(%rsp), %eax
movl %eax, 0x394(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x2d10
leaq 0x160(%rsp), %rdi
callq 0x2d10
jmp 0x29b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x258(%rsp)
movl %eax, 0x254(%rsp)
jmp 0x28f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x258(%rsp)
movl %eax, 0x254(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x2d10
leaq 0x160(%rsp), %rdi
callq 0x2d10
jmp 0x29bf
movl 0x36c(%rsp), %esi
movq 0x370(%rsp), %rdx
leaq 0x28(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x2de0
movq 0x8(%rsp), %rdi
callq 0x2e50
movq %rax, 0x10(%rsp)
jmp 0x2935
movq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdi
callq 0x2e60
jmp 0x2946
leaq 0xc727(%rip), %rdi      # 0xf074
xorl %eax, %eax
callq 0x2030
jmp 0x2956
movq 0x378(%rsp), %rsi
movzbl 0x387(%rsp), %edx
andl $0x1, %edx
leaq 0x20(%rsp), %rdi
callq 0x2e80
movl %eax, 0x4(%rsp)
jmp 0x2979
movl 0x4(%rsp), %eax
movl %eax, 0x394(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x2ed0
jmp 0x29b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x258(%rsp)
movl %eax, 0x254(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x2ed0
jmp 0x29bf
movl 0x394(%rsp), %eax
addq $0x398, %rsp            # imm = 0x398
retq
movq 0x258(%rsp), %rdi
callq 0x2350
nopl (%rax)