// File: /workspace/github_c_makefile_star2_O0/baileywjohnson[P]mpi-matrix-mult/MyMPI.c
// Function: read_row_striped_matrix
void read_row_striped_matrix (
   char        *s,        /* IN - File name */
   void      ***subs,     /* OUT - 2D submatrix indices */
   void       **storage,  /* OUT - Submatrix stored here */
   MPI_Datatype dtype,    /* IN - Matrix element type */
   int         *m,        /* OUT - Matrix rows */
   int         *n,        /* OUT - Matrix cols */
   MPI_Comm     comm)     /* IN - Communicator */
{
   int          datum_size;   /* Size of matrix element */
   int          i;
   int          id;           /* Process rank */
   FILE        *infileptr;    /* Input file pointer */
   int          local_rows;   /* Rows on this proc */
   void       **lptr;         /* Pointer into 'subs' */
   int          p;            /* Number of processes */
   void        *rptr;         /* Pointer into 'storage' */
   MPI_Status   status;       /* Result of receive */
   int          x;            /* Result of read */

   MPI_Comm_size (comm, &p);
   MPI_Comm_rank (comm, &id);
   datum_size = get_size (dtype);

   /* Process p-1 opens file, reads size of matrix,
      and broadcasts matrix dimensions to other procs */

   if (id == (p-1)) {
      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, p-1, comm);

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

   MPI_Bcast (n, 1, MPI_INT, p-1, comm);

   local_rows = BLOCK_SIZE(id,p,*m);

   /* Dynamically allocate matrix. Allow double subscripting
      through 'a'. */

   *storage = (void *) my_malloc (id,
       local_rows * *n * datum_size);
   *subs = (void **) my_malloc (id, local_rows * PTR_SIZE);

   lptr = (void *) &(*subs[0]);
   rptr = (void *) *storage;
   for (i = 0; i < local_rows; i++) {
      *(lptr++)= (void *) rptr;
      rptr += *n * datum_size;
   }

   /* Process p-1 reads blocks of rows from file and
      sends each block to the correct destination process.
      The last block it keeps. */

   if (id == (p-1)) {
      for (i = 0; i < p-1; i++) {
         x = fread (*storage, datum_size,
            BLOCK_SIZE(i,p,*m) * *n, infileptr);
         MPI_Send (*storage, BLOCK_SIZE(i,p,*m) * *n, dtype,
            i, DATA_MSG, comm);
      }
      x = fread (*storage, datum_size, local_rows * *n,
         infileptr);
      fclose (infileptr);
   } else
      MPI_Recv (*storage, local_rows * *n, dtype, p-1,
         DATA_MSG, comm, &status);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq 0x10(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x10f0
movq 0x10(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x1160
movq -0x20(%rbp), %rdi
callq 0x1f70
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2b81
movq -0x8(%rbp), %rdi
leaq 0x1669(%rip), %rsi      # 0x419e
callq 0x10b0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x2b51
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
jmp 0x2b7f
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x1150
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x1150
jmp 0x2b81
movq -0x28(%rbp), %rdi
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
movq 0x10(%rbp), %r8
movl $0x1, %esi
movq 0x340d(%rip), %rdx      # 0x5fa8
callq 0x1170
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x2bba
movq 0x3428(%rip), %rdi      # 0x5fd8
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1110
movq -0x30(%rbp), %rdi
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
movq 0x10(%rbp), %r8
movl $0x1, %esi
movq 0x33d4(%rip), %rdx      # 0x5fa8
callq 0x1170
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movq -0x28(%rbp), %rcx
imull (%rcx), %eax
cltd
idivl -0x5c(%rbp)
subl $0x1, %eax
movl %eax, -0x88(%rbp)
movl -0x3c(%rbp), %eax
movq -0x28(%rbp), %rcx
imull (%rcx), %eax
cltd
idivl -0x5c(%rbp)
movl %eax, %ecx
movl -0x88(%rbp), %eax
subl %ecx, %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movl -0x3c(%rbp), %edi
movl -0x4c(%rbp), %esi
movq -0x30(%rbp), %rax
imull (%rax), %esi
imull -0x34(%rbp), %esi
callq 0x2020
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl -0x3c(%rbp), %edi
movslq -0x4c(%rbp), %rax
shlq $0x3, %rax
movl %eax, %esi
callq 0x2020
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x2cab
movq -0x68(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, -0x58(%rbp)
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movl (%rax), %ecx
imull -0x34(%rbp), %ecx
movq -0x68(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2c6a
movl -0x3c(%rbp), %eax
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2db7
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2d85
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movslq -0x34(%rbp), %rsi
movl -0x38(%rbp), %eax
addl $0x1, %eax
movq -0x28(%rbp), %rcx
imull (%rcx), %eax
cltd
idivl -0x5c(%rbp)
subl $0x1, %eax
movl %eax, -0x8c(%rbp)
movl -0x38(%rbp), %eax
movq -0x28(%rbp), %rcx
imull (%rcx), %eax
cltd
idivl -0x5c(%rbp)
movl %eax, %ecx
movl -0x8c(%rbp), %eax
subl %ecx, %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
imull (%rcx), %eax
movslq %eax, %rdx
movq -0x48(%rbp), %rcx
callq 0x1150
movl %eax, -0x84(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x38(%rbp), %eax
addl $0x1, %eax
movq -0x28(%rbp), %rcx
imull (%rcx), %eax
cltd
idivl -0x5c(%rbp)
movl %eax, %esi
subl $0x1, %esi
movl -0x38(%rbp), %eax
movq -0x28(%rbp), %rcx
imull (%rcx), %eax
cltd
idivl -0x5c(%rbp)
subl %eax, %esi
addl $0x1, %esi
movq -0x30(%rbp), %rax
imull (%rax), %esi
movq -0x20(%rbp), %rdx
movl -0x38(%rbp), %ecx
movq 0x10(%rbp), %r9
xorl %r8d, %r8d
callq 0x1040
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2cc3
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movslq -0x34(%rbp), %rsi
movl -0x4c(%rbp), %eax
movq -0x30(%rbp), %rcx
imull (%rcx), %eax
movslq %eax, %rdx
movq -0x48(%rbp), %rcx
callq 0x1150
movl %eax, -0x84(%rbp)
movq -0x48(%rbp), %rdi
callq 0x11b0
jmp 0x2de6
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x4c(%rbp), %esi
movq -0x30(%rbp), %rax
imull (%rax), %esi
movq -0x20(%rbp), %rdx
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
movq 0x10(%rbp), %r9
xorl %r8d, %r8d
leaq -0x80(%rbp), %rax
movq %rax, (%rsp)
callq 0x1130
addq $0xa0, %rsp
popq %rbp
retq
nop