// File: /workspace/github_c_makefile_star2_O2/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 0x1d878
movq %rdi, %r14
callq 0x1d600
testq %rax, %rax
je 0x1d878
movq %rax, %rbx
andl $0x0, (%rax)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
andq $0x0, 0x8(%rbx)
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0x1d7c2
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
callq 0x6a067
testl %eax, %eax
je 0x1d836
movq 0x10(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
callq 0x45c0
jmp 0x1d870
movl (%r14), %ebp
movslq %ebp, %r15
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x4300
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x1d870
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x4300
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x1d870
movq %rax, %r15
xorl %r12d, %r12d
movslq %ebp, %rax
cmpq %rax, %r12
jge 0x1d868
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 0x1d868
incq %r12
jmp 0x1d7fa
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
andq $0x0, 0x10(%rsp)
movq %rax, 0x8(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq %rcx, 0x28(%rbx)
movq 0x38(%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x69130
testl %eax, %eax
jne 0x1d870
jmp 0x1d87a
movl %r12d, (%rbx)
cmpl %r12d, %ebp
jle 0x1d88a
movq %rbx, %rdi
callq 0x1d61e
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 0x1d8a0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1d6a4
testl %eax, %eax
js 0x1d870
movq 0x38(%r14), %rdi
testq %rdi, %rdi
jne 0x1d78b
movq 0x28(%r14), %r15
testq %r15, %r15
je 0x1d8bc
movq 0x8(%r14), %r14
jmp 0x1d8bf
xorl %r14d, %r14d
movq %r14, 0x8(%rbx)
leaq 0x1(%r14), %rdi
callq 0x4680
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x1d870
movq %rax, %r12
testq %r15, %r15
je 0x1d8eb
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x43c0
movb $0x0, (%r12,%r14)
jmp 0x1d87a