// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x24(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0xd8836
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd8bfd
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0xd886e
movq -0x10(%rbp), %rdi
callq 0xd7490
cmpl $0x0, %eax
je 0xd8862
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd8bfd
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x98(%rax)
je 0xd8885
movq -0x18(%rbp), %rax
cmpl $0x0, 0x58(%rax)
jne 0xd8891
movl $0x0, -0x4(%rbp)
jmp 0xd8bfd
movq -0x18(%rbp), %rax
movl 0x58(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x64(%rax)
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x18(%rbp), %rax
movslq 0x58(%rax), %rsi
shlq $0x2, %rsi
callq 0x4820
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xd88cf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd8bfd
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x18(%rbp), %rax
movslq 0x58(%rax), %rdi
movl $0x4, %esi
callq 0x42f0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xd8904
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd8bfd
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jge 0xd8934
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xd890b
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jge 0xd8b0b
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0xd8967
jmp 0xd8986
leaq 0x200bd(%rip), %rdi     # 0xf8a2b
leaq 0x1f7d2(%rip), %rsi     # 0xf8147
movl $0x85b, %edx            # imm = 0x85B
leaq 0x200c2(%rip), %rcx     # 0xf8a43
callq 0x42e0
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xd89de
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movsbl (%rax), %eax
cmpl $0x50, %eax
jne 0xd89cf
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x50, %eax
jne 0xd89cf
jmp 0xd89de
jmp 0xd89d1
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0xd89a5
cmpq $0x0, -0x48(%rbp)
jne 0xd89ea
jmp 0xd8afd
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0xdae00
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x70(%rcx), %rcx
cmpl (%rcx), %eax
jne 0xd8a56
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x48(%rbp), %rax
movq 0x8(%rax), %r8
addq $0x3, %r8
movl $0x3, %edi
leaq 0x20021(%rip), %rsi     # 0xf8a64
leaq 0x2002a(%rip), %rdx     # 0xf8a74
movb $0x0, %al
callq 0x1b030
jmp 0xd8afd
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0xd8aa5
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movl $0x3, %edi
leaq 0x1ffcf(%rip), %rsi     # 0xf8a64
leaq 0x20011(%rip), %rdx     # 0xf8aad
movb $0x0, %al
callq 0x1b030
jmp 0xd8afd
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x4c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x14(%rax), %ecx
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x1c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x4c(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xd893b
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x20(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jge 0xd8b95
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x1c(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
je 0xd8b88
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0xd8b86
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movl -0x20(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x20(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xd8b88
jmp 0xd8b8a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xd8b20
cmpl $0x0, -0x20(%rbp)
jne 0xd8bbf
movq -0x18(%rbp), %rax
cmpl $0x0, 0x60(%rax)
jle 0xd8bbf
cmpl $0x0, -0x50(%rbp)
jl 0xd8bbf
movl -0x50(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movl %ecx, (%rax)
movl $0x1, -0x20(%rbp)
movl -0x20(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x98(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x90(%rax)
movq -0x10(%rbp), %rdi
callq 0xd8e20
movq -0x38(%rbp), %rdi
callq 0x45a0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)