// File: /workspace/github_cpp_makefile_star2_O2/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 0x6c22
movq %rsi, %r14
movq 0x8(%rsi), %r12
movq 0x10(%rsi), %rdi
callq 0x2240
movl %eax, %r15d
movb $0x1, 0x10(%rsp)
xorpd %xmm1, %xmm1
addl $-0x4, %ebx
cmpl $0x6, %ebx
ja 0x66c8
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r12, 0x40(%rsp)
movl %r15d, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x1c0f(%rip), %rax      # 0x81c0
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 0x2240
movq %rax, 0x18(%rsp)
movq 0x40(%r14), %rdi
callq 0x21c0
movapd %xmm0, 0x20(%rsp)
movq 0x38(%r14), %rdi
callq 0x2240
movq 0x30(%r14), %rdi
movl %eax, %r13d
callq 0x21c0
movsd %xmm0, 0x10(%rsp)
movq 0x28(%r14), %rdi
callq 0x2240
movl %eax, %r15d
movsd 0x10(%rsp), %xmm2
movl %r13d, %r12d
movsd %xmm2, 0x8(%rsp)
movq 0x20(%r14), %rdi
callq 0x2240
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 0x2240
cmpl $0x2, %eax
pushq $0x1
popq %rcx
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 0x1b12(%rip), %xmm1     # 0x8198
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 0x66f6
jmp 0x66ec
movsd 0x1ac8(%rip), %xmm0     # 0x8198
movapd %xmm0, 0x30(%rsp)
movl $0x12c, %r14d           # imm = 0x12C
pushq $0x1
popq %rax
movl %eax, 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 0x2220
testl %eax, %eax
jne 0x6c45
leaq 0x1b61(%rip), %rdi      # 0x8275
leaq 0x1a13(%rip), %rsi      # 0x812e
callq 0x2190
testq %rax, %rax
je 0x6c62
movq %rax, %r12
leaq 0x1b65(%rip), %rsi      # 0x8298
leaq 0x1b92(%rip), %rdx      # 0x82cc
leaq 0xc8(%rsp), %r13
movq %r13, %rdi
callq 0x636c
movq (%r13), %rsi
leaq 0x1bab(%rip), %rax      # 0x8300
leaq 0x1ba9(%rip), %r8       # 0x8305
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 0x2200
popq %rcx
popq %rdx
movq %r12, %rdi
callq 0x2160
movq %r13, %rdi
callq 0x2090
leaq 0x1b6b(%rip), %rdi      # 0x830b
leaq 0x19a3(%rip), %rsi      # 0x814a
callq 0x2190
testq %rax, %rax
je 0x6c8f
movq %rax, %rbx
movl %r14d, 0x18(%rsp)
movl 0x8(%rsp), %r14d
leaq 0x1b6b(%rip), %rsi      # 0x8334
leaq 0x4(%rsp), %r12
leaq 0x6c(%rsp), %r13
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x22a0
movq %rbx, %rdi
callq 0x2160
movl (%r12), %esi
movl (%r13), %edx
leaq 0x90(%rsp), %r12
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x55f8
callq 0x20d0
movq %rax, %rbx
leaq 0x1b25(%rip), %rdi      # 0x8339
movq %r12, %rsi
callq 0x5891
callq 0x20d0
cvtsi2sd %rbx, %xmm1
cvtsi2sd %rax, %xmm0
subsd %xmm1, %xmm0
divsd 0x1959(%rip), %xmm0     # 0x8190
leaq 0x1b02(%rip), %rdi      # 0x8340
movsd %xmm0, 0x48(%rsp)
movb $0x1, %al
callq 0x2030
leaq 0xc8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
pushq $0x2
popq %rcx
movl %r15d, %r13d
movl %r15d, %edx
movl %ebp, %r8d
movapd 0x20(%rsp), %xmm0
callq 0x6d5c
callq 0x20d0
movq %rax, %r15
movq 0x5757(%rip), %r12      # 0xbfd8
movq (%r12), %r8
subq $0x8, %rsp
movzbl 0x18(%rsp), %esi
leaq 0xd0(%rsp), %rdi
pushq $0x1
popq %r9
movl %r14d, %edx
movl 0x20(%rsp), %ecx
movapd 0x38(%rsp), %xmm0
pushq $0x1
callq 0x2c76
popq %rax
popq %rcx
movsd %xmm0, 0x8(%rsp)
callq 0x20d0
movq %rax, %rbx
leaq 0x1a93(%rip), %rdi      # 0x8358
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x2030
cvtsi2sd %r15, %xmm1
cvtsi2sd %rbx, %xmm0
subsd %xmm1, %xmm0
divsd 0x18a8(%rip), %xmm0     # 0x8190
leaq 0x1a79(%rip), %rdi      # 0x8368
movsd %xmm0, 0x18(%rsp)
movb $0x1, %al
callq 0x2030
callq 0x20d0
movq %rax, %r14
xorpd %xmm0, %xmm0
leaq 0x70(%rsp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0xc8(%rsp), %rdi
callq 0x308c
leaq 0x50(%rsp), %rsi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0xc8(%rsp), %rdi
callq 0x2fdc
leaq 0xf0(%rsp), %rsi
leaq 0x1a2d(%rip), %rdi      # 0x837e
callq 0x57e3
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
leaq 0x1a25(%rip), %rdi      # 0x838c
callq 0x6dde
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x1a1a(%rip), %rdi      # 0x8397
callq 0x6e56
movq 0x58(%rsp), %rsi
subq 0x50(%rsp), %rsi
sarq $0x3, %rsi
movq %rsi, %xmm1
punpckldq 0x1803(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x180b(%rip), %xmm1     # 0x81b0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
cvtsi2sdl 0x4(%rsp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %r13d, %xmm2
movsd %xmm2, 0x20(%rsp)
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
leaq 0x19cc(%rip), %rdi      # 0x83a1
movb $0x2, %al
callq 0x2030
callq 0x20d0
movq %rax, %rbx
movl 0x4(%rsp), %edx
leaq 0x138(%rsp), %r8
leaq 0x19c5(%rip), %rdi      # 0x83bc
callq 0x6269
cvtsi2sdl 0x4(%rsp), %xmm2
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movapd %xmm1, %xmm2
divsd 0x20(%rsp), %xmm2
leaq 0x19af(%rip), %rdi      # 0x83ca
leaq 0x19a3(%rip), %rsi      # 0x83c5
movsd %xmm0, 0x40(%rsp)
movb $0x3, %al
callq 0x2030
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
cmpb $0x0, 0x10(%rsp)
je 0x6a8b
movl 0x4(%rsp), %edx
leaq 0x150(%rsp), %r8
leaq 0x1992(%rip), %rdi      # 0x83e5
callq 0x6269
cvtsi2sdl 0x4(%rsp), %xmm2
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movapd %xmm1, %xmm2
divsd 0x20(%rsp), %xmm2
leaq 0x1953(%rip), %rdi      # 0x83ca
leaq 0x1a24(%rip), %rsi      # 0x84a2
movsd %xmm0, 0x30(%rsp)
movb $0x3, %al
callq 0x2030
cvtsi2sd %r14, %xmm1
cvtsi2sd %rbx, %xmm0
subsd %xmm1, %xmm0
divsd 0x16ef(%rip), %xmm0     # 0x8190
leaq 0x194a(%rip), %rdi      # 0x83f2
movsd %xmm0, 0x10(%rsp)
movb $0x1, %al
callq 0x2030
leaq 0x1949(%rip), %rdi      # 0x8405
leaq 0x166b(%rip), %rsi      # 0x812e
callq 0x2190
testq %rax, %rax
je 0x6cbf
movq %rax, %rbx
leaq 0x194e(%rip), %rsi      # 0x8429
leaq 0x1960(%rip), %rdx      # 0x8442
leaq 0xa8(%rsp), %rdi
callq 0x636c
leaq 0xa8(%rsp), %r14
movq (%r14), %rsi
movq %rbx, %rdi
movsd 0x48(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movb $0x3, %al
callq 0x2200
movq %rbx, %rdi
callq 0x2160
movq %r14, %rdi
callq 0x2090
leaq 0x192e(%rip), %rdi      # 0x845b
leaq 0x15fa(%rip), %rsi      # 0x812e
callq 0x2190
testq %rax, %rax
je 0x6ce6
movq %rax, %rbx
leaq 0x1934(%rip), %rsi      # 0x8480
leaq 0x1958(%rip), %rdx      # 0x84ab
leaq 0xa8(%rsp), %rdi
callq 0x636c
movq 0x58(%rsp), %rax
subq 0x50(%rsp), %rax
sarq $0x3, %rax
movq %rax, %xmm0
punpckldq 0x1625(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
leaq 0xa8(%rsp), %r14
subpd 0x1625(%rip), %xmm0     # 0x81b0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
cvtsi2sdl 0x4(%rsp), %xmm0
movq (%r14), %rsi
divsd %xmm0, %xmm1
movsd 0x40(%rsp), %xmm2
divsd %xmm0, %xmm2
movsd 0x30(%rsp), %xmm3
divsd %xmm0, %xmm3
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
movb $0x4, %al
callq 0x2200
movq %rbx, %rdi
callq 0x2160
movq %r14, %rdi
callq 0x2090
pushq $0xa
popq %rdi
callq 0x21f0
leaq 0x50(%rsp), %rdi
callq 0x3af4
leaq 0x70(%rsp), %rdi
callq 0x414e
leaq 0xc8(%rsp), %rdi
callq 0x6ece
leaq 0x90(%rsp), %rdi
callq 0x348c
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x53af(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
movq 0x554d(%rip), %rdx      # 0xc180
leaq 0x1615(%rip), %rsi      # 0x824f
xorl %eax, %eax
callq 0x2200
pushq $0x2
jmp 0x6cb9
movq 0x538c(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
leaq 0x1604(%rip), %rsi      # 0x825a
movq %r12, %rdx
xorl %eax, %eax
callq 0x2200
jmp 0x6cab
movq 0x536f(%rip), %rax      # 0xbfd8
movq (%rax), %rcx
leaq 0x160c(%rip), %rdi      # 0x827f
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x22c0
leaq 0x15eb(%rip), %rdi      # 0x8270
callq 0x2100
jmp 0x6799
movq 0x5342(%rip), %rax      # 0xbfd8
movq (%rax), %rcx
leaq 0x1678(%rip), %rdi      # 0x8318
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x22c0
leaq 0x15be(%rip), %rdi      # 0x8270
callq 0x2100
pushq $0x1
popq %rax
jmp 0x6c10
movq (%r12), %rcx
leaq 0x1746(%rip), %rdi      # 0x8410
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x22c0
leaq 0x1594(%rip), %rdi      # 0x8270
callq 0x2100
jmp 0x6b26
movq (%r12), %rcx
leaq 0x1775(%rip), %rdi      # 0x8466
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x22c0
leaq 0x156d(%rip), %rdi      # 0x8270
callq 0x2100
jmp 0x6bd8
jmp 0x6d22
jmp 0x6d22
movq %rax, %rbx
jmp 0x6d2f
movq %rax, %rbx
jmp 0x6d39
jmp 0x6d1d
movq %rax, %rbx
jmp 0x6d46
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x3af4
leaq 0x70(%rsp), %rdi
callq 0x414e
leaq 0xc8(%rsp), %rdi
callq 0x6ece
leaq 0x90(%rsp), %rdi
callq 0x348c
movq %rbx, %rdi
callq 0x2290
nop