// File: /workspace/github_cpp_makefile_star2_O1/IlyaSamoylov45[P]MapReduce-Project/main.cpp
// Function: map_proc(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, int, ProcData*)
void map_proc(std::vector<std::string> array, int maps, ProcData proc_data[]){
	std::string sIdentifier;
	pid_t pids[maps];
	pid_t pid;

  //Remove if parent
  shared_memory_object::remove("shared_memory");
	// create a new SHM object and allocate space
	//managed_shared_memory managed_shm(open_or_create, "my_shm", 1024);

  //shared_memory_object shm_obj(create_only, "shared_memory", read_write);
  //shm_obj.truncate(SHAREDMEMSIZE);

  managed_shared_memory segment(create_only,"shared_memory", SHAREDMEMSIZE);
	int status;
  int shm_size = SHAREDMEMSIZE/maps; //size of shared memory each process arguments
  //TODO maybe? handle remainder from division
	for(int i = 0; i < maps; i++){
		if ((pids[i] = fork()) < 0){
			std::cerr << "Error forking" << std::endl;
			exit(EXIT_FAILURE);
		}
		else if (pids[i] == 0){
			sIdentifier = "Child Process:";
			
    

      //map part of shared memory for each process
      //mapped_region region(shm_obj, read_write, shm_size/i, shm_size - shm_size/i);
			//mapped_region region(shm_obj, read_write, shm_size*i, shm_size);

			proc_data[i].id = i;
			proc_data[i].line_array = array[i];
			map_function_proc((void*) &proc_data[i]);
			
			exit(0); //exit cleanly
		}
		else if (pids[i] > 0){
			sIdentifier = "Parent Process:";
			pid = wait(&status);
			red_proc();
			shared_memory_object::remove("shared_memory");

			printf("Child process %ld exited with status 0x%x. \n", long(pid), status);
			
		}
	}


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, -0x38(%rbp)
movl %esi, %r15d
movq %rdi, %r14
movq %rsp, %r12
leaq -0x78(%rbp), %r13
movq %r13, -0x10(%r13)
xorl %eax, %eax
movq %rax, -0x8(%r13)
movb %al, (%r13)
leaq -0x58(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq %rax, -0x8(%rbx)
movb %al, (%rbx)
leaq -0x68(%rbp), %rdi
movl $0x1, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl $0x2f, %r8d
callq 0x35d0
leaq 0x10ade(%rip), %rsi     # 0x16190
leaq -0x68(%rbp), %rdi
callq 0x35f0
movq -0x68(%rbp), %rdi
callq 0x35e0
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x56d2
callq 0x32f0
leaq -0x2c(%rbp), %r8
movl $0x1a4, (%r8)           # imm = 0x1A4
leaq 0x10aac(%rip), %rsi     # 0x16190
leaq -0x68(%rbp), %rdi
movl $0xa0000, %edx          # imm = 0xA0000
xorl %ecx, %ecx
callq 0x738a
testl %r15d, %r15d
jg 0x5725
leaq -0x60(%rbp), %rdi
callq 0xc2fc
movq %r12, %rsp
movq -0x88(%rbp), %rdi
cmpq %r13, %rdi
je 0x5716
callq 0x32f0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3090
testl %eax, %eax
jns 0x575c
movq 0x1a8bb(%rip), %rdi     # 0x1fff0
leaq 0x10a62(%rip), %rsi     # 0x1619e
movl $0xd, %edx
callq 0x3340
movq 0x1a8a3(%rip), %rdi     # 0x1fff0
callq 0x3170
movl $0x1, %edi
callq 0x3370
jne 0x5790
leaq 0x10a47(%rip), %rsi     # 0x161ac
leaq -0x88(%rbp), %rdi
callq 0x33c0
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
movq (%r14), %rsi
leaq 0x8(%rax), %rdi
callq 0x31a0
movq -0x38(%rbp), %rdi
callq 0x63a9
leaq -0x88(%rbp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x10a19(%rip), %rcx     # 0x161bb
movl $0xf, %r8d
xorl %esi, %esi
callq 0x34b0
leaq -0x2c(%rbp), %rdi
callq 0x3390
callq 0x67f3
movq %rax, %rbx
leaq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0xc2fc
movq $0x0, -0x8(%r14)
jmp 0x57d9
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r13, %rdi
je 0x57ea
callq 0x32f0
movq %rbx, %rdi
callq 0x3560
movq %rax, -0x40(%rbp)
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x5804
callq 0x32f0
movq -0x40(%rbp), %rdi
callq 0x3120
callq 0x34f0
jmp 0x56d2
jmp 0x57d6