// File: /workspace/github_cpp_makefile_star2_O1/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, %ebx
cmpl %esi, %edx
cmovll %edx, %ebx
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 %ebx, %esi
callq 0x5960
movq %rsp, -0x58(%rbp)
movl %ebx, %r13d
movq %rsp, %rcx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
movq %r13, -0x48(%rbp)
shlq $0x5, %r13
subq %r13, %rcx
movq %rcx, %rsp
testl %ebx, %ebx
je 0x4f1a
xorl %eax, %eax
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx,%rax)
movq $0x0, 0x10(%rcx,%rax)
addq $0x20, %rax
cmpq %rax, %r13
jne 0x4f04
movq %rcx, -0x38(%rbp)
movl %ebx, -0x2c(%rbp)
testl %ebx, %ebx
jle 0x4fe4
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 0x4f8c
xorl %ebx, %ebx
movslq (%rax,%rbx,4), %rax
imulq $0x58, %rax, %rsi
addq %r15, %rsi
movq %r12, %rdi
callq 0x701a
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 0x4f5d
movq -0x60(%rbp), %rbx
movq %rbx, %r14
shlq $0x5, %r14
addq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x7052
movq -0x40(%rbp), %rax
movq %rax, 0x18(%r14)
movq -0x50(%rbp), %rax
leaq (%rax,%rbx,8), %rdi
xorl %esi, %esi
leaq 0xada(%rip), %rdx       # 0x5a99
movq %r14, %rcx
callq 0x3590
testl %eax, %eax
jne 0x5083
movq %r12, %rdi
callq 0x712a
incq %rbx
cmpq -0x48(%rbp), %rbx
jne 0x4f32
movl -0x2c(%rbp), %r14d
testl %r14d, %r14d
movq -0x38(%rbp), %r12
jle 0x5012
xorl %ebx, %ebx
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rdi
xorl %esi, %esi
callq 0x32c0
movl %eax, %r15d
testl %eax, %eax
jne 0x5052
incq %rbx
cmpq %rbx, -0x48(%rbp)
jne 0x4ff3
testl %r14d, %r14d
movq -0x48(%rbp), %rbx
je 0x5032
addq $-0x20, %r12
shlq $0x5, %rbx
leaq (%r12,%rbx), %rdi
callq 0x712a
addq $-0x20, %rbx
jne 0x5023
movq -0x58(%rbp), %rsp
leaq -0x78(%rbp), %rdi
callq 0x715e
movq -0x40(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1af77(%rip), %rdi     # 0x1ffd0
leaq 0x110be(%rip), %rsi     # 0x1611e
movl $0x17, %edx
callq 0x3340
movq 0x1af5f(%rip), %rdi     # 0x1ffd0
movl %r15d, %esi
callq 0x3550
movq %rax, %rdi
callq 0x3170
jmp 0x50b5
movl %eax, %r14d
movq 0x1af43(%rip), %rdi     # 0x1ffd0
leaq 0x11071(%rip), %rsi     # 0x16105
movl $0x18, %edx
callq 0x3340
movq 0x1af2b(%rip), %rdi     # 0x1ffd0
movl %r14d, %esi
callq 0x3550
movq %rax, %rdi
callq 0x3170
movl $0x1, %edi
callq 0x3370
movq %rax, %r15
jmp 0x510a
jmp 0x50d2
movq %rax, %r15
jmp 0x50e1
jmp 0x50d2
movq %rax, %r15
jmp 0x50e9
movq %rax, %r15
leaq -0x90(%rbp), %rdi
callq 0x712a
movl -0x2c(%rbp), %r14d
movq -0x38(%rbp), %r12
testl %r14d, %r14d
je 0x5101
addq $-0x20, %r12
leaq (%r12,%r13), %rdi
callq 0x712a
addq $-0x20, %r13
jne 0x50f2
leaq -0x78(%rbp), %rdi
callq 0x715e
movq -0x40(%rbp), %rdi
callq 0x783e
movq %r15, %rdi
callq 0x3560