// File: /workspace/github_c_makefile_star2_O1/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 0x645(%rip), %rdi       # 0x20c1
leaq 0x652(%rip), %rsi       # 0x20d5
callq 0x1100
testq %rax, %rax
je 0x1c19
movq %rax, %rbx
leaq 0x649(%rip), %r14       # 0x20e4
leaq 0x2646(%rip), %rdx      # 0x40e8
leaq 0x2643(%rip), %rcx      # 0x40ec
movq %rax, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x2623(%rip), %r15      # 0x40e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x1820
movq %r15, %rdi
movq %rbx, %rsi
callq 0x18b1
leaq 0x25ff(%rip), %rdx      # 0x40d8
leaq 0x25fc(%rip), %rcx      # 0x40dc
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x25dc(%rip), %r14      # 0x40d0
movq %r14, %rdi
xorl %esi, %esi
callq 0x1820
movq %r14, %rdi
movq %rbx, %rsi
callq 0x18b1
callq 0x124c
movq %rbx, %rdi
callq 0x1090
movl 0x25d0(%rip), %eax      # 0x40ec
cmpl 0x25b6(%rip), %eax      # 0x40d8
jne 0x1bfe
movl 0x25ba(%rip), %eax      # 0x40e8
movl %eax, 0x25c4(%rip)      # 0x40f8
leaq 0x25b5(%rip), %rdi      # 0x40f0
movl 0x259b(%rip), %eax      # 0x40dc
movl %eax, 0x25b5(%rip)      # 0x40fc
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x1820
movl $0x0, -0x30(%rbp)
movslq 0x2589(%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 -0x1be(%rip), %rbx      # 0x19d6
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 0x1b94
xorl %ebx, %ebx
movq -0x50(%rbp,%rbx,8), %rdi
xorl %esi, %esi
callq 0x14d0
incq %rbx
cmpq $0x3, %rbx
jne 0x1bb8
leaq 0x251c(%rip), %rbx      # 0x40f0
movq %rbx, %rdi
callq 0x1928
leaq 0x24fd(%rip), %rdi      # 0x40e0
callq 0x199f
leaq 0x24e1(%rip), %rdi      # 0x40d0
callq 0x199f
movq %rbx, %rdi
callq 0x199f
jmp 0x1c0c
leaq 0x4e5(%rip), %rdi       # 0x20ea
xorl %eax, %eax
callq 0x10b0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4b7(%rip), %rdi       # 0x20d7
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
callq 0x1120