// File: 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
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0xa5e0
movq -0x1d8(%rbp), %r8
leaq 0x1f7d1(%rip), %rsi     # 0x28186
xorl %eax, %eax
movl %eax, %ecx
leaq -0x38(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
movl $0xa0000, %edx          # imm = 0xA0000
callq 0xa6f0
movq -0x1d0(%rbp), %rdi
callq 0xa7b0
movq %rax, -0x1c8(%rbp)
jmp 0x89e3
movq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xa7d0
jmp 0x89f5
leaq 0x1f7f2(%rip), %rsi     # 0x281ee
leaq -0x88(%rbp), %rdi
callq 0xa8a0
jmp 0x8a0a
movq -0x88(%rbp), %rdx
leaq -0x80(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xaaa0
jmp 0x8a20
leaq -0x80(%rbp), %rdi
leaq -0x89(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0xaae0
movq %rax, -0x1e0(%rbp)
jmp 0x8a3d
movq -0x1e0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
leaq -0xc8(%rbp), %rdi
callq 0x4120
jmp 0x8a5e
leaq -0xa8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x7d80
jmp 0x8a73
leaq -0xc8(%rbp), %rdi
callq 0x41f0
leaq -0xf8(%rbp), %rdi
callq 0x9910
movl $0x0, -0xfc(%rbp)
movslq -0xfc(%rbp), %rax
movq %rax, -0x1e8(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x9fc0
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
cmpq %rcx, %rax
jae 0x8c03
movslq -0xfc(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x98f0
movq %rax, %rsi
leaq -0xf8(%rbp), %rdi
callq 0x9fe0
movq %rax, -0x1f0(%rbp)
jmp 0x8aed
movq -0x1f0(%rbp), %rax
cmpq $0x0, %rax
je 0x8bb5
movslq -0xfc(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x98f0
movq %rax, %rsi
leaq -0xf8(%rbp), %rdi
callq 0xa040
movq %rax, -0x1f8(%rbp)
jmp 0x8b29
movq -0x1f8(%rbp), %rax
movl (%rax), %eax
incl %eax
movl %eax, -0x204(%rbp)
movslq -0xfc(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x98f0
movq %rax, %rsi
leaq -0xf8(%rbp), %rdi
callq 0xa040
movq %rax, -0x200(%rbp)
jmp 0x8b65
movq -0x200(%rbp), %rax
movl -0x204(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x8bed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8e4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x41f0
jmp 0x8e4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8e34
movslq -0xfc(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x98f0
movq %rax, %rsi
leaq -0xf8(%rbp), %rdi
callq 0xa040
movq %rax, -0x210(%rbp)
jmp 0x8be0
movq -0x210(%rbp), %rax
movl $0x1, (%rax)
jmp 0x8bef
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x8a95
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
leaq -0xf8(%rbp), %rsi
callq 0xa120
jmp 0x8c19
movq -0x10(%rbp), %rsi
addq $0x28, %rsi
leaq -0x130(%rbp), %rdi
callq 0x9840
jmp 0x8c2f
leaq -0x130(%rbp), %rdi
callq 0x9cf0
movq %rax, -0x138(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x9d50
movq %rax, -0x140(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x9d20
testb $0x1, %al
jne 0x8c71
jmp 0x8d2e
movq -0x60(%rbp), %rax
movq %rax, -0x228(%rbp)
leaq -0x138(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0xa290
movq -0x220(%rbp), %rdi
movq %rax, -0x218(%rbp)
callq 0xa290
movq -0x218(%rbp), %rsi
movq %rax, %rdx
addq $0x20, %rdx
leaq -0x168(%rbp), %rdi
callq 0xabf0
jmp 0x8cbe
movq -0x228(%rbp), %rsi
leaq -0x178(%rbp), %rdi
leaq -0x168(%rbp), %rdx
callq 0xaba0
jmp 0x8cda
leaq -0x168(%rbp), %rdi
callq 0xac30
leaq -0x138(%rbp), %rdi
xorl %esi, %esi
callq 0x9d80
movq %rax, -0x180(%rbp)
jmp 0x8c42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8e28
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x168(%rbp), %rdi
callq 0xac30
jmp 0x8e28
leaq -0x188(%rbp), %rdi
callq 0xa800
jmp 0x8d3c
leaq 0x1f4ab(%rip), %rsi     # 0x281ee
leaq -0x1a8(%rbp), %rdi
callq 0xa8a0
jmp 0x8d51
movq -0x1a8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0xa820
movq %rdx, -0x238(%rbp)
movq %rax, -0x230(%rbp)
jmp 0x8d71
movq -0x238(%rbp), %rax
movq -0x230(%rbp), %rcx
movq %rcx, -0x1a0(%rbp)
movq %rax, -0x198(%rbp)
movq -0x1a0(%rbp), %rsi
leaq -0x190(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0xa8c0
movq -0x240(%rbp), %rdi
callq 0xa910
movq %rax, %rsi
leaq -0x1b0(%rbp), %rdi
callq 0xa930
jmp 0x8dc4
leaq -0x188(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0xa980
leaq -0x190(%rbp), %rdi
callq 0xa910
movq %rax, %rsi
leaq -0x1b8(%rbp), %rdi
callq 0xaa00
jmp 0x8df4
leaq -0x188(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0xa9d0
testb $0x1, %al
jne 0x8e0d
jmp 0x8e26
jmp 0x8e0f
leaq -0x1c0(%rbp), %rdi
leaq -0x188(%rbp), %rsi
xorl %edx, %edx
callq 0xaa50
jmp 0x8dd7
ud2
leaq -0x130(%rbp), %rdi
callq 0x9870
leaq -0xf8(%rbp), %rdi
callq 0x9870
leaq -0xa8(%rbp), %rdi
callq 0x97e0
leaq -0x38(%rbp), %rdi
callq 0xa6c0
movq -0x50(%rbp), %rdi
callq 0x45c0
nop