// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x20(%rsp)
movl %edi, %eax
leal -0x4(%rax), %ecx
cmpl $-0x3, %ecx
ja 0x256b
movq (%rsi), %rsi
leaq 0xdc9(%rip), %rdi       # 0x3328
xorl %eax, %eax
callq 0x10a0
jmp 0x26e1
movq 0x8(%rsi), %rdi
cmpl $0x3, %eax
jne 0x257a
movq 0x10(%rsi), %rbx
jmp 0x257c
xorl %ebx, %ebx
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %rsp, %r8
leaq 0x4(%rsp), %r9
callq 0x1ea0
testl %eax, %eax
jne 0x272b
movl (%rsp), %r12d
movslq %r12d, %r15
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x10d0
movq %rax, %r14
shlq $0x3, %r15
movq %r15, %rdi
callq 0x1100
testq %r14, %r14
je 0x270f
movq %rax, %r15
testq %rax, %rax
je 0x270f
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
cmpq %rax, %r12
je 0x25f7
movq %rcx, (%r15,%rax,8)
incq %rax
jmp 0x25e9
xorl %edi, %edi
callq 0x2480
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movl 0x4(%rsp), %r9d
movq %r15, %rcx
movq %r14, %r8
callq 0x2504
leaq 0x20(%rsp), %r12
xorl %edi, %edi
movq %r12, %rsi
callq 0x2499
movsd (%r12), %xmm0
leaq 0xd13(%rip), %rdi       # 0x334c
movb $0x1, %al
callq 0x10a0
testq %rbx, %rbx
je 0x26b3
leaq 0xd22(%rip), %rdi       # 0x336e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10a0
leaq 0xb23(%rip), %rsi       # 0x3180
movq %rbx, %rdi
callq 0x1130
testq %rax, %rax
je 0x26f2
movq %rax, %r12
xorl %ebp, %ebp
leaq 0xd2c(%rip), %r13       # 0x33a6
movslq (%rsp), %rax
cmpq %rax, %rbp
jge 0x269b
movsd (%r14,%rbp,8), %xmm0
movq %r12, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x10f0
incq %rbp
jmp 0x267a
movq %r12, %rdi
callq 0x1080
testl %eax, %eax
jne 0x26f2
leaq 0xcff(%rip), %rdi       # 0x33ad
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
movq 0x28ff(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xc83(%rip), %rsi       # 0x3386
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x272b
movq 0x28e2(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0xbb0(%rip), %rdi       # 0x32d0
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1160
pushq $0x1
popq %rdi
callq 0x1150