// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %rbx
movq %r8, %r12
movq %rcx, %r15
movq %rdx, 0x28(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x90(%rsp), %rbp
leaq 0x10(%rsp), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1100
leaq 0x14(%rsp), %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1170
movq %r15, 0x30(%rsp)
movq %r15, %rdi
callq 0x1b80
movl %eax, 0x20(%rsp)
movl (%r13), %eax
decl %eax
cmpl %eax, (%r14)
jne 0x2570
leaq 0x1c4b(%rip), %rsi      # 0x4172
movq 0x38(%rsp), %rdi
callq 0x10c0
testq %rax, %rax
je 0x2565
movq %rax, %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rax, %rcx
callq 0x1160
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1160
jmp 0x2570
movl $0x0, (%r12)
xorl %r15d, %r15d
movl 0x10(%rsp), %ecx
decl %ecx
movq 0x3a2b(%rip), %rdx      # 0x5fa8
movq %r12, %rdi
movl $0x1, %esi
movq 0x90(%rsp), %r14
movq %r14, %r8
callq 0x1180
cmpl $0x0, (%r12)
jne 0x25ad
movq 0x3a35(%rip), %rdi      # 0x5fd8
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1120
movl 0x10(%rsp), %ecx
decl %ecx
movq 0x39ee(%rip), %rdx      # 0x5fa8
movq %rbx, %rdi
movl $0x1, %esi
movq %r14, %r8
callq 0x1180
movl 0x14(%rsp), %edi
leal 0x1(%rdi), %eax
movl (%r12), %ecx
imull %ecx, %eax
movl 0x10(%rsp), %esi
cltd
idivl %esi
movl %eax, %ebp
imull %edi, %ecx
movl %ecx, %eax
cltd
idivl %esi
subl %eax, %ebp
movl (%rbx), %esi
movl 0x20(%rsp), %r13d
imull %r13d, %esi
imull %ebp, %esi
callq 0x1bf2
movq 0x28(%rsp), %r14
movq %rax, (%r14)
movl 0x14(%rsp), %edi
leal (,%rbp,8), %esi
callq 0x1bf2
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq (%r14), %rdi
movslq %r13d, %r14
testl %ebp, %ebp
jle 0x2646
movslq (%rbx), %rcx
imulq %r14, %rcx
movl %ebp, %edx
xorl %r9d, %r9d
movq %rdi, %r8
movq %r8, (%rax,%r9,8)
addq %rcx, %r8
incq %r9
cmpl %r9d, %edx
jne 0x2637
movl 0x10(%rsp), %r8d
leal -0x1(%r8), %ecx
cmpl %ecx, 0x14(%rsp)
movq 0x30(%rsp), %rdx
jne 0x2735
cmpl $0x2, %r8d
jl 0x2716
movq %rbp, 0x20(%rsp)
xorl %ebp, %ebp
movq 0x28(%rsp), %r13
movq %r15, 0x18(%rsp)
movq (%r13), %rdi
leal 0x1(%rbp), %r15d
movl (%r12), %ecx
movl %ecx, %eax
imull %r15d, %eax
cltd
idivl %r8d
movl %eax, %r9d
imull %ebp, %ecx
movl %ecx, %eax
cltd
idivl %r8d
subl %eax, %r9d
movslq (%rbx), %rax
movslq %r9d, %rdx
imulq %rax, %rdx
movq %r14, %rsi
movq 0x18(%rsp), %rcx
callq 0x1160
movq (%r13), %rdi
movl (%r12), %ecx
movl %ecx, %eax
imull %r15d, %eax
movl 0x10(%rsp), %r8d
cltd
idivl %r8d
movl %eax, %esi
imull %ebp, %ecx
movl %ecx, %eax
cltd
idivl %r8d
subl %eax, %esi
imull (%rbx), %esi
movq 0x30(%rsp), %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
movq 0x90(%rsp), %r9
callq 0x1040
movl 0x10(%rsp), %r8d
leal -0x1(%r8), %eax
movl %r15d, %ebp
cmpl %eax, %r15d
jl 0x2679
movq (%r13), %rdi
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %rbp
movslq (%rbx), %rax
movslq %ebp, %rdx
imulq %rax, %rdx
movq %r14, %rsi
movq %r15, %rcx
callq 0x1160
movq %r15, %rdi
callq 0x11c0
jmp 0x2753
imull (%rbx), %ebp
leaq 0x40(%rsp), %rax
movq %rax, (%rsp)
movl %ebp, %esi
xorl %r8d, %r8d
movq 0x90(%rsp), %r9
callq 0x1140
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq