// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq 0x38(%rdi), %r14
testq %r14, %r14
je 0x87778
xorl %ebp, %ebp
cmpl $0x0, 0x98(%r14)
je 0x877e6
movslq 0x58(%r14), %rsi
testq %rsi, %rsi
je 0x877e6
movl %esi, 0x64(%r14)
movq 0x78(%r14), %rdi
shlq $0x2, %rsi
callq 0x4840
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x877e6
movq %rax, %r15
movq %rax, 0x78(%r14)
movslq 0x58(%r14), %rdi
movl $0x4, %esi
callq 0x4300
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x877e6
cmpl $0x0, 0x58(%r14)
jle 0x87545
xorl %eax, %eax
movl %eax, (%r15,%rax,4)
incq %rax
movslq 0x58(%r14), %rcx
cmpq %rcx, %rax
jl 0x87535
cmpl $0x0, 0x58(%r14)
jle 0x87734
xorl %r13d, %r13d
movq %rbx, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movq 0x68(%r14), %rdx
movq %r13, %rsi
shlq $0x5, %rsi
movq 0x8(%rdx,%rsi), %rax
testq %rax, %rax
je 0x877f7
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x87724
movq 0x8(%rcx), %rax
cmpb $0x50, (%rax)
jne 0x87592
cmpb $0x50, 0x1(%rax)
je 0x87597
movq (%rcx), %rcx
jmp 0x8757a
movq 0x70(%r14), %rcx
leaq 0x3(%rax), %r8
movq %rcx, 0x18(%rsp)
movl (%rcx), %ecx
testl %ecx, %ecx
je 0x875e4
movq %rcx, 0x10(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x38(%rsp)
movb (%r8), %cl
testb %cl, %cl
movq %r13, 0x40(%rsp)
je 0x875ec
addq $0x4, %rax
movl $0x811c9dc5, %ebp       # imm = 0x811C9DC5
movzbl %cl, %ecx
xorl %ebp, %ecx
imull $0x1000193, %ecx, %ebp  # imm = 0x1000193
movb (%rax), %cl
incq %rax
testb %cl, %cl
jne 0x875ce
jmp 0x875f1
xorl %r12d, %r12d
jmp 0x876a1
movl $0x811c9dc5, %ebp       # imm = 0x811C9DC5
movq 0x10(%rsp), %rax
leal -0x1(%rax), %r15d
andl %r15d, %ebp
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rsi
movl $0x1, %r14d
movl %ebp, %r12d
movq %r8, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movl %r12d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %ebx
leal (%r12,%r12), %r13d
andl $0x1e, %r13d
movl %ebx, %eax
movl %r13d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x87677
testb $0x1, %al
jne 0x8765f
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl %r12d, %ecx
movq (%rax,%rcx,8), %rdi
movq %r8, %rsi
callq 0x46c0
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %r8
testl %eax, %eax
je 0x87677
addl %r14d, %r12d
andl %r15d, %r12d
incl %r14d
cmpl %ebp, %r12d
jne 0x87619
movq 0x10(%rsp), %rcx
movl %ecx, %r12d
jmp 0x87688
movl %r13d, %ecx
shrl %cl, %ebx
testb $0x3, %bl
movq 0x10(%rsp), %rcx
cmovnel %ecx, %r12d
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r14
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rsi
cmpl %ecx, %r12d
jne 0x876c6
movq (%rdx,%rsi), %rcx
movl $0x3, %edi
leaq 0x1d953(%rip), %rsi     # 0xa5009
leaq 0x1d95c(%rip), %rdx     # 0xa5019
xorl %eax, %eax
callq 0x14378
jmp 0x87724
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movl %r12d, %ecx
movl (%rax,%rcx,4), %eax
cmpq %rax, %r13
jne 0x876fa
movq (%rdx,%rsi), %rcx
movl $0x3, %edi
leaq 0x1d91f(%rip), %rsi     # 0xa5009
leaq 0x1d961(%rip), %rdx     # 0xa5052
xorl %eax, %eax
callq 0x14378
jmp 0x87724
cltq
movq %rax, %rcx
shlq $0x5, %rcx
movl 0x14(%rdx,%rcx), %ecx
movl %ecx, 0x18(%rdx,%rsi)
movq 0x78(%r14), %rcx
movl $0xffffffff, (%rcx,%rax,4) # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rcx
movl (%rcx,%rax,4), %eax
incl %eax
movl %eax, (%rcx,%r13,4)
incq %r13
movslq 0x58(%r14), %rax
cmpq %rax, %r13
jl 0x8755d
cmpl $0x0, 0x58(%r14)
jle 0x87792
movq 0x78(%r14), %rdx
xorl %esi, %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movq 0x8(%rsp), %r15
movl (%rdx,%rsi,4), %ecx
cmpl $-0x1, %ecx
je 0x87766
cmpl $0x0, (%r15,%rsi,4)
jle 0x87768
movslq %eax, %rdi
incl %eax
movl %ecx, (%rdx,%rdi,4)
jmp 0x87768
movl %edi, %ecx
incq %rsi
movslq 0x58(%r14), %r8
movl %ecx, %edi
cmpq %r8, %rsi
jl 0x8774d
jmp 0x8779e
movq %rbx, %rdi
callq 0x866a8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x877e6
movq 0x38(%rbx), %r14
jmp 0x874cd
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
movq 0x8(%rsp), %r15
testl %eax, %eax
jne 0x877ba
xorl %eax, %eax
cmpl $0x0, 0x60(%r14)
jle 0x877ba
testl %ecx, %ecx
js 0x877ba
movq 0x78(%r14), %rax
movl %ecx, (%rax)
movl $0x1, %eax
movl %eax, 0x60(%r14)
movl $0x0, 0x98(%r14)
movl $0x1, 0x90(%r14)
movq %rbx, %rdi
callq 0x879db
movq %r15, %rdi
callq 0x45c0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d7d2(%rip), %rdi     # 0xa4fd0
leaq 0x1cee7(%rip), %rsi     # 0xa46ec
leaq 0x1d7dc(%rip), %rcx     # 0xa4fe8
movl $0x85b, %edx            # imm = 0x85B
callq 0x42f0