// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %r8, %r12
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, 0x18(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x90(%rsp), %r14
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x1100
leaq 0x14(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1170
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
callq 0x1b5c
movl %eax, 0x2c(%rsp)
leaq 0x10(%rsp), %rax
movl (%rax), %eax
decl %eax
cmpl %eax, (%rbp)
jne 0x24d7
leaq 0x1ceb(%rip), %rsi      # 0x4172
movq 0x38(%rsp), %rdi
callq 0x10c0
testq %rax, %rax
je 0x24c9
pushq $0x4
popq %rbx
pushq $0x1
popq %r14
movq %rax, %rbp
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %rax, %rcx
callq 0x1160
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %rbp, 0x20(%rsp)
movq %rbp, %rcx
callq 0x1160
jmp 0x24d7
andl $0x0, (%r12)
movq $0x0, 0x20(%rsp)
movl 0x10(%rsp), %ecx
decl %ecx
movq 0x3ac4(%rip), %rdx      # 0x5fa8
pushq $0x1
popq %rsi
movq %r12, %rdi
movq 0x90(%rsp), %rbp
movq %rbp, %r8
callq 0x1180
cmpl $0x0, (%r12)
jne 0x2510
movq 0x3ad0(%rip), %rdi      # 0x5fd8
pushq $-0x1
popq %rsi
callq 0x1120
movl 0x10(%rsp), %ecx
decl %ecx
movq 0x3a8b(%rip), %rdx      # 0x5fa8
pushq $0x1
popq %r14
movq %r15, %rdi
movl %r14d, %esi
movq %rbp, %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, %ebx
imull %edi, %ecx
movl %ecx, %eax
cltd
idivl %esi
subl %eax, %ebx
movl (%r15), %esi
movl 0x2c(%rsp), %ebp
imull %ebp, %esi
imull %ebx, %esi
callq 0x1bc5
movq %rax, (%r13)
movl 0x14(%rsp), %edi
leal (,%rbx,8), %esi
callq 0x1bc5
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq (%r13), %rdi
xorl %ecx, %ecx
testl %ebx, %ebx
movl $0x0, %edx
movq %rbx, 0x18(%rsp)
cmovgl %ebx, %edx
movslq %ebp, %rbx
movq %rdi, %rsi
cmpl %ecx, %edx
je 0x25b0
movq %rsi, (%rax,%rcx,8)
movslq (%r15), %r8
imulq %rbx, %r8
addq %r8, %rsi
incq %rcx
jmp 0x2599
movl 0x10(%rsp), %r8d
leal -0x1(%r8), %ecx
cmpl %ecx, 0x14(%rsp)
jne 0x2655
leal -0x1(%r14), %ebp
leal -0x1(%r8), %eax
movq (%r13), %rdi
cmpl %eax, %ebp
jge 0x267e
movl (%r12), %ecx
movl %ecx, %eax
imull %r14d, %eax
cltd
idivl %r8d
movl %eax, %esi
imull %ebp, %ecx
movl %ecx, %eax
cltd
idivl %r8d
subl %eax, %esi
movslq (%r15), %rax
movslq %esi, %rdx
imulq %rax, %rdx
movq %rbx, %rsi
movq 0x20(%rsp), %rcx
callq 0x1160
movq (%r13), %rdi
movl (%r12), %ecx
movl %ecx, %eax
imull %r14d, %eax
movl 0x10(%rsp), %r8d
cltd
idivl %r8d
movl %eax, %esi
imull %ebp, %ecx
movl %ecx, %eax
cltd
idivl %r8d
subl %eax, %esi
imull (%r15), %esi
movq 0x30(%rsp), %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
movq 0x90(%rsp), %r9
callq 0x1040
movl 0x10(%rsp), %r8d
incl %r14d
jmp 0x25c3
movq 0x18(%rsp), %rsi
imull (%r15), %esi
leaq 0x40(%rsp), %rax
movq %rax, (%rsp)
movq 0x30(%rsp), %rdx
xorl %r8d, %r8d
movq 0x90(%rsp), %r9
callq 0x1140
jmp 0x26a2
movslq (%r15), %rax
movslq 0x18(%rsp), %rdx
imulq %rax, %rdx
movq %rbx, %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rcx
callq 0x1160
movq %rbx, %rdi
callq 0x11c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq