// File: adrianphoulady[P]weighted-tsetlin-machine-cpp/./utils.h
// Function: load_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, array2d<unsigned int>*&, array1d<int>*&)
int load_file(std::string const &fname, array2d<word> *&x, array1d<int> *&y) {
    std::ifstream fin(fname);
    std::vector<std::vector<int>> data;
    if (!fin) {
        printf("File %s is missing!\n", fname.c_str());
        exit(1);
    }
    for (std::string line; std::getline(fin, line); ) {
        std::istringstream iss{line};
        data.emplace_back(std::vector<int>{});
        std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(data.back()));
    }
    fin.close();

    int features = data[0].size() - 1, samples = data.size(), literal_words = (2 * features + word_bits - 1) / word_bits;
    x = new array2d<word>{samples, literal_words};
    y = new array1d<int>{samples};
    std::fill((*x)(0), (*x)(samples), 0);
    for (int s = 0; s < samples; ++s) {
        if (data[s].size() != features + 1u) {
            printf("Inconsistent sample at line %d of %s!", s + 1, fname.c_str());
            exit(2);
        }
        for (int f = 0; f < features; ++f) {
            int l = f + !data[s][f] * features;
            (*x)(s, l / word_bits) |= (word) 1 << l % word_bits;
        }
        (*y)(s) = data[s].back();
    }

    return features;
}

// Assembly:
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdi, 0x4d0(%rsp)
movq %rsi, 0x4c8(%rsp)
movq %rdx, 0x4c0(%rsp)
movq 0x4d0(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
movl $0x8, %edx
callq 0x2320
leaq 0x2a0(%rsp), %rdi
callq 0x3e00
movq 0x2b8(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x2b8(%rsp,%rax), %rdi
callq 0x2130
movb %al, 0x83(%rsp)
jmp 0x2888
movb 0x83(%rsp), %al
testb $0x1, %al
jne 0x2895
jmp 0x28d8
movq 0x4d0(%rsp), %rdi
callq 0x2100
movq %rax, %rsi
leaq 0x586b(%rip), %rdi      # 0x8117
xorl %eax, %eax
callq 0x2040
jmp 0x28b5
movl $0x1, %edi
callq 0x2250
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
jmp 0x2e12
leaq 0x270(%rsp), %rdi
callq 0x22c0
leaq 0x2b8(%rsp), %rdi
leaq 0x270(%rsp), %rsi
callq 0x2070
movq %rax, 0x78(%rsp)
jmp 0x2901
movq 0x78(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2270
movb %al, 0x77(%rsp)
jmp 0x291b
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0x2950
jmp 0x2925
leaq 0x270(%rsp), %rdi
callq 0x2190
jmp 0x2a9e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
jmp 0x2a8c
leaq 0xf0(%rsp), %rdi
leaq 0x270(%rsp), %rsi
movl $0x8, %edx
callq 0x2340
jmp 0x296c
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movq $0x0, 0xe0(%rsp)
leaq 0xd0(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x3e80
movq 0x68(%rsp), %rsi
leaq 0x2a0(%rsp), %rdi
callq 0x3e10
jmp 0x29a9
leaq 0xd0(%rsp), %rdi
callq 0x3e90
leaq 0xc0(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x3f80
jmp 0x29cd
leaq 0xb0(%rsp), %rdi
callq 0x3fb0
jmp 0x29dc
leaq 0x2a0(%rsp), %rdi
callq 0x4000
movq %rax, %rdi
callq 0x3fd0
movq %rax, 0x60(%rsp)
jmp 0x29f8
movq 0x60(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x3ef0
movq %rax, 0x58(%rsp)
jmp 0x2a29
movq 0x58(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x2140
jmp 0x28e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x3e90
jmp 0x2a7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x2140
leaq 0x270(%rsp), %rdi
callq 0x2190
jmp 0x2e12
leaq 0x2b8(%rsp), %rdi
callq 0x2080
jmp 0x2aad
xorl %eax, %eax
movl %eax, %esi
leaq 0x2a0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x4040
movq %rax, %rdi
callq 0x4060
movq 0x48(%rsp), %rdi
decl %eax
movl %eax, 0x9c(%rsp)
callq 0x4080
movl %eax, 0x98(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, %ecx
leal 0x1f(%rcx,%rcx), %eax
leal 0x3e(%rcx,%rcx), %ecx
testl %eax, %eax
cmovsl %ecx, %eax
sarl $0x5, %eax
movl %eax, 0x94(%rsp)
movl $0x10, %edi
callq 0x2210
movq %rax, 0x50(%rsp)
jmp 0x2b16
movq 0x50(%rsp), %rdi
movl 0x98(%rsp), %esi
movl 0x94(%rsp), %edx
callq 0x40a0
jmp 0x2b30
movq 0x50(%rsp), %rcx
movq 0x4c8(%rsp), %rax
movq %rcx, (%rax)
movl $0x10, %edi
callq 0x2210
movq %rax, 0x40(%rsp)
jmp 0x2b51
movq 0x40(%rsp), %rdi
movl 0x98(%rsp), %esi
callq 0x40f0
jmp 0x2b64
movq 0x40(%rsp), %rcx
movq 0x4c0(%rsp), %rax
movq %rcx, (%rax)
movq 0x4c8(%rsp), %rax
movq (%rax), %rdi
xorl %esi, %esi
callq 0x4160
movq %rax, 0x38(%rsp)
jmp 0x2b8d
movq 0x4c8(%rsp), %rax
movq (%rax), %rdi
movl 0x98(%rsp), %esi
callq 0x4160
movq %rax, 0x30(%rsp)
jmp 0x2bab
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdi
movl $0x0, 0x90(%rsp)
leaq 0x90(%rsp), %rdx
callq 0x4130
jmp 0x2bcf
movl $0x0, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
cmpl 0x98(%rsp), %eax
jge 0x2de1
movslq 0x8c(%rsp), %rsi
leaq 0x2a0(%rsp), %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4060
movl 0x9c(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
je 0x2ca1
movl 0x8c(%rsp), %eax
incl %eax
movl %eax, 0x2c(%rsp)
movq 0x4d0(%rsp), %rdi
callq 0x2100
movl 0x2c(%rsp), %esi
movq %rax, %rdx
leaq 0x54e4(%rip), %rdi      # 0x812c
xorl %eax, %eax
callq 0x2040
jmp 0x2c51
movl $0x2, %edi
callq 0x2250
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
callq 0x2200
jmp 0x2e12
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x298(%rsp)
movl %eax, 0x294(%rsp)
callq 0x2200
jmp 0x2e12
movl $0x0, 0x88(%rsp)
movl 0x88(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x2d7f
movl 0x88(%rsp), %eax
movl %eax, 0x18(%rsp)
movslq 0x8c(%rsp), %rsi
leaq 0x2a0(%rsp), %rdi
callq 0x4040
movq %rax, %rdi
movslq 0x88(%rsp), %rsi
callq 0x4190
movq %rax, %rcx
movl 0x18(%rsp), %eax
movl (%rcx), %ecx
testl %ecx, %ecx
sete %cl
movzbl %cl, %ecx
movl 0x9c(%rsp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
movl %eax, %ecx
addl $0x1f, %ecx
testl %eax, %eax
movl %eax, %edx
cmovsl %ecx, %edx
movl %edx, %ecx
andl $-0x20, %ecx
subl %ecx, %eax
movb %al, %cl
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x1c(%rsp)
movq 0x4c8(%rsp), %rax
movq (%rax), %rdi
movl 0x8c(%rsp), %esi
sarl $0x5, %edx
callq 0x41b0
movq %rax, 0x20(%rsp)
jmp 0x2d5c
movq 0x20(%rsp), %rax
movl 0x1c(%rsp), %ecx
orl (%rax), %ecx
movl %ecx, (%rax)
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x2cac
movslq 0x8c(%rsp), %rsi
leaq 0x2a0(%rsp), %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41e0
movl (%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x4c0(%rsp), %rax
movq (%rax), %rdi
movl 0x8c(%rsp), %esi
callq 0x4220
movq %rax, 0x10(%rsp)
jmp 0x2dc0
movq 0x10(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x2bda
movl 0x9c(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x2a0(%rsp), %rdi
callq 0x4240
leaq 0x2b8(%rsp), %rdi
callq 0x20a0
movl 0x8(%rsp), %eax
addq $0x4d8, %rsp            # imm = 0x4D8
retq
leaq 0x2a0(%rsp), %rdi
callq 0x4240
leaq 0x2b8(%rsp), %rdi
callq 0x20a0
movq 0x298(%rsp), %rdi
callq 0x2360
nopl (%rax)