// File: /workspace/github_c_makefile_star2_O2/baileywjohnson[P]mpi-matrix-mult/MyMPI.c
// Function: read_checkerboard_matrix
void read_checkerboard_matrix (
   char *s,              /* IN - File name */
   void ***subs,         /* OUT - 2D array */
   void **storage,       /* OUT - Array elements */
   MPI_Datatype dtype,   /* IN - Element type */
   int *m,               /* OUT - Array rows */
   int *n,               /* OUT - Array cols */
   MPI_Comm grid_comm)   /* IN - Communicator */
{
   void      *buffer;         /* File buffer */
   int        coords[2];      /* Coords of proc receiving
                                 next row of matrix */
   int        datum_size;     /* Bytes per elements */
   int        dest_id;        /* Rank of receiving proc */
   int        grid_coord[2];  /* Process coords */
   int        grid_id;        /* Process rank */
   int        grid_period[2]; /* Wraparound */
   int        grid_size[2];   /* Dimensions of grid */
   int        i, j, k;
   FILE      *infileptr;      /* Input file pointer */
   void      *laddr;          /* Used when proc 0 gets row */
   int        local_cols;     /* Matrix cols on this proc */
   int        local_rows;     /* Matrix rows on this proc */
   void     **lptr;           /* Pointer into 'subs' */
   int        p;              /* Number of processes */
   void      *raddr;          /* Address of first element
                                 to send */
   void      *rptr;           /* Pointer into 'storage' */
   MPI_Status status;         /* Results of read */

   MPI_Comm_rank (grid_comm, &grid_id);
   MPI_Comm_size (grid_comm, &p);
   datum_size = get_size (dtype);

   /* Process 0 opens file, gets number of rows and
      number of cols, and broadcasts this information
      to the other processes. */

   if (grid_id == 0) {
      infileptr = fopen (s, "r");
      if (infileptr == NULL) *m = 0;
      else {
         fread (m, sizeof(int), 1, infileptr);
         fread (n, sizeof(int), 1, infileptr);
      }
   }
   MPI_Bcast (m, 1, MPI_INT, 0, grid_comm);

   if (!(*m)) MPI_Abort (MPI_COMM_WORLD, OPEN_FILE_ERROR);

   MPI_Bcast (n, 1, MPI_INT, 0, grid_comm);

   /* Each process determines the size of the submatrix
      it is responsible for. */

   MPI_Cart_get (grid_comm, 2, grid_size, grid_period,
      grid_coord);
   local_rows = BLOCK_SIZE(grid_coord[0],grid_size[0],*m);
   local_cols = BLOCK_SIZE(grid_coord[1],grid_size[1],*n);

   /* Dynamically allocate two-dimensional matrix 'subs' */

   *storage = my_malloc (grid_id,
      local_rows * local_cols * datum_size);
   *subs = (void **) my_malloc (grid_id,local_rows*PTR_SIZE);
   lptr = (void *) *subs;
   rptr = (void *) *storage;
   for (i = 0; i < local_rows; i++) {
      *(lptr++) = (void *) rptr;
      rptr += local_cols * datum_size;
   }

   /* Grid process 0 reads in the matrix one row at a time
      and distributes each row among the MPI processes. */

   if (grid_id == 0)
      buffer = my_malloc (grid_id, *n * datum_size);

   /* For each row of processes in the process grid... */
   for (i = 0; i < grid_size[0]; i++) {
      coords[0] = i;

      /* For each matrix row controlled by this proc row...*/
      for (j = 0; j < BLOCK_SIZE(i,grid_size[0],*m); j++) {

         /* Read in a row of the matrix */

         if (grid_id == 0) {
            fread (buffer, datum_size, *n, infileptr);
         }

         /* Distribute it among process in the grid row */

         for (k = 0; k < grid_size[1]; k++) {
            coords[1] = k;

            /* Find address of first element to send */
            raddr = buffer +
               BLOCK_LOW(k,grid_size[1],*n) * datum_size;

            /* Determine the grid ID of the process getting
               the subrow */
            MPI_Cart_rank (grid_comm, coords, &dest_id);

            /* Process 0 is responsible for sending...*/
            if (grid_id == 0) {

               /* It is sending (copying) to itself */
               if (dest_id == 0) {
                  laddr = (*subs)[j];
                  memcpy (laddr, raddr,
                     local_cols * datum_size);

               /* It is sending to another process */
               } else {
                  MPI_Send (raddr,
                     BLOCK_SIZE(k,grid_size[1],*n), dtype,
                  dest_id, 0, grid_comm);
               }

            /* Process 'dest_id' is responsible for
               receiving... */
            } else if (grid_id == dest_id) {
               MPI_Recv ((*subs)[j], local_cols, dtype, 0,
                  0, grid_comm,&status);
            }
         }
      }
   }
   if (grid_id == 0) free (buffer);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, %rbx
movq %r8, %r13
movq %rcx, %r12
movq %rdx, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, %r14
movq 0xe0(%rsp), %rbp
leaq 0x10(%rsp), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1170
leaq 0x7c(%rsp), %rsi
movq %rbp, %rdi
callq 0x1100
movq %r12, 0x50(%rsp)
movq %r12, %rdi
callq 0x1b5c
movl %eax, 0x14(%rsp)
cmpl $0x0, (%r15)
jne 0x1ec7
leaq 0x22fa(%rip), %rsi      # 0x4172
movq %r14, %rdi
callq 0x10c0
testq %rax, %rax
je 0x1eb9
pushq $0x4
popq %r14
pushq $0x1
popq %r15
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rax, %r12
movq %rax, %rcx
callq 0x1160
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, 0x40(%rsp)
movq %r12, %rcx
callq 0x1160
jmp 0x1ec7
andl $0x0, (%r13)
movq $0x0, 0x40(%rsp)
movq 0x40da(%rip), %rdx      # 0x5fa8
pushq $0x1
popq %rsi
xorl %r15d, %r15d
movq %r13, %rdi
xorl %ecx, %ecx
movq %rbp, %r8
callq 0x1180
cmpl $0x0, (%r13)
jne 0x1ef7
movq 0x40e9(%rip), %rdi      # 0x5fd8
pushq $-0x1
popq %rsi
callq 0x1120
movq 0x40aa(%rip), %rdx      # 0x5fa8
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %ecx, %ecx
movq %rbp, %r8
callq 0x1180
pushq $0x2
popq %rsi
movq %rbp, %rdi
leaq 0x28(%rsp), %r14
leaq 0x80(%rsp), %rcx
leaq 0x88(%rsp), %rbp
movq %rdi, %r12
movq %r14, %rdx
movq %rbp, %r8
callq 0x1110
movl (%rbp), %r8d
movl 0x4(%rbp), %esi
leal 0x1(%r8), %eax
movl (%r13), %ecx
imull %ecx, %eax
movl (%r14), %r9d
movl 0x4(%r14), %edi
cltd
idivl %r9d
movl %eax, %r14d
imull %r8d, %ecx
movl %ecx, %eax
cltd
idivl %r9d
subl %eax, %r14d
leal 0x1(%rsi), %eax
movl (%rbx), %ecx
imull %ecx, %eax
cltd
idivl %edi
movl %eax, %ebp
imull %esi, %ecx
movl %ecx, %eax
cltd
idivl %edi
subl %eax, %ebp
movl 0x10(%rsp), %edi
movl %ebp, 0x38(%rsp)
imull 0x14(%rsp), %ebp
movl %ebp, %esi
imull %r14d, %esi
callq 0x1bc5
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movl 0x10(%rsp), %edi
leal (,%r14,8), %esi
callq 0x1bc5
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movslq %ebp, %rdx
movq %rdx, 0x48(%rsp)
testl %r14d, %r14d
cmovlel %r15d, %r14d
cmpl %r15d, %r14d
je 0x1fde
movq %rcx, (%rax,%r15,8)
addq 0x48(%rsp), %rcx
incq %r15
jmp 0x1fcb
cmpl $0x0, 0x10(%rsp)
jne 0x1ff8
movl (%rbx), %esi
imull 0x14(%rsp), %esi
xorl %edi, %edi
callq 0x1bc5
movq %rax, 0x18(%rsp)
movslq 0x14(%rsp), %rax
movq %rax, 0x60(%rsp)
xorl %eax, %eax
movl 0x28(%rsp), %edi
leaq 0x3c(%rsp), %r14
movq %r13, 0x68(%rsp)
cmpl %edi, %eax
jge 0x216a
movl %eax, 0x58(%rsp)
movq %rax, 0x70(%rsp)
incl %eax
movl %eax, 0x24(%rsp)
xorl %ebp, %ebp
movl (%r13), %ecx
movl %ecx, %eax
imull 0x24(%rsp), %eax
cltd
idivl %edi
movl %eax, %esi
imull 0x70(%rsp), %ecx
movl %ecx, %eax
cltd
idivl %edi
subl %eax, %esi
movslq %esi, %rax
cmpq %rax, %rbp
jge 0x2161
cmpl $0x0, 0x10(%rsp)
jne 0x2071
movslq (%rbx), %rdx
movq 0x18(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x40(%rsp), %rcx
callq 0x1160
xorl %r13d, %r13d
movl 0x2c(%rsp), %ecx
cmpl %ecx, %r13d
jge 0x2150
movl %r13d, 0x5c(%rsp)
movl (%rbx), %eax
imull %r13d, %eax
cltd
idivl %ecx
movl %eax, %r15d
movq %r12, %rdi
leaq 0x58(%rsp), %rsi
movq %r14, %rdx
callq 0x11d0
movl 0x10(%rsp), %eax
movl 0x3c(%rsp), %ecx
testl %eax, %eax
je 0x20e6
cmpl %ecx, %eax
jne 0x2148
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %rdi
leaq 0x90(%rsp), %rax
movq %rax, (%rsp)
movl 0x38(%rsp), %esi
movq 0x50(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x1140
jmp 0x2148
imull 0x14(%rsp), %r15d
movslq %r15d, %rsi
addq 0x18(%rsp), %rsi
testl %ecx, %ecx
je 0x2132
leal 0x1(%r13), %eax
movl (%rbx), %edi
imull %edi, %eax
movl 0x2c(%rsp), %r9d
cltd
idivl %r9d
movl %eax, %r8d
imull %r13d, %edi
movl %edi, %eax
cltd
idivl %r9d
subl %eax, %r8d
movq %rsi, %rdi
movl %r8d, %esi
movq 0x50(%rsp), %rdx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x1040
jmp 0x2148
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %rdi
movq 0x48(%rsp), %rdx
callq 0x1220
incl %r13d
jmp 0x2074
incq %rbp
movl 0x28(%rsp), %edi
movq 0x68(%rsp), %r13
jmp 0x202b
movl 0x24(%rsp), %eax
jmp 0x2012
cmpl $0x0, 0x10(%rsp)
jne 0x217b
movq 0x18(%rsp), %rdi
callq 0x10f0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq