// File: /workspace/github_c_makefile_star2_O3/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 0x26ab
movq (%rsi), %rsi
leaq 0xcc1(%rip), %rdi       # 0x3360
xorl %eax, %eax
callq 0x10a0
jmp 0x28cb
movq 0x8(%rsi), %rdi
cmpl $0x3, %eax
jne 0x26ba
movq 0x10(%rsi), %rbx
jmp 0x26bc
xorl %ebx, %ebx
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %rsp, %r8
leaq 0x4(%rsp), %r9
callq 0x2010
testl %eax, %eax
jne 0x28dc
movslq (%rsp), %r12
movl $0x8, %esi
movq %r12, %rdi
callq 0x10d0
movq %rax, %r14
leaq (,%r12,8), %rdi
callq 0x1100
testq %r14, %r14
je 0x28e6
movq %rax, %r15
testq %rax, %rax
je 0x28e6
testl %r12d, %r12d
jle 0x27ba
leal 0x1(%r12), %eax
decq %r12
movq %r12, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %eax
shlq $0x4, %rax
movdqa 0xbf1(%rip), %xmm1      # 0x3330
xorl %ecx, %ecx
movdqa 0xbf7(%rip), %xmm2      # 0x3340
pxor %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
pcmpeqd %xmm3, %xmm3
movdqa 0xbed(%rip), %xmm4      # 0x3350
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x2799
movq %rdx, (%r15,%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x27ad
movq %rdx, 0x8(%r15,%rcx)
paddq %xmm4, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x2763
xorl %edi, %edi
callq 0x25b8
movslq 0x4(%rsp), %rax
testq %rax, %rax
jle 0x2803
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 0x27dc
leaq 0x20(%rsp), %r12
xorl %edi, %edi
movq %r12, %rsi
callq 0x25d3
movsd (%r12), %xmm0
leaq 0xb65(%rip), %rdi       # 0x3384
movb $0x1, %al
callq 0x10a0
testq %rbx, %rbx
je 0x289d
leaq 0xb74(%rip), %rdi       # 0x33a6
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10a0
leaq 0x93d(%rip), %rsi       # 0x3180
movq %rbx, %rdi
callq 0x1120
testq %rax, %rax
je 0x28eb
movq %rax, %r12
cmpl $0x0, (%rsp)
jle 0x2885
xorl %ebp, %ebp
leaq 0xb78(%rip), %r13       # 0x33de
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 0x2866
movq %r12, %rdi
callq 0x1080
testl %eax, %eax
jne 0x28f3
leaq 0xb4d(%rip), %rdi       # 0x33e5
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