// File: /workspace/github_cpp_makefile_star2_O3/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, 0x54(%rsp)
movl %edx, 0x58(%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 0x27c6
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0x3a0a
movq %rbx, %rdi
callq 0x4fc8
movq %rax, 0x178(%rsp)
jmp 0x27eb
leaq 0x100(%rsp), %rdi
movq (%rsi), %rax
movq %rax, -0x10(%rdi)
callq 0x3a0a
leaq 0x120(%rsp), %rdi
movslq -0x30(%rdi), %rsi
callq 0x3a1c
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movq $0x0, 0xd0(%rsp)
cmpb $0x0, 0x58(%rsp)
movl 0x54(%rsp), %ebx
je 0x2843
movslq (%r15), %rsi
leaq 0x60(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0xc0(%rsp), %rdi
callq 0x36ce
movslq 0x30(%r15), %rdx
testq %rdx, %rdx
je 0x2843
movq 0x38(%r15), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20a0
testb %bl, %bl
je 0x2867
leaq 0x58(%r15), %rbx
movq 0x58(%r15), %rsi
movq %rbx, %rdi
callq 0x4504
movslq 0xc(%r15), %rsi
movq %rbx, %rdi
movl 0x54(%rsp), %ebx
callq 0x3702
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movapd %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movq %rax, 0x20(%rsp)
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x80(%rsp)
movq %rax, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movslq (%r15), %rsi
callq 0x3744
cmpl $0x0, 0xc(%r15)
jle 0x2e9c
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0xe8(%rsp)
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x80(%rsp)
movslq (%r15), %rsi
leaq 0x10(%rsp), %rdi
callq 0x3744
movslq (%r15), %rsi
leaq 0x30(%rsp), %rdi
callq 0x37d6
movq 0x20(%r15), %rax
movslq 0x4(%rax), %rcx
movslq %r12d, %rsi
imulq %rcx, %rsi
shlq $0x3, %rsi
addq 0x10(%rax), %rsi
testb %bpl, %bpl
je 0x2924
leaq 0x138(%rsp), %rdi
callq 0x3868
jmp 0x2931
leaq 0xf0(%rsp), %rdi
callq 0x5e38
movq %r12, 0xa8(%rsp)
movl %r14d, 0x5c(%rsp)
cmpl $0x0, 0x8(%r15)
jle 0x2b59
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
movapd 0x90(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
xorl %r14d, %r14d
testb %bpl, %bpl
je 0x297e
leaq 0x138(%rsp), %rdi
callq 0x6276
jmp 0x298b
leaq 0xf0(%rsp), %rdi
callq 0x5ec2
movsd %xmm0, 0xb8(%rsp)
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x2b25
cmpl (%r15), %eax
jge 0x2b25
ucomisd 0x565f(%rip), %xmm0     # 0x8010
jne 0x29b9
jnp 0x2b71
movsd 0xe8(%rsp), %xmm1
callq 0x2270
movsd %xmm0, 0x48(%rsp)
movl %r12d, %r13d
movapd %xmm0, 0x90(%rsp)
subl $0x1, %r13d
jb 0x29f0
movsd 0xb0(%rsp), %xmm1
movaps %xmm1, 0x90(%rsp)
movsd 0x18(%r15), %xmm1
ucomisd 0x5612(%rip), %xmm1     # 0x8010
jbe 0x2a56
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movapd %xmm0, %xmm3
divsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
jbe 0x2a56
addl (%r15), %r12d
xorps %xmm3, %xmm3
cvtsi2sd %r12d, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
movapd 0x90(%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 0x2b63
movq 0x68(%rsp), %rsi
cmpq 0x70(%rsp), %rsi
je 0x2a7a
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x68(%rsp)
jmp 0x2a8c
leaq 0x60(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x485c
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x2aa9
movl 0x4(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x2ab8
leaq 0x30(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x3f44
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2ad9
movsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x2aee
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x485c
movsd 0x48(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
incl %r14d
movl %r13d, %r12d
movapd 0x90(%rsp), %xmm1
movsd %xmm1, 0xb0(%rsp)
cmpl 0x8(%r15), %r14d
jl 0x296a
jmp 0x2c5f
movq 0x94ac(%rip), %rcx      # 0xbfd8
movq (%rcx), %rdi
leaq 0x552a(%rip), %rsi      # 0x8060
movq 0xa8(%rsp), %rdx
movl %r14d, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x21f0
movsd 0xb8(%rsp), %xmm0
jmp 0x29a9
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movapd 0x90(%rsp), %xmm1
jmp 0x2c5f
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %rax
cmpq %rcx, %rax
je 0x2b88
movq %rcx, 0x68(%rsp)
movq %rcx, %rax
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rsi
cmpq %rcx, %rsi
movabsq $0x3ff0000000000000, %r14 # imm = 0x3FF0000000000000
je 0x2ba9
movq %rcx, 0x38(%rsp)
movq %rcx, %rsi
movq 0x10(%rsp), %rcx
cmpq %rcx, 0x18(%rsp)
je 0x2bba
movq %rcx, 0x18(%rsp)
movq $0x0, 0x48(%rsp)
cmpq 0x70(%rsp), %rax
je 0x2bdc
movq $0x0, (%rax)
addq $0x8, %rax
movq %rax, 0x68(%rsp)
jmp 0x2bf3
leaq 0x60(%rsp), %rdi
movq %rax, %rsi
leaq 0x48(%rsp), %rdx
callq 0x4784
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x2c0b
movl 0x4(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x2c1a
leaq 0x30(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x3f44
movq %r14, 0x48(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2c39
movq %r14, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x2c48
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x4784
movsd 0xb0(%rsp), %xmm1
movsd 0x53af(%rip), %xmm0     # 0x8008
movsd %xmm0, 0x8(%rsp)
movapd %xmm1, 0x90(%rsp)
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rax, %r14
subq %rcx, %r14
xorl %r9d, %r9d
testb %bpl, %bpl
je 0x2c88
movl 0x138(%rsp), %r9d
sarq $0x3, %r14
cmpq %rcx, %rax
movq 0xa8(%rsp), %r12
movsd 0x8(%rsp), %xmm1
je 0x2cb9
xorl %eax, %eax
movsd (%rcx,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rax
movl %eax, %edx
cmpq %rdx, %r14
ja 0x2ca1
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rdx
movq %rdx, %rsi
subq %rax, %rsi
je 0x2dd5
sarq $0x3, %rsi
xorl %edi, %edi
movsd 0x88(%rsp), %xmm1
movsd (%rax,%rdi,8), %xmm0
mulsd (%rcx,%rdi,8), %xmm0
addsd %xmm0, %xmm1
incq %rdi
movl %edi, %r8d
cmpq %r8, %rsi
ja 0x2cde
movsd %xmm1, 0x88(%rsp)
cmpq %rax, %rdx
sete %cl
movl 0x58(%rsp), %esi
xorb $0x1, %sil
orb %cl, %sil
jne 0x2dd5
movl %r9d, 0x8(%rsp)
xorl %r13d, %r13d
movslq %r12d, %r12
movq 0x30(%rsp), %rax
movslq (%rax,%r13,4), %rbx
movq 0x10(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x4(%r15), %xmm1
callq 0x2270
movq 0xc0(%rsp), %rax
movsd (%rax,%rbx,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rbx,8)
movslq 0x10(%r15), %rax
testq %rax, %rax
jle 0x2da4
movq 0x20(%r15), %rsi
movslq 0x4(%rsi), %rcx
imulq %r12, %rcx
movslq 0x2c(%r15), %rdx
imulq %rbx, %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 0x2d89
incl %r13d
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
ja 0x2d22
movl 0x54(%rsp), %ebx
movq 0xa8(%rsp), %r12
movl 0x8(%rsp), %r9d
cmpq %rax, %rdx
je 0x2ddf
movq %rax, 0x68(%rsp)
testb %bl, %bl
je 0x2e4b
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)
movq 0x80(%rsp), %rcx
jmp 0x2e75
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x2e5c
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
movq 0x80(%rsp), %rcx
je 0x2e75
movq %rax, 0x38(%rsp)
movl 0x5c(%rsp), %eax
addl %r14d, %eax
movl %eax, %r14d
addl %r9d, %ecx
movq %rcx, 0x80(%rsp)
incq %r12
movslq 0xc(%r15), %rax
cmpq %rax, %r12
jl 0x28df
jmp 0x2eac
xorpd %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
xorl %r14d, %r14d
movl %r14d, 0x5c(%rsp)
cmpb $0x0, 0x58(%rsp)
je 0x2f83
movslq (%r15), %rax
testq %rax, %rax
jle 0x2f83
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), %rbx
xorl %r14d, %r14d
movsd (%rcx,%r14,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2f53
jp 0x2f53
testl %edx, %edx
jle 0x2f74
xorl %r15d, %r15d
testb %bpl, %bpl
movq %r10, %r12
cmovneq %r9, %r12
movq %rbx, %r13
cmovneq %r11, %r13
movslq (%r13), %r13
imulq %r8, %r13
addq (%r12), %r13
movsd (%r13,%r15,8), %xmm1
movsd %xmm1, (%rsi,%r15,8)
incq %r15
cmpq %r15, %rdx
jne 0x2f1f
jmp 0x2f74
testl %edx, %edx
jle 0x2f74
xorl %r15d, %r15d
movsd (%rsi,%r15,8), %xmm1
divsd (%rcx,%r14,8), %xmm1
movsd %xmm1, (%rsi,%r15,8)
incq %r15
cmpq %r15, %rdx
jne 0x2f5a
incq %r14
addq %rdi, %rsi
addq $0x8, %r8
cmpq %rax, %r14
jne 0x2f0a
movq 0xd8(%rsp), %rax
testq %rax, %rax
je 0x2f96
movl 0x5c(%rsp), %ecx
movl %ecx, (%rax)
movq 0xe0(%rsp), %rcx
testq %rcx, %rcx
sete %al
xorb $0x1, %bpl
orb %al, %bpl
jne 0x2fb7
movq 0x80(%rsp), %rax
movl %eax, (%rcx)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2fc6
callq 0x2130
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2fd5
callq 0x2130
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x2fe4
callq 0x2130
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x2ff6
callq 0x2130
leaq 0x138(%rsp), %rdi
callq 0x38ba
movq 0x120(%rsp), %rdi
testq %rdi, %rdi
je 0x3015
callq 0x2130
cmpl $0x0, 0x108(%rsp)
je 0x302c
movq 0x110(%rsp), %rdi
callq 0x2190
movsd 0x88(%rsp), %xmm0
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3049
movq %rax, %rbx
jmp 0x3093
jmp 0x3063
jmp 0x3063
jmp 0x3063
jmp 0x3063
movq %rax, %rbx
jmp 0x30a5
jmp 0x3063
jmp 0x3063
jmp 0x3063
jmp 0x3063
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3075
callq 0x2130
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3084
callq 0x2130
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3093
callq 0x2130
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x30a5
callq 0x2130
leaq 0x138(%rsp), %rdi
callq 0x38ba
leaq 0xf0(%rsp), %rdi
callq 0x390a
movq %rbx, %rdi
callq 0x2280
nop