// File: 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:
subq $0x588, %rsp            # imm = 0x588
movl %r9d, 0x34(%rsp)
movl %r8d, %eax
movl %ecx, %r8d
movl 0x34(%rsp), %ecx
movl %r8d, 0x38(%rsp)
movl %edx, 0x3c(%rsp)
movl %esi, %r8d
movl 0x38(%rsp), %esi
movq %rdi, %r9
movl 0x3c(%rsp), %edi
movb %al, %dl
movb 0x590(%rsp), %al
movq %r9, 0x580(%rsp)
movl %r8d, 0x57c(%rsp)
movsd %xmm0, 0x570(%rsp)
movsd %xmm1, 0x568(%rsp)
movl %edi, 0x564(%rsp)
movl %esi, 0x560(%rsp)
andb $0x1, %dl
movb %dl, 0x55f(%rsp)
andb $0x1, %cl
movb %cl, 0x55e(%rsp)
andb $0x1, %al
movb %al, 0x55d(%rsp)
callq 0x21a0
movq %rax, 0x550(%rsp)
movq 0x580(%rsp), %rdi
leaq 0x54c(%rsp), %rsi
leaq 0x548(%rsp), %rdx
leaq 0x540(%rsp), %rcx
leaq 0x528(%rsp), %r8
leaq 0x538(%rsp), %r9
leaq 0x520(%rsp), %rax
movq %rax, (%rsp)
callq 0x2e40
movq 0x540(%rsp), %rdi
movq 0x528(%rsp), %rsi
movq 0x538(%rsp), %rax
movl (%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %r8d
leaq 0x530(%rsp), %rdx
leaq 0x518(%rsp), %rcx
callq 0x30a0
leaq 0x4b0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x580(%rsp), %rdi
callq 0x2100
movq %rax, 0x48(%rsp)
movl 0x57c(%rsp), %eax
movl %eax, 0x54(%rsp)
movsd 0x570(%rsp), %xmm0
movsd 0x4ad2(%rip), %xmm1     # 0x8068
mulsd %xmm1, %xmm0
movsd 0x4abe(%rip), %xmm1     # 0x8060
addsd %xmm1, %xmm0
callq 0x2110
cvttsd2si %xmm0, %eax
movl %eax, 0x58(%rsp)
movsd 0x568(%rsp), %xmm0
movsd 0x4a94(%rip), %xmm1     # 0x8058
mulsd %xmm1, %xmm0
movsd 0x4a90(%rip), %xmm1     # 0x8060
addsd %xmm1, %xmm0
callq 0x2110
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rdx
movl 0x54(%rsp), %ecx
movl 0x58(%rsp), %r8d
cvttsd2si %xmm0, %r9d
movl 0x564(%rsp), %eax
leaq 0x4b70(%rip), %rsi      # 0x816f
movl %eax, (%rsp)
movb $0x0, %al
callq 0x2060
movq $0x0, 0x4a8(%rsp)
testb $0x1, 0x55d(%rsp)
je 0x372c
leaq 0x2a0(%rsp), %rdi
leaq 0x4b0(%rsp), %rsi
movl $0x4, %edx
callq 0x2380
movq 0x2a0(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x2a0(%rsp,%rax), %rdi
callq 0x2270
movb %al, 0x33(%rsp)
jmp 0x365c
movb 0x33(%rsp), %al
testb $0x1, %al
jne 0x3669
jmp 0x370b
movl $0x18, %edi
callq 0x2210
movq %rax, 0x28(%rsp)
jmp 0x367a
movq 0x28(%rsp), %rdi
leaq 0x2a0(%rsp), %rsi
callq 0x4550
jmp 0x368e
movq 0x28(%rsp), %rax
movq %rax, 0x4a8(%rsp)
movq 0x4a8(%rsp), %rdi
callq 0x4620
movl %eax, 0x24(%rsp)
jmp 0x36ae
movl 0x24(%rsp), %esi
incl %esi
leaq 0x4adf(%rip), %rdi      # 0x819a
xorl %eax, %eax
callq 0x2040
jmp 0x36c4
leaq 0x2a0(%rsp), %rdi
callq 0x2080
jmp 0x36d3
jmp 0x370b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
jmp 0x371a
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
callq 0x2200
jmp 0x371a
leaq 0x2a0(%rsp), %rdi
callq 0x20a0
jmp 0x372c
leaq 0x2a0(%rsp), %rdi
callq 0x20a0
jmp 0x3a4f
cmpq $0x0, 0x4a8(%rsp)
jne 0x37be
movl $0x18, %edi
callq 0x2210
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movl 0x548(%rsp), %esi
movl 0x54c(%rsp), %edx
movl 0x57c(%rsp), %ecx
movsd 0x570(%rsp), %xmm0
movsd 0x568(%rsp), %xmm1
movl 0x564(%rsp), %r8d
movl $0x8, %r9d
callq 0x4630
jmp 0x378c
movq 0x18(%rsp), %rax
movq %rax, 0x4a8(%rsp)
jmp 0x37be
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
callq 0x2200
jmp 0x3a4f
movq 0x540(%rsp), %rax
movl (%rax), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl %eax, %esi
movl 0x54c(%rsp), %edx
movl 0x548(%rsp), %ecx
movl 0x57c(%rsp), %r8d
movsd 0x570(%rsp), %xmm0
movsd 0x568(%rsp), %xmm1
movl 0x564(%rsp), %r9d
leaq 0x49a9(%rip), %rdi      # 0x81b2
movb $0x2, %al
callq 0x2040
movq 0x4a8(%rsp), %rdi
callq 0x4620
cmpl 0x560(%rsp), %eax
jge 0x3966
callq 0x21a0
movq %rax, 0x288(%rsp)
movq 0x4a8(%rsp), %rdi
movq 0x540(%rsp), %rsi
movq 0x528(%rsp), %rdx
movb 0x55f(%rsp), %al
movl $0x1, %ecx
andb $0x1, %al
movzbl %al, %r8d
callq 0x4700
callq 0x21a0
movq %rax, 0x280(%rsp)
movq 0x4a8(%rsp), %rdi
movq 0x538(%rsp), %rsi
movq 0x520(%rsp), %rdx
callq 0x48d0
movsd %xmm0, 0x278(%rsp)
callq 0x21a0
movq %rax, 0x270(%rsp)
movq 0x4a8(%rsp), %rdi
movq 0x530(%rsp), %rsi
movq 0x518(%rsp), %rdx
callq 0x48d0
movsd %xmm0, 0x268(%rsp)
movq 0x4a8(%rsp), %rdi
callq 0x4620
movl %eax, %esi
leaq 0x4925(%rip), %rdi      # 0x8207
movb $0x0, %al
callq 0x2040
movq 0x280(%rsp), %rax
subq 0x288(%rsp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, %rsi
movq 0x270(%rsp), %rax
subq 0x280(%rsp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, %rdx
leaq 0x4902(%rip), %rdi      # 0x822c
movb $0x0, %al
callq 0x2040
movsd 0x4737(%rip), %xmm0     # 0x8070
mulsd 0x268(%rsp), %xmm0
movsd 0x4726(%rip), %xmm1     # 0x8070
mulsd 0x278(%rsp), %xmm1
leaq 0x48e7(%rip), %rdi      # 0x8241
movb $0x2, %al
callq 0x2040
jmp 0x3810
testb $0x1, 0x55e(%rsp)
je 0x39d8
leaq 0x68(%rsp), %rdi
movq %rdi, 0x10(%rsp)
leaq 0x4b0(%rsp), %rsi
movl $0x4, %edx
callq 0x2230
movq 0x10(%rsp), %rsi
movq 0x4a8(%rsp), %rdi
callq 0x4980
jmp 0x39a0
leaq 0x68(%rsp), %rdi
callq 0x22e0
jmp 0x39ac
leaq 0x68(%rsp), %rdi
callq 0x22f0
jmp 0x39d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x22f0
jmp 0x3a4f
callq 0x21a0
subq 0x550(%rsp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl $0x3c, %ecx
cltd
idivl %ecx
movl %eax, 0x60(%rsp)
movl 0x60(%rsp), %eax
movl $0x3c, %ecx
cltd
idivl %ecx
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %esi
movl 0x60(%rsp), %eax
movl $0x3c, %ecx
cltd
idivl %ecx
movl %edx, 0xc(%rsp)
movl 0x64(%rsp), %eax
movl $0x3c, %ecx
cltd
idivl %ecx
movl %edx, %ecx
movl 0xc(%rsp), %edx
leaq 0x4818(%rip), %rdi      # 0x8258
movb $0x0, %al
callq 0x2040
addq $0x588, %rsp            # imm = 0x588
retq
movq 0x298(%rsp), %rdi
callq 0x2360
nopl (%rax)