// File: /workspace/github_cpp_makefile_star2_O2/IlyaSamoylov45[P]MapReduce-Project/main.cpp
// Function: main
int main(int argc, char* argv[]){

  if(argc != 13){
    std::cout << "\n" << "Usage is --app [wordcount, sort] --impl [procs, threads] --maps num_maps --reduces num_reduces --input infile --output outfile" << "\n" <<std::endl;
    exit(EXIT_FAILURE);
  }

  parse_cmdline(argc - 1, argv + 1);
  std::string app = argv[2];
  std::string impl = argv[4];
  int maps = std::stoi(argv[6]);
  int reduces = std::stoi(argv[8]);
  std::string infile = argv[10];
  std::string outfile = argv[12];
  ThreadData thread_data[maps];
  ProcData proc_data[maps];

  std::cout << "\nCurrent arguments are " << argv[1] << " " << app << " "
                                     << argv[3] << " " << impl << " "
                                     << argv[5] << " " << maps << " "
                                     << argv[7] << " " << reduces << " "
					<< argv[9] << " " << infile << " "
                                     << argv[11] << " " << outfile << "\n" <<std::endl;

  std::vector<std::string> sorted_ary = split_input(infile, maps);
  if(impl.compare("threads") == 0){
    map_threads(sorted_ary, maps, thread_data);
    std::cout << "Finished mapping Threads" << std::endl;
    //std::cout << thread_data[0].line_array << std::endl;
    std::map<std::string, int> result = reduce_threads(reduces, maps, thread_data);
    if(app.compare("wordcount") == 0){
      wordCountPrint(result, outfile);
    } else { // sort
      sortPrint(result, outfile);
    }
  }
  if(impl.compare("procs") == 0){

    
    map_proc(sorted_ary, maps, proc_data);
		//std::string result = proc_data[0].line_array;
		//if(app.compare("wordcount") == 0)
		//{
		//	wordCountPrint(result, outfile);
		//} else {
		//	sortPrint(result, outfile);	
		//}
	  
    std::cout << "Finished mapping Processes" << std::endl;
    map_threads(sorted_ary, maps, thread_data);
    std::map<std::string, int> result = reduce_threads(reduces, maps, thread_data);
    if(app.compare("wordcount") == 0){
     wordCountPrint(result, outfile);
    }else {
      sortPrint(result, outfile);
      
    }
    //std::cout << proc_data[0].line_array << std::endl;
    //std::map<std::string, int> result = reduce_threads(reduces, maps, thread_data);
    //std::cout << "Process" << std::endl;
    //if(app.compare("wordcount") == 0){
    //  wordCountPrint(result, outfile);
    //} else { // sort
    //  sortPrint(result, outfile);
    //}
	  
  }
  //print for error checking
  //for (std::vector<std::string>::const_iterator i = sorted_ary.begin(); i != sorted_ary.end(); i++){
  //  std::cout << *i << '\n';
  //}
  //
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
cmpl $0xd, %edi
jne 0x3cf0
movq %rsi, %r13
leaq 0x8(%rsi), %r12
movq %r12, %rsi
callq 0x3e99
movq 0x10(%r13), %rsi
leaq -0x118(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x30e0
movq 0x20(%r13), %rsi
leaq -0xf8(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x30e0
movq 0x30(%r13), %rsi
leaq -0x70(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
callq 0x30e0
leaq -0x70(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x5efb
movl %eax, %ebx
leaq -0x70(%rbp), %rdi
callq 0x3120
movq 0x40(%r13), %rsi
leaq -0x70(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
callq 0x30e0
leaq -0x70(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x5efb
movl %eax, -0x2c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x3120
movq 0x50(%r13), %rsi
leaq -0xd8(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x30e0
movq 0x60(%r13), %rsi
leaq -0xb8(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x30e0
movq %rsp, -0x80(%rbp)
movl %ebx, %edx
movq %rsp, %r14
movl %ebx, %ecx
imulq $0x58, %rdx, %rbx
leaq 0xf(%rbx), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movl %ecx, -0x3c(%rbp)
testl %ecx, %ecx
movq %rdx, -0x38(%rbp)
je 0x3865
movq %r12, -0x78(%rbp)
movq %r14, %r15
movq %r15, %rdi
callq 0x5f1c
addq $0x58, %r15
addq $-0x58, %rbx
jne 0x3822
movq %rsp, %r15
imulq $0x58, -0x38(%rbp), %rbx
leaq 0xf(%rbx), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
xorl %r12d, %r12d
leaq (%r15,%r12), %rdi
callq 0x5f4e
addq $0x58, %r12
cmpq %r12, %rbx
jne 0x384d
movq -0x78(%rbp), %r12
jmp 0x387a
movq %rsp, %r15
imulq $0x58, %rdx, %rax
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x1b747(%rip), %rdi     # 0x1efc8
leaq 0xe819(%rip), %rsi      # 0x120a1
movl -0x3c(%rbp), %ebx
callq 0x32b0
movq (%r12), %rsi
movq %rax, %rdi
callq 0x32b0
leaq 0xe891(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
leaq -0x118(%rbp), %rsi
movq %rax, %rdi
callq 0x3260
leaq 0xe873(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
movq 0x18(%r13), %rsi
movq %rax, %rdi
callq 0x32b0
leaq 0xe858(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
leaq -0xf8(%rbp), %rsi
movq %rax, %rdi
callq 0x3260
leaq 0xe83a(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
movq 0x28(%r13), %rsi
movq %rax, %rdi
callq 0x32b0
leaq 0xe81f(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
movq %rax, %rdi
movl %ebx, %esi
callq 0x34d0
leaq 0xe806(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
movq 0x38(%r13), %rsi
movq %rax, %rdi
callq 0x32b0
leaq 0xe7eb(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x34d0
leaq 0xe7d1(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
movq 0x48(%r13), %rsi
movq %rax, %rdi
callq 0x32b0
leaq 0xe7b6(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
leaq -0xd8(%rbp), %rsi
movq %rax, %rdi
callq 0x3260
leaq 0xe798(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
movq 0x58(%r13), %rsi
movq %rax, %rdi
callq 0x32b0
leaq 0xe77d(%rip), %rsi      # 0x12134
movq %rax, %rdi
callq 0x32b0
leaq -0xb8(%rbp), %rsi
movq %rax, %rdi
callq 0x3260
leaq 0xe64b(%rip), %rsi      # 0x12020
movq %rax, %rdi
callq 0x32b0
movq %rax, %rdi
callq 0x3150
leaq -0x180(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x3370
leaq -0x98(%rbp), %rdi
leaq -0x180(%rbp), %rsi
movl %ebx, %edx
callq 0x44b3
leaq -0x180(%rbp), %rdi
callq 0x3120
leaq 0xe699(%rip), %rsi      # 0x120b9
leaq -0xf8(%rbp), %rdi
callq 0x30a0
testl %eax, %eax
jne 0x3b2a
leaq -0x160(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x5f80
leaq -0x160(%rbp), %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x45c3
leaq -0x160(%rbp), %rdi
callq 0x5fd4
movq 0x1b55d(%rip), %rdi     # 0x1efc8
leaq 0xe64f(%rip), %rsi      # 0x120c1
callq 0x32b0
movq %rax, %rdi
callq 0x3150
leaq -0x70(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl %ebx, %edx
movq %r14, %rcx
callq 0x46c6
leaq 0xe643(%rip), %rsi      # 0x120da
leaq -0x118(%rbp), %rdi
callq 0x30a0
testl %eax, %eax
je 0x3add
leaq -0x290(%rbp), %r12
leaq -0x70(%rbp), %rsi
movq %r12, %rdi
callq 0x67b2
leaq -0x1e0(%rbp), %r13
leaq -0xb8(%rbp), %rsi
movq %r13, %rdi
callq 0x3370
movq %r12, %rdi
movq %r13, %rsi
callq 0x4b57
jmp 0x3b11
leaq -0x2c0(%rbp), %r12
leaq -0x70(%rbp), %rsi
movq %r12, %rdi
callq 0x67b2
leaq -0x200(%rbp), %r13
leaq -0xb8(%rbp), %rsi
movq %r13, %rdi
callq 0x3370
movq %r12, %rdi
movq %r13, %rsi
callq 0x493d
movq %r13, %rdi
callq 0x3120
movq %r12, %rdi
callq 0x6adc
leaq -0x70(%rbp), %rdi
callq 0x6adc
leaq 0xe5b3(%rip), %rsi      # 0x120e4
leaq -0xf8(%rbp), %rdi
callq 0x30a0
testl %eax, %eax
jne 0x3c6b
leaq -0x148(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x5f80
leaq -0x148(%rbp), %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x4c95
leaq -0x148(%rbp), %rdi
callq 0x5fd4
movq 0x1b44c(%rip), %rdi     # 0x1efc8
leaq 0xe567(%rip), %rsi      # 0x120ea
callq 0x32b0
movq %rax, %rdi
callq 0x3150
leaq -0x130(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x5f80
leaq -0x130(%rbp), %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x45c3
leaq -0x130(%rbp), %rdi
callq 0x5fd4
leaq -0x70(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl %ebx, %edx
movq %r14, %rcx
callq 0x46c6
leaq 0xe502(%rip), %rsi      # 0x120da
leaq -0x118(%rbp), %rdi
callq 0x30a0
testl %eax, %eax
je 0x3c1e
leaq -0x230(%rbp), %r12
leaq -0x70(%rbp), %rsi
movq %r12, %rdi
callq 0x67b2
leaq -0x1a0(%rbp), %r13
leaq -0xb8(%rbp), %rsi
movq %r13, %rdi
callq 0x3370
movq %r12, %rdi
movq %r13, %rsi
callq 0x4b57
jmp 0x3c52
leaq -0x260(%rbp), %r12
leaq -0x70(%rbp), %rsi
movq %r12, %rdi
callq 0x67b2
leaq -0x1c0(%rbp), %r13
leaq -0xb8(%rbp), %rsi
movq %r13, %rdi
callq 0x3370
movq %r12, %rdi
movq %r13, %rsi
callq 0x493d
movq %r13, %rdi
callq 0x3120
movq %r12, %rdi
callq 0x6adc
leaq -0x70(%rbp), %rdi
callq 0x6adc
leaq -0x98(%rbp), %rdi
callq 0x5fd4
testl %ebx, %ebx
je 0x3cab
addq $-0x58, %r15
imulq $0x58, -0x38(%rbp), %rbx
leaq (%r15,%rbx), %rdi
callq 0x6000
addq $-0x58, %rbx
jne 0x3c84
addq $-0x58, %r14
imulq $0x58, -0x38(%rbp), %rbx
leaq (%r14,%rbx), %rdi
callq 0x6024
addq $-0x58, %rbx
jne 0x3c9c
movq -0x80(%rbp), %rsp
leaq -0xb8(%rbp), %rdi
callq 0x3120
leaq -0xd8(%rbp), %rdi
callq 0x3120
leaq -0xf8(%rbp), %rdi
callq 0x3120
leaq -0x118(%rbp), %rdi
callq 0x3120
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1b2d1(%rip), %rdi     # 0x1efc8
leaq 0xe322(%rip), %rbx      # 0x12020
movq %rbx, %rsi
callq 0x32b0
leaq 0xe315(%rip), %rsi      # 0x12022
movq %rax, %rdi
callq 0x32b0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x32b0
movq %rax, %rdi
callq 0x3150
pushq $0x1
popq %rdi
callq 0x3310
movq %rax, %r12
leaq -0x1c0(%rbp), %rdi
callq 0x3120
jmp 0x3d44
movq %rax, %r12
leaq -0x260(%rbp), %rdi
jmp 0x3da2
movq %rax, %r12
leaq -0x200(%rbp), %rdi
callq 0x3120
jmp 0x3d61
movq %rax, %r12
leaq -0x2c0(%rbp), %rdi
jmp 0x3da2
movq %rax, %r12
leaq -0x1a0(%rbp), %rdi
callq 0x3120
jmp 0x3d7e
movq %rax, %r12
leaq -0x230(%rbp), %rdi
jmp 0x3da2
movq %rax, %r12
leaq -0x1e0(%rbp), %rdi
callq 0x3120
jmp 0x3d9b
movq %rax, %r12
leaq -0x290(%rbp), %rdi
callq 0x6adc
jmp 0x3dbc
jmp 0x3db9
jmp 0x3e17
movq %rax, %r12
leaq -0x130(%rbp), %rdi
jmp 0x3ddf
movq %rax, %r12
leaq -0x70(%rbp), %rdi
callq 0x6adc
jmp 0x3e1a
movq %rax, %r12
leaq -0x148(%rbp), %rdi
jmp 0x3ddf
jmp 0x3e17
movq %rax, %r12
leaq -0x160(%rbp), %rdi
callq 0x5fd4
jmp 0x3e1a
movq %rax, %r12
leaq -0x180(%rbp), %rdi
callq 0x3120
jmp 0x3e2b
jmp 0x3e28
movq %rax, %r12
jmp 0x3e6d
jmp 0x3e0d
jmp 0x3e04
jmp 0x3e0d
movq %rax, %r12
leaq -0x70(%rbp), %rdi
jmp 0x3e74
movq %rax, %r12
jmp 0x3e79
movq %rax, %r12
jmp 0x3e85
movq %rax, %r12
leaq -0x98(%rbp), %rdi
callq 0x5fd4
jmp 0x3e2b
movq %rax, %r12
cmpl $0x0, -0x3c(%rbp)
je 0x3e61
addq $-0x58, %r15
imulq $0x58, -0x38(%rbp), %rbx
leaq (%r15,%rbx), %rdi
callq 0x6000
addq $-0x58, %rbx
jne 0x3e3a
addq $-0x58, %r14
imulq $0x58, -0x38(%rbp), %rbx
leaq (%r14,%rbx), %rdi
callq 0x6024
addq $-0x58, %rbx
jne 0x3e52
leaq -0xb8(%rbp), %rdi
callq 0x3120
leaq -0xd8(%rbp), %rdi
callq 0x3120
leaq -0xf8(%rbp), %rdi
callq 0x3120
leaq -0x118(%rbp), %rdi
callq 0x3120
movq %r12, %rdi
callq 0x34e0