// File: /workspace/github_cpp_makefile_star2_O3/WeihuaZhu[P]hpc_prog2/main.cpp
// Function: main
int main(int argc, char *argv[])
{
   // set up MPI
   MPI_Init(&argc, &argv);

   // get communicator size
   MPI_Comm comm = MPI_COMM_WORLD;
   int p;
   MPI_Comm_size(comm, &p);

   // Ax = b
   std::vector<double> A;
   std::vector<double> b;
   std::vector<double> x;


   /**********************************
    *  Parse command line arguments  *
    **********************************/
   bool write_output = false;
   std::string outfile_name;
   int n;
   bool rand_input = false;
   double difficulty = 0.5;
   std::string fileA;
   std::string fileB;
   if (argc < 3)
   {
      print_usage();
      exit(EXIT_FAILURE);
   }
   if (std::string(argv[1]) == "-n")
   {
      // randomly generate input
      n = atoi(argv[2]);
      if (!(n > 0))
      {
         print_usage();
         exit(EXIT_FAILURE);
      }
      if (argc == 5)
      {
         if (std::string(argv[3]) != "-d")
         {
            print_usage();
            exit(EXIT_FAILURE);
         }
         std::istringstream iss(argv[4]);
         iss >> difficulty;
      }
      rand_input = true;
   }
   else
   {
      if (argc != 4)
      {
         print_usage();
         exit(EXIT_FAILURE);
      }

      // get output filename
      outfile_name = std::string(argv[3]);
      write_output = true;
   }


   // start timer
   //   we omit the file loading and argument parsing from the runtime
   //   timings, we measure the time needed by the processor with rank 0
   struct timespec t_start, t_end;
   if (p > 1)
   {
      // get the dimensions
      int q = (int)sqrt(p);
      if (p != q*q)
      {
         throw std::runtime_error("The number of MPI processes must be a perfect square");
      }
      // create 2D cartesian grid for the processors (enable reordering)
      MPI_Comm grid_comm;
      int dims[2] = {q, q};
      int periods[2] = {0, 0};
      MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 1, &grid_comm);
      // get the rank of process with coordinates (0,0)
      int rank00;
      int myrank;
      int coords[2] = {0, 0};
      MPI_Cart_rank(grid_comm, coords, &rank00);
      MPI_Comm_rank(grid_comm, &myrank);

      // process (0,0) loads input
      if (myrank == rank00)
      {
         if (rand_input) {
            // generate random input of the given size
            A = diag_dom_rand(n, difficulty);
            b = randn(n);
         } else {
            // read input from file
            A = read_binary_file<double>(argv[1]);
            b = read_binary_file<double>(argv[2]);
            n = b.size();
            if (A.size() != n*(size_t)n)
            {
               throw std::runtime_error("The input dimensions are not matching");
            }
         }
      }
      MPI_Bcast(&n, 1, MPI_INT, rank00, grid_comm);

      // start timer
      clock_gettime(CLOCK_MONOTONIC,  &t_start);

      // allocate output and run the parallel jacobi implementation
      if (myrank == rank00)
         x = std::vector<double>(n);
      mpi_jacobi(n, &A[0], &b[0], &x[0], grid_comm);

      if (myrank == rank00)
      {
         // get time
         clock_gettime(CLOCK_MONOTONIC,  &t_end);
         // time in seconds
         double time_secs = (t_end.tv_sec - t_start.tv_sec)
            + (double) (t_end.tv_nsec - t_start.tv_nsec) * 1e-9;
         // output time
         std::cerr << time_secs << std::endl;
         // write output
         if (write_output)
         {
            write_binary_file(outfile_name.c_str(), x);
         }
      }
   }
   else
   {
      std::cerr << "[WARNING]: Running the sequential solver. Start with mpirun to execute the parallel version." << std::endl;

      // get input
      if (rand_input) {
         // generate random input of the given size
         A = diag_dom_rand(n, difficulty);
         b = randn(n);
      } else {
         // read input from file
         A = read_binary_file<double>(argv[1]);
         b = read_binary_file<double>(argv[2]);
         n = b.size();
         if (A.size() != n*(size_t)n)
         {
            throw std::runtime_error("The input dimensions are not matching");
         }
      }

      clock_gettime(CLOCK_MONOTONIC,  &t_start);
      // sequential jacobi
      x = std::vector<double>(n);
      jacobi(n, &A[0], &b[0], &x[0]);
      // get time
      clock_gettime(CLOCK_MONOTONIC,  &t_end);
      // time in seconds
      double time_secs = (t_end.tv_sec - t_start.tv_sec)
         + (double) (t_end.tv_nsec - t_start.tv_nsec) * 1e-9;
      // output time
      std::cerr << time_secs << std::endl;
      // write output
      if (write_output)
      {
         write_binary_file(outfile_name.c_str(), x);
      }
   }

   // finalize MPI
   MPI_Finalize();
   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
leaq 0x4c(%rsp), %rbx
movl %edi, (%rbx)
leaq 0x10(%rsp), %rax
movq %rsi, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2320
movq 0x4537(%rip), %rdi      # 0x6f98
leaq 0x5c(%rsp), %rsi
callq 0x2290
xorl %eax, %eax
movq %rax, 0x30(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
leaq 0x90(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movabsq $0x3fe0000000000000, %rcx # imm = 0x3FE0000000000000
movq %rcx, 0x50(%rsp)
leaq 0xd0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0xb0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
cmpl $0x2, (%rbx)
jle 0x3328
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x3564
leaq 0x2723(%rip), %rsi      # 0x520f
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
callq 0x20c0
movl %eax, %ebp
movq (%rbx), %rdi
leaq 0x100(%rsp), %r15
cmpq %r15, %rdi
je 0x2b1e
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2260
testl %ebp, %ebp
je 0x2b81
cmpl $0x4, 0x4c(%rsp)
jne 0x332f
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rsi
leaq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x3564
leaq 0x80(%rsp), %rdi
leaq 0xf0(%rsp), %rbx
movq %rbx, %rsi
callq 0x22c0
movq (%rbx), %rdi
cmpq %r15, %rdi
je 0x2c75
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x2c75
movq 0x10(%rsp), %rbx
movq 0x10(%rbx), %rdi
callq 0x2370
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0x3368
cmpl $0x5, 0x4c(%rsp)
jne 0x2c75
movq 0x18(%rbx), %rsi
leaq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x3564
leaq 0x264f(%rip), %rsi      # 0x5212
leaq 0xf0(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movl %eax, %ebx
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x2bed
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2260
testl %ebx, %ebx
jne 0x336f
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x60(%rsp), %rdi
leaq 0xe0(%rsp), %rdx
callq 0x3564
leaq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movl $0x8, %edx
callq 0x23c0
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2c42
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2260
leaq 0xf0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x2160
movq 0x435d(%rip), %rsi      # 0x6fb8
leaq 0xf0(%rsp), %rdi
callq 0x2340
leaq 0x168(%rsp), %rdi
callq 0x20d0
movl 0x5c(%rsp), %eax
cmpl $0x2, %eax
jl 0x2dd6
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
sqrtsd %xmm0, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, %edx
imull %ecx, %edx
cmpl %edx, %eax
jne 0x3336
leaq 0x280(%rsp), %rdx
movl %ecx, (%rdx)
movl %ecx, 0x4(%rdx)
leaq 0x278(%rsp), %rcx
movq $0x0, (%rcx)
movq 0x42d7(%rip), %rdi      # 0x6f98
leaq 0x40(%rsp), %r9
movl $0x2, %esi
movl $0x1, %r8d
callq 0x2050
leaq 0x270(%rsp), %rsi
movq $0x0, (%rsi)
xorl %ebx, %ebx
movq 0x40(%rsp), %rdi
leaq 0xc(%rsp), %rdx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
callq 0x2410
xorl %ebx, %ebx
movq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
callq 0x2310
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x2ef8
testl %ebp, %ebp
je 0x3098
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x35a1
leaq 0xf0(%rsp), %rdi
movq (%rdi), %rbx
movq 0x8(%rdi), %r13
movq 0x10(%rdi), %r14
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x35a1
movq 0xf0(%rsp), %r15
movq 0xf8(%rsp), %rax
movq 0x100(%rsp), %r12
subq %r15, %rax
shrq $0x3, %rax
movl %eax, 0x8(%rsp)
subq %rbx, %r13
sarq $0x3, %r13
cltq
imulq %rax, %rax
cmpq %rax, %r13
je 0x30e3
movl $0x10, %edi
callq 0x2120
movq %rax, %rbp
leaq 0x2497(%rip), %rsi      # 0x524a
movq %rax, %rdi
callq 0x2100
movq 0x422e(%rip), %rsi      # 0x6ff0
movq 0x41f7(%rip), %rdx      # 0x6fc0
movq %rbp, %rdi
callq 0x23d0
jmp 0x3368
xorl %ebx, %ebx
movq 0x4209(%rip), %r13      # 0x6fe8
leaq 0x248a(%rip), %rsi      # 0x5270
movl $0x5c, %edx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r13, %rdi
callq 0x2280
movq (%r13), %rax
addq -0x18(%rax), %r13
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r13, %rdi
movl $0xa, %esi
callq 0x2240
xorl %ebx, %ebx
movsbl %al, %esi
movq 0x41c0(%rip), %rdi      # 0x6fe8
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
callq 0x2040
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rax, %rdi
callq 0x2170
testl %ebp, %ebp
je 0x2f08
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x35a1
leaq 0xf0(%rsp), %rdi
movq (%rdi), %rbx
movq 0x8(%rdi), %r13
movq 0x10(%rdi), %r14
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x35a1
movq 0xf0(%rsp), %r15
movq 0xf8(%rsp), %rax
movq 0x100(%rsp), %r12
subq %r15, %rax
shrq $0x3, %rax
movl %eax, 0x8(%rsp)
subq %rbx, %r13
sarq $0x3, %r13
cltq
imulq %rax, %rax
cmpq %rax, %r13
je 0x2f53
movl $0x10, %edi
callq 0x2120
movq %rax, %rbp
leaq 0x2375(%rip), %rsi      # 0x524a
movq %rax, %rdi
callq 0x2100
movq 0x410c(%rip), %rsi      # 0x6ff0
movq 0x40d5(%rip), %rdx      # 0x6fc0
movq %rbp, %rdi
callq 0x23d0
jmp 0x3368
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x30e3
movl 0x8(%rsp), %esi
movsd 0x50(%rsp), %xmm0
leaq 0xf0(%rsp), %rdi
callq 0x27a0
leaq 0xf0(%rsp), %rdi
movq (%rdi), %rbx
movq 0x10(%rdi), %r14
movl 0x8(%rsp), %esi
movsd 0x20fe(%rip), %xmm1     # 0x5038
xorpd %xmm0, %xmm0
callq 0x273a
movq 0xf0(%rsp), %r15
movq 0x100(%rsp), %r12
leaq 0x60(%rsp), %rsi
movl $0x1, %edi
callq 0x2230
movslq 0x8(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x34d6
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movaps 0xf0(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movq $0x0, 0x100(%rsp)
testq %rdi, %rdi
je 0x2fde
subq %rdi, %rsi
callq 0x2260
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x2fde
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movl 0x8(%rsp), %edi
movq 0x20(%rsp), %rcx
movsd 0x2051(%rip), %xmm0     # 0x5040
movq %rbx, %rsi
movq %r15, %rdx
movl $0x64, %r8d
callq 0x3ac6
leaq 0xe0(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2230
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq 0x60(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
subq 0x68(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x2009(%rip), %xmm0     # 0x5048
addsd %xmm1, %xmm0
movq 0x3f9e(%rip), %rdi      # 0x6fe8
callq 0x23a0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
testl %ebp, %ebp
je 0x3263
movq 0x80(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x37b5
jmp 0x3263
movl 0x8(%rsp), %esi
movsd 0x50(%rsp), %xmm0
leaq 0xf0(%rsp), %rdi
callq 0x27a0
leaq 0xf0(%rsp), %rdi
movq (%rdi), %rbx
movq 0x10(%rdi), %r14
movl 0x8(%rsp), %esi
movsd 0x1f6e(%rip), %xmm1     # 0x5038
xorpd %xmm0, %xmm0
callq 0x273a
movq 0xf0(%rsp), %r15
movq 0x100(%rsp), %r12
movl 0xc(%rsp), %ecx
movq 0x40(%rsp), %r8
movq 0x3ed5(%rip), %rdx      # 0x6fc8
leaq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0x2060
leaq 0x60(%rsp), %rsi
movl $0x1, %edi
callq 0x2230
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x319f
movslq 0x8(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
leaq 0x1f(%rsp), %rdx
callq 0x34d6
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movapd 0xf0(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x30(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xf0(%rsp)
movq $0x0, 0x100(%rsp)
testq %rdi, %rdi
je 0x319f
subq %rdi, %rsi
callq 0x2260
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x319f
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movl 0x8(%rsp), %edi
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %r8
movsd 0x1e8b(%rip), %xmm0     # 0x5040
movq %rbx, %rsi
movq %r15, %rdx
movl $0x64, %r9d
callq 0x4d75
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x3263
leaq 0xe0(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2230
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq 0x60(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
subq 0x68(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x1e35(%rip), %xmm0     # 0x5048
addsd %xmm1, %xmm0
movq 0x3dca(%rip), %rdi      # 0x6fe8
callq 0x23a0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
testl %ebp, %ebp
je 0x3263
movq 0x80(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x37b5
callq 0x21a0
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x328d
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x32b2
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x32d7
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x32ee
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
testq %r15, %r15
je 0x3301
subq %r15, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2260
testq %rbx, %rbx
je 0x3314
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2260
xorl %eax, %eax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2889
jmp 0x3374
callq 0x2889
jmp 0x3374
movl $0x10, %edi
callq 0x2120
movq %rax, %rbx
leaq 0x1ecb(%rip), %rsi      # 0x5215
movq %rax, %rdi
callq 0x2100
movq 0x3c97(%rip), %rsi      # 0x6ff0
movq 0x3c60(%rip), %rdx      # 0x6fc0
movq %rbx, %rdi
callq 0x23d0
callq 0x2889
jmp 0x3374
callq 0x2889
movl $0x1, %edi
callq 0x22b0
jmp 0x3387
jmp 0x33fa
jmp 0x3409
movq %rax, %r13
movq %rbp, %rdi
callq 0x2190
jmp 0x3422
jmp 0x33fa
jmp 0x3409
jmp 0x33fa
jmp 0x3409
movq %rax, %r13
movq 0x3c0f(%rip), %rsi      # 0x6fb8
leaq 0xf0(%rsp), %rdi
callq 0x2340
leaq 0x168(%rsp), %rdi
callq 0x20d0
jmp 0x340c
movq %rax, %r13
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x340c
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x340c
jmp 0x3409
jmp 0x3409
jmp 0x341f
jmp 0x3409
movq %rax, %r13
movq %rbx, %rdi
callq 0x2190
jmp 0x340c
movq %rax, %r13
jmp 0x3411
jmp 0x3409
jmp 0x341f
jmp 0x3409
jmp 0x3409
jmp 0x3409
movq %rax, %r13
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x3422
jmp 0x341f
jmp 0x341f
jmp 0x341f
movq %rax, %r13
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x3447
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x346c
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x3491
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x34a8
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
testq %r15, %r15
je 0x34bb
subq %r15, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2260
testq %rbx, %rbx
je 0x34ce
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2260
movq %r13, %rdi
callq 0x23f0