// File: skourta[P]xGEMV-Implementation-with-Halide-and-C/src/c_gemv.c
// Function: main
int main(int argc, char **argv)
{
    // set the random seed to avoid getting the same values
    srand(time(0));

    // initialize the variables
    int debug = 0;
    int aheight = AHEIGHT;
    int awidth = AWIDTH;
    int alpha = ALPHA;
    int beta = BETA;
    int random = 1;
    int saveFile = 0;

    // check if debug is the only argument set
    if (argc == 2)
    {
        //enable printing the results
        debug = atoi(argv[1]);
    }
    else
    {
        //set all arguments to their specific variables
        if (argc > 2)
        {
            //enable printing the results
            debug = atoi(argv[1]);
            aheight = atoi(argv[2]);
            awidth = atoi(argv[3]);
            alpha = atoi(argv[4]);
            beta = atoi(argv[5]);
            //enable generating random values
            random = atoi(argv[6]);
            if (argc == 8)
            {
                //enable saving files
                saveFile = 1;
            }
        }
    }
    //declare the variables containing the values we are gonna use
    int A[aheight][awidth];
    int x[awidth];
    int y[aheight];
    int res[aheight];
    int yTimesBeta[aheight];

    //filling the variables
    for (int i = 0; i < awidth; i++)
    {
        for (int j = 0; j < aheight; j++)
        {
            if (random)
            {
                // filling randomly with a value between -50 and 50
                A[j][i] = (rand() % (UPPER - LOWER + 1)) + LOWER;
            }
            else
            {
                //filling with index of row
                A[j][i] = j;
            }
        }
        if (random)
        {
            // filling randomly with a value between -50 and 50
            x[i] = (rand() % (UPPER - LOWER + 1)) + LOWER;
        }
        else
        {
            //filling with index of row
            x[i] = i;
        }
        if (i < aheight)
        {
            if (random)
            {
                // filling randomly with a value between -50 and 50
                y[i] = (rand() % (UPPER - LOWER + 1)) + LOWER;
            }
            else
            {
                //filling with index of row
                y[i] = i;
            }
        }
    }
    // Print the Values to the screen if in debug
    if (debug)
    {
        printf("\n\nMatrix A\n");
        for (int i = 0; i < aheight; i++)
        {
            for (int j = 0; j < awidth; j++)
            {
                printf("%d \t", A[i][j]);
            }
            printf("\n");
        }
        printf("\nVector x\n");
        for (int i = 0; i < awidth; i++)
        {
            printf("%d \t", x[i]);
        }
        printf("\n\nVector y\n");
        for (int i = 0; i < aheight; i++)
        {
            printf("%d \t", y[i]);
        }
    }

    // timing the execution of the gemv pipeline
    clock_t t;
    t = clock();
    //calculating the values
    for (int i = 0; i < aheight; i++)
    {
        int sum = 0;
        for (int j = 0; j < awidth; j++)
        {
            sum += A[i][j] * x[j];
        }
        res[i] = alpha * sum + beta * y[i];
    }
    // calculating the execution time of the gemv pipeline
    t = clock() - t;
    double time_taken = ((double)t) / CLOCKS_PER_SEC; // in seconds
    // Print the Results to the screen if in debug
    if (debug)
    {
        printf("\n\nResult\n");
        for (int i = 0; i < aheight; i++)
        {
            printf("%d \t", res[i]);
        }
    }
    //save the results to the "./results/c_results.txt" file
    if (saveFile)
        if (saveFile)
        {
            FILE *fp;
            int i;
            /* open the file for writing*/
            fp = fopen("./results/c_results.txt", "w");

            /* write 10 lines of text into the file stream*/
            for (i = 0; i < aheight; i++)
            {
                fprintf(fp, "%d ", res[i]);
            }

            /* close the file*/
            fclose(fp);
        }

    printf("\nC GEMV took %f seconds to execute on width=%d and height=%d\n", time_taken, awidth, aheight);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1080
movl %eax, %edi
callq 0x1060
movl $0x0, -0x14(%rbp)
movl $0x5, -0x18(%rbp)
movl $0xa, -0x1c(%rbp)
movl $0x3, -0x20(%rbp)
movl $0x2, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x2, -0x8(%rbp)
jne 0x1232
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10a0
movl %eax, -0x14(%rbp)
jmp 0x12a9
cmpl $0x2, -0x8(%rbp)
jle 0x12a7
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10a0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10a0
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x10a0
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x10a0
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x10a0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x10a0
movl %eax, -0x28(%rbp)
cmpl $0x8, -0x8(%rbp)
jne 0x12a5
movl $0x1, -0x2c(%rbp)
jmp 0x12a7
jmp 0x12a9
movl -0x18(%rbp), %eax
movl %eax, %ecx
movl -0x1c(%rbp), %eax
movq %rax, -0xe0(%rbp)
movq %rsp, %rdx
movq %rdx, -0x38(%rbp)
movq %rcx, %rdx
imulq %rax, %rdx
leaq 0xf(,%rdx,4), %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0xd8(%rbp)
movq %rdx, %rsp
movq %rcx, -0x40(%rbp)
movq %rax, -0x48(%rbp)
movl -0x1c(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xd0(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
movl -0x18(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl -0x18(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movl -0x18(%rbp), %eax
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1474
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x13ec
cmpl $0x0, -0x28(%rbp)
je 0x13b8
callq 0x10b0
movq -0xe0(%rbp), %rsi
movl $0x65, %ecx
cltd
idivl %ecx
movq -0xd8(%rbp), %rax
addl $-0x32, %edx
movslq -0x70(%rbp), %rcx
imulq %rsi, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x6c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x13df
movq -0xd8(%rbp), %rax
movq -0xe0(%rbp), %rsi
movl -0x70(%rbp), %edx
movslq -0x70(%rbp), %rcx
imulq %rsi, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x6c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x13e1
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x1374
cmpl $0x0, -0x28(%rbp)
je 0x1412
callq 0x10b0
movl $0x65, %ecx
cltd
idivl %ecx
movq -0xd0(%rbp), %rax
addl $-0x32, %edx
movslq -0x6c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1423
movq -0xd0(%rbp), %rax
movl -0x6c(%rbp), %edx
movslq -0x6c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x6c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x1464
cmpl $0x0, -0x28(%rbp)
je 0x1451
callq 0x10b0
movl $0x65, %ecx
cltd
idivl %ecx
movq -0xc8(%rbp), %rax
addl $-0x32, %edx
movslq -0x6c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1462
movq -0xc8(%rbp), %rax
movl -0x6c(%rbp), %edx
movslq -0x6c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1464
jmp 0x1466
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1361
cmpl $0x0, -0x14(%rbp)
je 0x158a
leaq 0xb8b(%rip), %rdi       # 0x2010
movb $0x0, %al
callq 0x1050
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x1500
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x14e7
movq -0xd8(%rbp), %rax
movq -0xe0(%rbp), %rdx
movslq -0x74(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0xb47(%rip), %rdi       # 0x201c
movb $0x0, %al
callq 0x1050
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x14a2
leaq 0xb2c(%rip), %rdi       # 0x201a
movb $0x0, %al
callq 0x1050
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x1493
leaq 0xb1a(%rip), %rdi       # 0x2021
movb $0x0, %al
callq 0x1050
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1544
movq -0xd0(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0xaea(%rip), %rdi       # 0x201c
movb $0x0, %al
callq 0x1050
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x1515
leaq 0xae1(%rip), %rdi       # 0x202c
movb $0x0, %al
callq 0x1050
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x1588
movq -0xc8(%rbp), %rax
movslq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0xaa6(%rip), %rdi       # 0x201c
movb $0x0, %al
callq 0x1050
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x1559
jmp 0x158a
callq 0x1030
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x166d
movl $0x0, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1627
movq -0xd0(%rbp), %rcx
movq -0xd8(%rbp), %rax
movq -0xe0(%rbp), %rsi
movslq -0x8c(%rbp), %rdx
imulq %rsi, %rdx
shlq $0x2, %rdx
addq %rdx, %rax
movslq -0x94(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movslq -0x94(%rbp), %rdx
imull (%rcx,%rdx,4), %eax
addl -0x90(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x15c3
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rsi
movl -0x20(%rbp), %edx
imull -0x90(%rbp), %edx
movl -0x24(%rbp), %ecx
movslq -0x8c(%rbp), %rdi
imull (%rsi,%rdi,4), %ecx
addl %ecx, %edx
movslq -0x8c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x15a0
callq 0x1030
subq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
cvtsi2sdq -0x88(%rbp), %xmm0
movsd 0x977(%rip), %xmm1      # 0x2008
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x16f8
leaq 0x98e(%rip), %rdi       # 0x2038
movb $0x0, %al
callq 0x1050
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x16f6
movq -0xc0(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0x93e(%rip), %rdi       # 0x201c
movb $0x0, %al
callq 0x1050
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x16bb
jmp 0x16f8
cmpl $0x0, -0x2c(%rbp)
je 0x1778
cmpl $0x0, -0x2c(%rbp)
je 0x1776
leaq 0x937(%rip), %rdi       # 0x2042
leaq 0x948(%rip), %rsi       # 0x205a
callq 0x1090
movq %rax, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x176a
movq -0xc0(%rbp), %rax
movq -0xb0(%rbp), %rdi
movslq -0xb4(%rbp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x90a(%rip), %rsi       # 0x205c
movb $0x0, %al
callq 0x1070
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x1728
movq -0xb0(%rbp), %rdi
callq 0x1040
jmp 0x1778
movsd -0xa0(%rbp), %xmm0
movl -0x1c(%rbp), %esi
movl -0x18(%rbp), %edx
leaq 0x8d3(%rip), %rdi       # 0x2060
movb $0x1, %al
callq 0x1050
movq -0x38(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq