// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rdi, %r14
movq 0x38(%rdi), %rbp
pushq $-0x1
popq %rbx
testq %rbp, %rbp
je 0x69eb3
xorl %eax, %eax
cmpl $0x0, 0x98(%rbp)
je 0x6a039
movslq 0x58(%rbp), %rsi
testq %rsi, %rsi
je 0x6a039
movl %esi, 0x64(%rbp)
movq 0x78(%rbp), %rdi
shlq $0x2, %rsi
callq 0x4840
testq %rax, %rax
je 0x69ebf
movq %rax, %r15
movq %rax, 0x78(%rbp)
movl 0x58(%rbp), %r13d
movslq %r13d, %r12
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0x4300
movq %rax, %rcx
movl %ebx, %eax
movq %rcx, 0x8(%rsp)
testq %rcx, %rcx
je 0x6a039
xorl %eax, %eax
cmpq %r12, %rax
jge 0x69ecf
movl %eax, (%r15,%rax,4)
incq %rax
jmp 0x69ea5
movq %r14, %rdi
callq 0x6933d
testl %eax, %eax
je 0x69ec6
movl %ebx, %eax
jmp 0x6a039
movq 0x38(%r14), %rbp
jmp 0x69e46
movq %r14, 0x10(%rsp)
xorl %r14d, %r14d
movslq %r13d, %rax
cmpq %rax, %r14
jge 0x69fb9
movq 0x68(%rbp), %rbx
movq %r14, %r15
shlq $0x5, %r15
movq 0x8(%rbx,%r15), %rax
testq %rax, %rax
je 0x6a048
addq $0x20, %rax
movq (%rax), %rax
testq %rax, %rax
je 0x69fad
movq 0x8(%rax), %r13
cmpb $0x50, (%r13)
jne 0x69f00
cmpb $0x50, 0x1(%r13)
jne 0x69f00
movq 0x70(%rbp), %r12
addq $0x3, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x6b87d
cmpl (%r12), %eax
jne 0x69f58
movq (%rbx,%r15), %rcx
pushq $0x3
popq %rdi
leaq 0x1a133(%rip), %rsi     # 0x84078
leaq 0x1a13c(%rip), %rdx     # 0x84088
movq %r13, %r8
xorl %eax, %eax
callq 0x110c6
jmp 0x69fad
movq 0x20(%r12), %rcx
movl %eax, %eax
movl (%rcx,%rax,4), %eax
cmpq %rax, %r14
jne 0x69f85
movq (%rbx,%r15), %rcx
pushq $0x3
popq %rdi
leaq 0x1a103(%rip), %rsi     # 0x84078
leaq 0x1a145(%rip), %rdx     # 0x840c1
xorl %eax, %eax
callq 0x110c6
jmp 0x69fad
cltq
movq %rax, %rcx
shlq $0x5, %rcx
movl 0x14(%rbx,%rcx), %ecx
movl %ecx, 0x18(%rbx,%r15)
movq 0x78(%rbp), %rcx
orl $-0x1, (%rcx,%rax,4)
movq 0x8(%rsp), %rcx
movl (%rcx,%rax,4), %eax
incl %eax
movl %eax, (%rcx,%r14,4)
incq %r14
movl 0x58(%rbp), %r13d
jmp 0x69ed7
pushq $-0x1
popq %rdx
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rbx
movslq %r13d, %rsi
cmpq %rsi, %rcx
jge 0x69ffd
movq 0x78(%rbp), %r8
movl (%r8,%rcx,4), %esi
cmpl $-0x1, %esi
je 0x69ff4
cmpl $0x0, (%rbx,%rcx,4)
jle 0x69ff6
movslq %eax, %rdx
incl %eax
movl %esi, (%r8,%rdx,4)
movl 0x58(%rbp), %r13d
jmp 0x69ff6
movl %edx, %esi
incq %rcx
movl %esi, %edx
jmp 0x69fca
testl %eax, %eax
jne 0x6a016
xorl %eax, %eax
cmpl $0x0, 0x60(%rbp)
jle 0x6a016
testl %edx, %edx
js 0x6a016
movq 0x78(%rbp), %rax
movl %edx, (%rax)
pushq $0x1
popq %rax
movl %eax, 0x60(%rbp)
andl $0x0, 0x98(%rbp)
movl $0x1, 0x90(%rbp)
callq 0x6a1b4
movq %rbx, %rdi
callq 0x45c0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19ff0(%rip), %rdi     # 0x8403f
leaq 0x19705(%rip), %rsi     # 0x8375b
leaq 0x19ffa(%rip), %rcx     # 0x84057
movl $0x85b, %edx            # imm = 0x85B
callq 0x42f0