// File: 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
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0xb8(%rbp)
movq %rdi, %rax
movq %rax, -0xb0(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x6768
movl -0x10(%rbp), %eax
movl %eax, -0xc(%rbp)
movq -0xb8(%rbp), %rdi
movb $0x0, -0x19(%rbp)
callq 0x9910
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
leaq -0x38(%rbp), %rdi
callq 0x7810
jmp 0x6789
movl -0xc(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x50(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xd0(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl -0xc(%rbp), %eax
movq %rax, -0xc8(%rbp)
movq %rax, %rdx
shlq $0x5, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0x6835
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rdx
shlq $0x5, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0xe0(%rbp)
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x9930
movq -0xe8(%rbp), %rax
movq -0xe0(%rbp), %rcx
addq $0x20, %rax
cmpq %rcx, %rax
movq %rax, -0xd8(%rbp)
jne 0x6804
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x6a01
leaq -0x88(%rbp), %rdi
callq 0x9950
movl $0x0, -0x8c(%rbp)
movslq -0x8c(%rbp), %rax
movq %rax, -0xf0(%rbp)
movslq -0x6c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x9970
movq %rax, %rdi
callq 0x9990
movq %rax, %rcx
movq -0xf0(%rbp), %rax
cmpq %rcx, %rax
jae 0x691e
movq -0x18(%rbp), %rax
movq %rax, -0xf8(%rbp)
movslq -0x6c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x9970
movq %rax, %rdi
movslq -0x8c(%rbp), %rsi
callq 0x9a20
movq -0xf8(%rbp), %rsi
movslq (%rax), %rax
imulq $0x58, %rax, %rax
addq %rax, %rsi
leaq -0x88(%rbp), %rdi
callq 0x99b0
jmp 0x68da
jmp 0x68dc
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x685e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6c07
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x9c90
jmp 0x6b8f
movq -0xc0(%rbp), %rdi
movslq -0x6c(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
leaq -0x88(%rbp), %rsi
callq 0x9a40
jmp 0x693e
movq -0xc0(%rbp), %rcx
movq -0xd0(%rbp), %rdi
movq -0xb8(%rbp), %rdx
movslq -0x6c(%rbp), %rsi
shlq $0x5, %rsi
movq %rcx, %rax
addq %rsi, %rax
movq %rdx, 0x18(%rax)
movslq -0x6c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movslq -0x6c(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rcx
xorl %eax, %eax
movl %eax, %esi
leaq 0xfda(%rip), %rdx       # 0x7960
callq 0x45f0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x69e7
movq 0x39625(%rip), %rdi     # 0x3ffc0
leaq 0x21759(%rip), %rsi     # 0x280fb
callq 0x4340
movq %rax, -0x100(%rbp)
jmp 0x69b0
movq -0x100(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x45b0
movq %rax, -0x108(%rbp)
jmp 0x69c8
movq -0x108(%rbp), %rdi
movq 0x395d2(%rip), %rsi     # 0x3ffa8
callq 0x4370
jmp 0x69dd
movl $0x1, %edi
callq 0x43c0
leaq -0x88(%rbp), %rdi
callq 0x9c90
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x683c
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x6ac6
movq -0xd0(%rbp), %rax
movslq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x42e0
movl %eax, -0x10c(%rbp)
jmp 0x6a3d
movl -0x10c(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x6ab0
movq 0x3956d(%rip), %rdi     # 0x3ffc0
leaq 0x216ba(%rip), %rsi     # 0x28114
callq 0x4340
movq %rax, -0x118(%rbp)
jmp 0x6a68
movq -0x118(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x45b0
movq %rax, -0x120(%rbp)
jmp 0x6a80
movq -0x120(%rbp), %rdi
movq 0x3951a(%rip), %rsi     # 0x3ffa8
callq 0x4370
jmp 0x6a95
movl $0x1, %edi
callq 0x43c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6b8f
jmp 0x6ab2
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x6a0b
movq -0xb8(%rbp), %rdi
callq 0x9cf0
movq %rax, -0x98(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x9d50
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x9d20
testb $0x1, %al
jne 0x6b05
jmp 0x6b1e
jmp 0x6b07
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
callq 0x9d80
movq %rax, -0xa8(%rbp)
jmp 0x6ad9
movq -0xc0(%rbp), %rcx
movq -0xc8(%rbp), %rdx
movb $0x1, -0x19(%rbp)
shlq $0x5, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x128(%rbp)
je 0x6b77
movq -0x128(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x130(%rbp)
callq 0x9dc0
movq -0xc0(%rbp), %rcx
movq -0x130(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x128(%rbp)
jne 0x6b46
movq -0x50(%rbp), %rax
movq %rax, %rsp
leaq -0x38(%rbp), %rdi
callq 0x9de0
testb $0x1, -0x19(%rbp)
jne 0x6bfb
jmp 0x6bef
movq -0xc0(%rbp), %rcx
movq -0xc8(%rbp), %rdx
shlq $0x5, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x138(%rbp)
je 0x6be4
movq -0x138(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x140(%rbp)
callq 0x9dc0
movq -0xc0(%rbp), %rcx
movq -0x140(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x138(%rbp)
jne 0x6bb3
leaq -0x38(%rbp), %rdi
callq 0x9de0
jmp 0x6c07
movq -0xb8(%rbp), %rdi
callq 0x9870
movq -0xb0(%rbp), %rax
movq %rbp, %rsp
popq %rbp
retq
movq -0xb8(%rbp), %rdi
callq 0x9870
movq -0x40(%rbp), %rdi
callq 0x45c0
nopl (%rax)