// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/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 0x62774
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x62808
movzwl 0xc(%r14), %edi
movq %rbx, %rsi
callq 0x62a91
movl %ebp, %edi
movq %rbx, %rsi
callq 0x62808
movslq (%r14), %rax
testq %rax, %rax
js 0x63c46
movq 0x20(%r15), %rcx
movq (%rcx,%rax,8), %rdi
movq %rbx, %rsi
callq 0x62a6e
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x62808
jmp 0x63c58
leaq 0x259e8(%rip), %rdi     # 0x89635
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movl 0x4(%r14), %edi
incl %edi
movq %rbx, %rsi
callq 0x62a91
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x62808
movzbl 0xa(%r14), %edi
movq %rbx, %rsi
callq 0x62a91
movl %ebp, %edi
movq %rbx, %rsi
callq 0x62808
movq 0x8(%r14), %rax
movq %rax, %rcx
shrq $0x30, %rcx
jne 0x63cc4
pushq $0x2a
popq %rdi
movq %rbx, %rsi
callq 0x62808
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x62808
movslq 0x14(%r14), %rax
testq %rax, %rax
js 0x63d0d
cmpl (%r14), %eax
jne 0x63d21
leaq 0x25976(%rip), %rdi     # 0x89638
jmp 0x63d14
movl %eax, %r12d
shrl $0x18, %r12d
addq 0x28(%r14), %r12
xorl %r13d, %r13d
leaq 0x25698(%rip), %rbp     # 0x89371
shrq $0x30, %rax
cmpq %rax, %r13
jae 0x63ca2
movl (%r12,%r13,4), %edi
shrl $0x4, %edi
movq %rbx, %rsi
callq 0x62a91
movl (%r12,%r13,4), %eax
andl $0xf, %eax
movsbl (%rax,%rbp), %edi
movq %rbx, %rsi
callq 0x62808
incq %r13
movq 0x8(%r14), %rax
jmp 0x63cd9
leaq 0x25921(%rip), %rdi     # 0x89635
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
jmp 0x63d3c
movq 0x20(%r15), %rcx
movq (%rcx,%rax,8), %rdi
movq %rbx, %rsi
callq 0x62a6e
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x62808
movl 0x18(%r14), %edi
incl %edi
movq %rbx, %rsi
callq 0x62a91
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x62808
movl 0x1c(%r14), %edi
movq %rbx, %rsi
callq 0x62a91
movl %ebp, %edi
movq %rbx, %rsi
callq 0x62808
movl 0x10(%r14), %eax
testl %eax, %eax
je 0x63e31
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 0x2589f(%rip), %r12     # 0x8963b
xorl %r13d, %r13d
cmpl %eax, %r13d
jge 0x63dd3
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 0x62808
incl %r13d
movl 0x10(%r14), %eax
addl $0x4, %ebp
jmp 0x63d9f
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x62808
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 0x63e45
addq %rcx, %r15
xorl %r12d, %r12d
cltq
cmpq %rax, %r12
jge 0x63e50
movzbl (%r15,%r12), %edi
addl $0x21, %edi
movq %rbx, %rsi
callq 0x62808
incq %r12
movl 0x10(%r14), %eax
jmp 0x63e11
leaq 0x25814(%rip), %rdi     # 0x8964c
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x62774
jmp 0x63e50
pushq $0x2a
popq %rdi
movq %rbx, %rsi
callq 0x62808
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 0x6421b
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 0x62808
leaq 0x6(%rsp), %rdi
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
pushq $0x3a
popq %rdi
movq %rbx, %rsi
callq 0x62808
leal -0x41(%r15), %eax
cmpl $0x8, %eax
jbe 0x63f10
leal -0x63(%r15), %eax
cmpl $0x6, %eax
ja 0x63f44
leaq 0x252d5(%rip), %rcx     # 0x891c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x25758(%rip), %rdi     # 0x89653
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movsbl (%r13), %edi
jmp 0x63fde
leaq 0x25285(%rip), %rcx     # 0x8919c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x25729(%rip), %rdi     # 0x89650
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movzbl (%r13), %edi
movq %rbx, %rsi
callq 0x62808
jmp 0x6420a
cmpl $0x53, %r15d
je 0x641d7
cmpl $0x5a, %r15d
je 0x63f8f
cmpl $0x73, %r15d
jne 0x641d2
addq $0x5, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x64216
leaq 0x256d9(%rip), %rdi     # 0x89653
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movswl (%r13), %edi
jmp 0x641ff
movl %r15d, %edi
movq %rbx, %rsi
callq 0x62808
pushq $0x3a
popq %rdi
movq %rbx, %rsi
callq 0x62808
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r13
jae 0x64216
movzbl (%r13), %edi
incq %r13
testl %edi, %edi
jne 0x63f9d
jmp 0x641d2
leaq 0x25685(%rip), %rdi     # 0x89653
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movzbl (%r13), %edi
movq %rbx, %rsi
callq 0x62a91
jmp 0x6420a
movzbl (%r13), %r15d
movl %r15d, %edi
callq 0x642f6
testl %eax, %eax
je 0x64216
movslq 0x20(%r14), %rcx
addq 0x28(%r14), %rcx
movq %rcx, %rdx
subq %rbp, %rdx
cmpq $0x4, %rdx
jl 0x64216
movl 0x4(%r12), %r13d
addq $0x8, %r12
subq %r12, %rcx
movl %eax, %esi
movq %rcx, %rax
cqto
idivq %rsi
cmpq %r13, %rax
jl 0x64216
leaq 0x25622(%rip), %rdi     # 0x89660
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movl %r15d, %edi
movq %rbx, %rsi
callq 0x62808
pushq $0x2c
popq %rbp
subl $0x1, %r13d
jb 0x641cf
movl %ebp, %edi
movq %rbx, %rsi
callq 0x62808
cmpl $0x43, %r15d
je 0x64101
cmpl $0x49, %r15d
je 0x640f1
cmpl $0x53, %r15d
je 0x640b2
cmpl $0x73, %r15d
je 0x640d2
cmpl $0x66, %r15d
je 0x640b9
cmpl $0x69, %r15d
je 0x640e3
cmpl $0x63, %r15d
jne 0x64216
movsbl (%r12), %edi
movq %rbx, %rsi
callq 0x62a91
pushq $0x1
popq %rax
pushq $0x2c
popq %rbp
jmp 0x64111
movzwl (%r12), %edi
jmp 0x640d7
cvtss2sd (%r12), %xmm0
movq %rbx, %rdi
leaq 0x25594(%rip), %rsi     # 0x8965d
movb $0x1, %al
callq 0x5b5f6
jmp 0x640fd
movswl (%r12), %edi
movq %rbx, %rsi
callq 0x62a91
pushq $0x2
jmp 0x64110
movl (%r12), %edi
movq %rbx, %rsi
callq 0x62a91
jmp 0x640fd
movl (%r12), %edi
movq %rbx, %rsi
callq 0x6422c
pushq $0x4
jmp 0x64110
movzbl (%r12), %edi
movq %rbx, %rsi
callq 0x62a91
pushq $0x1
popq %rax
addq %rax, %r12
jmp 0x64057
addq $0x7, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x64216
leaq 0x2551e(%rip), %rdi     # 0x89653
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movl (%r13), %edi
movq %rbx, %rsi
callq 0x6422c
jmp 0x64207
addq $0xb, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x64216
movsd (%r13), %xmm0
movq %rbx, %rdi
leaq 0x254e5(%rip), %rsi     # 0x8965b
jmp 0x641c6
addq $0x7, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x64216
leaq 0x254bf(%rip), %rdi     # 0x89653
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movl (%r13), %edi
jmp 0x641ff
addq $0x7, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x64216
cvtss2sd (%r13), %xmm0
movq %rbx, %rdi
leaq 0x25490(%rip), %rsi     # 0x89656
movb $0x1, %al
callq 0x5b5f6
jmp 0x64207
movq %r12, %r13
movq %r13, %rbp
jmp 0x6420a
addq $0x5, %r12
movslq 0x20(%r14), %rax
addq 0x28(%r14), %rax
cmpq %rax, %r12
ja 0x64216
leaq 0x25464(%rip), %rdi     # 0x89653
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x62774
movzwl (%r13), %edi
movq %rbx, %rsi
callq 0x62a91
movq %r12, %rbp
movq 0x28(%r14), %rax
movq %rbp, %r12
jmp 0x63e7b
pushq $-0x1
popq %rax
jmp 0x6421d
movl (%rbx), %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq