// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
leaq 0x11bd(%rip), %rdi      # 0x30c3
leaq 0x11ca(%rip), %rsi      # 0x30d7
callq 0x10f0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x1f35
leaq 0x11b5(%rip), %rdi      # 0x30d9
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x1110
movq -0x50(%rbp), %rdi
leaq 0x11a6(%rip), %rsi      # 0x30e6
leaq 0x3191(%rip), %rcx      # 0x50d8
movq %rcx, %rdx
addq $0x8, %rdx
addq $0xc, %rcx
movb $0x0, %al
callq 0x1040
leaq 0x3178(%rip), %rdi      # 0x50d8
xorl %esi, %esi
callq 0x1ba0
movq -0x50(%rbp), %rsi
leaq 0x3166(%rip), %rdi      # 0x50d8
callq 0x1c70
movq -0x50(%rbp), %rdi
leaq 0x1164(%rip), %rsi      # 0x30e6
leaq 0x313f(%rip), %rcx      # 0x50c8
movq %rcx, %rdx
addq $0x8, %rdx
addq $0xc, %rcx
movb $0x0, %al
callq 0x1040
leaq 0x3126(%rip), %rdi      # 0x50c8
xorl %esi, %esi
callq 0x1ba0
movq -0x50(%rbp), %rsi
leaq 0x3114(%rip), %rdi      # 0x50c8
callq 0x1c70
callq 0x1220
movq -0x50(%rbp), %rdi
callq 0x1080
movl 0x3117(%rip), %eax      # 0x50e4
cmpl 0x30fd(%rip), %eax      # 0x50d0
jne 0x1fef
movl 0x3105(%rip), %eax      # 0x50e0
movl %eax, 0x310f(%rip)      # 0x50f0
movl 0x30ed(%rip), %eax      # 0x50d4
movl %eax, 0x3107(%rip)      # 0x50f4
jmp 0x2009
leaq 0x10f6(%rip), %rdi      # 0x30ec
movb $0x0, %al
callq 0x10a0
movl $0x0, -0x4(%rbp)
jmp 0x20fc
leaq 0x30d8(%rip), %rdi      # 0x50e8
xorl %esi, %esi
callq 0x1ba0
movl $0x0, -0x40(%rbp)
movl 0x30bc(%rip), %eax      # 0x50e0
movl $0x3, %ecx
cltd
idivl %ecx
subl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl 0x308d(%rip), %eax      # 0x50e0
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x54(%rbp)
cmpl $0x3, -0x54(%rbp)
jge 0x209b
movslq -0x54(%rbp), %rax
leaq -0x20(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movslq -0x54(%rbp), %rax
leaq -0x40(%rbp), %rdx
shlq $0x3, %rax
addq %rax, %rdx
leaq -0x27b(%rip), %rsi      # 0x1e10
callq 0x1390
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2060
movl $0x0, -0x58(%rbp)
cmpl $0x3, -0x58(%rbp)
jge 0x20c5
movslq -0x58(%rbp), %rax
movq -0x20(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x15f0
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x20a2
leaq 0x301c(%rip), %rdi      # 0x50e8
callq 0x1cf0
leaq 0x3000(%rip), %rdi      # 0x50d8
callq 0x1dc0
leaq 0x2fe4(%rip), %rdi      # 0x50c8
callq 0x1dc0
leaq 0x2ff8(%rip), %rdi      # 0x50e8
callq 0x1dc0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq