// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
cmpl $0x2, -0x8(%rbp)
jl 0x3102
cmpl $0x3, -0x8(%rbp)
jle 0x3123
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x1208(%rip), %rdi      # 0x4318
movb $0x0, %al
callq 0x10a0
movl $0x0, -0x4(%rbp)
jmp 0x3343
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
cmpl $0x3, -0x8(%rbp)
jne 0x3141
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x3143
movq -0x18(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x38(%rbp), %rdx
leaq -0x28(%rbp), %rcx
leaq -0x54(%rbp), %r8
leaq -0x58(%rbp), %r9
callq 0x2610
cmpl $0x0, %eax
je 0x316f
movl $0x1, %edi
callq 0x1150
jmp 0x3171
jmp 0x3173
movslq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x10d0
movq %rax, -0x48(%rbp)
movslq -0x54(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1110
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x31a4
cmpq $0x0, -0x40(%rbp)
jne 0x31c6
movq 0x2e4d(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1103(%rip), %rsi      # 0x42b8
movb $0x0, %al
callq 0x10f0
movl $0x1, %edi
callq 0x1150
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x31f5
movq -0x40(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd 0x112b(%rip), %xmm0     # 0x4310
movsd %xmm0, (%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x31cd
xorl %edi, %edi
callq 0x2f80
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
movl -0x58(%rbp), %r9d
callq 0x3050
xorl %edi, %edi
leaq -0x50(%rbp), %rsi
callq 0x2fb0
movsd -0x50(%rbp), %xmm0
leaq 0x110c(%rip), %rdi      # 0x433c
movb $0x1, %al
callq 0x10a0
cmpq $0x0, -0x20(%rbp)
je 0x330f
movq -0x20(%rbp), %rsi
leaq 0x1111(%rip), %rdi      # 0x435e
movb $0x0, %al
callq 0x10a0
movq -0x20(%rbp), %rdi
leaq 0xf21(%rip), %rsi       # 0x4180
callq 0x1130
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
jne 0x3294
movq 0x2d83(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x10f3(%rip), %rsi      # 0x4376
movb $0x0, %al
callq 0x10f0
movl $0x1, %edi
callq 0x1150
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x32cd
movq -0x68(%rbp), %rdi
movq -0x48(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x10db(%rip), %rsi      # 0x4396
movb $0x1, %al
callq 0x10f0
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x329b
movq -0x68(%rbp), %rdi
callq 0x1080
cmpl $0x0, %eax
je 0x3301
movq 0x2d16(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x1086(%rip), %rsi      # 0x4376
movb $0x0, %al
callq 0x10f0
movl $0x1, %edi
callq 0x1150
leaq 0x1095(%rip), %rdi      # 0x439d
movb $0x0, %al
callq 0x10a0
movq -0x28(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x40(%rbp), %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq