// File: /workspace/github_cpp_makefile_star2_O2/adrianphoulady[P]weighted-tsetlin-machine-cpp/./utils.h
// Function: fit(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int, double, double, int, int, bool, bool, bool)
void fit(std::string const &experiment, int clauses, double p, double gamma, int threshold, int epochs, bool shuffle = false, bool write = false, bool resume = false) {
    clock_t tc0 = clock();

    int features, classes;
    array2d<word> *x_train, *x_test, *x_tray;
    array1d<int> *y_train, *y_test, *y_tray;
    load_data(experiment, features, classes, x_train, y_train, x_test, y_test);
    sample_data(x_train, y_train, x_tray, y_tray, x_test->rows / 4);

    char mname[100];
    sprintf(mname, "results/%s-c%05d-p%04d-g%05d-t%04d.machine", experiment.c_str(), clauses, (int) round(p * 10000 + .0001), (int) round(gamma * 100000 + .0001), threshold);

    multiweightm *wtm = nullptr;
    if (resume) { // deserializing machine
        std::ifstream min(mname, std::ifstream::binary);
        if (min) {
            wtm = new multiweightm(min);
            printf("Continuing at epoch %d\n", wtm->get_epoch() + 1);
            min.close();
        }
    }
    if (!wtm)
        wtm = new multiweightm(classes, features, clauses, p, gamma, threshold);

    printf("samples=%dK, features=%d, classes=%d - clauses=%d, p=%.4f, gamma=%.5f, threshold=%d\n", x_train->rows / 1000, features, classes, clauses, p, gamma, threshold);

    while (wtm->get_epoch() < epochs) {
        clock_t c0 = clock();
        wtm->fit(*x_train, *y_train, 1, shuffle);
        clock_t c1 = clock();
        double e1 = wtm->evaluate(*x_test, *y_test);
        clock_t c2 = clock();
        double e2 = wtm->evaluate(*x_tray, *y_tray);

        printf("epoch %03d of training and testing -", wtm->get_epoch());
        printf(" %04lus and %04lus -", (c1 - c0) / CLOCKS_PER_SEC, (c2 - c1) / CLOCKS_PER_SEC);
        printf(" %6.2f%%  and %6.2f%%\n", 100 * e2, 100 * e1);
    }

    if (write) {
        // serializing machine
        std::ofstream mout(mname, std::ofstream::binary);
        wtm->serialize(mout);
        mout.close();
    }

    int ss = (clock() - tc0) / CLOCKS_PER_SEC, mm = ss / 60, hh = mm / 60;
    printf("total time: %02d:%02d:%02d\n", hh, mm % 60, ss % 60);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2e8, %rsp            # imm = 0x2E8
movl %r9d, 0x28(%rsp)
movl %r8d, 0x1c(%rsp)
movl %ecx, 0x2c(%rsp)
movl %edx, %r13d
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x8(%rsp)
movl %esi, %ebx
movq %rdi, %r14
callq 0x2160
movq %rax, 0x38(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x68(%rsp), %r15
leaq 0x50(%rsp), %r12
leaq 0x60(%rsp), %rbp
movq %r14, %rdi
movq %r15, %rcx
movq %r12, %r8
movq %rbp, %r9
callq 0x29c0
movq (%r15), %rdi
movq (%r12), %rsi
movq (%rbp), %rax
movl (%rax), %eax
pushq $0x4
popq %rcx
cltd
idivl %ecx
leaq 0x58(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movq %rdi, 0x30(%rsp)
movl %eax, %r8d
callq 0x2afc
movq (%r14), %r14
movsd 0x235f(%rip), %xmm0     # 0x5070
mulsd 0x8(%rsp), %xmm0
addsd 0x2359(%rip), %xmm0     # 0x5078
callq 0x2100
cvttsd2si %xmm0, %ebp
movsd 0x2350(%rip), %xmm0     # 0x5080
mulsd 0x10(%rsp), %xmm0
addsd 0x233a(%rip), %xmm0     # 0x5078
callq 0x2100
cvttsd2si %xmm0, %r9d
movl %r13d, (%rsp)
leaq 0x23df(%rip), %rsi      # 0x5132
leaq 0x70(%rsp), %rdi
movq %r14, %rdx
movl %ebx, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x2060
cmpb $0x0, 0x320(%rsp)
je 0x2dfc
leaq 0xe0(%rsp), %r14
leaq 0x70(%rsp), %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x22e0
movq (%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x100(%rsp,%rax)
je 0x2da3
xorl %r15d, %r15d
jmp 0x2dde
pushq $0x18
popq %rdi
callq 0x21c0
movq %rax, %r15
leaq 0xe0(%rsp), %rsi
movq %rax, %rdi
callq 0x330a
movl (%r15), %esi
incl %esi
leaq 0x2393(%rip), %rdi      # 0x515d
xorl %eax, %eax
callq 0x2040
leaq 0xe0(%rsp), %rdi
callq 0x2080
leaq 0xe0(%rsp), %rdi
callq 0x20a0
testq %r15, %r15
je 0x2dfc
movl 0x24(%rsp), %r12d
movl 0x20(%rsp), %r14d
jmp 0x2e34
pushq $0x18
popq %rdi
callq 0x21c0
movq %rax, %r15
movl 0x20(%rsp), %r14d
movl 0x24(%rsp), %r12d
pushq $0x8
popq %r9
movq %rax, %rdi
movl %r14d, %esi
movl %r12d, %edx
movl %ebx, %ecx
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movl %r13d, %r8d
callq 0x3382
movl 0x1c(%rsp), %ebp
movq 0x30(%rsp), %rax
movl (%rax), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
leaq 0x2327(%rip), %rdi      # 0x5175
movl %eax, %esi
movl %r12d, %edx
movl %r14d, %ecx
movl %ebx, %r8d
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movl %r13d, %r9d
movb $0x2, %al
callq 0x2040
movzbl %bpl, %ebx
movl $0xf4240, %r14d         # imm = 0xF4240
movl 0x2c(%rsp), %eax
cmpl %eax, (%r15)
jge 0x2f50
callq 0x2160
movq %rax, %rbp
movq 0x68(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %r15, %rdi
pushq $0x1
popq %rcx
movl %ebx, %r8d
callq 0x3426
callq 0x2160
movq %rax, %r12
movq 0x60(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r15, %rdi
callq 0x350c
movsd %xmm0, 0x10(%rsp)
callq 0x2160
movq %rax, %r13
movq 0x58(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x350c
movsd %xmm0, 0x8(%rsp)
movl (%r15), %esi
leaq 0x22da(%rip), %rdi      # 0x51ca
xorl %eax, %eax
callq 0x2040
movq %r12, %rax
subq %rbp, %rax
cqto
idivq %r14
movq %rax, %rsi
subq %r12, %r13
movq %r13, %rax
cqto
idivq %r14
leaq 0x22d8(%rip), %rdi      # 0x51ef
movq %rax, %rdx
xorl %eax, %eax
callq 0x2040
movsd 0x215f(%rip), %xmm2     # 0x5088
movsd 0x8(%rsp), %xmm0
mulsd %xmm2, %xmm0
movsd 0x10(%rsp), %xmm1
mulsd %xmm2, %xmm1
leaq 0x22c0(%rip), %rdi      # 0x5204
movb $0x2, %al
callq 0x2040
jmp 0x2e79
cmpb $0x0, 0x28(%rsp)
je 0x2f94
leaq 0xe0(%rsp), %rbx
leaq 0x70(%rsp), %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x21d0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x356e
leaq 0xe0(%rsp), %rdi
callq 0x2250
leaq 0xe0(%rsp), %rdi
callq 0x2260
callq 0x2160
subq 0x38(%rsp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, %rcx
movl $0xe10, %esi            # imm = 0xE10
cltd
idivl %esi
movl %eax, %esi
pushq $0x3c
popq %rdi
movl %ecx, %eax
cltd
idivl %edi
movl %edx, %ecx
cltd
idivl %edi
leaq 0x2252(%rip), %rdi      # 0x521b
xorl %eax, %eax
callq 0x2040
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r15, %rdi
callq 0x21b0
jmp 0x2ff2
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x20a0
jmp 0x301e
movq %rax, %rbx
movq %r15, %rdi
callq 0x21b0
jmp 0x301e
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x2260
movq %rbx, %rdi
callq 0x22d0