// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/header.c
// Function: sam_hdr_link_pg
static int sam_hdr_link_pg(sam_hdr_t *bh) {
    sam_hrecs_t *hrecs;
    int i, j, ret = 0, *new_pg_end;

    if (!bh)
        return -1;

    if (!(hrecs = bh->hrecs)) {
        if (sam_hdr_fill_hrecs(bh) != 0)
            return -1;
        hrecs = bh->hrecs;
    }

    if (!hrecs->pgs_changed || !hrecs->npg)
        return 0;

    hrecs->npg_end_alloc = hrecs->npg;
    new_pg_end = realloc(hrecs->pg_end, hrecs->npg * sizeof(*new_pg_end));
    if (!new_pg_end)
        return -1;
    hrecs->pg_end = new_pg_end;
    int *chain_size = calloc(hrecs->npg, sizeof(int));
    if (!chain_size)
        return -1;

    for (i = 0; i < hrecs->npg; i++)
        hrecs->pg_end[i] = i;

    for (i = 0; i < hrecs->npg; i++) {
        khint_t k;
        sam_hrec_tag_t *tag;

        assert(hrecs->pg[i].ty != NULL);
        for (tag = hrecs->pg[i].ty->tag; tag; tag = tag->next) {
            if (tag->str[0] == 'P' && tag->str[1] == 'P')
                break;
        }
        if (!tag) {
            /* Chain start points */
            continue;
        }

        k = kh_get(m_s2i, hrecs->pg_hash, tag->str+3);

        if (k == kh_end(hrecs->pg_hash)) {
            hts_log_warning("PG line with ID:%s has a PP link to missing program '%s'",
                    hrecs->pg[i].name, tag->str+3);
            continue;
        }

        int pp_idx = kh_val(hrecs->pg_hash, k);
        if (pp_idx == i) {
            hts_log_warning("PG line with ID:%s has a PP link to itself",
                            hrecs->pg[i].name);
            continue;
        }

        hrecs->pg[i].prev_id = hrecs->pg[pp_idx].id;
        hrecs->pg_end[pp_idx] = -1;
        chain_size[i] = chain_size[pp_idx]+1;
    }

    int last_end = -1;
    for (i = j = 0; i < hrecs->npg; i++) {
        if (hrecs->pg_end[i] != -1) {
            last_end = hrecs->pg_end[i];
            if (chain_size[i] > 0)
                hrecs->pg_end[j++] = hrecs->pg_end[i];
        }
    }
    /* Only leafs? Choose the last one! */
    if (!j && hrecs->npg_end > 0 && last_end >= 0) {
        hrecs->pg_end[0] = last_end;
        j = 1;
    }

    hrecs->npg_end = j;
    hrecs->pgs_changed = 0;

    /* mark as dirty or empty for rebuild */
    hrecs->dirty = 1;
    redact_header_text(bh);
    free(chain_size);

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r14
movq 0x38(%rdi), %r12
testq %r12, %r12
je 0x851ca
xorl %ecx, %ecx
cmpl $0x0, 0x98(%r12)
je 0x85217
movslq 0x58(%r12), %rsi
testq %rsi, %rsi
je 0x85217
movl %esi, 0x64(%r12)
movq 0x78(%r12), %rdi
shlq $0x2, %rsi
callq 0x4840
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x85217
movq %rax, %r15
movq %rax, 0x78(%r12)
movl 0x58(%r12), %ebx
movslq %ebx, %rdi
movl $0x4, %esi
callq 0x4300
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x85217
movl $0x0, %eax
testl %ebx, %ebx
jle 0x851e6
xorl %eax, %eax
movl %eax, (%r15,%rax,4)
incq %rax
movslq 0x58(%r12), %rcx
cmpq %rcx, %rax
jl 0x84f41
testl %ecx, %ecx
jle 0x851e4
xorl %ebx, %ebx
movq %r14, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq 0x68(%r12), %rdx
movq %rbx, %rsi
shlq $0x5, %rsi
movq 0x8(%rdx,%rsi), %rax
testq %rax, %rax
je 0x85228
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x85155
movq 0x8(%rcx), %rax
cmpb $0x50, (%rax)
jne 0x84f9c
cmpb $0x50, 0x1(%rax)
je 0x84fa1
movq (%rcx), %rcx
jmp 0x84f84
movq %rsi, 0x20(%rsp)
movq 0x70(%r12), %rcx
leaq 0x3(%rax), %r8
movq %rcx, 0x30(%rsp)
movl (%rcx), %esi
testl %esi, %esi
movq %rbx, 0x38(%rsp)
movq %rdx, 0x28(%rsp)
je 0x84fed
movb (%r8), %cl
testb %cl, %cl
je 0x84ff5
addq $0x4, %rax
movl $0x811c9dc5, %r12d      # imm = 0x811C9DC5
movzbl %cl, %ecx
xorl %r12d, %ecx
imull $0x1000193, %ecx, %r12d # imm = 0x1000193
movb (%rax), %cl
incq %rax
testb %cl, %cl
jne 0x84fd5
jmp 0x84ffb
xorl %r14d, %r14d
jmp 0x85089
movl $0x811c9dc5, %r12d      # imm = 0x811C9DC5
movq %rsi, 0x40(%rsp)
leal -0x1(%rsi), %r15d
andl %r15d, %r12d
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rsi
movl $0x1, %ebx
movl %r12d, %r14d
movq %r8, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movl %r14d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %ebp
leal (%r14,%r14), %r13d
andl $0x1e, %r13d
movl %ebp, %eax
movl %r13d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x85077
testb $0x1, %al
jne 0x85068
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rdi
movq %r8, %rsi
callq 0x46c0
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %r8
testl %eax, %eax
je 0x85077
addl %ebx, %r14d
andl %r15d, %r14d
incl %ebx
cmpl %r12d, %r14d
jne 0x85022
jmp 0x8508e
movl %r13d, %ecx
shrl %cl, %ebp
testb $0x3, %bpl
movq 0x40(%rsp), %rsi
cmovnel %esi, %r14d
cmpl %esi, %r14d
jne 0x850ca
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx), %rcx
movl $0x3, %edi
leaq 0x1d6e0(%rip), %rsi     # 0xa2788
leaq 0x1d6e9(%rip), %rdx     # 0xa2798
xorl %eax, %eax
callq 0x13de5
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r12
movq 0x38(%rsp), %rbx
jmp 0x85155
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rax
movl %r14d, %ecx
movl (%rax,%rcx,4), %eax
movq 0x38(%rsp), %rbx
cmpq %rax, %rbx
jne 0x85117
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx), %rcx
movl $0x3, %edi
leaq 0x1d68b(%rip), %rsi     # 0xa2788
leaq 0x1d6cd(%rip), %rdx     # 0xa27d1
xorl %eax, %eax
callq 0x13de5
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r12
jmp 0x85155
cltq
movq %rax, %rcx
shlq $0x5, %rcx
movq 0x28(%rsp), %rdx
movl 0x14(%rdx,%rcx), %ecx
movq 0x20(%rsp), %rsi
movl %ecx, 0x18(%rdx,%rsi)
movq 0x8(%rsp), %r12
movq 0x78(%r12), %rcx
movl $0xffffffff, (%rcx,%rax,4) # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rcx
movl (%rcx,%rax,4), %eax
incl %eax
movl %eax, (%rcx,%rbx,4)
movq 0x10(%rsp), %r14
incq %rbx
movslq 0x58(%r12), %rcx
cmpq %rcx, %rbx
jl 0x84f66
testl %ecx, %ecx
jle 0x851e4
movq 0x78(%r12), %rdx
xorl %edi, %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movq 0x18(%rsp), %r9
movl (%rdx,%rdi,4), %r8d
cmpl $-0x1, %r8d
je 0x8519f
cmpl $0x0, (%r9,%rdi,4)
jle 0x8519c
movslq %eax, %rcx
incl %eax
movl %r8d, (%rdx,%rcx,4)
movl 0x58(%r12), %ecx
movl %r8d, %esi
incq %rdi
movslq %ecx, %r8
cmpq %r8, %rdi
jl 0x8517d
testl %eax, %eax
jne 0x851e6
xorl %eax, %eax
cmpl $0x0, 0x60(%r12)
jle 0x851e6
testl %esi, %esi
js 0x851e6
movq 0x78(%r12), %rax
movl %esi, (%rax)
movl $0x1, %eax
jmp 0x851e6
movq %r14, %rdi
callq 0x8413e
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x85217
movq 0x38(%r14), %r12
jmp 0x84ec5
xorl %eax, %eax
movl %eax, 0x60(%r12)
movl $0x0, 0x98(%r12)
movl $0x1, 0x90(%r12)
movq %r14, %rdi
callq 0x853ed
movq 0x18(%rsp), %rdi
callq 0x45c0
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d520(%rip), %rdi     # 0xa274f
leaq 0x1cc35(%rip), %rsi     # 0xa1e6b
leaq 0x1d52a(%rip), %rcx     # 0xa2767
movl $0x85b, %edx            # imm = 0x85B
callq 0x42f0