// File: /workspace/github_c_makefile_star2_O3/deepika9294[P]Threading-Library/one-one/test/matrix.c
// Function: main
int main() {
    dthread_t tid[3];
    rows args[3];
    int temp;
    FILE* file; 

    file = fopen("./test/mat_data.txt", "r");
    if(file == NULL) {
        printf("no such file");
        exit(EXIT_FAILURE);
    }
    fscanf(file,"%d %d", &(a.r), &(a.c));
    init(&a,0);
    read_input(&a, file);

    fscanf(file,"%d %d", &(b.r), &(b.c));
    init(&b,0);
    read_input(&b,file);
    dthread_init();
    fclose(file);

    // initialising size of output matrix if they are multiplicable
    if(a.c == b.r) {
        res.r = a.r;
        res.c = b.c;
    }
    else {
        printf("Matrix cannot be multiplied");
        return 0;
    }
    init(&res,0);
    
    // dividing the array in 3 parts by rows
    args[0].row_start = 0;
    args[0].row_end = a.r / 3 - 1;
    args[1].row_start = args[0].row_end + 1;
    args[1].row_end = args[1].row_start + args[0].row_end ;
    args[2].row_start = args[1].row_end + 1;
    args[2].row_end = a.r - 1;

    // debugging 
    // for(int i = 0; i < 3; i++)
    //     printf("%d %d \n", args[i].row_start, args[i].row_end);
 
    // creating three different threads
    for(int i = 0; i < 3; i++) {
        dthread_create(&tid[i], partial_multiply,&args[i]);
    }

    for(int i = 0; i < 3; i++) {
        dthread_join(tid[i], NULL);
    }    


    print_matrix(&res);

    destroy_matrix(&a);
    destroy_matrix(&b);
    destroy_matrix(&res);

    return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x38, %rsp
leaq 0x639(%rip), %rdi       # 0x20c1
leaq 0x646(%rip), %rsi       # 0x20d5
callq 0x1100
testq %rax, %rax
je 0x1c25
movq %rax, %rbx
leaq 0x63d(%rip), %r14       # 0x20e4
leaq 0x263a(%rip), %rdx      # 0x40e8
leaq 0x2637(%rip), %rcx      # 0x40ec
movq %rax, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x2617(%rip), %r15      # 0x40e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x1820
movq %r15, %rdi
movq %rbx, %rsi
callq 0x18b3
leaq 0x25f3(%rip), %rdx      # 0x40d8
leaq 0x25f0(%rip), %rcx      # 0x40dc
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x25d0(%rip), %r14      # 0x40d0
movq %r14, %rdi
xorl %esi, %esi
callq 0x1820
movq %r14, %rdi
movq %rbx, %rsi
callq 0x18b3
callq 0x124c
movq %rbx, %rdi
callq 0x1090
movl 0x25c4(%rip), %eax      # 0x40ec
cmpl 0x25aa(%rip), %eax      # 0x40d8
jne 0x1c0a
movl 0x25ae(%rip), %eax      # 0x40e8
movl %eax, 0x25b8(%rip)      # 0x40f8
leaq 0x25a9(%rip), %rdi      # 0x40f0
movl 0x258f(%rip), %eax      # 0x40dc
movl %eax, 0x25a9(%rip)      # 0x40fc
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x1820
movl $0x0, -0x30(%rbp)
movslq 0x257d(%rip), %rax      # 0x40e8
imulq $0x55555556, %rax, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
leal (%rcx,%rdx), %esi
addl %edx, %ecx
decl %ecx
movl %ecx, -0x2c(%rbp)
movl %esi, -0x28(%rbp)
addl %esi, %ecx
movl %ecx, -0x24(%rbp)
addl %esi, %esi
movl %esi, -0x20(%rbp)
decl %eax
movl %eax, -0x1c(%rbp)
leaq -0x1c1(%rip), %rbx      # 0x19df
leaq (%r14,%rbp), %rdi
addq $-0x50, %rdi
leaq (%r14,%rbp), %rdx
addq $-0x30, %rdx
movq %rbx, %rsi
callq 0x1340
addq $0x8, %r14
cmpq $0x18, %r14
jne 0x1ba0
xorl %ebx, %ebx
movq -0x50(%rbp,%rbx,8), %rdi
xorl %esi, %esi
callq 0x14d0
incq %rbx
cmpq $0x3, %rbx
jne 0x1bc4
leaq 0x2510(%rip), %rbx      # 0x40f0
movq %rbx, %rdi
callq 0x1931
leaq 0x24f1(%rip), %rdi      # 0x40e0
callq 0x19a8
leaq 0x24d5(%rip), %rdi      # 0x40d0
callq 0x19a8
movq %rbx, %rdi
callq 0x19a8
jmp 0x1c18
leaq 0x4d9(%rip), %rdi       # 0x20ea
xorl %eax, %eax
callq 0x10b0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4ab(%rip), %rdi       # 0x20d7
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
callq 0x1120