// File: /workspace/github_c_makefile_star2_O2/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 0x6ce(%rip), %rdi       # 0x20c1
leaq 0x6db(%rip), %rsi       # 0x20d5
callq 0x1100
testq %rax, %rax
je 0x1b87
movq %rax, %rbx
leaq 0x6d2(%rip), %r14       # 0x20e4
leaq 0x26cf(%rip), %rdx      # 0x40e8
leaq 0x26cc(%rip), %rcx      # 0x40ec
movq %rax, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x26ac(%rip), %r15      # 0x40e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x17bc
movq %r15, %rdi
movq %rbx, %rsi
callq 0x184c
leaq 0x2688(%rip), %rdx      # 0x40d8
leaq 0x2685(%rip), %rcx      # 0x40dc
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x2665(%rip), %r14      # 0x40d0
movq %r14, %rdi
xorl %esi, %esi
callq 0x17bc
movq %r14, %rdi
movq %rbx, %rsi
callq 0x184c
callq 0x122c
movq %rbx, %rdi
callq 0x1090
movl 0x2659(%rip), %eax      # 0x40ec
cmpl 0x263f(%rip), %eax      # 0x40d8
jne 0x1b22
movl 0x2643(%rip), %eax      # 0x40e8
movl %eax, 0x264d(%rip)      # 0x40f8
leaq 0x263e(%rip), %rdi      # 0x40f0
movl 0x2624(%rip), %eax      # 0x40dc
movl %eax, 0x263e(%rip)      # 0x40fc
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x17bc
andl $0x0, -0x30(%rbp)
movl 0x2616(%rip), %ecx      # 0x40e8
pushq $0x3
popq %rsi
movl %ecx, %eax
cltd
idivl %esi
leal -0x1(%rax), %edx
movl %edx, -0x2c(%rbp)
movl %eax, -0x28(%rbp)
leal -0x1(,%rax,2), %edx
movl %edx, -0x24(%rbp)
addl %eax, %eax
movl %eax, -0x20(%rbp)
decl %ecx
movl %ecx, -0x1c(%rbp)
leaq -0x19c(%rip), %rbx      # 0x1962
cmpq $0x18, %r14
je 0x1b32
leaq (%r14,%rbp), %rdi
addq $-0x50, %rdi
leaq (%r14,%rbp), %rdx
addq $-0x30, %rdx
movq %rbx, %rsi
callq 0x1312
addq $0x8, %r14
jmp 0x1afe
leaq 0x5c1(%rip), %rdi       # 0x20ea
xorl %eax, %eax
callq 0x10b0
jmp 0x1b7a
xorl %ebx, %ebx
cmpq $0x3, %rbx
je 0x1b4b
movq -0x50(%rbp,%rbx,8), %rdi
xorl %esi, %esi
callq 0x1487
incq %rbx
jmp 0x1b34
leaq 0x259e(%rip), %rbx      # 0x40f0
movq %rbx, %rdi
callq 0x18b3
leaq 0x257f(%rip), %rdi      # 0x40e0
callq 0x1932
leaq 0x2563(%rip), %rdi      # 0x40d0
callq 0x1932
movq %rbx, %rdi
callq 0x1932
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x549(%rip), %rdi       # 0x20d7
xorl %eax, %eax
callq 0x10b0
pushq $0x1
popq %rdi
callq 0x1120