// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
subq $0x20, %rsp
testq %rdi, %rdi
je 0x22dfc
movq %rdi, %r14
movl $0x1, %edi
movl $0x48, %esi
callq 0x4300
testq %rax, %rax
je 0x22c72
movq %rax, %rbx
leaq 0x77794(%rip), %rax     # 0x9a400
movq %rax, 0x18(%rbx)
jmp 0x22c74
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x22dfc
movl $0x0, (%rbx)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
movq $0x0, 0x8(%rbx)
cmpq $0x0, 0x38(%r14)
je 0x22ce3
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0x22d6b
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
callq 0x87816
testl %eax, %eax
je 0x22d7a
movq 0x10(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
callq 0x45c0
jmp 0x22df4
movslq (%r14), %r15
movl $0x4, %esi
movq %r15, %rdi
callq 0x4300
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x22df4
movl $0x8, %esi
movq %r15, %rdi
callq 0x4300
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x22df4
cmpl $0x0, (%r14)
jle 0x22de9
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movl (%rax,%r15,4), %eax
movq 0x10(%rbx), %rcx
movl %eax, (%rcx,%r15,4)
movq 0x20(%r14), %rax
movq (%rax,%r15,8), %rdi
callq 0x4930
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
movq 0x20(%rbx), %rax
cmpq $0x0, (%rax,%r15,8)
je 0x22dec
incq %r15
movslq (%r14), %rax
cmpq %rax, %r15
jl 0x22d27
jmp 0x22dec
movq 0x28(%r14), %r15
testq %r15, %r15
je 0x22daf
movq 0x8(%r14), %r14
jmp 0x22db2
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 0x86410
testl %eax, %eax
jne 0x22df4
jmp 0x22dfe
xorl %r14d, %r14d
movq %r14, 0x8(%rbx)
leaq 0x1(%r14), %rdi
callq 0x4680
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x22df4
testq %r15, %r15
je 0x22ddb
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x43c0
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rcx
movb $0x0, (%rcx,%rax)
jmp 0x22dfe
xorl %r15d, %r15d
movl %r15d, (%rbx)
cmpl (%r14), %r15d
jge 0x22e0b
movq %rbx, %rdi
callq 0x22ae6
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
cmpq $0x0, 0x30(%r14)
je 0x22c99
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22b73
testl %eax, %eax
jns 0x22c99
jmp 0x22df4