// File: /workspace/github_c_makefile_star2_O3/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 0x26a0a
movq %rdi, %rbx
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x26a0c
movq 0x28(%rbx), %r14
xorl %ecx, %ecx
movb $0xa, %bpl
xorl %r15d, %r15d
movb (%r14,%r15), %dl
testb %dl, %dl
je 0x26919
cmpb $0xa, %bpl
jne 0x26908
incl %ecx
cmpb $0x40, %dl
jne 0x26932
incq %r15
movl %edx, %ebp
cmpq %r15, %rax
jne 0x268f3
movq %rax, %r15
movl %edx, %ebp
jmp 0x2696b
cmpq %rax, %r15
jae 0x2696b
movq %r15, %rcx
cmpb $0x0, (%r14,%rcx)
jne 0x26951
incq %rcx
cmpq %rax, %rcx
jb 0x26921
jmp 0x2696b
leaq 0x717d7(%rip), %rsi     # 0x98110
leaq 0x717e1(%rip), %rdx     # 0x98121
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x26a02
leaq 0x717b8(%rip), %rsi     # 0x98110
leaq 0x717e2(%rip), %rdx     # 0x98141
movl $0x3, %edi
xorl %eax, %eax
callq 0x13de5
cmpb $0xa, %bpl
je 0x26a0c
leaq 0x71794(%rip), %rsi     # 0x98110
leaq 0x717f5(%rip), %rdx     # 0x98178
movl $0x3, %edi
xorl %eax, %eax
callq 0x13de5
movq 0x8(%rbx), %rsi
cmpq $0x2, %rsi
setae %al
leaq -0x2(%rsi), %rcx
cmpq %rcx, %r15
setb %cl
testb %cl, %al
jne 0x269e3
cmpq $-0x3, %rsi
jb 0x269ca
leaq 0x7175b(%rip), %rsi     # 0x98110
leaq 0x717f7(%rip), %rdx     # 0x981b3
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x26a02
movq 0x28(%rbx), %rdi
addq $0x2, %rsi
callq 0x4840
testq %rax, %rax
je 0x26a02
movq %rax, %r14
movq %rax, 0x28(%rbx)
movb $0xa, (%r14,%r15)
incq %r15
movq 0x8(%rbx), %rax
cmpq %r15, %rax
jae 0x269fb
movq %r15, 0x8(%rbx)
movq %r15, %rax
movb $0x0, (%r14,%rax)
jmp 0x26a0c
movq %rbx, %rdi
callq 0x22102
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq