// File: /workspace/github_c_makefile_star2_O1/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 0x2758e
movq %rdi, %rbx
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x27590
movq 0x28(%rbx), %r14
movb (%r14), %sil
xorl %ecx, %ecx
testb %sil, %sil
je 0x27480
leaq -0x1(%rax), %rdx
movb $0xa, %bpl
xorl %r15d, %r15d
movl %ebp, %edi
movl %esi, %ebp
cmpb $0xa, %dil
jne 0x27466
incl %ecx
cmpb $0x40, %bpl
jne 0x27534
cmpq %r15, %rdx
je 0x274d2
movb 0x1(%r14,%r15), %sil
incq %r15
testb %sil, %sil
jne 0x27450
cmpq %rax, %r15
setb %cl
jmp 0x27488
movb $0xa, %bpl
movb $0x1, %cl
xorl %r15d, %r15d
testb %cl, %cl
je 0x274d5
cmpq %rax, %r15
setb %cl
jae 0x274d5
cmpb $0x0, (%r14,%r15)
jne 0x274b2
leaq 0x1(%r15), %rdx
cmpq %rax, %rdx
setb %cl
jae 0x274d5
cmpb $0x0, (%r14,%rdx)
leaq 0x1(%rdx), %rdx
je 0x2749f
testb %cl, %cl
je 0x274d5
leaq 0x73c40(%rip), %rsi     # 0x9b0fd
leaq 0x73c6a(%rip), %rdx     # 0x9b12e
movl $0x3, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x274d5
movq %rax, %r15
cmpb $0xa, %bpl
je 0x27590
leaq 0x73c17(%rip), %rsi     # 0x9b0fd
leaq 0x73c78(%rip), %rdx     # 0x9b165
movl $0x3, %edi
xorl %eax, %eax
callq 0x14378
movq 0x8(%rbx), %rsi
cmpq $0x2, %rsi
setae %al
leaq -0x2(%rsi), %rcx
cmpq %rcx, %r15
setb %cl
testb %cl, %al
jne 0x27569
cmpq $-0x3, %rsi
jb 0x27550
leaq 0x73bde(%rip), %rsi     # 0x9b0fd
leaq 0x73c7a(%rip), %rdx     # 0x9b1a0
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x27586
leaq 0x73bc2(%rip), %rsi     # 0x9b0fd
leaq 0x73bcc(%rip), %rdx     # 0x9b10e
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x27586
movq 0x28(%rbx), %rdi
addq $0x2, %rsi
callq 0x4840
testq %rax, %rax
je 0x27586
movq %rax, %r14
movq %rax, 0x28(%rbx)
movb $0xa, (%r14,%r15)
incq %r15
cmpq %r15, 0x8(%rbx)
jae 0x2757b
movq %r15, 0x8(%rbx)
movq 0x8(%rbx), %rax
movb $0x0, (%r14,%rax)
jmp 0x27590
movq %rbx, %rdi
callq 0x22ae6
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq