// File: /workspace/github_cpp_makefile_star2_O1/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, 0x34(%rsp)
movl %r8d, 0x30(%rsp)
movl %ecx, 0x24(%rsp)
movl %edx, %ebp
movsd %xmm1, 0x18(%rsp)
movsd %xmm0, 0x10(%rsp)
movl %esi, %ebx
movq %rdi, %r14
callq 0x2130
movq %rax, 0x38(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
leaq 0x2c(%rsp), %rsi
leaq 0x28(%rsp), %rdx
leaq 0x68(%rsp), %r15
leaq 0x50(%rsp), %r12
leaq 0x60(%rsp), %r13
movq %r14, %rdi
movq %r15, %rcx
movq %r12, %r8
movq %r13, %r9
callq 0x2b06
movq (%r15), %rcx
movq (%r12), %rsi
movq (%r13), %rax
movl %ebp, %r13d
movq %rcx, %rbp
movl (%rax), %eax
leal 0x3(%rax), %r8d
testl %eax, %eax
cmovnsl %eax, %r8d
sarl $0x2, %r8d
leaq 0x58(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movq %rbp, %rdi
callq 0x2d58
movq (%r14), %r14
movsd 0x2049(%rip), %xmm0     # 0x50c0
mulsd 0x10(%rsp), %xmm0
addsd 0x2043(%rip), %xmm0     # 0x50c8
callq 0x20e0
cvttsd2si %xmm0, %r12d
movsd 0x2039(%rip), %xmm0     # 0x50d0
mulsd 0x18(%rsp), %xmm0
addsd 0x2023(%rip), %xmm0     # 0x50c8
callq 0x20e0
cvttsd2si %xmm0, %r9d
movl %r13d, (%rsp)
leaq 0x20ca(%rip), %rsi      # 0x5184
xorl %r15d, %r15d
leaq 0x70(%rsp), %rdi
movq %r14, %rdx
movl %ebx, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x2060
cmpb $0x0, 0x320(%rsp)
je 0x3155
leaq 0xe0(%rsp), %r14
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0x2310
movq (%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x100(%rsp,%rax)
je 0x310b
xorl %r15d, %r15d
jmp 0x3148
movl $0x18, %edi
callq 0x21d0
movq %rax, %r15
leaq 0xe0(%rsp), %rsi
movq %rax, %rdi
callq 0x3650
movl (%r15), %esi
incl %esi
leaq 0x207b(%rip), %rdi      # 0x51af
xorl %eax, %eax
callq 0x2040
leaq 0xe0(%rsp), %rdi
callq 0x2070
leaq 0xe0(%rsp), %rdi
callq 0x2090
testq %r15, %r15
jne 0x318e
movl $0x18, %edi
callq 0x21d0
movq %rax, %r15
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
movq %rax, %rdi
movl %ebx, %ecx
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movl %r13d, %r8d
movl $0x8, %r9d
callq 0x36f2
movslq (%rbp), %rax
imulq $0x10624dd3, %rax, %rsi # imm = 0x10624DD3
movq %rsi, %rax
shrq $0x3f, %rax
sarq $0x26, %rsi
addl %eax, %esi
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %ecx
leaq 0x2012(%rip), %rdi      # 0x51c7
movl %ebx, %r8d
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movl %r13d, %r9d
movb $0x2, %al
callq 0x2040
movabsq $0x431bde82d7b634db, %rbp # imm = 0x431BDE82D7B634DB
movl 0x24(%rsp), %eax
cmpl %eax, (%r15)
jge 0x32d0
movzbl 0x30(%rsp), %ebx
callq 0x2130
movq %rax, %r14
movq 0x68(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %r15, %rdi
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x37a0
callq 0x2130
movq %rax, %r12
movq 0x60(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r15, %rdi
callq 0x396c
movsd %xmm0, 0x18(%rsp)
callq 0x2130
movq %rax, %r13
movq 0x58(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x396c
movsd %xmm0, 0x10(%rsp)
movl (%r15), %esi
leaq 0x1fc6(%rip), %rdi      # 0x521c
xorl %eax, %eax
callq 0x2040
subq %r12, %r13
subq %r14, %r12
movq %r12, %rax
imulq %rbp
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x12, %rdx
addq %rdx, %rsi
movq %r13, %rax
imulq %rbp
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
leaq 0x1faf(%rip), %rdi      # 0x5241
xorl %eax, %eax
callq 0x2040
movsd 0x1e37(%rip), %xmm2     # 0x50d8
movsd 0x10(%rsp), %xmm0
mulsd %xmm2, %xmm0
movsd 0x18(%rsp), %xmm1
mulsd %xmm2, %xmm1
leaq 0x1f9a(%rip), %rdi      # 0x5256
movb $0x2, %al
callq 0x2040
movl 0x24(%rsp), %eax
cmpl %eax, (%r15)
jl 0x31ea
cmpb $0x0, 0x34(%rsp)
je 0x3341
leaq 0xe0(%rsp), %rbx
leaq 0x70(%rsp), %rsi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x21e0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x39d8
leaq 0xe0(%rsp), %rdi
callq 0x2260
movq 0x4cc8(%rip), %rax      # 0x7fd8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0xe8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0xe0(%rsp,%rcx)
callq 0x2190
leaq 0x1d8(%rsp), %rdi
callq 0x20b0
callq 0x2130
subq 0x38(%rsp), %rax
imulq %rbp
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
movslq %edx, %rcx
imulq $-0x6e5d4c3b, %rcx, %rsi # imm = 0x91A2B3C5
shrq $0x20, %rsi
addl %ecx, %esi
movl %esi, %eax
shrl $0x1f, %eax
sarl $0xb, %esi
addl %eax, %esi
imulq $-0x77777777, %rcx, %rax # imm = 0x88888889
shrq $0x20, %rax
addl %ecx, %eax
movl %eax, %edx
shrl $0x1f, %edx
sarl $0x5, %eax
addl %edx, %eax
movslq %eax, %rdx
imulq $-0x77777777, %rdx, %rax # imm = 0x88888889
shrq $0x20, %rax
addl %edx, %eax
movl %eax, %edi
shrl $0x1f, %edi
sarl $0x5, %eax
addl %edi, %eax
imull $0x3c, %eax, %eax
imull $0x3c, %edx, %edi
subl %eax, %edx
subl %edi, %ecx
leaq 0x1eb5(%rip), %rdi      # 0x526d
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 0x33e1
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x2090
jmp 0x340d
movq %rax, %rbx
movq %r15, %rdi
callq 0x21b0
jmp 0x340d
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x2280
movq %rbx, %rdi
callq 0x22f0