// File: /workspace/github_cpp_makefile_star2_O1/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 0x74b0
movq %rsi, %r12
movq 0x8(%rsi), %r13
movq 0x10(%rsi), %rdi
callq 0x2230
movl %eax, %r15d
xorl %r14d, %r14d
movb $0x1, 0x10(%rsp)
addl $-0x4, %ebx
cmpl $0x6, %ebx
ja 0x7495
movq %r13, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
movl %r15d, 0x48(%rsp)
leaq 0x14bd(%rip), %rax      # 0x8210
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
xorpd %xmm0, %xmm0
xorps %xmm4, %xmm4
xorl %r15d, %r15d
xorps %xmm5, %xmm5
xorpd %xmm3, %xmm3
xorl %r14d, %r14d
xorps %xmm6, %xmm6
xorps %xmm7, %xmm7
xorl %ebp, %ebp
xorl %r13d, %r13d
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
xorl %eax, %eax
xorl %ebx, %ebx
jmpq *%rcx
movq 0x48(%r12), %rdi
callq 0x2230
movq %rax, 0x18(%rsp)
movq 0x40(%r12), %rdi
callq 0x21b0
movsd %xmm0, 0x10(%rsp)
movq 0x38(%r12), %rdi
callq 0x2230
movl %eax, %r15d
movsd 0x10(%rsp), %xmm4
movsd %xmm4, 0x10(%rsp)
movq 0x30(%r12), %rdi
callq 0x21b0
movapd %xmm0, %xmm3
movsd 0x10(%rsp), %xmm5
movl %r15d, %r14d
movsd %xmm5, 0x20(%rsp)
movsd %xmm3, 0x10(%rsp)
movq 0x28(%r12), %rdi
callq 0x2230
movl %eax, %r13d
movsd 0x20(%rsp), %xmm6
movsd 0x10(%rsp), %xmm7
movl %r14d, %ebp
movsd %xmm6, 0x30(%rsp)
movsd %xmm7, 0x20(%rsp)
movq 0x20(%r12), %rdi
callq 0x2230
testl %eax, %eax
setne 0x10(%rsp)
movsd 0x30(%rsp), %xmm2
movsd 0x20(%rsp), %xmm1
movl %ebp, %eax
movl %r13d, %ebx
movapd %xmm1, 0x30(%rsp)
movapd %xmm2, 0x20(%rsp)
movq 0x18(%r12), %rdi
movq %rax, %r14
callq 0x2230
movq %r14, %rcx
movl 0x48(%rsp), %r15d
movq 0x18(%rsp), %r14
movq 0x40(%rsp), %r13
cmpl $0x2, %eax
movl $0x1, %ebp
cmovgel %eax, %ebp
testl %ebx, %ebx
movl $0x12c, %r12d           # imm = 0x12C
cmovgl %ebx, %r12d
testl %ecx, %ecx
cmovlel %r15d, %ecx
movq %rcx, 0x18(%rsp)
testl %r14d, %r14d
jg 0x6e88
xorl %edi, %edi
callq 0x2120
movq %rax, %r14
movl %r14d, %edi
callq 0x2140
movq %r13, %rdi
callq 0x2210
testl %eax, %eax
jne 0x74bc
movapd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm3
xorpd %xmm1, %xmm1
cmpnlesd %xmm1, %xmm3
movsd 0x1325(%rip), %xmm0     # 0x81e0
andpd %xmm3, %xmm2
andnpd %xmm0, %xmm3
orpd %xmm2, %xmm3
movapd %xmm3, 0x30(%rsp)
movapd 0x20(%rsp), %xmm0
movapd %xmm0, %xmm2
cmplesd %xmm1, %xmm2
andnpd %xmm0, %xmm2
movapd %xmm2, 0x20(%rsp)
leaq 0x13d8(%rip), %rdi      # 0x82c5
leaq 0x1282(%rip), %rsi      # 0x8176
callq 0x2180
testq %rax, %rax
je 0x74c6
movq %rax, %r13
leaq 0x13dc(%rip), %rsi      # 0x82e8
leaq 0x1409(%rip), %rdx      # 0x831c
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x6af0
movq (%rbx), %rsi
leaq 0x1423(%rip), %rax      # 0x8350
leaq 0x1421(%rip), %r8       # 0x8355
cmpb $0x0, 0x10(%rsp)
cmovneq %rax, %r8
movq %r13, %rdi
movl %r15d, %edx
movl %ebp, %ecx
movl %r12d, %r9d
movapd 0x30(%rsp), %xmm0
movapd 0x20(%rsp), %xmm1
movb $0x2, %al
pushq %r14
pushq 0x20(%rsp)
callq 0x21f0
addq $0x10, %rsp
movq %r13, %rdi
callq 0x2150
movq (%rbx), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x6f82
callq 0x2130
leaq 0x13d2(%rip), %rdi      # 0x835b
leaq 0x1202(%rip), %rsi      # 0x8192
callq 0x2180
testq %rax, %rax
je 0x74d0
movq %rax, %rbx
leaq 0x13dc(%rip), %rsi      # 0x8384
leaq 0xc(%rsp), %r14
leaq 0x8c(%rsp), %r13
movq %rax, %rdi
movq %r14, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x22a0
movq %rbx, %rdi
callq 0x2150
movl (%r14), %esi
movl (%r13), %edx
leaq 0x70(%rsp), %r14
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x5b48
callq 0x20c0
movq %rax, %rbx
leaq 0x1397(%rip), %rdi      # 0x8389
movq %r14, %rsi
callq 0x5e18
callq 0x20c0
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subsd %xmm1, %xmm0
divsd 0x11bd(%rip), %xmm0     # 0x81d8
leaq 0x136e(%rip), %rdi      # 0x8390
movsd %xmm0, 0x48(%rsp)
movb $0x1, %al
callq 0x2030
leaq 0xc8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl %r15d, %edx
movl $0x2, %ecx
movq 0x18(%rsp), %r8
movapd 0x20(%rsp), %xmm0
callq 0x7552
callq 0x20c0
movq %rax, %r14
movq 0x4f75(%rip), %rax      # 0xbfd8
movq (%rax), %r8
subq $0x8, %rsp
movzbl 0x18(%rsp), %esi
leaq 0xd0(%rsp), %rdi
movl %ebp, %edx
movl %r12d, %ecx
movapd 0x38(%rsp), %xmm0
movl $0x1, %r9d
pushq $0x1
callq 0x30fe
addq $0x10, %rsp
movsd %xmm0, 0x18(%rsp)
callq 0x20c0
movq %rax, %rbx
leaq 0x1300(%rip), %rdi      # 0x83a8
movsd 0x18(%rsp), %xmm0
movb $0x1, %al
callq 0x2030
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
subsd %xmm1, %xmm0
divsd 0x1107(%rip), %xmm0     # 0x81d8
leaq 0x12e0(%rip), %rdi      # 0x83b8
movsd %xmm0, 0x40(%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 0x3646
leaq 0x50(%rsp), %rsi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
leaq 0xc8(%rsp), %rdi
callq 0x352c
leaq 0xf0(%rsp), %rsi
leaq 0x128b(%rip), %rdi      # 0x83ce
callq 0x5d8f
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
leaq 0x127d(%rip), %rdi      # 0x83dc
callq 0x75dc
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x1272(%rip), %rdi      # 0x83e7
callq 0x764d
movq 0x58(%rsp), %rsi
subq 0x50(%rsp), %rsi
sarq $0x3, %rsi
movq %rsi, %xmm1
punpckldq 0x105b(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1063(%rip), %xmm1     # 0x8200
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rsp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %r15d, %xmm2
movsd %xmm2, 0x20(%rsp)
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
leaq 0x1221(%rip), %rdi      # 0x83f1
movb $0x2, %al
callq 0x2030
callq 0x20c0
movq %rax, %r15
movl 0xc(%rsp), %edx
leaq 0x138(%rsp), %rbx
leaq 0x121a(%rip), %rdi      # 0x840c
movq %rbx, %r8
callq 0x69a9
xorps %xmm2, %xmm2
cvtsi2sdl 0xc(%rsp), %xmm2
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movapd %xmm1, %xmm2
divsd 0x20(%rsp), %xmm2
leaq 0x11fe(%rip), %rdi      # 0x841a
leaq 0x11f2(%rip), %rsi      # 0x8415
movsd %xmm0, 0x68(%rsp)
movb $0x3, %al
callq 0x2030
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
cmpb $0x0, 0x10(%rsp)
je 0x728f
movl 0xc(%rsp), %edx
leaq 0x150(%rsp), %r8
leaq 0x11e1(%rip), %rdi      # 0x8435
callq 0x69a9
xorps %xmm2, %xmm2
cvtsi2sdl 0xc(%rsp), %xmm2
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movapd %xmm1, %xmm2
divsd 0x20(%rsp), %xmm2
leaq 0x119f(%rip), %rdi      # 0x841a
leaq 0x1270(%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 0xf2d(%rip), %xmm0      # 0x81d8
leaq 0x1190(%rip), %rdi      # 0x8442
movsd %xmm0, 0x10(%rsp)
movb $0x1, %al
callq 0x2030
leaq 0x118f(%rip), %rdi      # 0x8455
leaq 0xea9(%rip), %rsi       # 0x8176
callq 0x2180
testq %rax, %rax
je 0x74dc
movq %rax, %r14
leaq 0x1194(%rip), %rsi      # 0x8479
leaq 0x11a6(%rip), %rdx      # 0x8492
leaq 0xa8(%rsp), %rdi
callq 0x6af0
leaq 0xb8(%rsp), %r15
movq -0x10(%r15), %rsi
movq %r14, %rdi
movsd 0x48(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movb $0x3, %al
callq 0x21f0
movq %r14, %rdi
callq 0x2150
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x7337
callq 0x2130
leaq 0x116d(%rip), %rdi      # 0x84ab
leaq 0xe31(%rip), %rsi       # 0x8176
callq 0x2180
testq %rax, %rax
je 0x74e6
movq %rax, %r14
leaq 0x1173(%rip), %rsi      # 0x84d0
leaq 0x1197(%rip), %rdx      # 0x84fb
leaq 0xa8(%rsp), %rdi
callq 0x6af0
movq 0x58(%rsp), %rax
subq 0x50(%rsp), %rax
sarq $0x3, %rax
movq %rax, %xmm0
punpckldq 0xe64(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
leaq 0xb8(%rsp), %r15
subpd 0xe64(%rip), %xmm0      # 0x8200
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl 0xc(%rsp), %xmm0
movq -0x10(%r15), %rsi
divsd %xmm0, %xmm1
movsd 0x68(%rsp), %xmm2
divsd %xmm0, %xmm2
movsd 0x30(%rsp), %xmm3
divsd %xmm0, %xmm3
movq %r14, %rdi
movsd 0x18(%rsp), %xmm0
movb $0x4, %al
callq 0x21f0
movq %r14, %rdi
callq 0x2150
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x73f3
callq 0x2130
movl $0xa, %edi
callq 0x21e0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x740c
callq 0x2130
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x741e
callq 0x2130
leaq 0x150(%rsp), %rdi
callq 0x7704
movq %rbx, %rdi
callq 0x7704
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
callq 0x7744
cmpl $0x0, -0x10(%rbx)
je 0x7456
movq 0x118(%rsp), %rdi
callq 0x2190
cmpl $0x0, 0xf8(%rsp)
je 0x746d
movq 0x100(%rsp), %rdi
callq 0x2190
cmpl $0x0, 0x78(%rsp)
je 0x7481
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
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movapd %xmm0, 0x30(%rsp)
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %eax, %eax
jmp 0x6e57
callq 0x253c
movl $0x2, %eax
jmp 0x7483
movq %r13, %rdi
callq 0x245f
jmp 0x74d5
callq 0x2488
jmp 0x6f82
callq 0x250e
movl $0x1, %eax
jmp 0x7483
callq 0x24b2
jmp 0x7337
callq 0x24e0
jmp 0x73f3
jmp 0x7505
jmp 0x7505
movq %rax, %rbx
jmp 0x7517
movq %rax, %rbx
jmp 0x7529
jmp 0x7500
movq %rax, %rbx
jmp 0x7536
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x7517
callq 0x2130
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x7529
callq 0x2130
leaq 0xc8(%rsp), %rdi
callq 0x76c0
cmpl $0x0, 0x78(%rsp)
je 0x754a
movq 0x80(%rsp), %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x2280