// File: karel-brinda[P]ococo/src/stats.h
// Function: ococo::stats_t<unsigned short, 3, 4>::import_stats(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int stats_t<T, counter_size, refbase_size>::import_stats(
    const std::string &stats_fn) {
    assert(check_allocation());

    int error_code = 0;

    FILE *fo = fopen(stats_fn.c_str(), "r");
    if (fo == nullptr) {
        ococo::error("File with statistics could not be opened ('%s').\n",
                     stats_fn.c_str());
        return -1;
    }

    /* number of seqs */
    int32_t n_seqs_loaded;
    fread(&n_seqs_loaded, sizeof(int32_t), 1, fo);

    if (n_seqs_loaded != n_seqs) {
        error(
            "Numbers of sequences in stats and SAM/BAM do not correspond "
            "%" PRId32 "!=%" PRId32 ").\n",
            n_seqs_loaded, n_seqs);
        return -1;
    }

    for (int seqid = 0; seqid < n_seqs; seqid++) {
        /* sequence */

        single_seq_serial_t seq_ser;
        fread(&seq_ser, sizeof(single_seq_serial_t), 1, fo);

        if (seq_ser.seq_active != seq_active[seqid]) {
            error(
                "Active sequences in stats and SAM/BAM do not correspond "
                "(seqid %" PRId32 ").\n",
                seqid);
            return -1;
        }

        if (seq_ser.seq_len != seq_len[seqid]) {
            error(
                "Sequence lengths in stats and SAM/BAM do not correspond "
                "(seqid %" PRId32 ", %" PRId64 "!=%" PRId64 ").\n",
                seqid, seq_ser.seq_len, seq_len[seqid]);
            return -1;
        }

        if (seq_name[seqid].compare(seq_ser.seq_name) != 0) {
            error(
                "Sequence names in stats and SAM/BAM do not correspond "
                "(seqid %" PRId32 ", '%s'!='%s').\n",
                seqid, seq_ser.seq_name, seq_name[seqid].c_str());
            return -1;
        }

        fread(seq_stats[seqid], sizeof(T), seq_len[seqid], fo);
    }

    error_code = fclose(fo);
    if (error_code != 0) {
        ococo::error("File with statistics could not be closed ('%s').\n",
                     stats_fn.c_str());
        return -1;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x830, %rsp            # imm = 0x830
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x818(%rbp)
callq 0x9440
testb $0x1, %al
jne 0x94f9
jmp 0x94fb
jmp 0x951a
leaq 0x6ee7d(%rip), %rdi     # 0x7837f
leaq 0x6ee89(%rip), %rsi     # 0x78392
movl $0x10c, %edx            # imm = 0x10C
leaq 0x6ee89(%rip), %rcx     # 0x7839e
callq 0x4360
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4200
movq %rax, %rdi
leaq 0x724bb(%rip), %rsi     # 0x7b9ef
callq 0x4690
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x956a
movq -0x18(%rbp), %rdi
callq 0x4200
movq %rax, %rsi
leaq 0x6eecc(%rip), %rdi     # 0x78423
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x977e
movq -0x28(%rbp), %rcx
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x48b0
movq -0x818(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl (%rcx), %eax
je 0x95b5
movq -0x818(%rbp), %rax
movl -0x2c(%rbp), %esi
movl (%rax), %edx
leaq 0x6eeb3(%rip), %rdi     # 0x78455
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x977e
movl $0x0, -0x30(%rbp)
movq -0x818(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl (%rcx), %eax
jge 0x9742
movq -0x28(%rbp), %rcx
leaq -0x810(%rbp), %rdi
movl $0x7e0, %esi            # imm = 0x7E0
movl $0x1, %edx
callq 0x48b0
movq -0x818(%rbp), %rcx
movb -0x810(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movq 0x8(%rcx), %rcx
movslq -0x30(%rbp), %rdx
movb (%rcx,%rdx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x962c
movl -0x30(%rbp), %esi
leaq 0x6ee82(%rip), %rdi     # 0x7849b
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x977e
movq -0x818(%rbp), %rcx
movq -0x808(%rbp), %rax
movq 0x10(%rcx), %rcx
movslq -0x30(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
je 0x967f
movq -0x818(%rbp), %rax
movl -0x30(%rbp), %esi
movq -0x808(%rbp), %rdx
movq 0x10(%rax), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0x6ee74(%rip), %rdi     # 0x784e0
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x977e
movq -0x818(%rbp), %rax
movq 0x18(%rax), %rdi
movslq -0x30(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
leaq -0x810(%rbp), %rsi
addq $0x10, %rsi
callq 0x4150
cmpl $0x0, %eax
je 0x9707
movq -0x818(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, -0x824(%rbp)
leaq -0x810(%rbp), %rcx
addq $0x10, %rcx
movq %rcx, -0x820(%rbp)
movq 0x18(%rax), %rdi
movslq -0x30(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x4200
movl -0x824(%rbp), %esi
movq -0x820(%rbp), %rdx
movq %rax, %rcx
leaq 0x6ee38(%rip), %rdi     # 0x7852f
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x977e
movq -0x818(%rbp), %rax
movq 0x28(%rax), %rcx
movslq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rdi
movq 0x10(%rax), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rcx
movl $0x2, %esi
callq 0x48b0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x95bc
movq -0x28(%rbp), %rdi
callq 0x4590
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x9777
movq -0x18(%rbp), %rdi
callq 0x4200
movq %rax, %rsi
leaq 0x6ee17(%rip), %rdi     # 0x7857e
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x977e
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x830, %rsp            # imm = 0x830
popq %rbp
retq
nopw (%rax,%rax)