// File: /workspace/github_cpp_makefile_star2_O3/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 $0x318, %rsp            # imm = 0x318
movl %r9d, 0x3c(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, 0x24(%rsp)
movl %edx, %ebx
movsd %xmm1, 0x18(%rsp)
movsd %xmm0, 0x10(%rsp)
movl %esi, %ebp
movq %rdi, %r14
callq 0x2130
movq %rax, 0x40(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, (%rsp)
leaq 0x2c(%rsp), %rsi
leaq 0x28(%rsp), %rdx
leaq 0x98(%rsp), %r15
leaq 0x88(%rsp), %r12
leaq 0x90(%rsp), %r13
movq %r14, %rdi
movq %r15, %rcx
movq %r12, %r8
movq %r13, %r9
callq 0x2acd
movq (%r15), %rdi
movq (%r12), %r15
movl %ebp, %r12d
movq (%r13), %r13
movl (%r13), %eax
leal 0x3(%rax), %r8d
testl %eax, %eax
cmovnsl %eax, %r8d
sarl $0x2, %r8d
leaq 0x80(%rsp), %rdx
leaq 0x70(%rsp), %rcx
movq %rdi, 0x48(%rsp)
movq %r15, %rsi
callq 0x2d12
movq (%r14), %r14
movsd 0x206e(%rip), %xmm0     # 0x50b0
mulsd 0x10(%rsp), %xmm0
addsd 0x2068(%rip), %xmm0     # 0x50b8
callq 0x20e0
cvttsd2si %xmm0, %ebp
movsd 0x205f(%rip), %xmm0     # 0x50c0
mulsd 0x18(%rsp), %xmm0
addsd 0x2049(%rip), %xmm0     # 0x50b8
callq 0x20e0
cvttsd2si %xmm0, %r9d
movl %ebx, (%rsp)
leaq 0x20ef(%rip), %rsi      # 0x5172
leaq 0xa0(%rsp), %rdi
movq %r14, %rdx
movl %r12d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x2060
cmpb $0x0, 0x350(%rsp)
movq %r13, 0x50(%rsp)
movl %r12d, 0x30(%rsp)
je 0x30f9
movl %ebx, %r12d
leaq 0x110(%rsp), %r14
leaq 0xa0(%rsp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0x2300
movq (%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x130(%rsp,%rax)
je 0x33c6
leaq 0x110(%rsp), %rdi
callq 0x2090
movl %r12d, %ebx
movl 0x30(%rsp), %r12d
movq %r15, %r13
movl $0x18, %edi
callq 0x21d0
movq %rax, %rbp
movl 0x28(%rsp), %r14d
movl 0x2c(%rsp), %r15d
movq %rax, %rdi
movl %r14d, %esi
movl %r15d, %edx
movl %r12d, %ecx
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movl %ebx, %r12d
movl %ebx, %r8d
movl $0x8, %r9d
callq 0x374a
movq 0x48(%rsp), %rbx
movslq (%rbx), %rax
imulq $0x10624dd3, %rax, %rsi # imm = 0x10624DD3
movq %rsi, %rax
shrq $0x3f, %rax
sarq $0x26, %rsi
addl %eax, %esi
leaq 0x2056(%rip), %rdi      # 0x51b5
movl %r15d, %edx
movl %r14d, %ecx
movl 0x30(%rsp), %r8d
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movl %r12d, %r9d
movb $0x2, %al
callq 0x2040
movabsq $0x431bde82d7b634db, %r15 # imm = 0x431BDE82D7B634DB
movl 0x24(%rsp), %eax
cmpl %eax, (%rbp)
movq 0x50(%rsp), %r12
jge 0x32c2
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x58(%rsp)
movzbl 0xc(%rsp), %eax
movl %eax, 0xc(%rsp)
movq %r13, 0x68(%rsp)
callq 0x2130
movq %rax, %r15
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl $0x1, %ecx
movl 0xc(%rsp), %r8d
callq 0x37f8
callq 0x2130
movq %rbx, %r13
movq %rax, %rbx
movq %rbp, %rdi
movq %r12, %rsi
movq 0x30(%rsp), %rdx
callq 0x39c4
movsd %xmm0, 0x18(%rsp)
callq 0x2130
movq %rax, %r14
movq %rbp, %rdi
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x39c4
movsd %xmm0, 0x10(%rsp)
movl (%rbp), %esi
leaq 0x1fd4(%rip), %rdi      # 0x520a
xorl %eax, %eax
callq 0x2040
subq %rbx, %r14
subq %r15, %rbx
movabsq $0x431bde82d7b634db, %r15 # imm = 0x431BDE82D7B634DB
movq %rbx, %rax
movq %r13, %rbx
movq 0x68(%rsp), %r13
imulq %r15
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x12, %rdx
addq %rdx, %rsi
movq %r14, %rax
imulq %r15
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
leaq 0x1fab(%rip), %rdi      # 0x522f
xorl %eax, %eax
callq 0x2040
movsd 0x1e35(%rip), %xmm2     # 0x50c8
movsd 0x10(%rsp), %xmm0
mulsd %xmm2, %xmm0
movsd 0x18(%rsp), %xmm1
mulsd %xmm2, %xmm1
leaq 0x1f96(%rip), %rdi      # 0x5244
movb $0x2, %al
callq 0x2040
movl 0x24(%rsp), %eax
cmpl %eax, (%rbp)
jl 0x31cb
cmpb $0x0, 0x3c(%rsp)
je 0x3336
leaq 0x110(%rsp), %rbx
leaq 0xa0(%rsp), %rsi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x21e0
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3a30
leaq 0x110(%rsp), %rdi
callq 0x2250
movq 0x4cd3(%rip), %rax      # 0x7fd8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x118(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x110(%rsp,%rcx)
callq 0x2190
leaq 0x208(%rsp), %rdi
callq 0x20b0
callq 0x2130
subq 0x40(%rsp), %rax
imulq %r15
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 0x1eae(%rip), %rdi      # 0x525b
xorl %eax, %eax
callq 0x2040
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x18, %edi
callq 0x21d0
movq %rax, %rbp
leaq 0x110(%rsp), %rsi
movq %rax, %rdi
callq 0x36a8
movl (%rbp), %esi
incl %esi
leaq 0x1dae(%rip), %rdi      # 0x519d
xorl %eax, %eax
callq 0x2040
leaq 0x110(%rsp), %rdi
callq 0x2070
movq %r15, %r13
leaq 0x110(%rsp), %rdi
callq 0x2090
movl 0x2c(%rsp), %r15d
movl 0x28(%rsp), %r14d
jmp 0x313c
movq %rax, %rbx
movq %rbp, %rdi
callq 0x21b0
jmp 0x3432
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x2090
jmp 0x345e
movq %rax, %rbx
movq %rbp, %rdi
callq 0x21b0
jmp 0x345e
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x2270
movq %rbx, %rdi
callq 0x22e0