// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/InputUtilSt.cpp
// Function: InputUtilSt::createConnectivities(task*)
void InputUtilSt::createConnectivities(struct task *a_task) {
    // init
    a_task->n_connectivities=2;
    a_task->n_connectivity_X = Calloc(NDAT, (size_t)a_task->n_connectivities);
    a_task->n_connectivity_X[0] = a_task->nK;
    a_task->n_connectivity_X[1] = a_task->nG;
    a_task->n_connectivity_Y = Calloc(NDAT, (size_t)a_task->n_connectivities);
    a_task->n_connectivity_Y[0] = a_task->nK;
    a_task->n_connectivity_Y[1] = a_task->nK;
    a_task->connectivities = Calloc(NPAR**, (size_t)a_task->n_connectivities);
    a_task->connectivities[0] = initToValue2D<NPAR>(a_task->n_connectivity_X[0], a_task->n_connectivity_Y[0], 0);
    a_task->connectivities[1] = initToValue2D<NPAR>(a_task->n_connectivity_X[1], a_task->n_connectivity_Y[1], 0);

    // create
    for(NDAT t=0; t<a_task->N; t++) { // vv for all non-stacked rows
        // skill-skill connectivity
        NCAT g = a_task->dat_group[t]; // -1, because in data they were 1-starting
        // grab skill array (if exists)
        NCAT *ar;
        NPAR n;
        getSkillsAtRow(a_task, t, &ar, &n);
        if(n>1) {
            for(NPAR i1=0;i1<(n-1);i1++) {
                for(NPAR i2=(NPAR)(i1+1);i2<n;i2++) {
                    a_task->connectivities[0][ ar[i1] ][ ar[i2] ] = 1;
                    a_task->connectivities[0][ ar[i2] ][ ar[i1] ] = 1;
                }
            }
        }
        // student-skill connectivity
        for(NPAR l=0; l<n; l++) {
            a_task->connectivities[1][g][l] = 1;
        }
    } // ^^ for all non-stacked rows
//    std::ofstream file; // DEBUG
//    file.open("connectivity.txt");  // DEBUG
//    for(NCAT i=0; i<a_task->n_connectivity_X[0]; i++) {  // DEBUG
//        std::stringstream ss;  // DEBUG
//        for(NDAT j=0; j<a_task->n_connectivity_Y[0]; j++) {  // DEBUG
//            ss << ((j>0)?" ":"") << std::to_string(a_task->connectivities[0][i][j]);  // DEBUG
//        }  // DEBUG
//        ss << "\n";  // DEBUG
//        file << ss.str();  // DEBUG
//    }  // DEBUG
//    file.close(); // DEBUG
    
    // turn connectivity[0] to reachability
    // vvv Warshall’s Algorithm $O(n^3)$ https://en.wikipedia.org/wiki/Reachability
    // https://cs.winona.edu/lin/cs440/ch08-2.pdf
    NDAT x1 = 0; // count 1's
    for(NCAT k=0; k<a_task->nK; k++) {
        x1 = 0;
        for(NCAT i=0; i<a_task->nK; i++) {
            for(NCAT j=0; j<a_task->nK; j++) {
                a_task->connectivities[0][i][j] = a_task->connectivities[0][i][j] || (a_task->connectivities[0][i][k] && a_task->connectivities[0][k][j]);
                x1+=a_task->connectivities[0][i][j];
            }
        }
    }// ^^^ Warshall’s Algorithm
//    std::ofstream file; // DEBUG
//    file.open("reachability.txt");  // DEBUG
//    for(NCAT i=0; i<a_task->n_connectivity_X[0]; i++) {  // DEBUG
//        std::stringstream ss;  // DEBUG
//        for(NDAT j=0; j<a_task->n_connectivity_Y[0]; j++) {  // DEBUG
//            ss << ((j>0)?" ":"") << std::to_string(a_task->connectivities[0][i][j]);  // DEBUG
//        }  // DEBUG
//        ss << "\n";  // DEBUG
//        file << ss.str();  // DEBUG
//    }  // DEBUG
//    file.close(); // DEBUG
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movb $0x2, 0x8d8(%rdi)
movl $0x2, %edi
movl $0x4, %esi
callq 0x2110
movq %rax, 0x8e0(%rbx)
movl 0x908(%rbx), %ebp
movl %ebp, (%rax)
movl 0x904(%rbx), %ecx
movl %ecx, 0x4(%rax)
movl $0x2, %edi
movl $0x4, %esi
callq 0x2110
movq %rax, 0x8e8(%rbx)
movl %ebp, (%rax)
movl %ebp, 0x4(%rax)
movl $0x2, %edi
movl $0x8, %esi
callq 0x2110
movq %rax, 0x8f0(%rbx)
movl %ebp, %edi
movl %ebp, %esi
xorl %edx, %edx
callq 0xa6a7
movq 0x8f0(%rbx), %rcx
movq %rax, (%rcx)
movq 0x8e0(%rbx), %rax
movq 0x8e8(%rbx), %rcx
movl 0x4(%rax), %edi
movl 0x4(%rcx), %esi
xorl %edx, %edx
callq 0xa6a7
movq 0x8f0(%rbx), %rcx
movq %rax, 0x8(%rcx)
cmpl $0x0, 0x8f8(%rbx)
jle 0x9641
xorl %r14d, %r14d
leaq 0x10(%rsp), %r15
leaq 0xf(%rsp), %r12
movb $0x1, %bpl
movq 0x890(%rbx), %rax
movslq (%rax,%r14,4), %r13
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
movq %r12, %rcx
callq 0x7797
movb 0xf(%rsp), %cl
cmpb $0x2, %cl
jl 0x9607
movb $0x1, %al
xorl %edi, %edi
leal 0x1(%rdi), %edx
cmpb %cl, %dl
jge 0x95f4
movsbq %al, %rsi
movsbq %dil, %rdi
movq 0x8f0(%rbx), %rcx
movq (%rcx), %rcx
movq 0x10(%rsp), %r8
movslq (%r8,%rdi,4), %r9
movq (%rcx,%r9,8), %rcx
movslq (%r8,%rsi,4), %r8
movb %bpl, (%rcx,%r8)
movq 0x8f0(%rbx), %rcx
movq (%rcx), %rcx
movq 0x10(%rsp), %r8
movslq (%r8,%rsi,4), %r9
movq (%rcx,%r9,8), %rcx
movslq (%r8,%rdi,4), %r8
movb %bpl, (%rcx,%r8)
incq %rsi
movsbq 0xf(%rsp), %rcx
cmpq %rcx, %rsi
jl 0x95a8
movsbl %dl, %esi
movsbl %cl, %r8d
decl %r8d
incb %al
movl %edx, %edi
cmpl %esi, %r8d
jg 0x9599
testb %cl, %cl
jle 0x962e
xorl %eax, %eax
movq 0x8f0(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%r13,8), %rcx
movb $0x1, (%rcx,%rax)
incq %rax
movsbq 0xf(%rsp), %rcx
cmpq %rcx, %rax
jl 0x960d
incq %r14
movslq 0x8f8(%rbx), %rax
cmpq %rax, %r14
jl 0x9570
movl 0x908(%rbx), %edx
testl %edx, %edx
jle 0x96b3
xorl %eax, %eax
testl %edx, %edx
jle 0x96a8
xorl %ecx, %ecx
testl %edx, %edx
jle 0x969d
xorl %esi, %esi
movq 0x8f0(%rbx), %rdx
movq (%rdx), %r8
movq (%r8,%rcx,8), %rdx
movb $0x1, %dil
cmpb $0x0, (%rdx,%rsi)
jne 0x9686
cmpb $0x0, (%rdx,%rax)
je 0x9684
movq (%r8,%rax,8), %rdi
cmpb $0x0, (%rdi,%rsi)
setne %dil
jmp 0x9686
xorl %edi, %edi
movb %dil, (%rdx,%rsi)
incq %rsi
movl 0x908(%rbx), %edx
movslq %edx, %rdi
cmpq %rdi, %rsi
jl 0x9659
jmp 0x96a0
movslq %edx, %rdi
incq %rcx
cmpq %rdi, %rcx
jl 0x9653
incq %rax
movslq %edx, %rcx
cmpq %rcx, %rax
jl 0x964d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq