// File: /workspace/github_c_makefile_star2_O3/kkanellis[P]matvec-sparse/mmio-wrapper.c
// Function: read_matrix
int read_matrix (const char * filename, int **i_idx, int **j_idx, double **values, int *N, int *NZ)
{
    FILE *f;
    MM_typecode matcode;
    int errorcode, nrows, ncols, nz_elements;
     
    /* open the file */
    if ( (f = fopen(filename, "r")) == NULL ) {
        fprintf(stderr, "Cannot open '%s'\n", filename);
        return 1;
    }
    
    /* process first line */
    if ( (errorcode = mm_read_banner(f, &matcode)) != 0 ) {
        fprintf(stderr, "Error while processing banner (file:'%s') (code=%d)\n",
                filename, errorcode);
        return 1;
    }

    /* matrix should be sparse and real */
    if ( !mm_is_matrix(matcode) || 
         !mm_is_real(matcode)   ||
         !mm_is_sparse(matcode) ) {
        fprintf(stderr, "Not supported matrix type: %s\n", mm_typecode_to_str(matcode));
        return 1;
    }

    /* read info */
    if ( (errorcode = mm_read_mtx_crd_size(f, &nrows, &ncols, &nz_elements)) != 0) {
        fprintf(stderr, "Error while processing array (file:'%s') (code:%d)\n",
                filename, errorcode);
        return 1;
    }

    /* matrix should be square */
    if (nrows != ncols) {
        fprintf(stderr, "Matrix is NOT square (rows=%d, cols=%d)\n", nrows, ncols);
        return 1;
    }

    *N = nrows;
    *NZ = nz_elements;

    /* reserve memory for vector */
    *i_idx = (int *)malloc( nz_elements * sizeof(int) );
    *j_idx = (int *)malloc( nz_elements * sizeof(int) );
    *values = (double *)malloc( nz_elements * sizeof(double));
    
    /* read actual matrix */
    for (int i = 0; i < *NZ; i++) {
        fscanf(f, "%d %d %lf", &(*i_idx)[i], &(*j_idx)[i], &(*values)[i]);
        (*i_idx)[i]--; (*j_idx)[i]--;
    }

    /* close the file */
    if ( fclose(f) != 0 ) {
        fprintf(stderr, "Cannot close file (fil:'%s')\n", filename);
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %r12
movq %r8, %rbx
movq %rcx, 0x30(%rsp)
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r14
leaq 0x10ef(%rip), %rsi      # 0x3128
callq 0x1120
testq %rax, %rax
je 0x21a4
movq %rsp, %rsi
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x1553
testl %eax, %eax
jne 0x21c1
cmpb $0x4d, (%rsp)
jne 0x21d4
cmpb $0x52, 0x2(%rsp)
jne 0x21d4
cmpb $0x43, 0x1(%rsp)
jne 0x21d4
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
movq 0x10(%rsp), %rdi
callq 0x18f9
testl %eax, %eax
jne 0x21de
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
cmpl %ecx, %edx
jne 0x21fd
movq %r14, 0x28(%rsp)
movl %edx, (%rbx)
movslq 0x4(%rsp), %r14
movq 0x30(%rsp), %r15
movl %r14d, (%r12)
leaq (,%r14,4), %rbx
movq %rbx, %rdi
callq 0x1100
movq %rax, (%rbp)
movq %rbx, %rdi
callq 0x1100
movq %rax, (%r13)
leaq (,%r14,8), %rdi
callq 0x1100
movq %rax, (%r15)
testq %r14, %r14
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq 0x10(%rsp), %rbp
jle 0x2171
movq %r12, %rbx
movq %r15, %r13
movq 0x18(%rsp), %rax
movq (%rax), %rdx
movq 0x20(%rsp), %rax
movq (%rax), %rcx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
addq %r15, %rdx
addq %r15, %rcx
movq (%r13), %r8
addq %r14, %r8
movq %rbp, %rdi
leaq 0x1130(%rip), %rsi      # 0x326f
xorl %eax, %eax
callq 0x1050
movq 0x18(%rsp), %rax
movq (%rax), %rdx
decl (%rdx,%r15)
movq 0x20(%rsp), %rax
movq (%rax), %rcx
decl (%rcx,%r15)
incq %r12
movslq (%rbx), %rax
addq $0x4, %r15
addq $0x8, %r14
cmpq %rax, %r12
jl 0x2128
movq %rbp, %rdi
callq 0x1080
xorl %ebx, %ebx
testl %eax, %eax
je 0x221a
movq 0x2e6e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x10e5(%rip), %rsi      # 0x3279
xorl %ebx, %ebx
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x221a
movq 0x2e4d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xff7(%rip), %rsi       # 0x31ac
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x2215
movq 0x2e30(%rip), %rcx      # 0x4ff8
movq (%rcx), %rdi
leaq 0xfec(%rip), %rsi       # 0x31be
jmp 0x21ef
movq %rsp, %rdi
callq 0x11fb
jmp 0x2215
movq 0x2e13(%rip), %rcx      # 0x4ff8
movq (%rcx), %rdi
leaq 0x1023(%rip), %rsi      # 0x3212
movq %r14, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x10f0
jmp 0x2215
movq 0x2df4(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1038(%rip), %rsi      # 0x3246
xorl %eax, %eax
callq 0x10f0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq