// File: /workspace/github_cpp_makefile_star2_O3/sharmaeklavya2[P]ctmeans/src/main.cpp
// Function: main
int main(int argc, char* argv[]) {
    // parse command-line args

#ifdef DEBUG
    feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);
#endif
    int c, reps=0, max_epochs=0, max_t=0, seed=0;
    double eps_obj=0, eps_t=0;
    bool use_kd = true;
    const char* directory = nullptr;

    if(argc < 3 || argc > 10) {
        fprintf(stderr, "usage: %s\n", usage);
        return 2;
    }
    else {
        directory = argv[1];
        c = atoi(argv[2]);
        switch(argc) {
        case 10:
            seed = atoi(argv[9]);
        case 9:
            eps_t = atof(argv[8]);
        case 8:
            max_t = atoi(argv[7]);
        case 7:
            eps_obj = atof(argv[6]);
        case 6:
            max_epochs = atoi(argv[5]);
        case 5:
            use_kd = bool(atoi(argv[4]));
        case 4:
            reps = atoi(argv[3]);
        }

        if(reps <= 0) {reps = 1;}
        if(max_epochs <= 0) {max_epochs = 300;}
        if(eps_obj <= 0.0) {eps_obj = 0.0001;}
        if(max_t <= 0) {max_t = c;}
        if(eps_t <= 0.0) {eps_t = 0.0;}
        if(seed <= 0) {seed = int(time(nullptr));}
    }
    srand(seed);

    if(chdir(directory) != 0) {
        fprintf(stderr, "Couldn't chdir to %s\n", directory);
        perror("main");
        return 1;
    }

    FILE* fp = NULL;

    // store cmd args

    fp = fopen("args.json", "w");
    if(fp == nullptr) {
        fprintf(stderr, "Couldn't open args.json\n");
        perror("main");
    }
    else {
        string s = get_json_dict_template(
            " c,reps,use_kd,max_epochs,eps_obj,max_t, eps_t,seed",
            "%d,  %d,    %s,        %d, %.10lg,   %d,%.10lg,  %d");
        fprintf(fp, s.c_str(),
            c, reps, (use_kd? "true": "false"), max_epochs, eps_obj, max_t, eps_t, seed);
        fclose(fp);
    }

    // read input

    int n, d;

    fp = fopen("in.shape.txt", "r");
    if(fp == NULL) {
        fprintf(stderr, "Couldn't open in.shape.txt\n");
        perror("main");
        return 1;
    }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
    fscanf(fp, "%d%d", &n, &d);
#pragma GCC diagnostic pop
    fclose(fp);

    Matrix X(n, d);
    clock_t start_clock = clock();
    loadtxt("in.txt", X);
    clock_t end_clock = clock();
    double time_to_load_data = get_elapsed(start_clock, end_clock);
    printf("Time to load data: %lg\n", time_to_load_data);

    // cluster

    CTMeans model(X, c, 2, max_t, eps_t);
    start_clock = clock();
    double obj = model.cluster(use_kd, reps, max_epochs, eps_obj, stderr, 1, true);
    end_clock = clock();
    printf("Objective: %lg\n", obj);
    double time_to_cluster = get_elapsed(start_clock, end_clock);
    printf("Time to cluster: %lg\n", time_to_cluster);

    // write output

    start_clock = clock();
    // Matrix U(n, c, true);
    // model.get_dense_U(U);
    vector<int> labels;
    model.get_labels(labels);
    vector<double> flatu;
    model.get_flat_U(flatu);

    savetxt("centroids.txt", model.C);
    savetxt("labels.txt", labels.cbegin(), labels.cend());
    // savetxt("memberships.txt", U);
    savetxt("flatu.txt", flatu.cbegin(), flatu.cend());
    printf("flatu size: %zd, %lf, %lf\n", flatu.size(),
        double(flatu.size())/n, double(flatu.size())/n/c);
    end_clock = clock();

    double avg_sigC = output_vec_vec_to_file("sigc.txt", "SigC", n, c, model.SigC);
    printf("Average %s: %lf, %lf, %lf\n", "SigC", avg_sigC, avg_sigC/n, avg_sigC/n/c);
    double avg_heap_ops = 0;
    if(use_kd) {
        avg_heap_ops = output_vec_vec_to_file("heap_ops.txt", "heap_ops", n, c, model.P);
        printf("Average %s: %lf, %lf, %lf\n", "heap_ops", avg_heap_ops, avg_heap_ops/n, avg_heap_ops/n/c);
    }
    double time_to_save = get_elapsed(start_clock, end_clock);
    printf("Time to save: %lg\n", time_to_save);

    // write times

    fp = fopen("times.json", "w");
    if(fp == nullptr) {
        fprintf(stderr, "Couldn't open time.json\n");
        perror("main");
    }
    else {
        string s = get_json_dict_template(
            "load_data,cluster,  save",
            "   %.10lg, %.10lg,%.10lg");
        fprintf(fp, s.c_str(), time_to_load_data, time_to_cluster, time_to_save);
        fclose(fp);
    }

    // write stats

    fp = fopen("stats.json", "w");
    if(fp == nullptr) {
        fprintf(stderr, "Couldn't open stats.json\n");
        perror("main");
    }
    else {
        string s = get_json_dict_template(
            "objective,flatu_size,avg_sigC,avg_heap_ops",
            "   %.10lg,    %.10lg,  %.10lg,      %.10lg");
        fprintf(fp, s.c_str(), obj, double(flatu.size())/n, avg_sigC/n, avg_heap_ops/n);
        fclose(fp);
    }

    putchar('\n');
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movl %edi, %ebx
leal -0xb(%rbx), %eax
cmpl $-0x9, %eax
jbe 0x7434
movq %rsi, %r14
movq 0x8(%rsi), %r12
movq 0x10(%rsi), %rdi
callq 0x2230
movl %eax, %r15d
movb $0x1, 0x10(%rsp)
xorpd %xmm1, %xmm1
addl $-0x4, %ebx
cmpl $0x6, %ebx
ja 0x6e31
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r12, 0x40(%rsp)
movl %r15d, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x14f8(%rip), %rax      # 0x8210
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
xorl %eax, %eax
xorl %r13d, %r13d
xorps %xmm2, %xmm2
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %ebx, %ebx
jmpq *%rcx
movq 0x48(%r14), %rdi
callq 0x2230
movq %rax, 0x18(%rsp)
movq 0x40(%r14), %rdi
callq 0x21b0
movapd %xmm0, 0x20(%rsp)
movq 0x38(%r14), %rdi
callq 0x2230
movq 0x30(%r14), %rdi
movl %eax, %r13d
callq 0x21b0
movsd %xmm0, 0x10(%rsp)
movq 0x28(%r14), %rdi
callq 0x2230
movl %eax, %r15d
movsd 0x10(%rsp), %xmm2
movl %r13d, %r12d
movsd %xmm2, 0x8(%rsp)
movq 0x20(%r14), %rdi
callq 0x2230
testl %eax, %eax
setne 0x10(%rsp)
movsd 0x8(%rsp), %xmm1
movl %r12d, %ebp
movl %r15d, %ebx
movapd %xmm1, 0x30(%rsp)
movq 0x18(%r14), %rdi
callq 0x2230
cmpl $0x2, %eax
movl $0x1, %ecx
cmovgel %eax, %ecx
movl %ecx, 0x8(%rsp)
testl %ebx, %ebx
movl $0x12c, %r14d           # imm = 0x12C
cmovgl %ebx, %r14d
xorpd %xmm0, %xmm0
movapd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm2
cmpnlesd %xmm0, %xmm2
andpd %xmm2, %xmm1
movapd %xmm1, %xmm3
movsd 0x13f1(%rip), %xmm1     # 0x81e0
andnpd %xmm1, %xmm2
orpd %xmm3, %xmm2
movapd %xmm2, 0x30(%rsp)
testl %ebp, %ebp
movl 0x48(%rsp), %r15d
cmovlel %r15d, %ebp
movapd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm1
cmplesd %xmm0, %xmm1
andnpd %xmm2, %xmm1
movapd %xmm1, 0x20(%rsp)
movq 0x18(%rsp), %rbx
testl %ebx, %ebx
movq 0x40(%rsp), %r12
jg 0x6e60
jmp 0x6e56
movsd 0x13a7(%rip), %xmm0     # 0x81e0
movapd %xmm0, 0x30(%rsp)
movl $0x12c, %r14d           # imm = 0x12C
movl $0x1, 0x8(%rsp)
movl %r15d, %ebp
movapd %xmm1, 0x20(%rsp)
xorl %edi, %edi
callq 0x2120
movq %rax, %rbx
movl %ebx, %edi
callq 0x2140
movq %r12, %rdi
callq 0x2210
testl %eax, %eax
jne 0x7440
leaq 0x1447(%rip), %rdi      # 0x82c5
leaq 0x12f1(%rip), %rsi      # 0x8176
callq 0x2180
testq %rax, %rax
je 0x744a
movq %rax, %r12
leaq 0x144b(%rip), %rsi      # 0x82e8
leaq 0x1478(%rip), %rdx      # 0x831c
leaq 0xc8(%rsp), %r13
movq %r13, %rdi
callq 0x6abc
movq (%r13), %rsi
leaq 0x1491(%rip), %rax      # 0x8350
leaq 0x148f(%rip), %r8       # 0x8355
cmpb $0x0, 0x10(%rsp)
cmovneq %rax, %r8
movq %r12, %rdi
movl %r15d, %edx
movl 0x8(%rsp), %ecx
movl %r14d, %r9d
movapd 0x30(%rsp), %xmm0
movapd 0x20(%rsp), %xmm1
movb $0x2, %al
pushq %rbx
pushq %rbp
callq 0x21f0
addq $0x10, %rsp
movq %r12, %rdi
callq 0x2150
movq (%r13), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x6f13
callq 0x2130
leaq 0x1441(%rip), %rdi      # 0x835b
leaq 0x1271(%rip), %rsi      # 0x8192
callq 0x2180
testq %rax, %rax
je 0x7454
movq %rax, %rbx
movl %r14d, 0x18(%rsp)
movl 0x8(%rsp), %r14d
leaq 0x1441(%rip), %rsi      # 0x8384
leaq 0x4(%rsp), %r12
leaq 0x8c(%rsp), %r13
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x22a0
movq %rbx, %rdi
callq 0x2150
movl (%r12), %esi
movl (%r13), %edx
leaq 0x70(%rsp), %r12
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x5af0
callq 0x20c0
movq %rax, %rbx
leaq 0x13fb(%rip), %rdi      # 0x8389
movq %r12, %rsi
callq 0x5dc0
callq 0x20c0
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subsd %xmm1, %xmm0
divsd 0x1221(%rip), %xmm0     # 0x81d8
leaq 0x13d2(%rip), %rdi      # 0x8390
movsd %xmm0, 0x48(%rsp)
movb $0x1, %al
callq 0x2030
leaq 0xc8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl %r15d, %r12d
movl %r15d, %edx
movl $0x2, %ecx
movl %ebp, %r8d
movapd 0x20(%rsp), %xmm0
callq 0x74d6
callq 0x20c0
movq %rax, %r15
movq 0x4fd8(%rip), %rax      # 0xbfd8
movq (%rax), %r8
subq $0x8, %rsp
movzbl 0x18(%rsp), %esi
leaq 0xd0(%rsp), %rdi
movl %r14d, %edx
movl 0x20(%rsp), %ecx
movapd 0x38(%rsp), %xmm0
movl $0x1, %r9d
pushq $0x1
callq 0x30c8
addq $0x10, %rsp
movsd %xmm0, 0x8(%rsp)
callq 0x20c0
movq %rax, %rbx
leaq 0x1361(%rip), %rdi      # 0x83a8
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x2030
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
subsd %xmm1, %xmm0
divsd 0x1168(%rip), %xmm0     # 0x81d8
leaq 0x1341(%rip), %rdi      # 0x83b8
movsd %xmm0, 0x18(%rsp)
movb $0x1, %al
callq 0x2030
callq 0x20c0
movq %rax, %r14
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %rsi
movapd %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
leaq 0xc8(%rsp), %rdi
callq 0x3614
leaq 0x50(%rsp), %rsi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
leaq 0xc8(%rsp), %rdi
callq 0x34f6
leaq 0xf0(%rsp), %rsi
leaq 0x12ec(%rip), %rdi      # 0x83ce
callq 0x5d37
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
leaq 0x12de(%rip), %rdi      # 0x83dc
callq 0x7560
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x12d3(%rip), %rdi      # 0x83e7
callq 0x75d1
movq 0x58(%rsp), %rsi
subq 0x50(%rsp), %rsi
sarq $0x3, %rsi
movq %rsi, %xmm1
punpckldq 0x10bc(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x10c4(%rip), %xmm1     # 0x8200
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x4(%rsp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %r12d, %xmm2
movsd %xmm2, 0x20(%rsp)
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
leaq 0x1282(%rip), %rdi      # 0x83f1
movb $0x2, %al
callq 0x2030
callq 0x20c0
movq %rax, %r15
movl 0x4(%rsp), %edx
leaq 0x138(%rsp), %rbx
leaq 0x127b(%rip), %rdi      # 0x840c
movq %rbx, %r8
callq 0x6975
xorps %xmm2, %xmm2
cvtsi2sdl 0x4(%rsp), %xmm2
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movapd %xmm1, %xmm2
divsd 0x20(%rsp), %xmm2
leaq 0x125f(%rip), %rdi      # 0x841a
leaq 0x1253(%rip), %rsi      # 0x8415
movsd %xmm0, 0x40(%rsp)
movb $0x3, %al
callq 0x2030
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
cmpb $0x0, 0x10(%rsp)
je 0x722e
movl 0x4(%rsp), %edx
leaq 0x150(%rsp), %r8
leaq 0x1242(%rip), %rdi      # 0x8435
callq 0x6975
xorps %xmm2, %xmm2
cvtsi2sdl 0x4(%rsp), %xmm2
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movapd %xmm1, %xmm2
divsd 0x20(%rsp), %xmm2
leaq 0x1200(%rip), %rdi      # 0x841a
leaq 0x12d1(%rip), %rsi      # 0x84f2
movsd %xmm0, 0x30(%rsp)
movb $0x3, %al
callq 0x2030
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
subsd %xmm1, %xmm0
divsd 0xf8e(%rip), %xmm0      # 0x81d8
leaq 0x11f1(%rip), %rdi      # 0x8442
movsd %xmm0, 0x10(%rsp)
movb $0x1, %al
callq 0x2030
leaq 0x11f0(%rip), %rdi      # 0x8455
leaq 0xf0a(%rip), %rsi       # 0x8176
callq 0x2180
testq %rax, %rax
je 0x7460
movq %rax, %r14
leaq 0x11f5(%rip), %rsi      # 0x8479
leaq 0x1207(%rip), %rdx      # 0x8492
leaq 0xa8(%rsp), %rdi
callq 0x6abc
leaq 0xb8(%rsp), %r15
movq -0x10(%r15), %rsi
movq %r14, %rdi
movsd 0x48(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movb $0x3, %al
callq 0x21f0
movq %r14, %rdi
callq 0x2150
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x72d6
callq 0x2130
leaq 0x11ce(%rip), %rdi      # 0x84ab
leaq 0xe92(%rip), %rsi       # 0x8176
callq 0x2180
testq %rax, %rax
je 0x746a
movq %rax, %r14
leaq 0x11d4(%rip), %rsi      # 0x84d0
leaq 0x11f8(%rip), %rdx      # 0x84fb
leaq 0xa8(%rsp), %rdi
callq 0x6abc
movq 0x58(%rsp), %rax
subq 0x50(%rsp), %rax
sarq $0x3, %rax
movq %rax, %xmm0
punpckldq 0xec5(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
leaq 0xb8(%rsp), %r15
subpd 0xec5(%rip), %xmm0      # 0x8200
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%rsp), %xmm0
movq -0x10(%r15), %rsi
divsd %xmm0, %xmm1
movsd 0x40(%rsp), %xmm2
divsd %xmm0, %xmm2
movsd 0x30(%rsp), %xmm3
divsd %xmm0, %xmm3
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
movb $0x4, %al
callq 0x21f0
movq %r14, %rdi
callq 0x2150
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x7392
callq 0x2130
movl $0xa, %edi
callq 0x21e0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x73ab
callq 0x2130
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x73bd
callq 0x2130
leaq 0x150(%rsp), %rdi
callq 0x7688
movq %rbx, %rdi
callq 0x7688
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
callq 0x76ce
cmpl $0x0, -0x10(%rbx)
je 0x73f5
movq 0x118(%rsp), %rdi
callq 0x2190
cmpl $0x0, 0xf8(%rsp)
je 0x740c
movq 0x100(%rsp), %rdi
callq 0x2190
cmpl $0x0, 0x78(%rsp)
je 0x7420
movq 0x80(%rsp), %rdi
callq 0x2190
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2504
movl $0x2, %eax
jmp 0x7422
movq %r12, %rdi
callq 0x2427
jmp 0x7459
callq 0x2450
jmp 0x6f13
callq 0x24d6
movl $0x1, %eax
jmp 0x7422
callq 0x247a
jmp 0x72d6
callq 0x24a8
jmp 0x7392
jmp 0x7489
jmp 0x7489
movq %rax, %rbx
jmp 0x749b
movq %rax, %rbx
jmp 0x74ad
jmp 0x7484
movq %rax, %rbx
jmp 0x74ba
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x749b
callq 0x2130
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x74ad
callq 0x2130
leaq 0xc8(%rsp), %rdi
callq 0x7644
cmpl $0x0, 0x78(%rsp)
je 0x74ce
movq 0x80(%rsp), %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x2280