// File: /workspace/github_cpp_makefile_star2_O3/IlyaSamoylov45[P]MapReduce-Project/main.cpp
// Function: map_function_proc(void*)
void * map_function_proc(void* proc) {
  struct ProcData *curr_proc_data;
	curr_proc_data = (struct ProcData *) proc;
	managed_shared_memory segment(open_or_create, "shared_memory", SHAREDMEMSIZE);
			using namespace boost::interprocess;
			typedef std::string KeyType;
			typedef int MappedType;
			typedef std::pair<const std::string, int> ValueType;
			typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
			typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;
			ShmemAllocator alloc_inst (segment.get_segment_manager());
			MySHMMap *mymap = segment.construct<MySHMMap>("MySHMMapName") (std::less<std::string>(), alloc_inst);
			//offset_ptr<MySHMMap> m_pmap = segment.construct<MySHMMap>("MySHMMapName")(std::less<std::string>(), alloc_inst);
			
  /**

  /Need semaphore?
	//write proc_data to shared memory so parent process can read it
  std::memset(region, curr_proc_data, size);

  **/
	std::vector<std::string> words = split_string_by_space(curr_proc_data->line_array);
	std::map<std::string, int> counterMap;
  //std::cout <<  curr_proc_data->line_array << std::endl;
	for(int i = 0; i < words.size(); i++){
		if(counterMap.count(words[i])){
			counterMap[words[i]] = counterMap[words[i]] + 1;
		}
    else{
			counterMap[words[i]] = 1;
		}
	}
	curr_proc_data->counter = counterMap;
  std::map<std::string, int> testMap = curr_proc_data->counter;
  
  for(auto it = testMap.begin(); it != testMap.end(); it++){
	  //std::cout << it->first << " " << it->second << std::endl;
	  mymap->insert(std::pair<const std::string, int>(it->first, it->second));

	}
  
  MySHMMap :: iterator iter;
  offset_ptr<MySHMMap> m_pmap1 = segment.find<MySHMMap>("MySHMMapName").first;
  for(iter=m_pmap1->begin(); iter!=m_pmap1->end();iter++)
  {
      //std::cout << iter->first << " has " << iter->second << std::endl;
  }
  
 
  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %r14
leaq -0xf0(%rbp), %rbx
movl $0x1a4, (%rbx)          # imm = 0x1A4
leaq 0xed80(%rip), %rsi      # 0x15190
leaq -0x118(%rbp), %r15
movl $0xa0000, %edx          # imm = 0xA0000
movq %r15, %rdi
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x72ac
movq (%r15), %rdi
leaq -0xc0(%rbp), %rax
movq %rdi, %rcx
subq %rax, %rcx
testq %rdi, %rdi
movl $0x1, %edx
cmoveq %rdx, %rcx
movq %rcx, (%rax)
movq $0x8, 0x8(%rbx)
movq $0x28, 0x10(%rbx)
leaq 0x135a8(%rip), %rcx     # 0x19a07
movq %rcx, 0x18(%rbx)
leaq 0x188be(%rip), %rcx     # 0x1ed28
movq %rcx, (%rbx)
movq %rax, 0x20(%rbx)
leaq -0x148(%rbp), %rax
movq %rax, 0x28(%rbx)
leaq 0xed48(%rip), %rsi      # 0x151cb
leaq -0xf0(%rbp), %r9
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x104be
movq %rax, -0x50(%rbp)
leaq -0xa0(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
leaq -0xb0(%rbp), %rdi
callq 0x7a0c
leaq -0x40(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x5c82
movq %r14, -0x48(%rbp)
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x64e2
callq 0x32f0
leaq -0xe8(%rbp), %r12
movl $0x0, (%r12)
xorl %eax, %eax
movq %rax, 0x8(%r12)
movq %r12, 0x10(%r12)
movq %r12, 0x18(%r12)
movq %rax, 0x20(%r12)
movq -0x40(%rbp), %rsi
cmpq %rsi, -0x38(%rbp)
je 0x657d
xorl %r13d, %r13d
leaq -0xf0(%rbp), %r15
xorl %r14d, %r14d
addq %r13, %rsi
movq %r15, %rdi
callq 0x8644
movq -0x40(%rbp), %rsi
addq %r13, %rsi
cmpq %r12, %rax
je 0x6554
movq %r15, %rdi
callq 0x70aa
movl (%rax), %ebx
movq -0x40(%rbp), %rsi
addq %r13, %rsi
movq %r15, %rdi
callq 0x70aa
incl %ebx
movl %ebx, (%rax)
jmp 0x6562
movq %r15, %rdi
callq 0x70aa
movl $0x1, (%rax)
incq %r14
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
subq %rsi, %rax
sarq $0x5, %rax
addq $0x20, %r13
cmpq %r14, %rax
ja 0x651e
movq -0x48(%rbp), %rbx
addq $0x28, %rbx
leaq -0xf0(%rbp), %rsi
movq %rbx, %rdi
callq 0x76da
leaq -0x148(%rbp), %rdi
movq %rbx, %rsi
callq 0x7422
leaq -0x140(%rbp), %r13
movq 0x10(%r13), %r14
cmpq %r13, %r14
je 0x6613
leaq -0x80(%rbp), %rbx
leaq -0x90(%rbp), %r15
leaq -0x68(%rbp), %r12
movq %rbx, -0x90(%rbp)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x7a0c
movl 0x40(%r14), %eax
movl %eax, -0x70(%rbp)
movq %r12, %rdi
movq -0x50(%rbp), %rsi
movq %r15, %rdx
callq 0x13308
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x6603
callq 0x32f0
movq %r14, %rdi
callq 0x3210
movq %rax, %r14
cmpq %r13, %rax
jne 0x65c2
xorl %edx, %edx
cmpl $0x0, -0xf8(%rbp)
setne %dl
movq -0x118(%rbp), %rdi
leaq 0xeb9e(%rip), %rsi      # 0x151cb
callq 0xbab8
leaq -0xb8(%rbp), %rcx
movq %rax, %rdx
subq %rcx, %rdx
testq %rax, %rax
movl $0x1, %eax
cmovneq %rdx, %rax
movq %rax, (%rcx)
addq %rax, %rcx
xorl %esi, %esi
cmpq $0x1, %rax
cmovneq %rcx, %rsi
leaq -0x90(%rbp), %rbx
movq %rbx, %rdi
callq 0x7306
movq -0x90(%rbp), %rcx
leaq -0x68(%rbp), %r14
movq %rbx, %r15
subq %r14, %r15
xorl %eax, %eax
cmpq $0x1, %rcx
cmovneq %r15, %rax
addq %rcx, %rax
movq %rax, (%r14)
movq %rbx, %rcx
negq %rcx
leaq (%rcx,%rbp), %r12
addq $-0x68, %r12
leaq -0x58(%rbp), %rcx
negq %rcx
leaq (%rcx,%rbp), %r13
addq $-0x90, %r13
movl $0x0, %ecx
cmpq $0x1, %rax
je 0x66b9
movq %r12, %rcx
addq %rax, %rcx
movl $0x0, %eax
cmpq $0x1, %rcx
je 0x66ca
movq %r13, %rax
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xeeb8
movq -0x90(%rbp), %rcx
movl $0x0, %eax
cmpq $0x1, %rcx
je 0x66f1
movq %r15, %rax
addq %rcx, %rax
movq %rax, -0x68(%rbp)
jmp 0x66ab
jmp 0x6735
jmp 0x6735
jmp 0x6746
jmp 0x6746
movq %rax, %r14
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x675e
callq 0x32f0
jmp 0x675e
jmp 0x671a
movq %rax, %r14
jmp 0x675e
movq %rax, %r14
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x6738
callq 0x32f0
jmp 0x6738
movq %rax, %r14
leaq -0x148(%rbp), %rdi
callq 0x76c2
jmp 0x6749
movq %rax, %r14
leaq -0xf0(%rbp), %rdi
callq 0x76c2
leaq -0x40(%rbp), %rdi
callq 0x6e22
leaq -0x110(%rbp), %rdi
callq 0xb9fc
movq %r14, %rdi
callq 0x3560