// File: /workspace/github_cpp_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
movq %rdi, %r14
leaq -0x110(%rbp), %rbx
movl $0x1a4, (%rbx)          # imm = 0x1A4
leaq 0xfdbc(%rip), %rsi      # 0x16190
leaq -0x168(%rbp), %r15
movl $0xa0000, %edx          # imm = 0xA0000
movq %r15, %rdi
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x73e4
movq (%r15), %rdi
leaq -0xe0(%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 0x14634(%rip), %rcx     # 0x1aa57
movq %rcx, 0x18(%rbx)
leaq 0x198fa(%rip), %rcx     # 0x1fd28
movq %rcx, (%rbx)
movq %rax, 0x20(%rbx)
leaq -0x140(%rbp), %rax
movq %rax, 0x28(%rbx)
leaq 0xfd84(%rip), %rsi      # 0x161cb
leaq -0x110(%rbp), %r9
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x111a4
movq %rax, -0x40(%rbp)
leaq -0xc8(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
leaq -0xd8(%rbp), %rdi
callq 0x7b88
leaq -0x60(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x5c80
movq %r14, -0x38(%rbp)
movq -0xd8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x64a6
callq 0x32f0
leaq -0x108(%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 -0x60(%rbp), %rsi
cmpq %rsi, -0x58(%rbp)
je 0x6541
xorl %r13d, %r13d
leaq -0x110(%rbp), %r15
xorl %r14d, %r14d
addq %r13, %rsi
movq %r15, %rdi
callq 0x87ce
movq -0x60(%rbp), %rsi
addq %r13, %rsi
cmpq %r12, %rax
je 0x6518
movq %r15, %rdi
callq 0x71e4
movl (%rax), %ebx
movq -0x60(%rbp), %rsi
addq %r13, %rsi
movq %r15, %rdi
callq 0x71e4
incl %ebx
movl %ebx, (%rax)
jmp 0x6526
movq %r15, %rdi
callq 0x71e4
movl $0x1, (%rax)
incq %r14
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
subq %rsi, %rax
sarq $0x5, %rax
addq $0x20, %r13
cmpq %r14, %rax
ja 0x64e2
movq -0x38(%rbp), %rbx
addq $0x28, %rbx
leaq -0x110(%rbp), %rsi
movq %rbx, %rdi
callq 0x7856
leaq -0x140(%rbp), %rdi
movq %rbx, %rsi
callq 0x759e
leaq -0x138(%rbp), %r13
movq 0x10(%r13), %r14
cmpq %r13, %r14
je 0x65da
leaq -0x78(%rbp), %rbx
leaq -0x88(%rbp), %r15
leaq -0xa8(%rbp), %r12
movq %rbx, -0x88(%rbp)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x7b88
movl 0x40(%r14), %eax
movl %eax, -0x68(%rbp)
movq %r12, %rdi
movq -0x40(%rbp), %rsi
movq %r15, %rdx
callq 0x142c2
movq -0x88(%rbp), %rdi
cmpq %rbx, %rdi
je 0x65ca
callq 0x32f0
movq %r14, %rdi
callq 0x3210
movq %rax, %r14
cmpq %r13, %rax
jne 0x6589
movq $0x1, -0x30(%rbp)
leaq 0xfbe2(%rip), %rsi      # 0x161cb
leaq -0x168(%rbp), %rdi
callq 0x743e
leaq -0x48(%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 -0x88(%rbp), %rbx
movq %rbx, %rdi
callq 0x7454
movq -0x88(%rbp), %rax
leaq -0x30(%rbp), %rdx
movq %rbx, %rsi
subq %rdx, %rsi
xorl %ecx, %ecx
cmpq $0x1, %rax
movq %rsi, -0x40(%rbp)
cmovneq %rsi, %rcx
addq %rax, %rcx
movq %rcx, (%rdx)
leaq -0xa8(%rbp), %rax
negq %rax
leaq (%rax,%rbp), %rcx
addq $-0xb8, %rcx
negq %rbx
leaq (%rbx,%rbp), %r13
addq $-0xa8, %r13
addq %rcx, %r13
leaq -0xb0(%rbp), %rcx
negq %rcx
addq %rbp, %rcx
addq $-0x88, %rcx
addq %rcx, %r13
leaq (%rbx,%rbp), %rcx
addq $-0xb0, %rcx
movq %rcx, -0x38(%rbp)
leaq -0x98(%rbp), %rcx
negq %rcx
addq %rbp, %rcx
addq $-0x88, %rcx
movq %rcx, -0x90(%rbp)
leaq (%rbx,%rbp), %r15
addq $-0x30, %r15
leaq (%rax,%rbp), %r12
addq $-0x88, %r12
movl $0x10, %ebx
leaq -0xb8(%rbp), %r14
movq -0x48(%rbp), %rcx
leaq -0x38(%rbp), %rax
addq %rcx, %rax
cmpq $0x1, %rcx
cmoveq %rbx, %rax
subq %r14, %rax
addq %r13, %rax
movl $0x0, %ecx
cmpq $0x1, %rax
je 0x66fd
movq -0x38(%rbp), %rcx
addq %rax, %rcx
movl $0x0, %eax
cmpq $0x1, %rcx
je 0x6712
movq -0x90(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rcx
movl $0x0, %eax
cmpq $0x1, %rcx
je 0x672e
movq %r15, %rax
addq %rcx, %rax
movl $0x0, %ecx
cmpq $0x1, %rax
je 0x673f
movq %r12, %rcx
addq %rax, %rcx
movq %rcx, -0xa8(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xfb3c
movq -0x88(%rbp), %rax
movl $0x0, %ecx
cmpq $0x1, %rax
je 0x676f
movq -0x40(%rbp), %rcx
addq %rax, %rcx
movq %rcx, -0x30(%rbp)
jmp 0x66d5
jmp 0x67b6
jmp 0x67b6
jmp 0x67c7
jmp 0x67c7
movq %rax, %r14
movq -0xd8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x67df
callq 0x32f0
jmp 0x67df
jmp 0x679b
movq %rax, %r14
jmp 0x67df
movq %rax, %r14
movq -0x88(%rbp), %rdi
cmpq %rbx, %rdi
je 0x67b9
callq 0x32f0
jmp 0x67b9
movq %rax, %r14
leaq -0x140(%rbp), %rdi
callq 0x783e
jmp 0x67ca
movq %rax, %r14
leaq -0x110(%rbp), %rdi
callq 0x783e
leaq -0x60(%rbp), %rdi
callq 0x6f70
leaq -0x160(%rbp), %rdi
callq 0xc2fc
movq %r14, %rdi
callq 0x3560