// File: /workspace/github_cpp_makefile_star2_O2/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
movabsq $0x100000000000000, %r13 # imm = 0x100000000000000
movb $0x2, 0x8d8(%rdi)
pushq $0x2
popq %r14
pushq $0x4
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3100
movq %rax, 0x8e0(%rbx)
movl 0x908(%rbx), %ebp
movl %ebp, (%rax)
movl 0x904(%rbx), %ecx
movl %ecx, 0x4(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3100
movq %rax, 0x8e8(%rbx)
movl %ebp, (%rax)
movl %ebp, 0x4(%rax)
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x3100
movq %rax, 0x8f0(%rbx)
xorl %r14d, %r14d
movl %ebp, %edi
movl %ebp, %esi
xorl %edx, %edx
callq 0x9467
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 0x9467
movq 0x8f0(%rbx), %rcx
movq %rax, 0x8(%rcx)
leaq 0xf(%rsp), %r12
movb $0x1, %bpl
movslq 0x8f8(%rbx), %rax
cmpq %rax, %r14
jge 0x86ba
movq 0x890(%rbx), %rax
movslq (%rax,%r14,4), %r15
movq %rbx, %rdi
movl %r14d, %esi
leaq 0x10(%rsp), %rdx
movq %r12, %rcx
callq 0x6a19
movb 0xf(%rsp), %al
cmpb $0x2, %al
jl 0x868b
movb $0x1, %cl
xorl %edx, %edx
movq %r13, %rsi
movzbl %dl, %edx
movsbl %al, %edi
decl %edi
cmpl %edx, %edi
jle 0x868b
movl %ecx, %edi
movq %rsi, %r8
cmpb %dil, %al
jle 0x8682
movq 0x8f0(%rbx), %rax
movq (%rax), %rax
movq 0x10(%rsp), %r9
leal (,%rdx,4), %r10d
movslq (%r9,%r10), %r11
movq (%rax,%r11,8), %rax
movq %r8, %r11
sarq $0x36, %r11
movslq (%r9,%r11), %r9
movb %bpl, (%rax,%r9)
movq 0x8f0(%rbx), %rax
movq (%rax), %rax
movq 0x10(%rsp), %r9
movslq (%r9,%r11), %r11
movq (%rax,%r11,8), %rax
movslq (%r9,%r10), %r9
movb %bpl, (%rax,%r9)
movb 0xf(%rsp), %al
addq %r13, %r8
incb %dil
jmp 0x8624
incb %dl
addq %r13, %rsi
incb %cl
jmp 0x8613
xorl %ecx, %ecx
movsbq %al, %rax
cmpq %rax, %rcx
jge 0x86b2
movq 0x8f0(%rbx), %rax
movq 0x8(%rax), %rax
movq (%rax,%r15,8), %rax
movb $0x1, (%rax,%rcx)
incq %rcx
movb 0xf(%rsp), %al
jmp 0x868d
incq %r14
jmp 0x85d6
movl 0x908(%rbx), %edx
xorl %eax, %eax
movslq %edx, %rcx
cmpq %rcx, %rax
jge 0x8724
xorl %ecx, %ecx
movslq %edx, %rsi
cmpq %rsi, %rcx
jge 0x871f
xorl %esi, %esi
movslq %edx, %rdi
cmpq %rdi, %rsi
jge 0x871a
movq 0x8f0(%rbx), %rdx
movq (%rdx), %r8
movq (%r8,%rcx,8), %rdx
movb $0x1, %dil
cmpb $0x0, (%rdx,%rsi)
jne 0x870b
cmpb $0x0, (%rdx,%rax)
je 0x8709
movq (%r8,%rax,8), %rdi
cmpb $0x0, (%rdi,%rsi)
setne %dil
jmp 0x870b
xorl %edi, %edi
movb %dil, (%rdx,%rsi)
incq %rsi
movl 0x908(%rbx), %edx
jmp 0x86d6
incq %rcx
jmp 0x86cc
incq %rax
jmp 0x86c2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop