// File: /workspace/github_cpp_makefile_star2_O2/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 $0xf8, %rsp
movl %r9d, 0x28(%rsp)
movq %r8, %r14
movsd %xmm0, 0x78(%rsp)
movl %ecx, %r12d
movl %edx, %ebp
movl %esi, 0x10(%rsp)
movq %rdi, %rbx
callq 0x2040
cltq
xorl %ecx, %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %edx, %edx
divq %rsi
cmpq $0x1, %rdx
adcq $0x0, %rdx
movq %rdx, 0x88(%rsp)
cmpq $0x50, %rcx
je 0x2cd7
movb $0x20, 0x90(%rsp,%rcx)
incq %rcx
jmp 0x2cc4
leaq 0x90(%rsp), %rax
movb $0x0, 0x4f(%rax)
leaq 0x70(%rbx), %rax
movq %rax, 0x70(%rsp)
leaq 0x88(%rbx), %rax
movq %rax, 0x68(%rsp)
testq %r14, %r14
sete %al
orb 0x130(%rsp), %al
movb %al, 0x7(%rsp)
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
leaq 0x40(%rbx), %rcx
movq %rcx, 0x30(%rsp)
testl %ebp, %ebp
cmovlel %eax, %ebp
leaq 0x28(%rbx), %rax
movq %rax, 0x60(%rsp)
incl %ebp
movl %ebp, 0x20(%rsp)
movsd 0x52e1(%rip), %xmm0     # 0x8018
pushq $0x1
popq %r15
leaq 0x40(%rsp), %rbp
movl 0x10(%rsp), %r13d
movzbl %r13b, %eax
movl %eax, 0x1c(%rsp)
movsd %xmm0, 0x38(%rsp)
movl $0x0, 0x14(%rsp)
movl %r12d, 0x24(%rsp)
cmpl 0x20(%rsp), %r15d
je 0x2f39
movq %rbx, %rdi
leaq 0x88(%rsp), %rsi
callq 0x3412
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq 0x70(%rsp), %rdi
movq %rbp, %rsi
callq 0x430a
movq %rbp, %rdi
callq 0x414e
testb %r13b, %r13b
je 0x2dca
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x430a
movq %rbp, %rdi
callq 0x414e
movl %r15d, 0x18(%rsp)
xorl %r15d, %r15d
movsd 0x523e(%rip), %xmm1     # 0x8018
cmpl %r15d, %r12d
je 0x2ee7
movsd %xmm1, 0x80(%rsp)
movq %rbx, %rdi
movl 0x1c(%rsp), %esi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
movq %rbp, %r8
leaq 0x2c(%rsp), %r9
callq 0x2454
movsd %xmm0, 0x8(%rsp)
testb %r13b, %r13b
je 0x2e25
movq 0x90(%rbx), %rdi
addq $-0x18, %rdi
leaq 0x2c(%rsp), %rsi
callq 0x3376
movq 0x78(%rbx), %rdi
addq $-0x18, %rdi
movq %rbp, %rsi
callq 0x3376
testq %r14, %r14
je 0x2e46
movl %r15d, %eax
cltd
idivl 0x28(%rsp)
testl %edx, %edx
je 0x2e4e
movsd 0x8(%rsp), %xmm1
jmp 0x2ec3
pushq $0xa
popq %r8
cmpb $0x0, 0x130(%rsp)
je 0x2e9a
movslq 0x14(%rsp), %r13
movb $0x0, 0x90(%rsp,%r13)
movq %r14, %rdi
leaq 0x51d8(%rip), %rsi      # 0x804c
leaq 0x90(%rsp), %rdx
xorl %eax, %eax
callq 0x2200
movb $0x20, 0x90(%rsp,%r13)
movl 0x24(%rsp), %r12d
movl 0x10(%rsp), %r13d
pushq $0xd
popq %r8
leal 0x1(%r15), %ecx
movq %r14, %rdi
leaq 0x51a8(%rip), %rsi      # 0x8050
movl 0x18(%rsp), %edx
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x2200
movsd 0x8(%rsp), %xmm1
movl %eax, 0x14(%rsp)
movsd 0x80(%rsp), %xmm2
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
mulsd 0x78(%rsp), %xmm2
incl %r15d
ucomisd %xmm0, %xmm2
jbe 0x2dda
cmpb $0x0, 0x7(%rsp)
movsd %xmm1, 0x8(%rsp)
jne 0x2f05
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x21d0
movsd 0x8(%rsp), %xmm1
movsd 0x38(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movl 0x18(%rsp), %r15d
jbe 0x2f31
movq 0x30(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x3ac2
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
incl %r15d
jmp 0x2d60
testq %r14, %r14
setne %al
testb %al, 0x130(%rsp)
je 0x2f53
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x21d0
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
movq 0x30(%rsp), %rsi
callq 0x5630
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), %rax
movq 0x10(%r14), %rcx
movq %rcx, 0x38(%rbx)
movq %rax, 0x10(%r14)
movq %r14, %rdi
callq 0x348c
pushq $0x1
popq %rcx
movq %rbx, %rdi
movl 0x1c(%rsp), %esi
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2454
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2fc7
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x414e
movq %rbx, %rdi
callq 0x2290