// File: IlyaSamoylov45[P]MapReduce-Project/main.cpp
// Function: reduce_function_thread(void*)
void *reduce_function_thread(void* thread) {
  struct ReduceThreadArgs *curr_args;
  curr_args = (struct ReduceThreadArgs *) thread;

  std::map<std::string, int> mergeMap;
  std::vector<ThreadData> curData = curr_args->thread_data;

  for(int i = 0; i < curData.size(); i++){
    std::map<std::string, int> curMap = curData[i].counter;

    for(auto it = curMap.begin(); it != curMap.end(); it++){
      if(mergeMap.count(it->first)){
        mergeMap[it->first] = mergeMap[it->first] + it->second;
      } else {
        mergeMap[it->first] = it->second;
      }

    }
  }

  pthread_mutex_lock(&lock1);
  for(auto it = mergeMap.begin(); it != mergeMap.end(); it++){
      if(curr_args->resultMerged->count(it->first)){
        curr_args->resultMerged->operator[](it->first) =  curr_args->resultMerged->operator[](it->first) + it->second;
      } else {
         curr_args->resultMerged->operator[](it->first) = it->second;
      }
  }
  pthread_mutex_unlock(&lock1);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x9910
movq -0x10(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xa150
jmp 0x798f
movl $0x0, -0x68(%rbp)
movslq -0x68(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x58(%rbp), %rdi
callq 0xa240
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x7ba0
movslq -0x68(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xa270
movq %rax, %rsi
addq $0x28, %rsi
leaq -0x98(%rbp), %rdi
callq 0x9840
jmp 0x79df
leaq -0x98(%rbp), %rdi
callq 0x9cf0
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x9d50
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x9d20
testb $0x1, %al
jne 0x7a21
jmp 0x7b86
leaq -0xa0(%rbp), %rdi
callq 0xa290
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x9fe0
movq %rax, -0xd8(%rbp)
jmp 0x7a42
movq -0xd8(%rbp), %rax
cmpq $0x0, %rax
je 0x7b1e
leaq -0xa0(%rbp), %rdi
callq 0xa290
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0xa040
movq %rax, -0xe0(%rbp)
jmp 0x7a74
movq -0xe0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xf0(%rbp)
leaq -0xa0(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0xa290
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movl -0xf0(%rbp), %eax
movl 0x20(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0xec(%rbp)
callq 0xa290
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0xa040
movq %rax, -0xe8(%rbp)
jmp 0x7acb
movq -0xe8(%rbp), %rax
movl -0xec(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x7b6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x7d6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x7d61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x9870
jmp 0x7d61
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0xa290
movq -0x110(%rbp), %rdi
movl 0x20(%rax), %eax
movl %eax, -0x104(%rbp)
callq 0xa290
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0xa040
movq %rax, -0x100(%rbp)
jmp 0x7b5b
movq -0x100(%rbp), %rax
movl -0x104(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x7b6c
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
callq 0x9d80
movq %rax, -0xb0(%rbp)
jmp 0x79f2
leaq -0x98(%rbp), %rdi
callq 0x9870
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x7996
leaq 0x387e1(%rip), %rdi     # 0x40388
callq 0x44b0
leaq -0x40(%rbp), %rdi
callq 0x9cf0
movq %rax, -0xb8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x9d50
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x9d20
testb $0x1, %al
jne 0x7be8
jmp 0x7d53
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x120(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xa290
movq -0x120(%rbp), %rdi
movq %rax, %rsi
callq 0x9fe0
movq %rax, -0x118(%rbp)
jmp 0x7c1b
movq -0x118(%rbp), %rax
cmpq $0x0, %rax
je 0x7cd9
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x130(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xa290
movq -0x130(%rbp), %rdi
movq %rax, %rsi
callq 0xa040
movq %rax, -0x128(%rbp)
jmp 0x7c5f
movq -0x128(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x148(%rbp)
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0xa290
movq -0x150(%rbp), %rdi
movq %rax, %rcx
movl -0x148(%rbp), %eax
movl 0x20(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x144(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x140(%rbp)
callq 0xa290
movq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0xa040
movq %rax, -0x138(%rbp)
jmp 0x7cc8
movq -0x138(%rbp), %rax
movl -0x144(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x7d37
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0xa290
movq -0x170(%rbp), %rdi
movl 0x20(%rax), %eax
movl %eax, -0x164(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x160(%rbp)
callq 0xa290
movq -0x160(%rbp), %rdi
movq %rax, %rsi
callq 0xa040
movq %rax, -0x158(%rbp)
jmp 0x7d28
movq -0x158(%rbp), %rax
movl -0x164(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x7d39
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
callq 0x9d80
movq %rax, -0xc8(%rbp)
jmp 0x7bbc
leaq 0x3862e(%rip), %rdi     # 0x40388
callq 0x4260
ud2
leaq -0x58(%rbp), %rdi
callq 0x9c90
leaq -0x40(%rbp), %rdi
callq 0x9870
movq -0x60(%rbp), %rdi
callq 0x45c0
nopl (%rax)