// File: karel-brinda[P]ococo/ext/htslib/sam.c
// Function: sam_hdr_read
bam_hdr_t *sam_hdr_read(htsFile *fp)
{
    switch (fp->format.format) {
    case bam:
        return bam_hdr_read(fp->fp.bgzf);

    case cram:
        return cram_header_to_bam(fp->fp.cram->header);

    case sam: {
        kstring_t str;
        bam_hdr_t *h;
        int has_SQ = 0;
        str.l = str.m = 0; str.s = 0;
        while (hts_getline(fp, KS_SEP_LINE, &fp->line) >= 0) {
            if (fp->line.s[0] != '@') break;
            if (fp->line.l > 3 && strncmp(fp->line.s,"@SQ",3) == 0) has_SQ = 1;
            kputsn(fp->line.s, fp->line.l, &str);
            kputc('\n', &str);
        }
        if (! has_SQ && fp->fn_aux) {
            char line[2048];
            FILE *f = fopen(fp->fn_aux, "r");
            if (f == NULL) return NULL;
            while (fgets(line, sizeof line, f)) {
                const char *name = strtok(line, "\t");
                const char *length = strtok(NULL, "\t");
                ksprintf(&str, "@SQ\tSN:%s\tLN:%s\n", name, length);
            }
            fclose(f);
        }
        if (str.l == 0) kputsn("", 0, &str);
        h = sam_hdr_parse(str.l, str.s);
        h->l_text = str.l; h->text = str.s;
        return h;
        }

    default:
        abort();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x860, %rsp            # imm = 0x860
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
movl %eax, -0x85c(%rbp)
subl $0x3, %eax
je 0x1d050
jmp 0x1d003
movl -0x85c(%rbp), %eax
subl $0x4, %eax
je 0x1d020
jmp 0x1d010
movl -0x85c(%rbp), %eax
subl $0x6, %eax
je 0x1d036
jmp 0x1d213
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x1ac70
movq %rax, -0x8(%rbp)
jmp 0x1d218
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x54a50
movq %rax, -0x8(%rbp)
jmp 0x1d218
movl $0x0, -0x34(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x10, %rdx
movl $0x2, %esi
callq 0x145e0
cmpl $0x0, %eax
jl 0x1d0fa
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
je 0x1d09c
jmp 0x1d0fa
movq -0x10(%rbp), %rax
cmpq $0x3, 0x10(%rax)
jbe 0x1d0cc
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x5e3a3(%rip), %rsi     # 0x7b459
movl $0x3, %edx
callq 0x4290
cmpl $0x0, %eax
jne 0x1d0cc
movl $0x1, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %esi
leaq -0x28(%rbp), %rdx
callq 0x1d230
movl $0xa, %edi
leaq -0x28(%rbp), %rsi
callq 0x1d2c0
jmp 0x1d06f
cmpl $0x0, -0x34(%rbp)
jne 0x1d1c4
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x1d1c4
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
leaq 0x5e8cd(%rip), %rsi     # 0x7b9ef
callq 0x4690
movq %rax, -0x848(%rbp)
cmpq $0x0, -0x848(%rbp)
jne 0x1d145
movq $0x0, -0x8(%rbp)
jmp 0x1d218
jmp 0x1d147
leaq -0x840(%rbp), %rdi
movq -0x848(%rbp), %rdx
movl $0x800, %esi            # imm = 0x800
callq 0x4930
cmpq $0x0, %rax
je 0x1d1b8
leaq -0x840(%rbp), %rdi
leaq 0x5f340(%rip), %rsi     # 0x7c4b3
callq 0x4920
movq %rax, -0x850(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x5f329(%rip), %rsi     # 0x7c4b3
callq 0x4920
movq %rax, -0x858(%rbp)
movq -0x850(%rbp), %rdx
movq -0x858(%rbp), %rcx
leaq -0x28(%rbp), %rdi
leaq 0x5e2ae(%rip), %rsi     # 0x7b45d
movb $0x0, %al
callq 0x620e0
jmp 0x1d147
movq -0x848(%rbp), %rdi
callq 0x4590
cmpq $0x0, -0x28(%rbp)
jne 0x1d1dd
leaq 0x5f2fa(%rip), %rdi     # 0x7c4cc
xorl %esi, %esi
leaq -0x28(%rbp), %rdx
callq 0x1d230
movq -0x28(%rbp), %rax
movl %eax, %edi
movq -0x18(%rbp), %rsi
callq 0x1cbd0
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x1d218
callq 0x42e0
movq -0x8(%rbp), %rax
addq $0x860, %rsp            # imm = 0x860
popq %rbp
retq
nopw %cs:(%rax,%rax)