// File: /workspace/github_c_makefile_star2_O1/kkanellis[P]matvec-sparse/matvec_seq.c
// Function: main
int main(int argc, char * argv[])
{
    char *in_file,
         *out_file = NULL;

    double *values; /* a_values array */
    int *i_idx,     /* i_index array */
        *j_idx;     /* j_index array */

    double *x, *y;  /* Ax = y */
    double comp_time = 0;
    int N, NZ;

    /* read arguments */
    if (argc < 2 || argc > 3) {
        printf("Usage: %s input_file [output_file]\n", argv[0]);
        return 0;
    }
    else {
        in_file = argv[1];
        if (argc == 3) 
            out_file = argv[2];
    }

    /* read matrix */
    if ( read_matrix(in_file, &i_idx, &j_idx, &values, &N, &NZ) != 0) {
        exit(EXIT_FAILURE);
    }

    debug("Matrix properties: N = %d, NZ = %d\n", N, NZ);

    /* allocate x, y vector */
    y = (double *)calloc( N, sizeof(double) );
    x = (double *)malloc( N * sizeof(double) );
    if (y == NULL || x == NULL) {
        fprintf(stderr, "malloc: failed to allocate memory\n");
        exit(EXIT_FAILURE);
    }

    /* generate random vector */
    //random_vec(x, N, MAX_RANDOM_NUM);
    for (int i = 0; i < N; i++) {
        x[i] = 1;
    }
    
    /* perform the multiplication */
    __sw_start(0);
    mat_vec_mult(values, i_idx, j_idx, x, y, NZ);
    __sw_stop(0, &comp_time);

    printf("Total execution time: %10.3lf ms\n", comp_time);

    if (out_file != NULL) {
        printf("Writing result to '%s'\n", out_file);

        /* open file */
        FILE *f;
        if ( !(f = fopen(out_file, "w")) ) {
            fprintf(stderr, "fopen: failed to open file '%s'", out_file);
            exit(EXIT_FAILURE);
        }

        /* write result */
        for (int i = 0; i < N; i++) {
            fprintf(f, "%.8lf\n", y[i]);
        }
        
        /* close file */
        if ( fclose(f) != 0) {
            fprintf(stderr, "fopen: failed to open file '%s'", out_file);
            exit(EXIT_FAILURE);
        }

        printf("Done!\n");
    }

    /* free the memory */
    free(values);
    free(i_idx);
    free(j_idx);
    free(x);
    free(y);
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edi, %eax
movq $0x0, 0x20(%rsp)
leal -0x4(%rax), %ecx
cmpl $-0x3, %ecx
ja 0x2693
movq (%rsi), %rsi
leaq 0xc89(%rip), %rdi       # 0x3310
xorl %eax, %eax
callq 0x10a0
jmp 0x282c
movq 0x8(%rsi), %rdi
cmpl $0x3, %eax
jne 0x26a2
movq 0x10(%rsi), %rbx
jmp 0x26a4
xorl %ebx, %ebx
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %rsp, %r8
leaq 0x4(%rsp), %r9
callq 0x2018
testl %eax, %eax
jne 0x283d
movslq (%rsp), %r12
movl $0x8, %esi
movq %r12, %rdi
callq 0x10d0
movq %rax, %r14
leaq (,%r12,8), %rdi
callq 0x1100
testq %r14, %r14
je 0x2847
movq %rax, %r15
testq %rax, %rax
je 0x2847
testl %r12d, %r12d
jle 0x271b
xorl %eax, %eax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%r15,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x270f
xorl %edi, %edi
callq 0x25a0
movslq 0x4(%rsp), %rax
testq %rax, %rax
jle 0x2764
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rsi
xorl %edi, %edi
movsd (%rcx,%rdi,8), %xmm0
movslq (%rsi,%rdi,4), %r8
movslq (%rdx,%rdi,4), %r9
mulsd (%r15,%r8,8), %xmm0
addsd (%r14,%r9,8), %xmm0
movsd %xmm0, (%r14,%r9,8)
incq %rdi
cmpq %rdi, %rax
jne 0x273d
leaq 0x20(%rsp), %r12
xorl %edi, %edi
movq %r12, %rsi
callq 0x25bb
movsd (%r12), %xmm0
leaq 0xbb4(%rip), %rdi       # 0x3334
movb $0x1, %al
callq 0x10a0
testq %rbx, %rbx
je 0x27fe
leaq 0xbc3(%rip), %rdi       # 0x3356
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10a0
leaq 0x9dc(%rip), %rsi       # 0x3180
movq %rbx, %rdi
callq 0x1120
testq %rax, %rax
je 0x284c
movq %rax, %r12
cmpl $0x0, (%rsp)
jle 0x27e6
xorl %ebp, %ebp
leaq 0xbc7(%rip), %r13       # 0x338e
movsd (%r14,%rbp,8), %xmm0
movq %r12, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x10f0
incq %rbp
movslq (%rsp), %rax
cmpq %rax, %rbp
jl 0x27c7
movq %r12, %rdi
callq 0x1080
testl %eax, %eax
jne 0x2854
leaq 0xb9c(%rip), %rdi       # 0x3395
callq 0x1060
movq 0x18(%rsp), %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
callq 0x1030
movq 0x8(%rsp), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x1140
callq 0x12b3
movq %rbx, %rdi
callq 0x128f
movq %rbx, %rdi
callq 0x126b