// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/htslib/cram/sam_header.c
// Function: sam_hdr_link_pg
int sam_hdr_link_pg(SAM_hdr *hdr) {
    int i, j, ret = 0;

    hdr->npg_end_alloc = hdr->npg;
    hdr->pg_end = realloc(hdr->pg_end, hdr->npg * sizeof(*hdr->pg_end));
    if (!hdr->pg_end)
	return -1;

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

    for (i = 0; i < hdr->npg; i++) {
	khint_t k;
	SAM_hdr_tag *tag;
	char tmp;

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

	tmp = tag->str[tag->len]; tag->str[tag->len] = 0;
	k = kh_get(m_s2i, hdr->pg_hash, tag->str+3);
	tag->str[tag->len] = tmp;

	if (k == kh_end(hdr->pg_hash)) {
	    ret = -1;
	    continue;
	}

	hdr->pg[i].prev_id = hdr->pg[kh_val(hdr->pg_hash, k)].id;
	hdr->pg_end[kh_val(hdr->pg_hash, k)] = -1;
    }

    for (i = j = 0; i < hdr->npg; i++) {
	if (hdr->pg_end[i] != -1)
	    hdr->pg_end[j++] = hdr->pg_end[i];
    }
    hdr->npg_end = j;

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movslq 0x68(%rdi), %rsi
movl %esi, 0x70(%rdi)
movq 0x88(%rdi), %rdi
shlq $0x2, %rsi
callq 0x6c80
movq %rax, 0x88(%rbx)
testq %rax, %rax
je 0x641b9
movslq 0x68(%rbx), %rcx
testq %rcx, %rcx
jle 0x63fa2
xorl %edx, %edx
movl %edx, (%rax,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x63f97
cmpl $0x0, 0x68(%rbx)
movl $0x0, %eax
jle 0x6417e
xorl %edi, %edi
xorl %r8d, %r8d
movq %rbx, 0x18(%rsp)
movq 0x78(%rbx), %rax
leaq (%rdi,%rdi,4), %r9
movq 0x10(%rax,%r9,8), %r14
testq %r14, %r14
je 0x64168
movq 0x8(%r14), %rax
cmpb $0x50, (%rax)
jne 0x63fe0
cmpb $0x50, 0x1(%rax)
je 0x63fe5
movq (%r14), %r14
jmp 0x63fc8
movslq 0x10(%r14), %rcx
movb (%rax,%rcx), %r10b
movb $0x0, (%rax,%rcx)
movq 0x80(%rbx), %rax
movq 0x8(%r14), %rcx
movq %rax, 0x40(%rsp)
movl (%rax), %esi
testl %esi, %esi
je 0x64058
movq %r9, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
leaq 0x3(%rcx), %rax
movq %rax, 0x50(%rsp)
movsbl (%rax), %r15d
testl %r15d, %r15d
movl %r8d, 0x10(%rsp)
movb %r10b, 0xf(%rsp)
movq %rcx, 0x20(%rsp)
je 0x6405f
movb 0x4(%rcx), %al
testb %al, %al
je 0x64062
addq $0x5, %rcx
movl %r15d, %edx
shll $0x5, %edx
subl %r15d, %edx
movsbl %al, %r15d
addl %edx, %r15d
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x6403d
jmp 0x64062
xorl %edx, %edx
jmp 0x64123
xorl %r15d, %r15d
movq %rsi, 0x28(%rsp)
leal -0x1(%rsi), %edx
andl %edx, %r15d
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rsi
movl $0x1, %r13d
movl %r15d, %ebx
movl %edx, 0x14(%rsp)
movq %rsi, 0x48(%rsp)
movl %ebx, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %r12d
leal (%rbx,%rbx), %ebp
andl $0x1e, %ebp
movl %r12d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x640f4
testb $0x1, %al
jne 0x640cc
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x50(%rsp), %rsi
callq 0x6a50
movq 0x48(%rsp), %rsi
movl 0x14(%rsp), %edx
testl %eax, %eax
je 0x640f4
addl %r13d, %ebx
andl %edx, %ebx
incl %r13d
cmpl %r15d, %ebx
jne 0x64088
movq 0x18(%rsp), %rbx
movq 0x38(%rsp), %rdi
movl 0x10(%rsp), %r8d
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %rdx
jmp 0x64119
movl %ebp, %ecx
shrl %cl, %r12d
testb $0x3, %r12b
movq 0x28(%rsp), %rdx
cmovel %ebx, %edx
movq 0x18(%rsp), %rbx
movq 0x38(%rsp), %rdi
movl 0x10(%rsp), %r8d
movq 0x30(%rsp), %r9
movb 0xf(%rsp), %r10b
movq 0x20(%rsp), %rcx
movslq 0x10(%r14), %rax
movb %r10b, (%rcx,%rax)
movq 0x80(%rbx), %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl (%rcx), %edx
je 0x6416b
movq 0x78(%rbx), %rax
movq 0x20(%rcx), %rcx
movl %edx, %edx
movslq (%rcx,%rdx,4), %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0x1c(%rax,%rsi,8), %esi
movl %esi, 0x20(%rax,%r9,8)
movq 0x88(%rbx), %rax
movslq (%rcx,%rdx,4), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl %r8d, %eax
incq %rdi
movslq 0x68(%rbx), %rcx
movl %eax, %r8d
cmpq %rcx, %rdi
jl 0x63fbb
cmpl $0x0, 0x68(%rbx)
movl $0x0, %r8d
jle 0x641b3
movq 0x88(%rbx), %rcx
xorl %edx, %edx
xorl %r8d, %r8d
movl (%rcx,%rdx,4), %esi
cmpl $-0x1, %esi
je 0x641a7
movslq %r8d, %rdi
incl %r8d
movl %esi, (%rcx,%rdi,4)
incq %rdx
movslq 0x68(%rbx), %rsi
cmpq %rsi, %rdx
jl 0x64196
movl %r8d, 0x6c(%rbx)
jmp 0x641be
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq