// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movl %edi, %r15d
xorl %edi, %edi
callq 0x10a0
movl %eax, %edi
callq 0x1080
cmpl $0x2, %r15d
jne 0x1216
movq 0x8(%r14), %rdi
callq 0x10c0
movq %rax, -0x50(%rbp)
jmp 0x1275
jle 0x126d
movq 0x8(%r14), %rdi
callq 0x10c0
movq %rax, -0x50(%rbp)
movq 0x10(%r14), %rdi
callq 0x10c0
movl %eax, %ebx
movq 0x18(%r14), %rdi
callq 0x10c0
movl %eax, %r12d
movq 0x20(%r14), %rdi
callq 0x10c0
movl %eax, -0x40(%rbp)
movq 0x28(%r14), %rdi
callq 0x10c0
movl %eax, -0x3c(%rbp)
movq 0x30(%r14), %rdi
callq 0x10c0
cmpl $0x8, %r15d
setne -0x29(%rbp)
testl %eax, %eax
sete %r14b
jmp 0x1295
movq $0x0, -0x50(%rbp)
movl $0x5, %ebx
movl $0xa, %r12d
movl $0x3, -0x40(%rbp)
movl $0x2, -0x3c(%rbp)
movb $0x1, -0x29(%rbp)
xorl %r14d, %r14d
movl %ebx, %esi
movl %r12d, %r15d
movq %rsi, %rax
imulq %r15, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
movq %rsp, %r13
leaq 0xf(,%r15,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %rcx
leaq 0xf(,%rsi,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
testl %r12d, %r12d
movl %ebx, -0x30(%rbp)
movq %rsi, -0x48(%rbp)
movq %r13, -0x70(%rbp)
jle 0x1421
movslq %ebx, %rax
movq %rax, -0x80(%rbp)
leaq (,%r15,4), %r12
movq $0x0, -0x38(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
testl %ebx, %ebx
jle 0x1373
movq -0x60(%rbp), %rbx
xorl %r13d, %r13d
testb %r14b, %r14b
je 0x133d
movl %r13d, (%rbx)
jmp 0x1368
callq 0x10d0
movq -0x48(%rbp), %rsi
cltq
imulq $0x288df0cb, %rax, %rcx # imm = 0x288DF0CB
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x24, %rcx
addl %edx, %ecx
imull $0x65, %ecx, %ecx
negl %ecx
addl %ecx, %eax
addl $-0x32, %eax
movl %eax, (%rbx)
incq %r13
addq %r12, %rbx
cmpq %r13, %rsi
jne 0x1333
testb %r14b, %r14b
je 0x1387
movq -0x70(%rbp), %r13
movq -0x38(%rbp), %rdx
movl %edx, (%r13,%rdx,4)
jmp 0x13bd
callq 0x10d0
movq -0x48(%rbp), %rsi
cltq
imulq $0x288df0cb, %rax, %rcx # imm = 0x288DF0CB
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x24, %rcx
addl %edx, %ecx
imull $0x65, %ecx, %ecx
negl %ecx
addl %ecx, %eax
addl $-0x32, %eax
movq -0x70(%rbp), %r13
movq -0x38(%rbp), %rdx
movl %eax, (%r13,%rdx,4)
cmpq -0x80(%rbp), %rdx
movl -0x30(%rbp), %ebx
jge 0x140c
testb %r14b, %r14b
je 0x13d8
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rdx
movl %edx, (%rax,%rdx,4)
jmp 0x140c
callq 0x10d0
movq -0x48(%rbp), %rsi
cltq
imulq $0x288df0cb, %rax, %rcx # imm = 0x288DF0CB
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x24, %rcx
addl %edx, %ecx
imull $0x65, %ecx, %ecx
negl %ecx
addl %ecx, %eax
addl $-0x32, %eax
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rdx
movl %eax, (%rcx,%rdx,4)
incq %rdx
addq $0x4, -0x60(%rbp)
movq %rdx, -0x38(%rbp)
cmpq %r15, %rdx
jne 0x1328
cmpl $0x0, -0x50(%rbp)
movq %rsi, %r12
movq -0x58(%rbp), %r14
je 0x14fd
leaq 0xc38(%rip), %rdi       # 0x2071
callq 0x1040
cmpl $0x0, -0x30(%rbp)
jle 0x1492
leaq (,%r15,4), %rax
movq %rax, -0x38(%rbp)
leaq 0xbb9(%rip), %r14       # 0x2010
movq -0x68(%rbp), %r12
xorl %r13d, %r13d
testl %r15d, %r15d
jle 0x147b
xorl %ebx, %ebx
movl (%r12,%rbx,4), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1070
incq %rbx
cmpq %rbx, %r15
jne 0x1465
movl $0xa, %edi
callq 0x1030
incq %r13
addq -0x38(%rbp), %r12
cmpq -0x48(%rbp), %r13
jne 0x145e
leaq 0xbe3(%rip), %rdi       # 0x207c
callq 0x1040
testl %r15d, %r15d
movq -0x70(%rbp), %r13
jle 0x14c7
leaq 0xb62(%rip), %r14       # 0x2010
xorl %ebx, %ebx
movl (%r13,%rbx,4), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1070
incq %rbx
cmpq %rbx, %r15
jne 0x14b0
leaq 0xbb8(%rip), %rdi       # 0x2086
callq 0x1040
cmpl $0x0, -0x30(%rbp)
movq -0x48(%rbp), %r12
movq -0x58(%rbp), %r14
jle 0x14fd
xorl %ebx, %ebx
movl (%r14,%rbx,4), %esi
leaq 0xb22(%rip), %rdi       # 0x2010
xorl %eax, %eax
callq 0x1070
incq %rbx
cmpq %rbx, %r12
jne 0x14e3
callq 0x1050
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x30(%rbp)
movl -0x40(%rbp), %r9d
movl -0x3c(%rbp), %r10d
movq -0x78(%rbp), %rbx
movq -0x68(%rbp), %r11
jle 0x1564
leaq (,%r15,4), %rax
xorl %ecx, %ecx
movl $0x0, %edx
testl %r15d, %r15d
jle 0x1548
xorl %esi, %esi
xorl %edx, %edx
movl (%r13,%rsi,4), %edi
imull (%r11,%rsi,4), %edi
addl %edi, %edx
incq %rsi
cmpq %rsi, %r15
jne 0x1534
imull %r9d, %edx
movl (%r14,%rcx,4), %esi
imull %r10d, %esi
addl %edx, %esi
movl %esi, (%rbx,%rcx,4)
incq %rcx
addq %rax, %r11
cmpq %r12, %rcx
jne 0x1526
callq 0x1050
movq %rax, -0x60(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x15a8
movq %rbx, %r13
leaq 0xb14(%rip), %rdi       # 0x2091
callq 0x1040
cmpl $0x0, -0x30(%rbp)
jle 0x15a8
leaq 0xa81(%rip), %r14       # 0x2010
xorl %ebx, %ebx
movl (%r13,%rbx,4), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1070
incq %rbx
cmpq %rbx, %r12
jne 0x1591
cmpb $0x0, -0x29(%rbp)
movl -0x30(%rbp), %ebx
jne 0x15fd
leaq 0xa5d(%rip), %rdi       # 0x2015
leaq 0xa6e(%rip), %rsi       # 0x202d
callq 0x10b0
movq %rax, %r13
testl %ebx, %ebx
movq -0x48(%rbp), %r12
movq -0x78(%rbp), %r14
jle 0x15f2
xorl %ebx, %ebx
movl (%r14,%rbx,4), %edx
movq %r13, %rdi
leaq 0xa4c(%rip), %rsi       # 0x202f
xorl %eax, %eax
callq 0x1090
incq %rbx
cmpq %rbx, %r12
jne 0x15d5
movq %r13, %rdi
callq 0x1060
movl -0x30(%rbp), %ebx
movq -0x60(%rbp), %rax
subq -0x38(%rbp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x9f6(%rip), %xmm0      # 0x2008
leaq 0xa1a(%rip), %rdi       # 0x2033
movl %r15d, %esi
movl %ebx, %edx
movb $0x1, %al
callq 0x1070
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq