// File: /workspace/github_cpp_makefile_star2_O2/sharmaeklavya2[P]ctmeans/src/lib/ctmeans.cpp
// Function: CTMeans::step(bool, bool, bool, int*, int*)
double CTMeans::step(bool use_kd, bool update_centroids, bool store_u, int* p_sigc, int* p_heap_ops) {
    using std::vector;

    double obj = 0.0;
    int sigc = 0;
    NNIterSort nniter;
    NNIterKD nniter_kd;
    int heap_ops = 0;
    double mexp = 1/(1-m);

    if(use_kd) {
        nniter_kd.init(C);
    }
    else {
        nniter.init(C);
    }
    const Matrix& nniter_C(use_kd? nniter_kd.C: nniter.C);

    vector<double> usum;    // usum[j] = sum(U(i, j)^m for i in range(n))
    if(update_centroids) {
        usum.resize(c, 0);
        C.set_to_zero();
    }
    if(store_u) {
        U.clear();
        U.resize(n);
    }
    vector<double> norms, us;
        // norms[j] = norm of x from jth nearest centroid (norms.size() = t)
        // us[j] = membership value of x_i with c_j
    vector<int> indices;
        // indices[j] = index of jth nearest centroid (indices.size() = t)
    norms.reserve(c);
    for(int i=0; i<n; ++i) {
        us.reserve(c); indices.reserve(c);
        double norms2sum=0, norm12;
            // norms2sum = denominator of all U(i, j)
            // norm12 = norm2 of a point to the nearest cluster
        if(use_kd) {
            nniter_kd.set_point(X.begin_row(i));
        }
        else {
            nniter.set_point(X.begin_row(i));
        }

        // get the first t norms and corresponding membership values
        for(int j=0; j<max_t; ++j) {
            double norm; int index;
            std::pair<double, int> norm_index;
            if(use_kd) {
                norm_index = nniter_kd.get_neighbor();
            }
            else {
                norm_index = nniter.get_neighbor();
            }
            norm = norm_index.first;
            index = norm_index.second;

            if(index < 0 || index >= c) {
                fprintf(stderr, "step: i=%d, j=%d, index=%d\n", i, j, index);
            }
            if(norm == 0.0 || norm == -0.0) {
                norms.clear();
                indices.clear();
                us.clear();
                norms.push_back(0);
                indices.push_back(index);
                us.push_back(1);
                norms2sum = 1;
                // fprintf(stderr, "saw 0 norm\n");
                break;
            }
            double norm2 = std::pow(norm, mexp);
            if(j == 0) {norm12 = norm2;}
            if(eps_t > 0.0) {
                double den_u = norms2sum + norm2;
                double u_j = norm2 / den_u;
                double u_1 = norm12 / den_u;
                double v_1 = norm12 / (den_u + (c-j)*norm2);
                if((u_1 - v_1 < eps_t) && u_j < eps_t) {
                    break;
                }
            }
            norms.push_back(norm);
            indices.push_back(index);
            us.push_back(norm2);
            norms2sum += norm2;
        }
        /*
        if(i == 0) {
            for(int j=0; j<norms.size(); ++j) {
                fprintf(stderr, "%lg ", norms[j]);
            }
            fprintf(stderr, "\n");
        }
        */
        sigc += us.size();
        if(use_kd) {
            heap_ops += nniter_kd.get_heap_ops();
        }
        for(unsigned i2=0; i2 < us.size(); ++i2) {
            us[i2] /= norms2sum;
        }

        // calculate objective
        for(unsigned i2=0; i2<norms.size(); ++i2) {
            obj += norms[i2] * us[i2];
        }

        // update centroids
        if(update_centroids) {
            for(unsigned i2=0; i2<norms.size(); ++i2) {
                int j = indices[i2];
                double uij_m = std::pow(us[i2], m);
                usum[j] += uij_m;
                for(int k=0; k<d; ++k) {
                    C(j, k) += uij_m * X(i, k);
                }
            }
        }

        // store membership values
        norms.clear();
        if(store_u) {
            std::swap(us, U[i].first);
            std::swap(indices, U[i].second);
        }
        else {
            us.clear(); indices.clear();
        }
    }

    if(update_centroids) {
        for(int j=0; j<c; ++j) {
            if(usum[j] == 0) {
                for(int k=0; k<d; ++k) {
                    C(j, k) = nniter_C(j, k);
                }
            }
            else {
                for(int k=0; k<d; ++k) {
                    C(j, k) /= usum[j];
                }
            }
        }
    }

    if(p_sigc != nullptr) {
        *p_sigc = sigc;
    }
    if(use_kd) {
        if(p_heap_ops != nullptr) {
            *p_heap_ops = heap_ops;
        }
    }
    return obj;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r9, %r12
movq %r8, 0xd0(%rsp)
movl %ecx, 0x58(%rsp)
movl %edx, 0x4(%rsp)
andq $0x0, 0xe0(%rsp)
movl %esi, %ebp
andl $0x0, 0xe8(%rsp)
movq %rdi, %r15
andq $0x0, 0xf0(%rsp)
andl $0x0, 0xf8(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x100(%rsp)
andq $0x0, 0x120(%rsp)
movupd %xmm0, 0x110(%rsp)
andq $0x0, 0x140(%rsp)
movupd %xmm0, 0x130(%rsp)
andq $0x0, 0x150(%rsp)
andl $0x0, 0x158(%rsp)
movupd %xmm0, 0x160(%rsp)
andq $0x0, 0x170(%rsp)
movl 0x4(%rdi), %eax
pushq $0x1
popq %rcx
subl %eax, %ecx
xorl %r14d, %r14d
cmpl $0x3, %eax
cmovbl %ecx, %r14d
leaq 0x28(%rdi), %rsi
testl %ebp, %ebp
je 0x251c
leaq 0x128(%rsp), %rdi
callq 0x3138
jmp 0x2529
leaq 0xe0(%rsp), %rdi
callq 0x3160
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
andq $0x0, 0xc0(%rsp)
cmpb $0x0, 0x4(%rsp)
je 0x2577
movslq (%r15), %rsi
leaq 0x60(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0xb0(%rsp), %rdi
callq 0x3184
movslq 0x30(%r15), %rdx
testq %rdx, %rdx
je 0x2577
movq 0x38(%r15), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20b0
cmpb $0x0, 0x58(%rsp)
je 0x2596
leaq 0x58(%r15), %rbx
movq %rbx, %rdi
callq 0x31b8
movslq 0xc(%r15), %rsi
movq %rbx, %rdi
callq 0x31c0
movq %r12, 0xc8(%rsp)
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movapd %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movslq (%r15), %rsi
callq 0x31f6
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0xd8(%rsp)
leaq 0x30(%rsp), %r14
leaq 0x10(%rsp), %r13
xorl %ebx, %ebx
movl $0x0, 0x50(%rsp)
movl $0x0, 0x54(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
movslq 0xc(%r15), %rax
cmpq %rax, %rbx
jge 0x2a9b
movslq (%r15), %rsi
movq %r14, %rdi
callq 0x31f6
movslq (%r15), %rsi
movq %r13, %rdi
callq 0x3286
movq 0x20(%r15), %rax
movslq 0x4(%rax), %rcx
movslq %ebx, %rsi
imulq %rcx, %rsi
shlq $0x3, %rsi
addq 0x10(%rax), %rsi
testb %bpl, %bpl
movq %rbx, 0x90(%rsp)
je 0x265d
leaq 0x128(%rsp), %rdi
callq 0x3316
jmp 0x266a
leaq 0xe0(%rsp), %rdi
callq 0x58f4
xorpd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpl 0x8(%r15), %r12d
jge 0x288a
testb %bpl, %bpl
je 0x2697
leaq 0x128(%rsp), %rdi
callq 0x5d70
jmp 0x26a4
leaq 0xe0(%rsp), %rdi
callq 0x5978
movsd %xmm0, 0xa8(%rsp)
movl %eax, 0x5c(%rsp)
testl %eax, %eax
js 0x27c3
cmpl (%r15), %eax
jge 0x27c3
ucomisd 0x5946(%rip), %xmm0     # 0x8010
jne 0x26d2
jnp 0x27f7
movsd 0xd8(%rsp), %xmm1
callq 0x2280
movsd %xmm0, 0x8(%rsp)
movapd %xmm0, 0x80(%rsp)
testl %r12d, %r12d
je 0x2705
movsd 0xa0(%rsp), %xmm1
movaps %xmm1, 0x80(%rsp)
movsd 0x18(%r15), %xmm1
ucomisd 0x58fd(%rip), %xmm1     # 0x8010
jbe 0x2769
movsd 0x48(%rsp), %xmm2
addsd %xmm0, %xmm2
movapd %xmm0, %xmm3
divsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
jbe 0x2769
movl (%r15), %eax
addl %ebx, %eax
cvtsi2sd %eax, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
movapd 0x80(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
unpcklpd %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0]
divpd %xmm2, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
subsd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
ja 0x2878
leaq 0x60(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x3394
movq %r13, %rdi
leaq 0x5c(%rsp), %rsi
callq 0x3376
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x3394
movsd 0x48(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
incl %r12d
decl %ebx
movapd 0x80(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
jmp 0x2679
movq 0x980e(%rip), %rcx      # 0xbfd8
movq (%rcx), %rdi
leaq 0x585c(%rip), %rsi      # 0x8030
movq 0x90(%rsp), %rdx
movl %r12d, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x2200
movsd 0xa8(%rsp), %xmm0
jmp 0x26c2
movq 0x60(%rsp), %rax
cmpq %rax, 0x68(%rsp)
je 0x2808
movq %rax, 0x68(%rsp)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x2819
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x282a
movq %rax, 0x38(%rsp)
andq $0x0, 0x8(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x415c
movq %r13, %rdi
leaq 0x5c(%rsp), %rsi
callq 0x3376
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x415c
movsd 0x5798(%rip), %xmm0     # 0x8008
movsd %xmm0, 0x48(%rsp)
jmp 0x288a
movapd 0x80(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
xorl %ecx, %ecx
movl $0x0, %eax
testb %bpl, %bpl
je 0x289d
movl 0x128(%rsp), %eax
movl %eax, 0x80(%rsp)
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rax
subq %rdx, %rax
sarq $0x3, %rax
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x28d0
movsd (%rdx,%rcx,8), %xmm0
divsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incl %ecx
jmp 0x28b5
addl %eax, 0x54(%rsp)
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x3, %rsi
xorl %edi, %edi
movl %edi, %edi
cmpq %rdi, %rsi
jbe 0x2915
movsd (%rax,%rdi,8), %xmm0
mulsd (%rdx,%rdi,8), %xmm0
movsd 0x98(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x98(%rsp)
incl %edi
jmp 0x28ea
movq %r14, %r13
cmpb $0x0, 0x4(%rsp)
je 0x29dd
xorl %ebx, %ebx
movslq 0x90(%rsp), %r12
movl %ebx, %ebx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x29dd
movq 0x10(%rsp), %rax
movslq (%rax,%rbx,4), %r14
movq 0x30(%rsp), %rax
movsd (%rax,%rbx,8), %xmm0
cvtsi2sdl 0x4(%r15), %xmm1
callq 0x2280
movq 0xb0(%rsp), %rax
movapd %xmm0, %xmm1
addsd (%rax,%r14,8), %xmm1
movsd %xmm1, (%rax,%r14,8)
movl 0x10(%r15), %eax
movq 0x20(%r15), %rcx
movl 0x2c(%r15), %edx
imull %r14d, %edx
movslq %edx, %rdx
testl %eax, %eax
movl $0x0, %esi
cmovlel %esi, %eax
shlq $0x3, %rdx
addq 0x38(%r15), %rdx
xorl %esi, %esi
cmpq %rsi, %rax
je 0x29cc
movslq 0x4(%rcx), %rdi
imulq %r12, %rdi
shlq $0x3, %rdi
addq 0x10(%rcx), %rdi
movsd (%rdi,%rsi,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rdx,%rsi,8), %xmm1
movsd %xmm1, (%rdx,%rsi,8)
incq %rsi
jmp 0x299f
incl %ebx
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
jmp 0x292d
cmpq %rax, %rcx
je 0x29e7
movq %rax, 0x68(%rsp)
cmpb $0x0, 0x58(%rsp)
movq %r13, %r14
movq 0x90(%rsp), %rbx
je 0x2a61
movq 0x58(%r15), %rax
imulq $0x30, %rbx, %rcx
movq 0x40(%rsp), %rdx
movups (%rax,%rcx), %xmm0
movaps 0x30(%rsp), %xmm1
movaps %xmm0, 0x30(%rsp)
movq 0x10(%rax,%rcx), %rsi
movq %rsi, 0x40(%rsp)
movups %xmm1, (%rax,%rcx)
movq %rdx, 0x10(%rax,%rcx)
movq 0x20(%rsp), %rdx
movq 0x28(%rax,%rcx), %rsi
movq %rsi, 0x20(%rsp)
movapd 0x10(%rsp), %xmm0
movupd 0x18(%rax,%rcx), %xmm1
movapd %xmm1, 0x10(%rsp)
movupd %xmm0, 0x18(%rax,%rcx)
movq %rdx, 0x28(%rax,%rcx)
leaq 0x10(%rsp), %r13
movl 0x80(%rsp), %ecx
jmp 0x2a8f
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
leaq 0x10(%rsp), %r13
je 0x2a77
movq %rax, 0x38(%rsp)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
movl 0x80(%rsp), %ecx
je 0x2a8f
movq %rax, 0x18(%rsp)
addl %ecx, 0x50(%rsp)
incq %rbx
jmp 0x2607
cmpb $0x0, 0x4(%rsp)
movq 0xc8(%rsp), %r13
je 0x2b7f
movq 0xb0(%rsp), %rax
movl (%r15), %ecx
movl 0x10(%r15), %edx
xorl %esi, %esi
testl %edx, %edx
cmovlel %esi, %edx
testl %ecx, %ecx
cmovlel %esi, %ecx
xorpd %xmm0, %xmm0
leaq 0x160(%rsp), %rdi
leaq 0x100(%rsp), %r8
leaq 0x154(%rsp), %r9
leaq 0xf4(%rsp), %r10
cmpq %rcx, %rsi
je 0x2b7f
movsd (%rax,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
movq 0x38(%r15), %r11
movl 0x2c(%r15), %ebx
jne 0x2b50
jp 0x2b50
imull %esi, %ebx
movslq %ebx, %rbx
leaq (%r11,%rbx,8), %r11
xorl %ebx, %ebx
cmpq %rbx, %rdx
je 0x2b77
testb %bpl, %bpl
movq %r8, %r14
cmovneq %rdi, %r14
movq %r10, %r12
cmovneq %r9, %r12
movl (%r12), %r12d
imull %esi, %r12d
movslq %r12d, %r12
shlq $0x3, %r12
addq (%r14), %r12
movsd (%r12,%rbx,8), %xmm1
movsd %xmm1, (%r11,%rbx,8)
incq %rbx
jmp 0x2b17
imull %esi, %ebx
movslq %ebx, %rbx
leaq (%r11,%rbx,8), %r11
xorl %ebx, %ebx
cmpq %rbx, %rdx
je 0x2b77
movsd (%r11,%rbx,8), %xmm1
divsd (%rax,%rsi,8), %xmm1
movsd %xmm1, (%r11,%rbx,8)
incq %rbx
jmp 0x2b5c
incq %rsi
jmp 0x2aed
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x2b92
movl 0x54(%rsp), %ecx
movl %ecx, (%rax)
testq %r13, %r13
sete %al
xorb $0x1, %bpl
orb %al, %bpl
jne 0x2ba9
movl 0x50(%rsp), %eax
movl %eax, (%r13)
leaq 0x10(%rsp), %rdi
callq 0x414e
leaq 0x30(%rsp), %rdi
callq 0x3af4
leaq 0x60(%rsp), %rdi
callq 0x3af4
leaq 0xb0(%rsp), %rdi
callq 0x3af4
leaq 0x128(%rsp), %rdi
callq 0x33b6
leaq 0xe0(%rsp), %rdi
callq 0x33f8
movsd 0x98(%rsp), %xmm0
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c0b
movq %rax, %rbx
jmp 0x2c46
jmp 0x2c25
movq %rax, %rbx
jmp 0x2c53
jmp 0x2c25
jmp 0x2c25
jmp 0x2c25
jmp 0x2c25
jmp 0x2c25
jmp 0x2c25
jmp 0x2c25
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x414e
leaq 0x30(%rsp), %rdi
callq 0x3af4
leaq 0x60(%rsp), %rdi
callq 0x3af4
leaq 0xb0(%rsp), %rdi
callq 0x3af4
leaq 0x128(%rsp), %rdi
callq 0x33b6
leaq 0xe0(%rsp), %rdi
callq 0x33f8
movq %rbx, %rdi
callq 0x2290
nop