// File: /workspace/github_cpp_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movq %rdi, %r14
leaq -0x90(%rbp), %rbx
movl $0x1a4, (%rbx)          # imm = 0x1A4
leaq 0xca14(%rip), %rsi      # 0x12190
leaq -0x138(%rbp), %r15
movl $0xa0000, %edx          # imm = 0xA0000
movq %r15, %rdi
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x64a0
movq (%r15), %rax
leaq -0xb8(%rbp), %rdx
movq %rax, %rcx
subq %rdx, %rcx
testq %rax, %rax
pushq $0x1
popq %rsi
cmoveq %rsi, %rcx
movq %rcx, (%rdx)
leaq 0xca12(%rip), %rcx      # 0x121cb
movq %rcx, (%rbx)
movq %rax, 0x8(%rbx)
movq $0x1, 0x10(%rbx)
movw $0x100, 0x18(%rbx)      # imm = 0x100
leaq -0x90(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x65a2
movq %rax, %rbx
leaq 0x8(%r14), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x3370
leaq -0x50(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x518a
movq %r14, -0x38(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3120
leaq -0x88(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorl %r12d, %r12d
leaq -0x90(%rbp), %r15
xorl %r13d, %r13d
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r13, %rax
jbe 0x58a1
addq %r12, %rsi
movq %r15, %rdi
callq 0x6210
movq -0x50(%rbp), %rsi
addq %r12, %rsi
testq %rax, %rax
je 0x588a
movq %r15, %rdi
callq 0x6234
movl (%rax), %r14d
movq -0x50(%rbp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x6234
incl %r14d
movl %r14d, (%rax)
jmp 0x5898
movq %r15, %rdi
callq 0x6234
movl $0x1, (%rax)
incq %r13
addq $0x20, %r12
jmp 0x583d
movq -0x38(%rbp), %r14
addq $0x28, %r14
leaq -0x90(%rbp), %rsi
movq %r14, %rdi
callq 0x6af4
leaq -0xe8(%rbp), %rdi
movq %r14, %rsi
callq 0x67b2
leaq -0xe0(%rbp), %r13
movq 0x10(%r13), %r12
leaq -0x110(%rbp), %r14
leaq -0x60(%rbp), %r15
cmpq %r13, %r12
je 0x5917
leaq 0x20(%r12), %rsi
leaq 0x40(%r12), %rdx
movq %r14, %rdi
callq 0x65fe
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x11078
movq %r14, %rdi
callq 0x3120
movq %r12, %rdi
callq 0x31e0
movq %rax, %r12
jmp 0x58dd
leaq 0xc8ad(%rip), %rsi      # 0x121cb
leaq -0x138(%rbp), %rdi
callq 0x64f2
movq %rax, %rcx
subq %r15, %rcx
testq %rax, %rax
pushq $0x1
popq %rax
cmoveq %rax, %rcx
movq %rcx, -0x60(%rbp)
leaq (%rcx,%r15), %rax
xorl %esi, %esi
cmpq $0x1, %rcx
cmovneq %rax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x6508
leaq -0x30(%rbp), %rbx
movq (%rbx), %rax
movq %r14, %rcx
negq %rcx
xorl %edx, %edx
cmpq $0x1, %rax
leaq -0x30(%rbp,%rcx), %rcx
cmovneq %rcx, %rdx
addq %rax, %rdx
movq %rdx, -0x110(%rbp)
movq -0x60(%rbp), %rax
leaq (%rax,%r15), %rcx
cmpq $0x1, %rax
movl $0x0, %esi
cmovneq %rcx, %rsi
movq %rbx, %rdi
callq 0x654a
movq %r14, %rdi
movq %rbx, %rsi
callq 0x653d
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x6580
jmp 0x597b
jmp 0x59c9
jmp 0x59c9
jmp 0x59c9
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
callq 0x3120
jmp 0x59cc
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x6adc
jmp 0x59e1
jmp 0x59de
jmp 0x59de
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x6adc
leaq -0x50(%rbp), %rdi
callq 0x5fd4
jmp 0x5a0e
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3120
jmp 0x5a0e
jmp 0x5a0b
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
callq 0x6482
movq %rbx, %rdi
callq 0x34e0