// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rcx, %r15
movl %esi, %ebx
cmpl %esi, %edx
cmovll %edx, %ebx
leaq 0x8(%rdi), %rax
andl $0x0, 0x8(%rdi)
andq $0x0, 0x10(%rdi)
movq %rax, 0x18(%rdi)
movq %rax, 0x20(%rdi)
movq %rdi, -0x40(%rbp)
andq $0x0, 0x28(%rdi)
leaq -0x98(%rbp), %rdi
movl %ebx, %esi
callq 0x4ec9
movq %rsp, -0x60(%rbp)
movl %ebx, %r13d
movq %rsp, %rcx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
movq %r13, -0x58(%rbp)
shlq $0x5, %r13
subq %r13, %rcx
movq %rcx, %rsp
testl %ebx, %ebx
je 0x475a
xorl %eax, %eax
xorps %xmm0, %xmm0
andq $0x0, 0x10(%rcx,%rax)
movaps %xmm0, (%rcx,%rax)
addq $0x20, %rax
cmpq %rax, %r13
jne 0x4747
movq %rcx, -0x38(%rbp)
testl %ebx, %ebx
movl $0x0, %eax
movl %ebx, -0x2c(%rbp)
cmovgl %ebx, %eax
xorl %ebx, %ebx
movq %rax, -0x50(%rbp)
leaq -0x80(%rbp), %r12
cmpq -0x50(%rbp), %rbx
je 0x481c
andq $0x0, -0x70(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq %rbx, -0x68(%rbp)
imulq $0x18, %rbx, %r14
xorl %ebx, %ebx
movq -0x98(%rbp), %rcx
movq (%rcx,%r14), %rax
movq 0x8(%rcx,%r14), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rbx, %rcx
jbe 0x47c9
movslq (%rax,%rbx,4), %rax
imulq $0x58, %rax, %rsi
addq %r15, %rsi
movq %r12, %rdi
callq 0x6048
incq %rbx
jmp 0x4795
movq -0x68(%rbp), %rbx
movq %rbx, %r14
shlq $0x5, %r14
addq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x6084
movq -0x40(%rbp), %rax
movq %rax, 0x18(%r14)
movq -0x48(%rbp), %rax
leaq (%rax,%rbx,8), %rdi
xorl %esi, %esi
leaq 0x7c7(%rip), %rdx       # 0x4fc3
movq %r14, %rcx
callq 0x3500
testl %eax, %eax
jne 0x48b0
movq %r12, %rdi
callq 0x6156
incq %rbx
jmp 0x4775
xorl %ebx, %ebx
movl -0x2c(%rbp), %r14d
movq -0x38(%rbp), %r12
cmpq %rbx, -0x50(%rbp)
je 0x486d
movq -0x48(%rbp), %rax
movq (%rax,%rbx,8), %rdi
xorl %esi, %esi
callq 0x3270
movl %eax, %r15d
incq %rbx
testl %eax, %eax
je 0x4826
movq 0x1a77c(%rip), %rdi     # 0x1efc8
leaq 0xd8cb(%rip), %rsi      # 0x1211e
callq 0x32b0
movq %rax, %rdi
movl %r15d, %esi
callq 0x34d0
movq %rax, %rdi
callq 0x3150
jmp 0x48d9
testl %r14d, %r14d
movq -0x58(%rbp), %rbx
je 0x488d
addq $-0x20, %r12
shlq $0x5, %rbx
leaq (%r12,%rbx), %rdi
callq 0x6156
addq $-0x20, %rbx
jne 0x487e
movq -0x60(%rbp), %rsp
leaq -0x98(%rbp), %rdi
callq 0x6182
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 0x1a70e(%rip), %rdi     # 0x1efc8
leaq 0xd844(%rip), %rsi      # 0x12105
callq 0x32b0
movq %rax, %rdi
movl %r14d, %esi
callq 0x34d0
movq %rax, %rdi
callq 0x3150
pushq $0x1
popq %rdi
callq 0x3310
movq %rax, %r15
jmp 0x492c
movq %rax, %r15
jmp 0x4900
jmp 0x48f4
movq %rax, %r15
jmp 0x4908
jmp 0x48f4
movq %rax, %r15
leaq -0x80(%rbp), %rdi
callq 0x6156
movl -0x2c(%rbp), %r14d
movq -0x38(%rbp), %r12
testl %r14d, %r14d
je 0x4920
addq $-0x20, %r12
leaq (%r12,%r13), %rdi
callq 0x6156
addq $-0x20, %r13
jne 0x4911
leaq -0x98(%rbp), %rdi
callq 0x6182
movq -0x40(%rbp), %rdi
callq 0x6adc
movq %r15, %rdi
callq 0x34e0