// File: /workspace/github_c_makefile_star2_O3/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 0x64245
movslq 0x68(%rbx), %rcx
testq %rcx, %rcx
jle 0x6423c
leal 0x1(%rcx), %edx
andl $-0x2, %edx
leaq -0x1(%rcx), %rsi
movq %rsi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x2a6c9(%rip), %xmm1    # 0x8e690
xorl %esi, %esi
movdqa 0x299df(%rip), %xmm2    # 0x8d9b0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x359ef(%rip), %xmm4    # 0x999d0
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edi
notl %edi
testb $0x1, %dil
je 0x64016
movl %esi, (%rax,%rsi,4)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edi
testb $0x1, %dil
je 0x6402c
leal 0x1(%rsi), %edi
movl %edi, 0x4(%rax,%rsi,4)
addq $0x2, %rsi
paddq %xmm4, %xmm1
cmpq %rsi, %rdx
jne 0x63fe1
testl %ecx, %ecx
jle 0x6423c
xorl %edi, %edi
xorl %eax, %eax
movq %rbx, 0x18(%rsp)
movl %eax, %r8d
movq 0x78(%rbx), %rax
leaq (%rdi,%rdi,4), %r9
movq 0x10(%rax,%r9,8), %r14
testq %r14, %r14
je 0x641f7
movq 0x8(%r14), %rax
cmpb $0x50, (%rax)
jne 0x64072
cmpb $0x50, 0x1(%rax)
je 0x64077
movq (%r14), %r14
jmp 0x6405a
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 0x640e4
leaq 0x3(%rcx), %rax
movq %rax, 0x50(%rsp)
movsbl (%rax), %ebx
testl %ebx, %ebx
movq %rdi, 0x38(%rsp)
movl %r8d, 0x10(%rsp)
movq %r9, 0x30(%rsp)
movb %r10b, 0xf(%rsp)
movq %rcx, 0x20(%rsp)
je 0x640eb
movb 0x4(%rcx), %al
testb %al, %al
je 0x640ed
addq $0x5, %rcx
movl %ebx, %edx
shll $0x5, %edx
subl %ebx, %edx
movsbl %al, %ebx
addl %edx, %ebx
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x640cd
jmp 0x640ed
xorl %edx, %edx
jmp 0x641b2
xorl %ebx, %ebx
movq %rsi, 0x28(%rsp)
leal -0x1(%rsi), %edx
andl %edx, %ebx
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rsi
movl $0x1, %r13d
movl %ebx, %r12d
movl %edx, 0x14(%rsp)
movq %rsi, 0x48(%rsp)
movl %r12d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %r15d
leal (%r12,%r12), %ebp
andl $0x1e, %ebp
movl %r15d, %eax
movl %ebp, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x64182
testb $0x1, %al
jne 0x64159
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rax
movl %r12d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x50(%rsp), %rsi
callq 0x6a50
movq 0x48(%rsp), %rsi
movl 0x14(%rsp), %edx
testl %eax, %eax
je 0x64182
addl %r13d, %r12d
andl %edx, %r12d
incl %r13d
cmpl %ebx, %r12d
jne 0x64112
movq 0x18(%rsp), %rbx
movq 0x38(%rsp), %rdi
movl 0x10(%rsp), %r8d
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %rdx
jmp 0x641a8
movl %ebp, %ecx
shrl %cl, %r15d
testb $0x3, %r15b
movq 0x28(%rsp), %rdx
cmovel %r12d, %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 0x641fa
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), %rdx
cmpq %rdx, %rdi
jl 0x6404a
testl %edx, %edx
jle 0x6423e
movq 0x88(%rbx), %rsi
xorl %edi, %edi
xorl %ecx, %ecx
movl (%rsi,%rdi,4), %r8d
cmpl $-0x1, %r8d
je 0x6422f
movslq %ecx, %rdx
incl %ecx
movl %r8d, (%rsi,%rdx,4)
movl 0x68(%rbx), %edx
incq %rdi
movslq %edx, %r8
cmpq %r8, %rdi
jl 0x64219
jmp 0x64240
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, 0x6c(%rbx)
jmp 0x6424a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq