// File: /workspace/github_cpp_makefile_star2_O1/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 $0x188, %rsp            # imm = 0x188
movq %r9, 0xe0(%rsp)
movq %r8, 0xd8(%rsp)
movl %ecx, 0x88(%rsp)
movl %edx, 0x7c(%rsp)
movl %esi, %ebp
movq %rdi, %r15
xorl %eax, %eax
movq %rax, 0xf0(%rsp)
movl %eax, 0xf8(%rsp)
movq %rax, 0x100(%rsp)
movl %eax, 0x108(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x110(%rsp)
movupd %xmm0, 0x120(%rsp)
movq %rax, 0x130(%rsp)
movq %rax, 0x150(%rsp)
movupd %xmm0, 0x140(%rsp)
movq %rax, 0x160(%rsp)
movl %eax, 0x168(%rsp)
movupd %xmm0, 0x170(%rsp)
movq %rax, 0x180(%rsp)
movl 0x4(%rdi), %ecx
movl $0x1, %r14d
subl %ecx, %r14d
cmpl $0x3, %ecx
cmovael %eax, %r14d
leaq 0x28(%rdi), %rsi
testl %ebp, %ebp
je 0x2809
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0x3a34
movq %rbx, %rdi
callq 0x501c
movq %rax, 0x178(%rsp)
jmp 0x282e
leaq 0x100(%rsp), %rdi
movq (%rsi), %rax
movq %rax, -0x10(%rdi)
callq 0x3a34
leaq 0x120(%rsp), %rdi
movslq -0x30(%rdi), %rsi
callq 0x3a46
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movq $0x0, 0xd0(%rsp)
cmpb $0x0, 0x7c(%rsp)
je 0x2882
movslq (%r15), %rsi
leaq 0x50(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0xc0(%rsp), %rdi
callq 0x3700
movslq 0x30(%r15), %rdx
testq %rdx, %rdx
je 0x2882
movq 0x38(%r15), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20a0
cmpb $0x0, 0x88(%rsp)
je 0x28a8
leaq 0x58(%r15), %rbx
movq 0x58(%r15), %rsi
movq %rbx, %rdi
callq 0x4534
movslq 0xc(%r15), %rsi
movq %rbx, %rdi
callq 0x3734
xorpd %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
movapd %xmm0, (%rdi)
xorl %r13d, %r13d
movq %r13, 0x10(%rdi)
movq %r13, 0x20(%rsp)
movapd %xmm0, 0x10(%rsp)
movq %r13, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movslq (%r15), %rsi
callq 0x3776
cmpl $0x0, 0xc(%r15)
jle 0x2ebb
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0xe8(%rsp)
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movslq (%r15), %rsi
leaq 0x10(%rsp), %rdi
callq 0x3776
movslq (%r15), %rsi
leaq 0x30(%rsp), %rdi
callq 0x3804
movslq %r12d, %rax
testb %bpl, %bpl
je 0x2951
movq 0x20(%r15), %rcx
movslq 0x4(%rcx), %rsi
imulq %rax, %rsi
shlq $0x3, %rsi
addq 0x10(%rcx), %rsi
leaq 0x138(%rsp), %rdi
callq 0x3892
jmp 0x2972
movq 0x20(%r15), %rcx
movslq 0x4(%rcx), %rsi
imulq %rax, %rsi
shlq $0x3, %rsi
addq 0x10(%rcx), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x5e90
movq %r12, 0xa8(%rsp)
cmpl $0x0, 0x8(%r15)
jle 0x2c74
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %ebx, %ebx
movl $0x1, %r12d
testb %bpl, %bpl
je 0x29aa
leaq 0x138(%rsp), %rdi
callq 0x629e
jmp 0x29b7
leaq 0xf0(%rsp), %rdi
callq 0x5f1e
movsd %xmm0, 0xb8(%rsp)
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x2c47
cmpl (%r15), %eax
jge 0x2c47
movsd 0xb8(%rsp), %xmm0
ucomisd 0x562a(%rip), %xmm0     # 0x8010
jne 0x2a4b
jp 0x2a4b
movq 0x50(%rsp), %rax
cmpq %rax, 0x58(%rsp)
je 0x29fb
movq %rax, 0x58(%rsp)
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x2a0c
movq %rax, 0x38(%rsp)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x2a1d
movq %rax, 0x18(%rsp)
movq $0x0, 0x70(%rsp)
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x2b14
movq $0x0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x2b23
movsd 0xe8(%rsp), %xmm1
callq 0x2270
movsd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x90(%rsp)
testl %ebx, %ebx
je 0x2a7d
movsd 0xb0(%rsp), %xmm1
movaps %xmm1, 0x90(%rsp)
movsd 0x18(%r15), %xmm1
ucomisd 0x5585(%rip), %xmm1     # 0x8010
jbe 0x2ae9
movsd (%rsp), %xmm2
movl (%r15), %eax
addl %ebx, %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
addsd %xmm0, %xmm2
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
movapd 0x90(%rsp), %xmm4
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
divsd %xmm2, %xmm0
unpcklpd %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0]
divpd %xmm2, %xmm4
movapd %xmm4, %xmm2
unpckhpd %xmm4, %xmm2            # xmm2 = xmm2[1],xmm4[1]
subpd %xmm2, %xmm4
cmpnltpd %xmm1, %xmm4
cmpnltpd %xmm1, %xmm0
orpd %xmm4, %xmm0
movd %xmm0, %eax
testb $0x1, %al
je 0x2c1b
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x2b9b
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x2bad
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x47b4
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x2b40
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x2b4f
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x3f78
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x70(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2b78
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x2b87
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x47b4
xorl %eax, %eax
movsd 0x5477(%rip), %xmm0     # 0x8008
movsd %xmm0, (%rsp)
jmp 0x2c2d
leaq 0x50(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x488c
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x2bca
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x2bd9
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x3f78
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2bfa
movsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x2c09
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x488c
movsd (%rsp), %xmm0
addsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rsp)
movb $0x1, %al
movapd 0x90(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
testb $0x1, %al
je 0x2c7d
decl %ebx
leal 0x1(%r12), %eax
cmpl 0x8(%r15), %r12d
movl %eax, %r12d
jl 0x2996
jmp 0x2c7d
leal -0x1(%r12), %ecx
movq 0x9385(%rip), %rdx      # 0xbfd8
movq (%rdx), %rdi
leaq 0x5403(%rip), %rsi      # 0x8060
movq 0xa8(%rsp), %rdx
movl %eax, %r8d
xorl %eax, %eax
callq 0x21f0
jmp 0x29d5
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, %rbx
subq %rax, %rbx
xorl %edx, %edx
testb %bpl, %bpl
je 0x2c9b
movl 0x138(%rsp), %edx
movl %edx, 0x8c(%rsp)
sarq $0x3, %rbx
cmpq %rax, %rcx
movsd (%rsp), %xmm1
je 0x2cca
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
movl %ecx, %edx
cmpq %rdx, %rbx
ja 0x2cb2
movl %r14d, 0x90(%rsp)
movq %r13, (%rsp)
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
je 0x2d0e
sarq $0x3, %rdx
xorl %esi, %esi
movsd 0x80(%rsp), %xmm1
movsd (%rcx,%rsi,8), %xmm0
mulsd (%rax,%rsi,8), %xmm0
addsd %xmm0, %xmm1
incq %rsi
movl %esi, %edi
cmpq %rdi, %rdx
ja 0x2cf4
jmp 0x2d17
movsd 0x80(%rsp), %xmm1
movsd %xmm1, 0x80(%rsp)
cmpb $0x0, 0x7c(%rsp)
je 0x2de4
movq 0x58(%rsp), %rax
cmpq 0x50(%rsp), %rax
je 0x2de4
xorl %r12d, %r12d
movslq 0xa8(%rsp), %r13
movq 0x30(%rsp), %rax
movslq (%rax,%r12,4), %r14
movq 0x10(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x4(%r15), %xmm1
callq 0x2270
movq 0xc0(%rsp), %rax
movsd (%rax,%r14,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax,%r14,8)
movslq 0x10(%r15), %rax
testq %rax, %rax
jle 0x2dca
movq 0x20(%r15), %rsi
movslq 0x4(%rsi), %rcx
imulq %r13, %rcx
movslq 0x2c(%r15), %rdx
imulq %r14, %rdx
shlq $0x3, %rdx
addq 0x38(%r15), %rdx
shlq $0x3, %rcx
addq 0x10(%rsi), %rcx
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rdx,%rsi,8), %xmm1
movsd %xmm1, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x2daf
incl %r12d
movq 0x58(%rsp), %rax
subq 0x50(%rsp), %rax
sarq $0x3, %rax
cmpq %r12, %rax
ja 0x2d46
movq 0x50(%rsp), %rax
cmpq %rax, 0x58(%rsp)
je 0x2df5
movq %rax, 0x58(%rsp)
cmpb $0x0, 0x88(%rsp)
movq (%rsp), %r13
movl 0x90(%rsp), %r14d
movq 0xa8(%rsp), %r12
je 0x2e7a
movq 0x58(%r15), %rax
leaq (%r12,%r12,2), %rcx
shlq $0x4, %rcx
movq 0x20(%rsp), %rdx
movups (%rax,%rcx), %xmm0
movaps 0x10(%rsp), %xmm1
movaps %xmm0, 0x10(%rsp)
movq 0x10(%rax,%rcx), %rsi
movq %rsi, 0x20(%rsp)
movups %xmm1, (%rax,%rcx)
movq %rdx, 0x10(%rax,%rcx)
movq 0x40(%rsp), %rdx
movq 0x28(%rax,%rcx), %rsi
movq %rsi, 0x40(%rsp)
movapd 0x30(%rsp), %xmm0
movupd 0x18(%rax,%rcx), %xmm1
movapd %xmm1, 0x30(%rsp)
movupd %xmm0, 0x18(%rax,%rcx)
movq %rdx, 0x28(%rax,%rcx)
movl 0x8c(%rsp), %ecx
jmp 0x2ea3
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x2e8b
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
movl 0x8c(%rsp), %ecx
je 0x2ea3
movq %rax, 0x38(%rsp)
addl %ebx, %r14d
addl %ecx, %r13d
incq %r12
movslq 0xc(%r15), %rax
cmpq %rax, %r12
jl 0x290c
jmp 0x2ecb
xorpd %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
xorl %r14d, %r14d
movl %r14d, 0x90(%rsp)
movq %r13, (%rsp)
cmpb $0x0, 0x7c(%rsp)
je 0x2fba
movslq (%r15), %rax
movq %rax, 0xb0(%rsp)
testq %rax, %rax
jle 0x2fba
movq 0xc0(%rsp), %rcx
movl 0x10(%r15), %edx
movq 0x38(%r15), %rsi
movslq 0x2c(%r15), %rdi
shlq $0x3, %rdi
xorl %r8d, %r8d
xorpd %xmm0, %xmm0
leaq 0x170(%rsp), %r9
leaq 0x110(%rsp), %r10
leaq 0x164(%rsp), %r11
leaq 0x104(%rsp), %rax
movq %rsi, %r14
xorl %r15d, %r15d
movsd (%rcx,%r15,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2f82
jp 0x2f82
testl %edx, %edx
jle 0x2fa3
xorl %r12d, %r12d
testb %bpl, %bpl
movq %r10, %r13
cmovneq %r9, %r13
movq %rax, %rbx
cmovneq %r11, %rbx
movslq (%rbx), %rbx
imulq %r8, %rbx
addq (%r13), %rbx
movsd (%rbx,%r12,8), %xmm1
movsd %xmm1, (%rsi,%r12,8)
incq %r12
cmpq %r12, %rdx
jne 0x2f50
jmp 0x2fa3
testl %edx, %edx
jle 0x2fa3
xorl %r12d, %r12d
movsd (%r14,%r12,8), %xmm1
divsd (%rcx,%r15,8), %xmm1
movsd %xmm1, (%r14,%r12,8)
incq %r12
cmpq %r12, %rdx
jne 0x2f89
incq %r15
addq %rdi, %r14
addq %rdi, %rsi
addq $0x8, %r8
cmpq 0xb0(%rsp), %r15
jne 0x2f3b
movq 0xd8(%rsp), %rax
testq %rax, %rax
je 0x2fd0
movl 0x90(%rsp), %ecx
movl %ecx, (%rax)
movq 0xe0(%rsp), %rcx
testq %rcx, %rcx
sete %al
xorb $0x1, %bpl
orb %al, %bpl
movq (%rsp), %rax
jne 0x2fed
movl %eax, (%rcx)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2ffc
callq 0x2130
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x300b
callq 0x2130
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x301a
callq 0x2130
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x302c
callq 0x2130
leaq 0x138(%rsp), %rdi
callq 0x38e4
movq 0x120(%rsp), %rdi
testq %rdi, %rdi
je 0x304b
callq 0x2130
cmpl $0x0, 0x108(%rsp)
je 0x3062
movq 0x110(%rsp), %rdi
callq 0x2190
movsd 0x80(%rsp), %xmm0
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x307f
movq %rax, %rbx
jmp 0x30c9
jmp 0x3099
movq %rax, %rbx
jmp 0x30db
jmp 0x3099
jmp 0x3099
jmp 0x3099
jmp 0x3099
jmp 0x3099
jmp 0x3099
jmp 0x3099
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x30ab
callq 0x2130
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x30ba
callq 0x2130
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x30c9
callq 0x2130
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x30db
callq 0x2130
leaq 0x138(%rsp), %rdi
callq 0x38e4
leaq 0xf0(%rsp), %rdi
callq 0x3934
movq %rbx, %rdi
callq 0x2280
nop