// File: /workspace/github_c_makefile_star2_O3/baileywjohnson[P]mpi-matrix-mult/MyMPI.c
// Function: print_checkerboard_matrix
void print_checkerboard_matrix (
   void       **a,            /* IN -2D matrix */
   MPI_Datatype dtype,        /* IN -Matrix element type */
   int          m,            /* IN -Matrix rows */
   int          n,            /* IN -Matrix columns */
   MPI_Comm     grid_comm)    /* IN - Communicator */
{
   void      *buffer;         /* Room to hold 1 matrix row */
   int        coords[2];      /* Grid coords of process
                                 sending elements */
   int        datum_size;     /* Bytes per matrix element */
   int        els;            /* Elements received */
   int        grid_coords[2]; /* Coords of this process */
   int        grid_id;        /* Process rank in grid */
   int        grid_period[2]; /* Wraparound */
   int        grid_size[2];   /* Dims of process grid */
   int        i, j, k;
   void      *laddr;          /* Where to put subrow */
   int        local_cols;     /* Matrix cols on this proc */
   int        p;              /* Number of processes */
   int        src;            /* ID of proc with subrow */
   MPI_Status status;         /* Result of receive */

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

   MPI_Cart_get (grid_comm, 2, grid_size, grid_period,
      grid_coords);
   local_cols = BLOCK_SIZE(grid_coords[1],grid_size[1],n);

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

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

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

         /* Collect the matrix row on grid process 0 and
            print it */
         if (!grid_id) {
            for (k = 0; k < grid_size[1]; k++) {
               coords[1] = k;
               MPI_Cart_rank (grid_comm, coords, &src);
               els = BLOCK_SIZE(k,grid_size[1],n);
               laddr = buffer +
                  BLOCK_LOW(k,grid_size[1],n) * datum_size;
               if (src == 0) {
                  memcpy (laddr, a[j], els * datum_size);
               } else {
                  MPI_Recv(laddr, els, dtype, src, 0,
                     grid_comm, &status);
               }
            }
            print_subvector (buffer, dtype, n);
            putchar ('\n');
         } else if (grid_coords[0] == i) {
            MPI_Send (a[j], local_cols, dtype, 0, 0,
               grid_comm);
         }
      }
   }
   if (!grid_id) {
      free (buffer);
      putchar ('\n');
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, %rbx
movl %ecx, %r14d
movl %edx, 0x38(%rsp)
movq %rsi, %r15
movq %rdi, 0x48(%rsp)
leaq 0x1c(%rsp), %r13
movq %r8, %rdi
movq %r13, %rsi
callq 0x1170
leaq 0x6c(%rsp), %rsi
movq %rbx, %rdi
callq 0x1100
movq %r15, 0x20(%rsp)
movq %r15, %rdi
callq 0x1b80
movl %eax, 0x10(%rsp)
leaq 0x14(%rsp), %r12
leaq 0x78(%rsp), %rcx
leaq 0x70(%rsp), %rbp
movq %rbx, %rdi
movl $0x2, %esi
movq %r12, %rdx
movq %rbp, %r8
callq 0x1110
movl 0x4(%rbp), %ecx
leal 0x1(%rcx), %eax
imull %r14d, %eax
movl 0x4(%r12), %esi
cltd
idivl %esi
movl %eax, 0xc(%rsp)
movq %r14, 0x30(%rsp)
imull %r14d, %ecx
movl %ecx, %eax
cltd
idivl %esi
movl %eax, %r12d
cmpl $0x0, (%r13)
jne 0x2ce4
movl 0x10(%rsp), %esi
imull 0x30(%rsp), %esi
xorl %edi, %edi
callq 0x1bf2
movq %rax, 0x28(%rsp)
movl 0x14(%rsp), %esi
testl %esi, %esi
jle 0x2e75
subl %r12d, 0xc(%rsp)
xorl %eax, %eax
movl %eax, %ecx
movl %eax, 0x50(%rsp)
leal 0x1(%rcx), %eax
movq %rax, 0x58(%rsp)
movl %eax, %ebp
movl 0x38(%rsp), %eax
imull %eax, %ebp
movq %rcx, 0x60(%rsp)
movl %ecx, %r15d
imull %eax, %r15d
movl %ebp, %eax
cltd
idivl %esi
movl %eax, %ecx
movl %r15d, %eax
cltd
idivl %esi
subl %eax, %ecx
testl %ecx, %ecx
jle 0x2e68
xorl %r14d, %r14d
movl %ebp, 0x40(%rsp)
movl %r15d, 0x3c(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0x2d77
movq 0x60(%rsp), %rax
cmpl %eax, 0x70(%rsp)
jne 0x2e46
movq 0x48(%rsp), %rax
movq (%rax,%r14,8), %rdi
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x1040
jmp 0x2e46
cmpl $0x0, 0x18(%rsp)
jle 0x2e1f
xorl %r12d, %r12d
xorl %ebp, %ebp
movl %r12d, %r15d
movl %ebp, 0x54(%rsp)
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
leaq 0x44(%rsp), %rdx
callq 0x11d0
movq 0x30(%rsp), %rax
leal (%rax,%r15), %r12d
movl 0x18(%rsp), %r13d
movl %r12d, %eax
cltd
idivl %r13d
movl %eax, %r8d
movl %r15d, %eax
cltd
idivl %r13d
subl %eax, %r8d
movl 0x10(%rsp), %edx
imull %edx, %eax
movslq %eax, %rdi
addq 0x28(%rsp), %rdi
movl 0x44(%rsp), %ecx
testl %ecx, %ecx
je 0x2dff
leaq 0x80(%rsp), %rax
movq %rax, (%rsp)
movl %r8d, %esi
movq 0x20(%rsp), %rdx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x1140
movl 0x18(%rsp), %r13d
jmp 0x2e14
movq 0x48(%rsp), %rax
movq (%rax,%r14,8), %rsi
imull %edx, %r8d
movslq %r8d, %rdx
callq 0x1220
incl %ebp
cmpl %r13d, %ebp
jl 0x2d87
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x2ba4
movl $0xa, %edi
callq 0x1090
movl 0x40(%rsp), %ebp
movl 0x3c(%rsp), %r15d
incq %r14
movl 0x14(%rsp), %esi
movl %ebp, %eax
cltd
idivl %esi
movl %eax, %ecx
movl %r15d, %eax
cltd
idivl %esi
subl %eax, %ecx
movslq %ecx, %rax
cmpq %rax, %r14
jl 0x2d3d
movq 0x58(%rsp), %rax
cmpl %esi, %eax
jl 0x2cf7
cmpl $0x0, 0x1c(%rsp)
jne 0x2e90
movq 0x28(%rsp), %rdi
callq 0x10f0
movl $0xa, %edi
callq 0x1090
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq