// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/sam.c
// Function: sam_hdr_sanitise
static sam_hdr_t *sam_hdr_sanitise(sam_hdr_t *h) {
    if (!h)
        return NULL;

    // Special case for empty headers.
    if (h->l_text == 0)
        return h;

    size_t i;
    unsigned int lnum = 0;
    char *cp = h->text, last = '\n';
    for (i = 0; i < h->l_text; i++) {
        // NB: l_text excludes terminating nul.  This finds early ones.
        if (cp[i] == 0)
            break;

        // Error on \n[^@], including duplicate newlines
        if (last == '\n') {
            lnum++;
            if (cp[i] != '@') {
                hts_log_error("Malformed SAM header at line %u", lnum);
                sam_hdr_destroy(h);
                return NULL;
            }
        }

        last = cp[i];
    }

    if (i < h->l_text) { // Early nul found.  Complain if not just padding.
        size_t j = i;
        while (j < h->l_text && cp[j] == '\0') j++;
        if (j < h->l_text)
            hts_log_warning("Unexpected NUL character in header. Possibly truncated");
    }

    // Add trailing newline and/or trailing nul if required.
    if (last != '\n') {
        hts_log_warning("Missing trailing newline on SAM header. Possibly truncated");

        if (h->l_text < 2 || i >= h->l_text - 2) {
            if (h->l_text >= SIZE_MAX - 2) {
                hts_log_error("No room for extra newline");
                sam_hdr_destroy(h);
                return NULL;
            }

            cp = realloc(h->text, (size_t) h->l_text+2);
            if (!cp) {
                sam_hdr_destroy(h);
                return NULL;
            }
            h->text = cp;
        }
        cp[i++] = '\n';

        // l_text may be larger already due to multiple nul padding
        if (h->l_text < i)
            h->l_text = i;
        cp[h->l_text] = '\0';
    }

    return h;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x21105
movq %rdi, %rbx
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x21107
movq 0x28(%rbx), %r14
movb $0xa, %bpl
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpq %r15, %rax
je 0x2105e
movb (%r14,%r15), %dl
testb %dl, %dl
je 0x21063
cmpb $0xa, %bpl
jne 0x21057
incl %ecx
cmpb $0x40, %dl
jne 0x210e5
incq %r15
movl %edx, %ebp
jmp 0x21039
movq %rax, %r15
jmp 0x2108e
movq %r15, %rcx
cmpq %rcx, %rax
je 0x2108e
cmpb $0x0, (%r14,%rcx)
leaq 0x1(%rcx), %rcx
je 0x21066
leaq 0x58fee(%rip), %rsi     # 0x7a06b
leaq 0x59018(%rip), %rdx     # 0x7a09c
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x110c6
cmpb $0xa, %bpl
je 0x21107
leaq 0x58fd0(%rip), %rsi     # 0x7a06b
leaq 0x59031(%rip), %rdx     # 0x7a0d3
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq 0x8(%rbx), %rsi
cmpq $0x2, %rsi
setae %al
leaq -0x2(%rsi), %rcx
cmpq %rcx, %r15
setb %cl
testb %cl, %al
jne 0x2112e
cmpq $-0x3, %rsi
jb 0x21115
leaq 0x58f99(%rip), %rsi     # 0x7a06b
leaq 0x59035(%rip), %rdx     # 0x7a10e
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x210fd
leaq 0x58f7f(%rip), %rsi     # 0x7a06b
leaq 0x58f89(%rip), %rdx     # 0x7a07c
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq %rbx, %rdi
callq 0x1d61e
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rbx), %rdi
addq $0x2, %rsi
callq 0x4840
testq %rax, %rax
je 0x210fd
movq %rax, %r14
movq %rax, 0x28(%rbx)
movb $0xa, (%r14,%r15)
incq %r15
movq 0x8(%rbx), %rax
cmpq %r15, %rax
jae 0x21146
movq %r15, 0x8(%rbx)
movq %r15, %rax
movb $0x0, (%r14,%rax)
jmp 0x21107