// File: 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
subq $0xc0, %rsp
movq %rdi, -0xb0(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4450
movl -0xc(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x40(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa8(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
leaq 0x20dff(%rip), %rdi     # 0x28186
callq 0xa550
jmp 0x738e
leaq -0x90(%rbp), %rdi
callq 0xa5e0
jmp 0x739c
leaq 0x20de3(%rip), %rsi     # 0x28186
xorl %eax, %eax
movl %eax, %ecx
leaq -0x88(%rbp), %rdi
movl $0xa0000, %edx          # imm = 0xA0000
leaq -0x90(%rbp), %r8
callq 0xa600
jmp 0x73c1
movl $0xa0000, %eax          # imm = 0xA0000
cltd
idivl -0xc(%rbp)
movl %eax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x7586
callq 0x40a0
movq -0xa8(%rbp), %rcx
movslq -0x9c(%rbp), %rdx
movl %eax, (%rcx,%rdx,4)
cmpl $0x0, %eax
jge 0x746d
movq 0x38be5(%rip), %rdi     # 0x3fff0
leaq 0x20d82(%rip), %rsi     # 0x28194
callq 0x4340
movq %rax, -0xb8(%rbp)
jmp 0x7420
movq -0xb8(%rbp), %rdi
movq 0x38b7a(%rip), %rsi     # 0x3ffa8
callq 0x4370
jmp 0x7435
movl $0x1, %edi
callq 0x43c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x75a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0xa6c0
jmp 0x75a7
movq -0xa8(%rbp), %rax
movslq -0x9c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x74f8
leaq 0x20d1a(%rip), %rsi     # 0x281a2
leaq -0x38(%rbp), %rdi
callq 0x4420
jmp 0x7493
movq -0xb0(%rbp), %rdi
movslq -0x9c(%rbp), %rcx
movl %ecx, %edx
movq -0x18(%rbp), %rax
imulq $0x58, %rcx, %rcx
movl %edx, (%rax,%rcx)
movslq -0x9c(%rbp), %rsi
callq 0x98f0
movq %rax, %rsi
movq -0x18(%rbp), %rax
movslq -0x9c(%rbp), %rcx
imulq $0x58, %rcx, %rcx
leaq 0x8(%rax,%rcx), %rdi
callq 0x4040
jmp 0x74d8
movq -0x18(%rbp), %rdi
movslq -0x9c(%rbp), %rax
imulq $0x58, %rax, %rax
addq %rax, %rdi
callq 0x8980
jmp 0x74f1
xorl %edi, %edi
callq 0x43c0
movq -0xa8(%rbp), %rax
movslq -0x9c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x756c
leaq 0x20c9e(%rip), %rsi     # 0x281b1
leaq -0x38(%rbp), %rdi
callq 0x4420
jmp 0x751e
leaq -0x94(%rbp), %rdi
callq 0x43e0
movl %eax, -0xbc(%rbp)
jmp 0x7532
movl -0xbc(%rbp), %eax
movl %eax, -0x4c(%rbp)
callq 0x8e60
jmp 0x7542
leaq 0x20c3d(%rip), %rdi     # 0x28186
callq 0xa550
jmp 0x7550
movslq -0x4c(%rbp), %rsi
movl -0x94(%rbp), %edx
leaq 0x20c60(%rip), %rdi     # 0x281c1
xorl %eax, %eax
callq 0x4060
jmp 0x756a
jmp 0x756c
jmp 0x756e
jmp 0x7570
jmp 0x7572
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x73da
leaq -0x88(%rbp), %rdi
callq 0xa6c0
movq -0x40(%rbp), %rax
movq %rax, %rsp
leaq -0x38(%rbp), %rdi
callq 0x41f0
movq %rbp, %rsp
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x41f0
movq -0x58(%rbp), %rdi
callq 0x45c0
nopl (%rax)