// File: /workspace/github_cpp_makefile_star2_O3/IlyaSamoylov45[P]MapReduce-Project/main.cpp
// Function: reduce_threads[abi:cxx11](int, int, ThreadData*)
std::map<std::string, int> reduce_threads(int reduces, int maps, ThreadData thread_data[]){
  if(maps < reduces) reduces = maps;
  std::map<std::string, int> resultMerged;
  // Each reduce thread will get map_thread ids in order to know which
  std::vector<std::vector<int>> mapJobs = assignJobs(reduces, maps);
  pthread_t threads[reduces];

  int iret;
  ReduceThreadArgs reduceThreadArgs[reduces];
  for(int i = 0; i < reduces; i++){

    std::vector<ThreadData> curData;
    for(int j = 0 ; j < mapJobs[i].size(); j++){
	curData.push_back(thread_data[mapJobs[i][j]]);
    }
    reduceThreadArgs[i].thread_data = curData;
    reduceThreadArgs[i].resultMerged = &resultMerged;
    iret = pthread_create(&threads[i], NULL, reduce_function_thread, (void*) &reduceThreadArgs[i]);
    if (iret != 0) {
			std::cout << "Error: Creating thread: " << iret << std::endl;
			exit(EXIT_FAILURE);
    }
  }
    for (int i = 0; i < reduces; i++) {
		iret = pthread_join(threads[i], NULL);
		if (iret) {
			std::cout << "Error: Joining thread: " << iret << std::endl;
			exit(EXIT_FAILURE);
		}
		/*
		std::map<std::string, int> testMap = thread_data[i].counter;
		for(auto it = testMap.begin(); it != testMap.end(); it++){
		  std::cout << it->first << " " << it->second << std::endl;
		} */
      }
      for(auto it = resultMerged.begin(); it != resultMerged.end(); it++){
		  //std::cout << it->first << " " << it->second << std::endl;
      }
     return resultMerged;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r15
movl %esi, %r14d
cmpl %esi, %edx
cmovll %edx, %r14d
leaq 0x8(%rdi), %rax
movl $0x0, 0x8(%rdi)
xorl %ecx, %ecx
movq %rcx, 0x10(%rdi)
movq %rax, 0x18(%rdi)
movq %rax, 0x20(%rdi)
movq %rdi, -0x40(%rbp)
movq %rcx, 0x28(%rdi)
leaq -0x78(%rbp), %rdi
movl %r14d, %esi
callq 0x5958
movq %rsp, %rbx
movl %r14d, %r13d
movq %rsp, %rcx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq %rsp, %r12
movq %r13, %rcx
shlq $0x5, %rcx
subq %rcx, %r12
movq %r12, %rsp
testl %r14d, %r14d
je 0x5036
movq %rbx, -0x58(%rbp)
xorl %eax, %eax
xorps %xmm0, %xmm0
movaps %xmm0, (%r12,%rax)
movq $0x0, 0x10(%r12,%rax)
addq $0x20, %rax
cmpq %rax, %rcx
jne 0x4ef9
movq %rcx, -0x50(%rbp)
testl %r14d, %r14d
jle 0x5010
movq %r12, -0x38(%rbp)
movl %r14d, -0x2c(%rbp)
xorl %ebx, %ebx
leaq -0x90(%rbp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x78(%rbp), %rcx
movq %rbx, -0x60(%rbp)
leaq (%rbx,%rbx,2), %r14
movq (%rcx,%r14,8), %rax
cmpq %rax, 0x8(%rcx,%r14,8)
je 0x4f88
xorl %ebx, %ebx
movslq (%rax,%rbx,4), %rax
imulq $0x58, %rax, %rsi
addq %r15, %rsi
movq %r12, %rdi
callq 0x6ed6
incq %rbx
movq -0x78(%rbp), %rcx
movq (%rcx,%r14,8), %rax
movq 0x8(%rcx,%r14,8), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rbx, %rcx
ja 0x4f59
movq -0x60(%rbp), %rbx
movq %rbx, %r14
shlq $0x5, %r14
addq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x6f0e
movq -0x40(%rbp), %rax
movq %rax, 0x18(%r14)
movq -0x48(%rbp), %rax
leaq (%rax,%rbx,8), %rdi
xorl %esi, %esi
leaq 0xae0(%rip), %rdx       # 0x5a9b
movq %r14, %rcx
callq 0x3590
testl %eax, %eax
jne 0x5055
movq %r12, %rdi
callq 0x6fe6
incq %rbx
cmpq %r13, %rbx
jne 0x4f2e
movl -0x2c(%rbp), %r14d
testl %r14d, %r14d
movq -0x38(%rbp), %r12
jle 0x5010
xorl %ebx, %ebx
movq -0x48(%rbp), %rax
movq (%rax,%rbx,8), %rdi
xorl %esi, %esi
callq 0x32c0
movl %eax, %r15d
testl %eax, %eax
jne 0x5089
incq %rbx
cmpq %rbx, %r13
jne 0x4fee
testl %r14d, %r14d
movq -0x58(%rbp), %rbx
movq -0x50(%rbp), %rax
je 0x5036
addq $-0x20, %r12
leaq (%r12,%rax), %rdi
movq %rax, %r14
callq 0x6fe6
movq %r14, %rax
addq $-0x20, %rax
jne 0x5021
movq %rbx, %rsp
leaq -0x78(%rbp), %rdi
callq 0x701a
movq -0x40(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r14d
movq 0x19f71(%rip), %rdi     # 0x1efd0
leaq 0x1009f(%rip), %rsi     # 0x15105
movl $0x18, %edx
callq 0x3340
movq 0x19f59(%rip), %rdi     # 0x1efd0
movl %r14d, %esi
callq 0x3550
movq %rax, %rdi
callq 0x3170
jmp 0x50b8
movq 0x19f40(%rip), %rdi     # 0x1efd0
leaq 0x10087(%rip), %rsi     # 0x1511e
movl $0x17, %edx
callq 0x3340
movq 0x19f28(%rip), %rdi     # 0x1efd0
movl %r15d, %esi
callq 0x3550
movq %rax, %rdi
callq 0x3170
movl $0x1, %edi
callq 0x3370
movq %rax, %r15
jmp 0x5111
movq %rax, %r15
jmp 0x50e4
movq %rax, %r15
jmp 0x50ec
jmp 0x50d5
jmp 0x50d5
movq %rax, %r15
leaq -0x90(%rbp), %rdi
callq 0x6fe6
movl -0x2c(%rbp), %r14d
movq -0x38(%rbp), %r12
testl %r14d, %r14d
je 0x5108
addq $-0x20, %r12
shlq $0x5, %r13
leaq (%r12,%r13), %rdi
callq 0x6fe6
addq $-0x20, %r13
jne 0x50f9
leaq -0x78(%rbp), %rdi
callq 0x701a
movq -0x40(%rbp), %rdi
callq 0x76c2
movq %r15, %rdi
callq 0x3560