// File: /workspace/github_cpp_makefile_star2_O1/sharmaeklavya2[P]ctmeans/src/lib/ctmeans.cpp
// Function: CTMeans::cluster(bool, int, int, double, _IO_FILE*, int, bool)
double CTMeans::cluster(bool use_kd, int reps, int max_epochs, double obj_tol, FILE* fp, int epoch_interval, bool use_cr) {
    double minobj = std::numeric_limits<double>::infinity();
    std::minstd_rand reng(rand());

    int chars_to_erase = 0;
    char eol = (use_cr?'\r':'\n');
    const char space_str_size = 80;
    char space_str[space_str_size];
    for(int i=0; i<space_str_size; ++i) {
        space_str[i] = ' ';
    }
    space_str[space_str_size-1] = '\0';

    for(int repi=1; repi <= reps; ++repi) {
        init_centroids_rand(reng);
        SigC.push_back(std::vector<int>());
        if(use_kd) {
            P.push_back(std::vector<int>());
        }
        double obj = std::numeric_limits<double>::infinity();
        int epochi;
        for(epochi=0; epochi < max_epochs; ++epochi) {
            double old_obj = obj;
            int sigc;
            int heap_ops;
            obj = step(use_kd, true, false, &sigc, &heap_ops);
            if(use_kd) {
                P.back().push_back(heap_ops);
            }
            SigC.back().push_back(sigc);
            if(fp != nullptr && epochi % epoch_interval == 0) {
                if(use_cr) {
                    space_str[chars_to_erase] = '\0';
                    fprintf(fp, "%s\r", space_str);
                    space_str[chars_to_erase] = ' ';
                }
                chars_to_erase = fprintf(fp, "ctmeans %d-%d: %lg%c", repi, epochi+1, obj, eol);
            }
            if((old_obj - obj) < old_obj * obj_tol) {break;}
        }
        if(fp != nullptr && !use_cr) {
            fprintf(fp, "\n");
        }
        if(obj < minobj) {
            minobj = obj;
            std::swap(minC, C);
        }
    }
    if(fp != nullptr && use_cr) {
        fprintf(fp, "\n");
    }
    C = minC;
    minobj = step(use_kd, false, true);
    return minobj;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %r9d, %r12d
movq %r8, %r14
movsd %xmm0, 0x70(%rsp)
movl %ecx, %r13d
movl %edx, %r15d
movl %esi, %ebp
movq %rdi, %rbx
callq 0x2040
movslq %eax, %rcx
movabsq $0x200000005, %rdx      # imm = 0x200000005
movq %rcx, %rax
mulq %rdx
movq %rcx, %rax
subq %rdx, %rax
shrq %rax
addq %rdx, %rax
shrq $0x1e, %rax
movq %rax, %rdx
shlq $0x1f, %rdx
subq %rdx, %rax
leaq (%rcx,%rax), %rdx
cmpq $0x1, %rdx
adcq %rcx, %rax
movq %rax, 0x78(%rsp)
movapd 0x4eb0(%rip), %xmm0     # 0x8020
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movb $0x0, 0xcf(%rsp)
movl %ebp, 0x18(%rsp)
movzbl %bpl, %ebp
movl %r15d, 0x48(%rsp)
testl %r15d, %r15d
jle 0x3482
leaq 0x70(%rbx), %rax
movq %rax, 0x68(%rsp)
leaq 0x88(%rbx), %rax
movq %rax, 0x60(%rsp)
testq %r14, %r14
sete %al
orb 0x120(%rsp), %al
movb %al, 0x7(%rsp)
movl $0x1, %eax
movq %rax, 0x40(%rsp)
movsd 0x4e25(%rip), %xmm0     # 0x8018
movsd %xmm0, 0x50(%rsp)
movl $0x0, 0x14(%rsp)
movl %r13d, 0x4c(%rsp)
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
callq 0x3958
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x4964
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3244
callq 0x2130
cmpb $0x0, 0x18(%rsp)
je 0x327c
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x4964
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x327c
callq 0x2130
testl %r13d, %r13d
jle 0x3405
movl $0x1, %r15d
movsd 0x4d85(%rip), %xmm0     # 0x8018
movsd %xmm0, 0x58(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
movl $0x1, %edx
xorl %ecx, %ecx
leaq 0x20(%rsp), %r8
leaq 0x1c(%rsp), %r9
callq 0x272c
movapd %xmm0, %xmm1
cmpb $0x0, 0x18(%rsp)
je 0x32fa
movq 0x90(%rbx), %rdi
movq -0x10(%rdi), %rsi
cmpq -0x8(%rdi), %rsi
je 0x32e0
movl 0x1c(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x10(%rdi)
jmp 0x32fa
addq $-0x18, %rdi
leaq 0x1c(%rsp), %rdx
movsd %xmm1, 0x8(%rsp)
callq 0x3f78
movsd 0x8(%rsp), %xmm1
movq 0x78(%rbx), %rdi
movq -0x10(%rdi), %rsi
cmpq -0x8(%rdi), %rsi
je 0x3318
movl 0x20(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x10(%rdi)
jmp 0x3332
addq $-0x18, %rdi
leaq 0x20(%rsp), %rdx
movsd %xmm1, 0x8(%rsp)
callq 0x3f78
movsd 0x8(%rsp), %xmm1
testq %r14, %r14
je 0x33d3
leal -0x1(%r15), %eax
cltd
idivl %r12d
testl %edx, %edx
jne 0x33d3
movl $0xa, %r8d
cmpb $0x0, 0x120(%rsp)
movsd %xmm1, 0x8(%rsp)
je 0x33ac
movl %r12d, %r13d
movl %ebp, %r12d
movslq 0x14(%rsp), %rbp
movb $0x0, 0x80(%rsp,%rbp)
movq %r14, %rdi
leaq 0x4cfe(%rip), %rsi      # 0x807c
leaq 0x80(%rsp), %rdx
xorl %eax, %eax
callq 0x21f0
movsd 0x8(%rsp), %xmm1
movb $0x20, 0x80(%rsp,%rbp)
movl %r12d, %ebp
movl %r13d, %r12d
movl 0x4c(%rsp), %r13d
movl $0xd, %r8d
movq %r14, %rdi
leaq 0x4cca(%rip), %rsi      # 0x8080
movq 0x40(%rsp), %rdx
movl %r15d, %ecx
movapd %xmm1, %xmm0
movb $0x1, %al
callq 0x21f0
movsd 0x8(%rsp), %xmm1
movl %eax, 0x14(%rsp)
movsd 0x58(%rsp), %xmm2
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
mulsd 0x70(%rsp), %xmm2
ucomisd %xmm0, %xmm2
ja 0x340d
leal 0x1(%r15), %eax
movsd %xmm1, 0x58(%rsp)
cmpl %r13d, %r15d
movl %eax, %r15d
jl 0x3299
jmp 0x340d
movsd 0x4c0b(%rip), %xmm1     # 0x8018
cmpb $0x0, 0x7(%rsp)
jne 0x342d
movl $0xa, %edi
movq %r14, %rsi
movsd %xmm1, 0x8(%rsp)
callq 0x21c0
movsd 0x8(%rsp), %xmm1
movsd 0x50(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x346b
movq 0x40(%rbx), %rax
movq 0x28(%rbx), %rcx
movq %rcx, 0x40(%rbx)
movq %rax, 0x28(%rbx)
movl 0x48(%rbx), %eax
movl 0x30(%rbx), %ecx
movl %ecx, 0x48(%rbx)
movl %eax, 0x30(%rbx)
movq 0x50(%rbx), %rax
movq 0x38(%rbx), %rcx
movq %rcx, 0x50(%rbx)
movq %rax, 0x38(%rbx)
movsd %xmm1, 0x50(%rsp)
movq 0x40(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl 0x48(%rsp), %ecx
movq %rax, 0x40(%rsp)
jne 0x3206
testq %r14, %r14
setne %al
testb %al, 0x120(%rsp)
je 0x349e
movl $0xa, %edi
movq %r14, %rsi
callq 0x21c0
leaq 0x40(%rbx), %rsi
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
callq 0x5b82
movq (%r14), %rax
movq 0x28(%rbx), %rcx
movq %rax, 0x28(%rbx)
movq %rcx, (%r14)
movl 0x30(%rbx), %eax
movl 0x8(%r14), %ecx
movl %ecx, 0x30(%rbx)
movl %eax, 0x8(%r14)
movq 0x38(%rbx), %rdi
movq 0x10(%r14), %rcx
movq %rcx, 0x38(%rbx)
movq %rdi, 0x10(%r14)
testl %eax, %eax
je 0x34e7
callq 0x2190
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x272c
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3512
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3524
callq 0x2130
movq %rbx, %rdi
callq 0x2280