// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1aed(%rip), %rsi      # 0x4128
callq 0x1130
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x2672
movq 0x39a7(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x1b4d(%rip), %rsi      # 0x41ac
movb $0x0, %al
callq 0x10f0
movl $0x1, -0x4(%rbp)
jmp 0x2897
movq -0x40(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x14b0
movl %eax, -0x48(%rbp)
cmpl $0x0, %eax
je 0x26b2
movq 0x396a(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
movl -0x48(%rbp), %ecx
leaq 0x1b1f(%rip), %rsi      # 0x41be
movb $0x0, %al
callq 0x10f0
movl $0x1, -0x4(%rbp)
jmp 0x2897
movsbl -0x44(%rbp), %eax
cmpl $0x4d, %eax
jne 0x26cd
movsbl -0x42(%rbp), %eax
cmpl $0x52, %eax
jne 0x26cd
movsbl -0x43(%rbp), %eax
cmpl $0x43, %eax
je 0x2705
movq 0x3924(%rip), %rax      # 0x5ff8
movq (%rax), %rax
movq %rax, -0x60(%rbp)
leaq -0x44(%rbp), %rdi
callq 0x1880
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0x1b01(%rip), %rsi      # 0x41f3
movb $0x0, %al
callq 0x10f0
movl $0x1, -0x4(%rbp)
jmp 0x2897
movq -0x40(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
leaq -0x50(%rbp), %rdx
leaq -0x54(%rbp), %rcx
callq 0x1a60
movl %eax, -0x48(%rbp)
cmpl $0x0, %eax
je 0x274d
movq 0x38cf(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
movl -0x48(%rbp), %ecx
leaq 0x1ad8(%rip), %rsi      # 0x4212
movb $0x0, %al
callq 0x10f0
movl $0x1, -0x4(%rbp)
jmp 0x2897
movl -0x4c(%rbp), %eax
cmpl -0x50(%rbp), %eax
je 0x277f
movq 0x389c(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movl -0x4c(%rbp), %edx
movl -0x50(%rbp), %ecx
leaq 0x1ada(%rip), %rsi      # 0x4246
movb $0x0, %al
callq 0x10f0
movl $0x1, -0x4(%rbp)
jmp 0x2897
movl -0x4c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl -0x54(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movslq -0x54(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1110
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movslq -0x54(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1110
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movslq -0x54(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1110
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x2866
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movslq -0x58(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movslq -0x58(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq (%rax), %r8
movslq -0x58(%rbp), %rax
shlq $0x3, %rax
addq %rax, %r8
leaq 0x1a46(%rip), %rsi      # 0x426f
movb $0x0, %al
callq 0x1060
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x58(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $-0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x58(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $-0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x27dd
movq -0x40(%rbp), %rdi
callq 0x1080
cmpl $0x0, %eax
je 0x2890
movq 0x377d(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x19f0(%rip), %rsi      # 0x4279
movb $0x0, %al
callq 0x10f0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq