// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rcx, %r13
movq %rdx, 0x38(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x90(%rsp), %rbx
leaq 0x8(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1100
leaq 0xc(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1170
movq %r13, 0x20(%rsp)
movq %r13, %rdi
callq 0x1b74
movl %eax, %ebx
movl (%r12), %eax
decl %eax
cmpl %eax, (%rbp)
movq %r14, %rbp
jne 0x2579
movl %ebx, %r14d
leaq 0x1c4f(%rip), %rsi      # 0x4172
movq 0x30(%rsp), %rdi
callq 0x10c0
testq %rax, %rax
je 0x2566
movq %rax, %rbx
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %rax, %rcx
callq 0x1160
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, 0x18(%rsp)
movq %rbx, %rcx
callq 0x1160
jmp 0x2576
movl $0x0, (%rbp)
movq $0x0, 0x18(%rsp)
movl %r14d, %ebx
movl 0x8(%rsp), %ecx
decl %ecx
movq 0x3a22(%rip), %rdx      # 0x5fa8
movq %rbp, %rdi
movl $0x1, %esi
movq 0x90(%rsp), %r14
movq %r14, %r8
callq 0x1180
cmpl $0x0, (%rbp)
jne 0x25b5
movq 0x3a2d(%rip), %rdi      # 0x5fd8
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1120
movl 0x8(%rsp), %ecx
decl %ecx
movq 0x39e6(%rip), %rdx      # 0x5fa8
movq %r15, %rdi
movl $0x1, %esi
movq %r14, %r8
callq 0x1180
movl 0xc(%rsp), %edi
leal 0x1(%rdi), %eax
movl (%rbp), %ecx
imull %ecx, %eax
movl 0x8(%rsp), %esi
cltd
idivl %esi
movl %eax, %r13d
imull %edi, %ecx
movl %ecx, %eax
cltd
idivl %esi
subl %eax, %r13d
movl (%r15), %esi
imull %ebx, %esi
imull %r13d, %esi
callq 0x1be6
movq 0x38(%rsp), %r12
movq %rax, (%r12)
movl 0xc(%rsp), %edi
leal (,%r13,8), %esi
callq 0x1be6
movq %r13, %rsi
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq (%r12), %rdi
movslq %ebx, %r10
testl %esi, %esi
movq 0x20(%rsp), %r13
jle 0x2656
movslq (%r15), %rcx
imulq %r10, %rcx
movl %esi, %edx
xorl %r9d, %r9d
movq %rdi, %r8
movq %r8, (%rax,%r9,8)
addq %rcx, %r8
incq %r9
cmpl %r9d, %edx
jne 0x2647
movl 0x8(%rsp), %ecx
decl %ecx
cmpl %ecx, 0xc(%rsp)
jne 0x2746
movq %rsi, 0x10(%rsp)
movl 0x8(%rsp), %r8d
cmpl $0x2, %r8d
movq 0x18(%rsp), %rbx
jl 0x2721
movq %rbp, %rax
xorl %ebp, %ebp
movq %rax, 0x28(%rsp)
movq (%r12), %rdi
leal 0x1(%rbp), %r14d
movl (%rax), %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 %r10, %rsi
movq %rbx, %rcx
movq %r10, %r13
callq 0x1160
movq (%r12), %rdi
movq 0x28(%rsp), %rax
movl (%rax), %ecx
movl %ecx, %eax
imull %r14d, %eax
movl 0x8(%rsp), %r8d
cltd
idivl %r8d
movl %eax, %esi
imull %ebp, %ecx
movl %ecx, %eax
cltd
idivl %r8d
subl %eax, %esi
imull (%r15), %esi
movq 0x20(%rsp), %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
movq 0x90(%rsp), %r9
callq 0x1040
movq %r13, %r10
movl 0x8(%rsp), %r8d
leal -0x1(%r8), %eax
movl %r14d, %ebp
cmpl %eax, %r14d
movq 0x28(%rsp), %rax
jl 0x2689
movq (%r12), %rdi
movslq (%r15), %rax
movslq 0x10(%rsp), %rdx
imulq %rax, %rdx
movq %r10, %rsi
movq %rbx, %rcx
callq 0x1160
movq %rbx, %rdi
callq 0x11c0
jmp 0x2761
imull (%r15), %esi
leaq 0x40(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdx
xorl %r8d, %r8d
movq %r14, %r9
callq 0x1140
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq