// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/sam.c
// Function: sam_hdr_dup
sam_hdr_t *sam_hdr_dup(const sam_hdr_t *h0)
{
    if (h0 == NULL) return NULL;
    sam_hdr_t *h;
    if ((h = sam_hdr_init()) == NULL) return NULL;
    // copy the simple data
    h->n_targets = 0;
    h->ignore_sam_err = h0->ignore_sam_err;
    h->l_text = 0;

    // Then the pointery stuff

    if (!h0->hrecs) {
        h->target_len = (uint32_t*)calloc(h0->n_targets, sizeof(uint32_t));
        if (!h->target_len) goto fail;
        h->target_name = (char**)calloc(h0->n_targets, sizeof(char*));
        if (!h->target_name) goto fail;

        int i;
        for (i = 0; i < h0->n_targets; ++i) {
            h->target_len[i] = h0->target_len[i];
            h->target_name[i] = strdup(h0->target_name[i]);
            if (!h->target_name[i]) break;
        }
        h->n_targets = i;
        if (i < h0->n_targets) goto fail;

        if (h0->sdict) {
            if (sam_hdr_dup_sdict(h0, h) < 0) goto fail;
        }
    }

    if (h0->hrecs) {
        kstring_t tmp = { 0, 0, NULL };
        if (sam_hrecs_rebuild_text(h0->hrecs, &tmp) != 0) {
            free(ks_release(&tmp));
            goto fail;
        }

        h->l_text = tmp.l;
        h->text   = ks_release(&tmp);

        if (sam_hdr_update_target_arrays(h, h0->hrecs, 0) != 0)
            goto fail;
    } else {
        h->l_text = h0->text ? h0->l_text : 0;
        h->text = malloc(h->l_text + 1);
        if (!h->text) goto fail;
        if (h0->text)
            memcpy(h->text, h0->text, h->l_text);
        h->text[h->l_text] = '\0';
    }

    return h;

 fail:
    sam_hdr_destroy(h);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
testq %rdi, %rdi
je 0x22390
movq %rdi, %r14
movl $0x1, %edi
movl $0x48, %esi
callq 0x4300
testq %rax, %rax
je 0x22390
movq %rax, %rbx
leaq 0x75199(%rip), %rax     # 0x97410
movq %rax, 0x18(%rbx)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0x222c8
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
callq 0x85247
testl %eax, %eax
je 0x22348
movq 0x10(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
callq 0x45c0
jmp 0x22388
movl (%r14), %ebp
movslq %ebp, %r15
movl $0x4, %esi
movq %r15, %rdi
callq 0x4300
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x22388
movl $0x8, %esi
movq %r15, %rdi
callq 0x4300
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x22388
testl %ebp, %ebp
jle 0x2237d
movq %rax, %r15
xorl %r12d, %r12d
movq 0x10(%r14), %rax
movl (%rax,%r12,4), %eax
movq 0x10(%rbx), %rcx
movl %eax, (%rcx,%r12,4)
movq 0x20(%r14), %rax
movq (%rax,%r12,8), %rdi
callq 0x4930
movq %rax, (%r15,%r12,8)
movq 0x20(%rbx), %r15
cmpq $0x0, (%r15,%r12,8)
movl (%r14), %ebp
je 0x22380
movslq %ebp, %rax
incq %r12
cmpq %rax, %r12
jl 0x2230c
jmp 0x22380
movq (%rsp), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %rax, 0x28(%rbx)
movq 0x38(%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x83eac
testl %eax, %eax
jne 0x22388
jmp 0x22392
xorl %r12d, %r12d
movl %r12d, (%rbx)
cmpl %ebp, %r12d
jge 0x223a2
movq %rbx, %rdi
callq 0x22102
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x30(%r14)
je 0x223b8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22191
testl %eax, %eax
js 0x22388
movq 0x38(%r14), %rdi
testq %rdi, %rdi
jne 0x2228b
movq 0x28(%r14), %r15
testq %r15, %r15
je 0x223d4
movq 0x8(%r14), %r14
jmp 0x223d7
xorl %r14d, %r14d
movq %r14, 0x8(%rbx)
leaq 0x1(%r14), %rdi
callq 0x4680
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x22388
movq %rax, %r12
testq %r15, %r15
je 0x22403
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x43c0
movb $0x0, (%r12,%r14)
jmp 0x22392