// File: /workspace/github_c_makefile_star2_O2/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, 0x28(%rsp)
movq %r8, %rbx
movq %rcx, 0x20(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
leaq 0x125d(%rip), %rsi      # 0x3128
callq 0x1130
testq %rax, %rax
je 0x2025
movq %rax, %r15
leaq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x1457
testl %eax, %eax
jne 0x203b
cmpb $0x4d, 0x8(%rsp)
jne 0x204e
cmpb $0x52, 0xa(%rsp)
jne 0x204e
cmpb $0x43, 0x9(%rsp)
jne 0x204e
leaq 0x14(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %r15, %rdi
callq 0x1787
testl %eax, %eax
jne 0x2078
movl 0x14(%rsp), %edx
movl 0x10(%rsp), %ecx
cmpl %ecx, %edx
jne 0x2097
movq %r15, 0x18(%rsp)
movq %r14, 0x30(%rsp)
movl %edx, (%rbx)
movslq 0xc(%rsp), %rbx
movq 0x28(%rsp), %rax
movl %ebx, (%rax)
leaq (,%rbx,4), %r14
movq %r14, %rdi
callq 0x1100
movq %rax, (%r13)
movq %r14, %rdi
callq 0x1100
movq %rax, (%r12)
movq %rbx, %rdi
shlq $0x3, %rdi
callq 0x1100
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebp, %ebp
movslq %ebx, %rax
cmpq %rax, %rbp
jge 0x1fed
movq (%r13), %rdx
addq %r15, %rdx
movq (%r12), %rcx
addq %r15, %rcx
movq 0x20(%rsp), %rax
movq (%rax), %r8
addq %r14, %r8
movq 0x18(%rsp), %rdi
leaq 0x12ad(%rip), %rsi      # 0x326f
xorl %eax, %eax
callq 0x1050
movq (%r13), %rax
decl (%rax,%r15)
movq (%r12), %rax
decl (%rax,%r15)
incq %rbp
movq 0x28(%rsp), %rax
movl (%rax), %ebx
addq $0x4, %r15
addq $0x8, %r14
jmp 0x1f95
movq 0x18(%rsp), %rdi
callq 0x1080
xorl %ebx, %ebx
testl %eax, %eax
je 0x20b2
movq 0x2ff0(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1267(%rip), %rsi      # 0x3279
xorl %ebx, %ebx
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x20b2
movq 0x2fcc(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1176(%rip), %rsi      # 0x31ac
movq %r14, %rdx
jmp 0x206f
movq 0x2fb6(%rip), %rcx      # 0x4ff8
movq (%rcx), %rdi
leaq 0x1172(%rip), %rsi      # 0x31be
jmp 0x2089
movq 0x2fa3(%rip), %rax      # 0x4ff8
movq (%rax), %rbx
leaq 0x8(%rsp), %rdi
callq 0x16b9
leaq 0x118a(%rip), %rsi      # 0x31f3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x20af
movq 0x2f79(%rip), %rcx      # 0x4ff8
movq (%rcx), %rdi
leaq 0x1189(%rip), %rsi      # 0x3212
movq %r14, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x10f0
jmp 0x20af
movq 0x2f5a(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x119e(%rip), %rsi      # 0x3246
xorl %eax, %eax
callq 0x10f0
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq