// File: /workspace/github_cpp_makefile_star2_O2/ChnMasterOG[P]KmerGO2/kmergo/kmer_union.cpp
// Function: kmer_union(KmerGO_parameters&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
short kmer_union(KmerGO_parameters& p, std::string &loginfo)
{
	std::vector<std::string> files, ab_files, kmerdb_list, beacon_list, beacon_kmer;
	std::vector<uint64> ALLcounter;
	std::vector<float> ALLfcounter;
	uint32 files_number;
	uint32 i, j;
	uint64 k, Allkmernumber = 0;

	getFiles(p.kmercountings_path, ab_files, true);
	getFiles(p.kmercountings_path, files, false);
	files_number = files.size();

	for (i = 0; i < files_number; i++)
	{
		if (files[i].compare(files[i].length() - 8, 8, ".kmc_suf", 0, 8) == 0)
		{
			kmerdb_list.push_back(files[i].substr(0, files[i].length() - 8));
		}
	}

	if (kmerdb_list.size() * 2 < files_number)
	{
		loginfo = "The number of k-mer database is error.";
		return -1;
	}

	//count all k-mer counters and k-mer number
	CKMCFile kmer_data_base;
	CKmerAPI* kmer_object;
	uint64 temp, counter;
	uint32 _kmer_length = 4;
	for (i = 0; i < kmerdb_list.size(); i++)
	{
		temp = 0;
		if (!kmer_data_base.OpenForListing(joinPath(p.kmercountings_path, kmerdb_list[i])))
		{
			loginfo = "Can not open some k-mer database.";
			return -2;
		}
		_kmer_length = kmer_data_base.KmerLength();
		kmer_object = new CKmerAPI(_kmer_length);
		while (kmer_data_base.ReadNextKmer(*kmer_object, counter))
		{
			temp += counter;
			Allkmernumber++;
		}
		delete kmer_object;
		ALLcounter.push_back(temp);
		kmer_data_base.Close();
	}

	//divided to process_number blocks
	std::string temp_kmer;
	for (i = 0; i < p.process_number; i++)
	{
		temp_kmer = "";
		if (p.process_number < 40)	//distribute k-mer nonuniformly
			distribute_kmer_nonuniformly(0, i * 10000 / p.process_number, 10000, temp_kmer);
		else	//distribute k-mer uniformly
			distribute_kmer_uniformly(0, i * 256 / p.process_number, temp_kmer);
		for (j = 0; j < _kmer_length - 4; j++) temp_kmer += "A";
		beacon_list.push_back(temp_kmer);
	}
	temp_kmer = "TTTT";
	for (i = 0; i < _kmer_length - 4; i++) temp_kmer += "T";
	beacon_list.push_back(temp_kmer);
	
	// normalization coefficient
	short power = -5;
	for (i = 0; i < kmerdb_list.size(); i++)
	{
		j = 0;
		k = ALLcounter[i];
		while (k != 0)
		{
			k /= 10;
			j++;
		}
		if (power < (short)j - 4) power = j - 4;
	}
	for (i = 0; i < kmerdb_list.size(); i++)
	{
		if (power > 0) ALLfcounter.push_back(ALLcounter[i] / pow(10, power));
		else ALLfcounter.push_back(ALLcounter[i] * pow(10, -power));
	}

	//start threads
	std::vector<std::thread> sub_thread;
	for (i = 0; i < p.process_number; i++)
	{
		beacon_kmer.push_back(beacon_list[i]);
		beacon_kmer.push_back(beacon_list[i + 1]);
		sub_thread.push_back(std::thread(get_Son_Matrix, i, p, kmerdb_list, beacon_kmer, ALLfcounter));
		beacon_kmer.clear();
	}
	sub_thread.push_back(std::thread(show_Progress, Allkmernumber));
	for (i = 0; i < p.process_number; i++)
	{
		sub_thread[i].join();
	}
	sub_thread[p.process_number].join();

	switch (union_err_number)
	{
	case -11:
		loginfo = "Can not open some k-mer database in at least one thread.";
	case -12:
		loginfo = "Can not open output files in at least one thread.";
	default:
		break;
	}

	return union_err_number;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rsi, 0x38(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
movaps %xmm0, 0x150(%rsp)
andq $0x0, 0x160(%rsp)
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movaps %xmm0, 0xc0(%rsp)
andq $0x0, 0xd0(%rsp)
movaps %xmm0, 0x130(%rsp)
andq $0x0, 0x140(%rsp)
movaps %xmm0, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
movaps %xmm0, 0x100(%rsp)
andq $0x0, 0x110(%rsp)
andq $0x0, 0x78(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x40(%rdi), %rsi
leaq 0x1a8(%rsp), %rdi
movq %rsi, 0x40(%rsp)
callq 0x486820
leaq 0x1a8(%rsp), %rdi
leaq 0x150(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x405ddf
leaq 0x1a8(%rsp), %rdi
callq 0x484210
leaq 0x188(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x486820
leaq 0x188(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %r13d, %r13d
xorl %edx, %edx
callq 0x405ddf
leaq 0x188(%rsp), %rdi
callq 0x484210
movq 0x58(%rsp), %rbx
subq 0x50(%rsp), %rbx
sarq $0x5, %rbx
leaq 0x1e8(%rsp), %r14
pushq $0x8
popq %r12
movl %r13d, 0xc(%rsp)
cmpl %ebx, %r13d
jae 0x40771d
movl %r13d, %r15d
movq 0x50(%rsp), %rbp
shlq $0x5, %r15
movq 0x8(%rbp,%r15), %r13
movq %r14, %rdi
leaq 0x194a2b(%rip), %rsi    # 0x59c0c9
leaq 0xe0(%rsp), %rdx
callq 0x405d40
addq %r15, %rbp
addq $-0x8, %r13
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x486360
movl %eax, %ebp
movq %r14, %rdi
callq 0x484210
testl %ebp, %ebp
jne 0x407710
movl 0xc(%rsp), %eax
movq 0x50(%rsp), %rcx
shlq $0x5, %rax
leaq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rcx
addq $-0x8, %rcx
movq %r14, %rdi
xorl %edx, %edx
callq 0x486a40
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x4061f4
movq %r14, %rdi
callq 0x484210
movl 0xc(%rsp), %r13d
incl %r13d
jmp 0x407675
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x4, %rax
movl %ebx, %ecx
cmpq %rcx, %rax
jae 0x40774c
movw $0xffff, %bx            # imm = 0xFFFF
leaq 0x194c0b(%rip), %rsi    # 0x59c348
movq 0x38(%rsp), %rdi
callq 0x4851c0
jmp 0x407ce3
leaq 0x1e8(%rsp), %rdi
callq 0x4105b4
pushq $0x4
popq %rbp
xorl %r13d, %r13d
leaq 0xe0(%rsp), %rbx
leaq 0x1e8(%rsp), %r15
leaq 0x128(%rsp), %r14
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, %eax
movq 0x28(%rsp), %rcx
subq 0x20(%rsp), %rcx
sarq $0x5, %rcx
cmpq %rax, %rcx
jbe 0x407896
andq $0x0, 0x70(%rsp)
leaq 0x168(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x486820
movl 0xc(%rsp), %esi
shlq $0x5, %rsi
addq 0x20(%rsp), %rsi
leaq 0x1c8(%rsp), %r12
movq %r12, %rdi
callq 0x486820
movq %rbx, %rdi
leaq 0x168(%rsp), %rsi
movq %r12, %rdx
callq 0x405fcb
movq %r15, %rdi
movq %rbx, %rsi
callq 0x410460
movl %eax, %ebp
movq %rbx, %rdi
callq 0x484210
leaq 0x1c8(%rsp), %rdi
callq 0x484210
leaq 0x168(%rsp), %rdi
callq 0x484210
testb %bpl, %bpl
je 0x407cc1
movq %r15, %rdi
callq 0x410ff2
movl %eax, %ebp
pushq $0x18
popq %rdi
callq 0x414df0
movq %rax, %r12
movq %rax, %rdi
movl %ebp, %esi
callq 0x407f72
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x410c90
testb %al, %al
je 0x40785e
movq 0x128(%rsp), %rax
addq %rax, 0x70(%rsp)
incq %r13
movq %r13, 0x78(%rsp)
jmp 0x407835
movq %r12, %rdi
callq 0x40825c
movq %r12, %rdi
pushq $0x18
popq %rsi
callq 0x413f30
leaq 0xa0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4082e0
movq %r15, %rdi
callq 0x410e72
movl 0xc(%rsp), %eax
incl %eax
jmp 0x407779
leaq 0xf0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
addl $-0x4, %ebp
xorl %eax, %eax
leaq 0x1b95f4(%rip), %rbx    # 0x5c0eaa
leaq 0xe0(%rsp), %r14
leaq 0xc0(%rsp), %r15
leaq 0x1ab560(%rip), %r12    # 0x5b2e2d
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rcx
movzwl 0xc(%rcx), %ecx
cmpl %ecx, %eax
jae 0x407957
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4851c0
movq 0x10(%rsp), %rax
movzwl 0xc(%rax), %ecx
movl 0xc(%rsp), %eax
cmpl $0x27, %ecx
ja 0x407918
imull $0x2710, %eax, %eax     # imm = 0x2710
xorl %edx, %edx
divl %ecx
xorl %edi, %edi
movl %eax, %esi
movl $0x2710, %edx           # imm = 0x2710
movq %r14, %rcx
callq 0x4073f7
jmp 0x40792b
shll $0x8, %eax
xorl %edx, %edx
divl %ecx
xorl %edi, %edi
movl %eax, %esi
movq %r14, %rdx
callq 0x4074c7
movl %ebp, %r13d
subl $0x1, %r13d
jb 0x407941
movq %r14, %rdi
movq %r12, %rsi
callq 0x4859c0
jmp 0x40792e
movq %r15, %rdi
movq %r14, %rsi
callq 0x408300
movl 0xc(%rsp), %eax
incl %eax
jmp 0x4078cd
leaq 0x194a33(%rip), %rsi    # 0x59c391
leaq 0xe0(%rsp), %rdi
callq 0x4851c0
xorl %eax, %eax
leaq 0x1ab4c1(%rip), %rbx    # 0x5b2e35
leaq 0xe0(%rsp), %r14
movl %eax, 0xc(%rsp)
cmpl %ebp, %eax
jae 0x407997
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4859c0
movl 0xc(%rsp), %eax
incl %eax
jmp 0x40797c
leaq 0xc0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x408300
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq %rcx, %rdi
subq %rsi, %rdi
sarq $0x5, %rdi
movw $0xfffb, %bp            # imm = 0xFFFB
xorl %r8d, %r8d
movq 0xa0(%rsp), %r9
pushq $0xa
popq %r10
movl %r8d, %r8d
movswl %bp, %r11d
cmpq %r8, %rdi
jbe 0x407a12
movq (%r9,%r8,8), %rax
movw $0xfffc, %bx            # imm = 0xFFFC
xorl %r14d, %r14d
testq %rax, %rax
je 0x4079ff
xorl %edx, %edx
divq %r10
incl %ebx
addl $0x10000, %r14d         # imm = 0x10000
jmp 0x4079ea
sarl $0x10, %r14d
addl $-0x4, %r14d
cmpl %r11d, %r14d
cmovgl %ebx, %ebp
incl %r8d
jmp 0x4079d3
cvtsi2sd %r11d, %xmm0
movsd %xmm0, 0x120(%rsp)
negl %r11d
cvtsi2sd %r11d, %xmm0
movsd %xmm0, 0x118(%rsp)
xorl %eax, %eax
leaq 0x100(%rsp), %r14
leaq 0x80(%rsp), %rbx
subq %rsi, %rcx
movl %eax, 0xc(%rsp)
movl %eax, %eax
sarq $0x5, %rcx
cmpq %rax, %rcx
jbe 0x407b3d
testw %bp, %bp
jle 0x407ac6
movq 0xa0(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
unpcklps 0x19480e(%rip), %xmm0   # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x194816(%rip), %xmm0   # 0x59c290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movsd 0x1947c4(%rip), %xmm0   # 0x59c258
movsd 0x120(%rsp), %xmm1
callq 0x4b3810
movapd 0x40(%rsp), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x80(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x408fa8
jmp 0x407b28
movq 0xa0(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
unpcklps 0x1947a6(%rip), %xmm0   # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1947ae(%rip), %xmm0   # 0x59c290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movsd 0x19475c(%rip), %xmm0   # 0x59c258
movsd 0x118(%rsp), %xmm1
callq 0x4b3810
mulsd 0x40(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x80(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x408fa8
movl 0xc(%rsp), %eax
incl %eax
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
jmp 0x407a43
leaq 0x80(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rbx)
xorl %eax, %eax
leaq 0x130(%rsp), %r14
leaq -0x1563(%rip), %r12     # 0x406600
leaq 0x18(%rsp), %r13
leaq 0xc(%rsp), %rbp
leaq 0x20(%rsp), %r15
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rcx
movzwl 0xc(%rcx), %ecx
cmpl %ecx, %eax
jae 0x407bfe
movl %eax, %esi
shlq $0x5, %rsi
addq 0xc0(%rsp), %rsi
movq %r14, %rdi
callq 0x408300
movl 0xc(%rsp), %esi
incl %esi
shlq $0x5, %rsi
addq 0xc0(%rsp), %rsi
movq %r14, %rdi
callq 0x408300
leaq 0x100(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
movq %r15, %r8
movq %r14, %r9
callq 0x40832c
movq %rbx, %rdi
movq %r13, %rsi
callq 0x409b3e
movq %r13, %rdi
callq 0x4083e0
movq %r14, %rdi
callq 0x40618c
movl 0xc(%rsp), %eax
incl %eax
jmp 0x407b72
leaq 0x29c(%rip), %rsi       # 0x407ea1
leaq 0x18(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x4083ee
leaq 0x80(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x409b3e
leaq 0x18(%rsp), %rdi
callq 0x4083e0
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rcx
movzwl 0xc(%rcx), %edx
movq 0x80(%rsp), %rcx
cmpl %edx, %eax
jae 0x407c5e
movl %eax, %eax
leaq (%rcx,%rax,8), %rdi
callq 0x487040
movl 0xc(%rsp), %eax
incl %eax
jmp 0x407c32
leaq (%rcx,%rdx,8), %rdi
callq 0x487040
movzwl 0x20ea6c(%rip), %eax    # 0x6166da
cmpl $0xfff4, %eax           # imm = 0xFFF4
je 0x407c8d
cmpl $0xfff5, %eax           # imm = 0xFFF5
jne 0x407c9e
leaq 0x19463f(%rip), %rsi    # 0x59c2c2
movq 0x38(%rsp), %rdi
callq 0x4851c0
leaq 0x194673(%rip), %rsi    # 0x59c307
movq 0x38(%rsp), %rdi
callq 0x4851c0
movzwl 0x20ea35(%rip), %ebx    # 0x6166da
leaq 0x80(%rsp), %rdi
callq 0x408468
leaq 0xe0(%rsp), %rdi
callq 0x484210
jmp 0x407cd6
movw $0xfffe, %bx            # imm = 0xFFFE
leaq 0x1946a3(%rip), %rsi    # 0x59c36f
movq 0x38(%rsp), %rdi
callq 0x4851c0
leaq 0x1e8(%rsp), %rdi
callq 0x4105d6
leaq 0x100(%rsp), %rdi
callq 0x408710
leaq 0xa0(%rsp), %rdi
callq 0x408d28
leaq 0x130(%rsp), %rdi
callq 0x40616a
leaq 0xc0(%rsp), %rdi
callq 0x40616a
leaq 0x20(%rsp), %rdi
callq 0x40616a
leaq 0x150(%rsp), %rdi
callq 0x40616a
leaq 0x50(%rsp), %rdi
callq 0x40616a
movl %ebx, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x407e34
jmp 0x407dab
jmp 0x407df5
jmp 0x407e1d
jmp 0x407df5
jmp 0x407e22
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
jmp 0x407e16
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
jmp 0x407e16
jmp 0x407e1d
jmp 0x407e22
jmp 0x407e22
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r12, %rdi
callq 0x413f30
jmp 0x407e37
jmp 0x407df5
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4083e0
jmp 0x407df8
jmp 0x407df5
jmp 0x407e22
jmp 0x407e34
movq %rax, %rbx
jmp 0x407de7
movq %rax, %rbx
jmp 0x407dda
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x484210
leaq 0x1c8(%rsp), %rdi
callq 0x484210
leaq 0x168(%rsp), %rdi
jmp 0x407e2d
jmp 0x407e0b
jmp 0x407e1d
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x408468
jmp 0x407e25
jmp 0x407e22
jmp 0x407e34
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
callq 0x484210
jmp 0x407e44
movq %rax, %rbx
jmp 0x407e44
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x484210
jmp 0x407e37
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
callq 0x4105d6
leaq 0x100(%rsp), %rdi
callq 0x408710
leaq 0xa0(%rsp), %rdi
callq 0x408d28
leaq 0x130(%rsp), %rdi
callq 0x40616a
leaq 0xc0(%rsp), %rdi
callq 0x40616a
leaq 0x20(%rsp), %rdi
callq 0x40616a
leaq 0x150(%rsp), %rdi
callq 0x40616a
leaq 0x50(%rsp), %rdi
callq 0x40616a
movq %rbx, %rdi
callq 0x4b8e10