// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/htslib/sam.c
// Function: sam_format1
int sam_format1(const bam_hdr_t *h, const bam1_t *b, kstring_t *str)
{
    int i;
    uint8_t *s;
    const bam1_core_t *c = &b->core;

    str->l = 0;
    kputsn(bam_get_qname(b), c->l_qname-1, str); kputc('\t', str); // query name
    kputw(c->flag, str); kputc('\t', str); // flag
    if (c->tid >= 0) { // chr
        kputs(h->target_name[c->tid] , str);
        kputc('\t', str);
    } else kputsn("*\t", 2, str);
    kputw(c->pos + 1, str); kputc('\t', str); // pos
    kputw(c->qual, str); kputc('\t', str); // qual
    if (c->n_cigar) { // cigar
        uint32_t *cigar = bam_get_cigar(b);
        for (i = 0; i < c->n_cigar; ++i) {
            kputw(bam_cigar_oplen(cigar[i]), str);
            kputc(bam_cigar_opchr(cigar[i]), str);
        }
    } else kputc('*', str);
    kputc('\t', str);
    if (c->mtid < 0) kputsn("*\t", 2, str); // mate chr
    else if (c->mtid == c->tid) kputsn("=\t", 2, str);
    else {
        kputs(h->target_name[c->mtid], str);
        kputc('\t', str);
    }
    kputw(c->mpos + 1, str); kputc('\t', str); // mate pos
    kputw(c->isize, str); kputc('\t', str); // template len
    if (c->l_qseq) { // seq and qual
        uint8_t *s = bam_get_seq(b);
        for (i = 0; i < c->l_qseq; ++i) kputc("=ACMGRSVTWYHKDBN"[bam_seqi(s, i)], str);
        kputc('\t', str);
        s = bam_get_qual(b);
        if (s[0] == 0xff) kputc('*', str);
        else for (i = 0; i < c->l_qseq; ++i) kputc(s[i] + 33, str);
    } else kputsn("*\t*", 3, str);

    // FIXME change "s+N <= b->data+b->l_data" to "b->data+b->l_data - s >= N"
    // (or equivalent) everywhere to avoid looking past the end of the array
    s = bam_get_aux(b); // aux
    while (s+4 <= b->data + b->l_data) {
        uint8_t type, key[2];
        key[0] = s[0]; key[1] = s[1];
        s += 2; type = *s++;
        kputc('\t', str); kputsn((char*)key, 2, str); kputc(':', str);
        if (type == 'A') {
            kputsn("A:", 2, str);
            kputc(*s, str);
            ++s;
        } else if (type == 'C') {
            kputsn("i:", 2, str);
            kputw(*s, str);
            ++s;
        } else if (type == 'c') {
            kputsn("i:", 2, str);
            kputw(*(int8_t*)s, str);
            ++s;
        } else if (type == 'S') {
            if (s+2 <= b->data + b->l_data) {
                kputsn("i:", 2, str);
                kputw(*(uint16_t*)s, str);
                s += 2;
            } else return -1;
        } else if (type == 's') {
            if (s+2 <= b->data + b->l_data) {
                kputsn("i:", 2, str);
                kputw(*(int16_t*)s, str);
                s += 2;
            } else return -1;
        } else if (type == 'I') {
            if (s+4 <= b->data + b->l_data) {
                kputsn("i:", 2, str);
                kputuw(*(uint32_t*)s, str);
                s += 4;
            } else return -1;
        } else if (type == 'i') {
            if (s+4 <= b->data + b->l_data) {
                kputsn("i:", 2, str);
                kputw(*(int32_t*)s, str);
                s += 4;
            } else return -1;
        } else if (type == 'f') {
            if (s+4 <= b->data + b->l_data) {
                ksprintf(str, "f:%g", *(float*)s);
                s += 4;
            } else return -1;

        } else if (type == 'd') {
            if (s+8 <= b->data + b->l_data) {
                ksprintf(str, "d:%g", *(double*)s);
                s += 8;
            } else return -1;
        } else if (type == 'Z' || type == 'H') {
            kputc(type, str); kputc(':', str);
            while (s < b->data + b->l_data && *s) kputc(*s++, str);
            if (s >= b->data + b->l_data)
                return -1;
            ++s;
        } else if (type == 'B') {
            uint8_t sub_type = *(s++);
            int sub_type_size = aux_type2size(sub_type);
            uint32_t n;
            if (sub_type_size == 0 || b->data + b->l_data - s < 4)
                return -1;
            memcpy(&n, s, 4);
            s += 4; // now points to the start of the array
            if ((b->data + b->l_data - s) / sub_type_size < n)
                return -1;
            kputsn("B:", 2, str); kputc(sub_type, str); // write the typing
            for (i = 0; i < n; ++i) { // FIXME: for better performance, put the loop after "if"
                kputc(',', str);
                if ('c' == sub_type)      { kputw(*(int8_t*)s, str); ++s; }
                else if ('C' == sub_type) { kputw(*(uint8_t*)s, str); ++s; }
                else if ('s' == sub_type) { kputw(*(int16_t*)s, str); s += 2; }
                else if ('S' == sub_type) { kputw(*(uint16_t*)s, str); s += 2; }
                else if ('i' == sub_type) { kputw(*(int32_t*)s, str); s += 4; }
                else if ('I' == sub_type) { kputuw(*(uint32_t*)s, str); s += 4; }
                else if ('f' == sub_type) { ksprintf(str, "%g", *(float*)s); s += 4; }
                else return -1;
            }
        }
    }
    return str->l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
andq $0x0, (%rdx)
movq %rdi, %r15
movq 0x28(%rsi), %rdi
movzbl 0xb(%rsi), %esi
decl %esi
callq 0x12090
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x120dc
movzwl 0xc(%r14), %edi
movq %rbx, %rsi
callq 0x12327
movl %ebp, %edi
movq %rbx, %rsi
callq 0x120dc
movslq (%r14), %rax
testq %rax, %rax
js 0x13451
movq 0x20(%r15), %rcx
movq (%rcx,%rax,8), %rdi
movq %rbx, %rsi
callq 0x12304
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x120dc
jmp 0x13463
leaq 0x311eb(%rip), %rdi     # 0x44643
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movl 0x4(%r14), %edi
incl %edi
movq %rbx, %rsi
callq 0x12327
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x120dc
movzbl 0xa(%r14), %edi
movq %rbx, %rsi
callq 0x12327
movl %ebp, %edi
movq %rbx, %rsi
callq 0x120dc
movq 0x8(%r14), %rax
movq %rax, %rcx
shrq $0x30, %rcx
jne 0x134cf
pushq $0x2a
popq %rdi
movq %rbx, %rsi
callq 0x120dc
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x120dc
movslq 0x14(%r14), %rax
testq %rax, %rax
js 0x13518
cmpl (%r14), %eax
jne 0x1352c
leaq 0x31179(%rip), %rdi     # 0x44646
jmp 0x1351f
movl %eax, %r12d
shrl $0x18, %r12d
addq 0x28(%r14), %r12
xorl %r13d, %r13d
leaq 0x30e5d(%rip), %rbp     # 0x44341
shrq $0x30, %rax
cmpq %rax, %r13
jae 0x134ad
movl (%r12,%r13,4), %edi
shrl $0x4, %edi
movq %rbx, %rsi
callq 0x12327
movl (%r12,%r13,4), %eax
andl $0xf, %eax
movsbl (%rax,%rbp), %edi
movq %rbx, %rsi
callq 0x120dc
incq %r13
movq 0x8(%r14), %rax
jmp 0x134e4
leaq 0x31124(%rip), %rdi     # 0x44643
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
jmp 0x13547
movq 0x20(%r15), %rcx
movq (%rcx,%rax,8), %rdi
movq %rbx, %rsi
callq 0x12304
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x120dc
movl 0x18(%r14), %edi
incl %edi
movq %rbx, %rsi
callq 0x12327
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x120dc
movl 0x1c(%r14), %edi
movq %rbx, %rsi
callq 0x12327
movl %ebp, %edi
movq %rbx, %rsi
callq 0x120dc
movl 0x10(%r14), %eax
testl %eax, %eax
je 0x1363c
movq 0x8(%r14), %r15
movq %r15, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x28(%r14), %rcx
shrl $0x18, %r15d
addq %rcx, %r15
xorl %ebp, %ebp
leaq 0x310a2(%rip), %r12     # 0x44649
xorl %r13d, %r13d
cmpl %eax, %r13d
jge 0x135de
movl %r13d, %eax
shrl %eax
movzbl (%r15,%rax), %eax
movl %ebp, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %eax
andl $0xf, %eax
movsbl (%rax,%r12), %edi
movq %rbx, %rsi
callq 0x120dc
incl %r13d
movl 0x10(%r14), %eax
addl $0x4, %ebp
jmp 0x135aa
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x120dc
movq 0x8(%r14), %r15
movq %r15, %rax
shrq $0x30, %rax
shlq $0x2, %rax
addq 0x28(%r14), %rax
shrl $0x18, %r15d
addq %rax, %r15
movl 0x10(%r14), %eax
leal 0x1(%rax), %ecx
sarl %ecx
movslq %ecx, %rcx
cmpb $-0x1, (%rcx,%r15)
je 0x13650
addq %rcx, %r15
xorl %r12d, %r12d
cltq
cmpq %rax, %r12
jge 0x1365b
movzbl (%r15,%r12), %edi
addl $0x21, %edi
movq %rbx, %rsi
callq 0x120dc
incq %r12
movl 0x10(%r14), %eax
jmp 0x1361c
leaq 0x31017(%rip), %rdi     # 0x4465a
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x12090
jmp 0x1365b
pushq $0x2a
popq %rdi
movq %rbx, %rsi
callq 0x120dc
movq 0x8(%r14), %rcx
movq 0x28(%r14), %rax
movq %rcx, %rdx
shrq $0x30, %rdx
leaq (%rax,%rdx,4), %rdx
shrl $0x18, %ecx
addq %rdx, %rcx
movslq 0x10(%r14), %rdx
leal 0x1(%rdx), %esi
sarl %esi
movslq %esi, %r12
addq %rcx, %r12
addq %rdx, %r12
leaq 0x4(%r12), %rbp
movslq 0x20(%r14), %rcx
addq %rax, %rcx
cmpq %rcx, %rbp
ja 0x13a26
movb (%r12), %al
movb %al, 0x6(%rsp)
movb 0x1(%r12), %al
movb %al, 0x7(%rsp)
leaq 0x3(%r12), %r13
movzbl 0x2(%r12), %r15d
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x120dc
leaq 0x6(%rsp), %rdi
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
pushq $0x3a
popq %rdi
movq %rbx, %rsi
callq 0x120dc
leal -0x41(%r15), %eax
cmpl $0x8, %eax
jbe 0x1371b
leal -0x63(%r15), %eax
cmpl $0x6, %eax
ja 0x1374f
leaq 0x30a9a(%rip), %rcx     # 0x44190
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x30f5b(%rip), %rdi     # 0x44661
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movsbl (%r13), %edi
jmp 0x137e9
leaq 0x30a4a(%rip), %rcx     # 0x4416c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x30f2c(%rip), %rdi     # 0x4465e
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movzbl (%r13), %edi
movq %rbx, %rsi
callq 0x120dc
jmp 0x13a15
cmpl $0x53, %r15d
je 0x139e2
cmpl $0x5a, %r15d
je 0x1379a
cmpl $0x73, %r15d
jne 0x139dd
addq $0x5, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x13a21
leaq 0x30edc(%rip), %rdi     # 0x44661
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movswl (%r13), %edi
jmp 0x13a0a
movl %r15d, %edi
movq %rbx, %rsi
callq 0x120dc
pushq $0x3a
popq %rdi
movq %rbx, %rsi
callq 0x120dc
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r13
jae 0x13a21
movzbl (%r13), %edi
incq %r13
testl %edi, %edi
jne 0x137a8
jmp 0x139dd
leaq 0x30e88(%rip), %rdi     # 0x44661
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movzbl (%r13), %edi
movq %rbx, %rsi
callq 0x12327
jmp 0x13a15
movzbl (%r13), %r15d
movl %r15d, %edi
callq 0x13ab9
testl %eax, %eax
je 0x13a21
movslq 0x20(%r14), %rcx
addq 0x28(%r14), %rcx
movq %rcx, %rdx
subq %rbp, %rdx
cmpq $0x4, %rdx
jl 0x13a21
movl 0x4(%r12), %r13d
addq $0x8, %r12
subq %r12, %rcx
movl %eax, %esi
movq %rcx, %rax
cqto
idivq %rsi
cmpq %r13, %rax
jl 0x13a21
leaq 0x30e25(%rip), %rdi     # 0x4466e
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movl %r15d, %edi
movq %rbx, %rsi
callq 0x120dc
pushq $0x2c
popq %rbp
subl $0x1, %r13d
jb 0x139da
movl %ebp, %edi
movq %rbx, %rsi
callq 0x120dc
cmpl $0x43, %r15d
je 0x1390c
cmpl $0x49, %r15d
je 0x138fc
cmpl $0x53, %r15d
je 0x138bd
cmpl $0x73, %r15d
je 0x138dd
cmpl $0x66, %r15d
je 0x138c4
cmpl $0x69, %r15d
je 0x138ee
cmpl $0x63, %r15d
jne 0x13a21
movsbl (%r12), %edi
movq %rbx, %rsi
callq 0x12327
pushq $0x1
popq %rax
pushq $0x2c
popq %rbp
jmp 0x1391c
movzwl (%r12), %edi
jmp 0x138e2
cvtss2sd (%r12), %xmm0
movq %rbx, %rdi
leaq 0x30d97(%rip), %rsi     # 0x4466b
movb $0x1, %al
callq 0x35818
jmp 0x13908
movswl (%r12), %edi
movq %rbx, %rsi
callq 0x12327
pushq $0x2
jmp 0x1391b
movl (%r12), %edi
movq %rbx, %rsi
callq 0x12327
jmp 0x13908
movl (%r12), %edi
movq %rbx, %rsi
callq 0x13a37
pushq $0x4
jmp 0x1391b
movzbl (%r12), %edi
movq %rbx, %rsi
callq 0x12327
pushq $0x1
popq %rax
addq %rax, %r12
jmp 0x13862
addq $0x7, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x13a21
leaq 0x30d21(%rip), %rdi     # 0x44661
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movl (%r13), %edi
movq %rbx, %rsi
callq 0x13a37
jmp 0x13a12
addq $0xb, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x13a21
movsd (%r13), %xmm0
movq %rbx, %rdi
leaq 0x30ce8(%rip), %rsi     # 0x44669
jmp 0x139d1
addq $0x7, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x13a21
leaq 0x30cc2(%rip), %rdi     # 0x44661
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movl (%r13), %edi
jmp 0x13a0a
addq $0x7, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x13a21
cvtss2sd (%r13), %xmm0
movq %rbx, %rdi
leaq 0x30c93(%rip), %rsi     # 0x44664
movb $0x1, %al
callq 0x35818
jmp 0x13a12
movq %r12, %r13
movq %r13, %rbp
jmp 0x13a15
addq $0x5, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x13a21
leaq 0x30c67(%rip), %rdi     # 0x44661
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x12090
movzwl (%r13), %edi
movq %rbx, %rsi
callq 0x12327
movq %r12, %rbp
movq 0x28(%r14), %rax
movq %rbp, %r12
jmp 0x13686
pushq $-0x1
popq %rax
jmp 0x13a28
movl (%rbx), %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq