// File: karel-brinda[P]ococo/src/stats.h
// Function: ococo::stats_t<unsigned short, 3, 4>::load_fasta(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int stats_t<T, counter_size, refbase_size>::load_fasta(
    const std::string &fasta_fn) {
    gzFile fp;
    kseq_t *seq;
    int l;
    fp  = gzopen(fasta_fn.c_str(), "r");
    seq = kseq_init(fp);

    constexpr int32_t max_counter_value =
        ococo::right_full_mask<T, counter_size>();

    //if (errno != 0 || fp == nullptr) {
    if (fp == nullptr) {
        ococo::error("File '%s' could not be opened.\n", fasta_fn.c_str());
        return -1;
    }

    for (int seqid = 0; (l = kseq_read(seq)) >= 0; seqid++) {
        if (seq_name[seqid].compare(seq->name.s) != 0) {
            error(
                "Sequence names in BAM/SAM and in FASTA do not correspond "
                "('%s'!='%s').\n",
                seq_name[seqid].c_str(), seq->name.s);
            return -1;
        }

        if (seq_len[seqid] != static_cast<int64_t>(seq->seq.l)) {
            error(
                "Sequence lengths in BAM/SAM and in FASTA do not correspond "
                "(%" PRId64 "!=%" PRId64 ").\n",
                static_cast<int64_t>(seq->seq.l),
                static_cast<int64_t>(seq_len[seqid]));
            return -1;
        }

        if (seq->comment.l && seq_comment[seqid].empty()) {
            seq_comment[seqid] = std::string(seq->comment.s);
        }

        for (int64_t pos = 0; pos < static_cast<int64_t>(seq->seq.l); pos++) {
            assert(seq_stats[seqid][pos] == 0);

            pos_stats_uncompr_t psu = {0, {0, 0, 0, 0}, 0};
            psu.nt16 = nt256_nt16[static_cast<int32_t>(seq->seq.s[pos])];

            if (psu.nt16 != nt256_nt16[static_cast<int32_t>('N')]) {
                for (int32_t i = 0; i < 4; i++) {
                    psu.counters[i] = ((0x1 << i) & psu.nt16)
                                          ? std::min(params->init_ref_weight,
                                                     max_counter_value)
                                          : 0;
                }
            }

            psu.sum = psu.counters[0] + psu.counters[1] + psu.counters[2] +
                      psu.counters[3];
            seq_stats[seqid][pos] = compress_position_stats(psu);
        }
    }
    kseq_destroy(seq);  // STEP 5: destroy seq
    gzclose(fp);        // STEP 6: close the file handler
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4200
movq %rax, %rdi
leaq 0x7222e(%rip), %rsi     # 0x7b9ef
callq 0x4890
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4fa0
movq %rax, -0x28(%rbp)
movl $0x7, -0x30(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x980b
movq -0x18(%rbp), %rdi
callq 0x4200
movq %rax, %rsi
leaq 0x6edb8(%rip), %rdi     # 0x785b0
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9b40
movl $0x0, -0x34(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4fe0
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
jl 0x9b27
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rdi
movslq -0x34(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x4150
cmpl $0x0, %eax
je 0x988f
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rdi
movslq -0x34(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x4200
movq %rax, %rsi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x6ed54(%rip), %rdi     # 0x785d0
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9b40
movq -0xa0(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x28(%rbp), %rcx
cmpq 0x30(%rcx), %rax
je 0x98e1
movq -0xa0(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x30(%rcx), %rsi
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x6ed4a(%rip), %rdi     # 0x78618
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9b40
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x9999
movq -0xa0(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x34(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x49c0
testb $0x1, %al
jne 0x9914
jmp 0x9999
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xb0(%rbp)
leaq -0x59(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x4a50
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
leaq -0x58(%rbp), %rdi
callq 0x9bf0
jmp 0x994c
movq -0xa0(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x34(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
leaq -0x58(%rbp), %rsi
callq 0x46f0
leaq -0x58(%rbp), %rdi
callq 0x4340
leaq -0x59(%rbp), %rdi
callq 0x45f0
jmp 0x9999
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x59(%rbp), %rdi
callq 0x45f0
jmp 0x9b4c
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jge 0x9b17
movq -0xa0(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x78(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x99d5
jmp 0x99f4
leaq 0x6ec84(%rip), %rdi     # 0x78660
leaq 0x6e9af(%rip), %rsi     # 0x78392
movl $0xac, %edx
leaq 0x6ec8c(%rip), %rcx     # 0x7867b
callq 0x4360
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x42d0
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x78(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0x6ff8f(%rip), %rax     # 0x799b0
movb (%rax,%rcx), %al
movb %al, -0x90(%rbp)
movzbl -0x90(%rbp), %eax
movzbl 0x6ffc6(%rip), %ecx     # 0x799fe
cmpl %ecx, %eax
je 0x9ac4
movl $0x0, -0x94(%rbp)
cmpl $0x4, -0x94(%rbp)
jge 0x9ac2
movl -0x94(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movzbl -0x90(%rbp), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x9a93
movq -0xa0(%rbp), %rax
movq 0x30(%rax), %rdi
addq $0x1b8, %rdi            # imm = 0x1B8
leaq -0x30(%rbp), %rsi
callq 0x9eb0
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
jmp 0x9a9d
xorl %eax, %eax
movl %eax, -0xb4(%rbp)
jmp 0x9a9d
movl -0xb4(%rbp), %ecx
movslq -0x94(%rbp), %rax
movl %ecx, -0x8c(%rbp,%rax,4)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x9a4a
jmp 0x9ac4
movl -0x8c(%rbp), %eax
addl -0x88(%rbp), %eax
addl -0x84(%rbp), %eax
addl -0x80(%rbp), %eax
movl %eax, -0x7c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x9ef0
movw %ax, %dx
movq -0xa0(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x78(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x99a1
jmp 0x9b19
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x9812
movq -0x28(%rbp), %rdi
callq 0x53c0
movq -0x20(%rbp), %rdi
callq 0x49f0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x4a30
nopw %cs:(%rax,%rax)
nop