// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %r9, %r15
movq %r8, %r14
movq %rcx, %rbp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x10e9(%rip), %rsi      # 0x3128
callq 0x1120
testq %rax, %rax
je 0x219f
movq %rbp, 0x10(%rsp)
movq %r14, %rbp
movq %rsp, %rsi
movq %rax, %r14
movq %rax, %rdi
callq 0x1555
testl %eax, %eax
jne 0x21bc
cmpb $0x4d, (%rsp)
jne 0x21cf
cmpb $0x52, 0x2(%rsp)
jne 0x21cf
cmpb $0x43, 0x1(%rsp)
jne 0x21cf
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
movq %r14, %rdi
callq 0x18fb
testl %eax, %eax
jne 0x21d9
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
cmpl %ecx, %edx
jne 0x21f8
movq %rbx, 0x18(%rsp)
movl %edx, (%rbp)
movslq 0x4(%rsp), %rbx
movl %ebx, (%r15)
leaq (,%rbx,4), %rbp
movq %rbp, %rdi
callq 0x1100
movq %rax, (%r13)
movq %rbp, %rdi
callq 0x1100
movq %rax, (%r12)
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x1100
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
cmpl $0x0, (%r15)
movq %r14, %rbp
jle 0x216c
movq %rcx, %rax
movq %r15, %rcx
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %rcx, 0x20(%rsp)
movq (%r13), %rdx
addq %r14, %rdx
movq (%r12), %rcx
addq %r14, %rcx
movq (%rax), %r8
addq %r15, %r8
movq %rbp, %rdi
leaq 0x1137(%rip), %rsi      # 0x326f
xorl %eax, %eax
callq 0x1050
movq 0x20(%rsp), %rcx
movq (%r13), %rax
decl (%rax,%r14)
movq (%r12), %rax
decl (%rax,%r14)
incq %rbx
movslq (%rcx), %rax
addq $0x4, %r14
addq $0x8, %r15
cmpq %rax, %rbx
movq 0x10(%rsp), %rax
jl 0x211a
movq %rbp, %rdi
callq 0x1080
xorl %ebx, %ebx
testl %eax, %eax
je 0x2215
movq 0x2e73(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x10ea(%rip), %rsi      # 0x3279
xorl %ebx, %ebx
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x2215
movq 0x2e52(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xffc(%rip), %rsi       # 0x31ac
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x2210
movq 0x2e35(%rip), %rcx      # 0x4ff8
movq (%rcx), %rdi
leaq 0xff1(%rip), %rsi       # 0x31be
jmp 0x21ea
movq %rsp, %rdi
callq 0x11fb
jmp 0x2210
movq 0x2e18(%rip), %rcx      # 0x4ff8
movq (%rcx), %rdi
leaq 0x1028(%rip), %rsi      # 0x3212
movq %rbx, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x10f0
jmp 0x2210
movq 0x2df9(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x103d(%rip), %rsi      # 0x3246
xorl %eax, %eax
callq 0x10f0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq