// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movb $0x2, 0x8d8(%rax)
movq -0x8(%rbp), %rax
movsbq 0x8d8(%rax), %rdi
movl $0x4, %esi
callq 0x3170
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8e0(%rax)
movq -0x8(%rbp), %rax
movl 0x908(%rax), %ecx
movq -0x8(%rbp), %rax
movq 0x8e0(%rax), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movl 0x904(%rax), %ecx
movq -0x8(%rbp), %rax
movq 0x8e0(%rax), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movsbq 0x8d8(%rax), %rdi
movl $0x4, %esi
callq 0x3170
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8e8(%rax)
movq -0x8(%rbp), %rax
movl 0x908(%rax), %ecx
movq -0x8(%rbp), %rax
movq 0x8e8(%rax), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movl 0x908(%rax), %ecx
movq -0x8(%rbp), %rax
movq 0x8e8(%rax), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movsbq 0x8d8(%rax), %rdi
movl $0x8, %esi
callq 0x3170
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8f0(%rax)
movq -0x8(%rbp), %rax
movq 0x8e0(%rax), %rax
movl (%rax), %edi
movq -0x8(%rbp), %rax
movq 0x8e8(%rax), %rax
movl (%rax), %esi
xorl %edx, %edx
callq 0xf5b0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x8e0(%rax), %rax
movl 0x4(%rax), %edi
movq -0x8(%rbp), %rax
movq 0x8e8(%rax), %rax
movl 0x4(%rax), %esi
xorl %edx, %edx
callq 0xf5b0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8f8(%rcx), %eax
jge 0xd98a
movq -0x8(%rbp), %rax
movq 0x890(%rax), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
leaq -0x18(%rbp), %rdx
leaq -0x19(%rbp), %rcx
callq 0x9ce0
movsbl -0x19(%rbp), %eax
cmpl $0x1, %eax
jle 0xd940
movb $0x0, -0x1a(%rbp)
movsbl -0x1a(%rbp), %eax
movsbl -0x19(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xd93e
movsbl -0x1a(%rbp), %eax
addl $0x1, %eax
movb %al, -0x1b(%rbp)
movsbl -0x1b(%rbp), %eax
movsbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jge 0xd92f
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movsbq -0x1a(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movsbq -0x1b(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movb $0x1, (%rax,%rcx)
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movsbq -0x1b(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movsbq -0x1a(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movb $0x1, (%rax,%rcx)
movb -0x1b(%rbp), %al
addb $0x1, %al
movb %al, -0x1b(%rbp)
jmp 0xd8b9
jmp 0xd931
movb -0x1a(%rbp), %al
addb $0x1, %al
movb %al, -0x1a(%rbp)
jmp 0xd89c
jmp 0xd940
movb $0x0, -0x1c(%rbp)
movsbl -0x1c(%rbp), %eax
movsbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jge 0xd97a
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x1c(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movb -0x1c(%rbp), %al
addb $0x1, %al
movb %al, -0x1c(%rbp)
jmp 0xd944
jmp 0xd97c
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xd84f
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x908(%rcx), %eax
jge 0xdacf
movl $0x0, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x908(%rcx), %eax
jge 0xdabf
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x908(%rcx), %eax
jge 0xdaaf
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movslq -0x2c(%rbp), %rdx
movb $0x1, %al
cmpb $0x0, (%rcx,%rdx)
movb %al, -0x2d(%rbp)
jne 0xda5a
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movslq -0x24(%rbp), %rdx
xorl %eax, %eax
cmpb $0x0, (%rcx,%rdx)
movb %al, -0x2e(%rbp)
je 0xda54
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq (%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x2c(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
setne %al
movb %al, -0x2e(%rbp)
movb -0x2e(%rbp), %al
movb %al, -0x2d(%rbp)
movb -0x2d(%rbp), %dl
andb $0x1, %dl
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x2c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x8(%rbp), %rax
movq 0x8f0(%rax), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xd9d3
jmp 0xdab1
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xd9b9
jmp 0xdac1
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xd998
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)