// File: karel-brinda[P]ococo/src/stats.h
// Function: ococo::stats_t<unsigned short, 3, 4>::print_pileup_line(_IO_FILE*, int, long, ococo::pos_stats_uncompr_t const&) const
int stats_t<T, counter_size, refbase_size>::print_pileup_line(
    FILE *pileup_file, int32_t seqid, int64_t pos,
    const pos_stats_uncompr_t &psu) const {
    assert(check_allocation());
    assert(pileup_file != nullptr);

    // todo: fix situation when depth is larger (use the printing buffer more
    // timess)

    const int32_t max_depth = 1000;

    assert(psu.sum < max_depth);
    char bases[max_depth];
    char qualities[max_depth];

    char ref_nt256 = nt16_nt256[psu.nt16];

    if (psu.sum == 0) {
        return 0;
    }

    if (ref_nt256 == '=') {
        ref_nt256 = 'N';
    }

    int32_t j = 0;

    for (int32_t nt4 = 0; nt4 < 4; nt4++) {
        const char filling_char =
            nt4_nt16[nt4] == psu.nt16 ? '.' : nt4_nt256[nt4];
        for (int32_t i = 0; i < psu.counters[nt4]; i++, j++) {
            bases[j]     = filling_char;
            qualities[j] = '~';
        }
    }

    if (psu.sum >= max_depth) {
        ococo::error("Too high coverage at position %" PRId64
                     ". Pileup does not support coverage higher than %" PRId32
                     ".",
                     pos, max_depth);
        return -1;
    }

    bases[j]     = '\0';
    qualities[j] = '\0';

    fprintf(pileup_file, "%s\t%" PRId64 "\t%c\t%" PRId32 "\t%s\t%s\n",
            seq_name[seqid].c_str(), pos + 1, ref_nt256, psu.sum, bases,
            qualities);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x850, %rsp            # imm = 0x850
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x830(%rbp)
callq 0x9440
testb $0x1, %al
jne 0xb054
jmp 0xb056
jmp 0xb075
leaq 0x6d322(%rip), %rdi     # 0x7837f
leaq 0x6d32e(%rip), %rsi     # 0x78392
movl $0x211, %edx            # imm = 0x211
leaq 0x6dbdc(%rip), %rcx     # 0x78c4c
callq 0x4360
cmpq $0x0, -0x18(%rbp)
je 0xb07e
jmp 0xb09d
leaq 0x6dc79(%rip), %rdi     # 0x78cfe
leaq 0x6d306(%rip), %rsi     # 0x78392
movl $0x212, %edx            # imm = 0x212
leaq 0x6dbb4(%rip), %rcx     # 0x78c4c
callq 0x4360
movl $0x3e8, -0x34(%rbp)     # imm = 0x3E8
movq -0x30(%rbp), %rax
cmpl $0x3e8, 0x14(%rax)      # imm = 0x3E8
jge 0xb0b3
jmp 0xb0d2
leaq 0x6dc5b(%rip), %rdi     # 0x78d15
leaq 0x6d2d1(%rip), %rsi     # 0x78392
movl $0x219, %edx            # imm = 0x219
leaq 0x6db7f(%rip), %rcx     # 0x78c4c
callq 0x4360
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x6e9de(%rip), %rax     # 0x79ac0
movb (%rax,%rcx), %al
movb %al, -0x811(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jne 0xb101
movl $0x0, -0x4(%rbp)
jmp 0xb2bf
movsbl -0x811(%rbp), %eax
cmpl $0x3d, %eax
jne 0xb114
movb $0x4e, -0x811(%rbp)
movl $0x0, -0x818(%rbp)
movl $0x0, -0x81c(%rbp)
cmpl $0x4, -0x81c(%rbp)
jge 0xb1fd
movslq -0x81c(%rbp), %rcx
leaq 0x6ea9d(%rip), %rax     # 0x79be0
movzbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movzbl (%rcx), %ecx
cmpl %ecx, %eax
jne 0xb15f
movl $0x2e, %eax
movl %eax, -0x834(%rbp)
jmp 0xb177
movslq -0x81c(%rbp), %rcx
leaq 0x6ea78(%rip), %rax     # 0x79be5
movzbl (%rax,%rcx), %eax
movl %eax, -0x834(%rbp)
movl -0x834(%rbp), %eax
movb %al, -0x81d(%rbp)
movl $0x0, -0x824(%rbp)
movl -0x824(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x81c(%rbp), %rdx
cmpl 0x4(%rcx,%rdx,4), %eax
jge 0xb1e7
movb -0x81d(%rbp), %cl
movslq -0x818(%rbp), %rax
movb %cl, -0x420(%rbp,%rax)
movslq -0x818(%rbp), %rax
movb $0x7e, -0x810(%rbp,%rax)
movl -0x824(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x824(%rbp)
movl -0x818(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x818(%rbp)
jmp 0xb18d
jmp 0xb1e9
movl -0x81c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x81c(%rbp)
jmp 0xb128
movq -0x30(%rbp), %rax
cmpl $0x3e8, 0x14(%rax)      # imm = 0x3E8
jl 0xb22d
movq -0x28(%rbp), %rsi
leaq 0x6db14(%rip), %rdi     # 0x78d29
movl $0x3e8, %edx            # imm = 0x3E8
movb $0x0, %al
callq 0xf460
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xb2bf
movq -0x830(%rbp), %rax
movslq -0x818(%rbp), %rcx
movb $0x0, -0x420(%rbp,%rcx)
movslq -0x818(%rbp), %rcx
movb $0x0, -0x810(%rbp,%rcx)
movq -0x18(%rbp), %rcx
movq %rcx, -0x840(%rbp)
movq 0x18(%rax), %rdi
movslq -0x1c(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x4200
movq -0x840(%rbp), %rdi
movq %rax, %rdx
movq -0x28(%rbp), %rcx
addq $0x1, %rcx
movsbl -0x811(%rbp), %r8d
movq -0x30(%rbp), %rax
movl 0x14(%rax), %r9d
leaq -0x420(%rbp), %r10
leaq -0x810(%rbp), %rax
leaq 0x6dad5(%rip), %rsi     # 0x78d7d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x4830
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x850, %rsp            # imm = 0x850
popq %rbp
retq
nopl (%rax,%rax)