// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/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 0x48d0
movq %rax, 0x88(%rbx)
testq %rax, %rax
je 0x3ffa6
movslq 0x68(%rbx), %rcx
testq %rcx, %rcx
jle 0x3ff9d
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 0x12108(%rip), %xmm1    # 0x51e30
xorl %esi, %esi
movdqa 0x1210e(%rip), %xmm2    # 0x51e40
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x13b8e(%rip), %xmm4    # 0x538d0
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 0x3fd77
movl %esi, (%rax,%rsi,4)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edi
testb $0x1, %dil
je 0x3fd8d
leal 0x1(%rsi), %edi
movl %edi, 0x4(%rax,%rsi,4)
addq $0x2, %rsi
paddq %xmm4, %xmm1
cmpq %rsi, %rdx
jne 0x3fd42
testl %ecx, %ecx
jle 0x3ff9d
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 0x3ff58
movq 0x8(%r14), %rax
cmpb $0x50, (%rax)
jne 0x3fdd3
cmpb $0x50, 0x1(%rax)
je 0x3fdd8
movq (%r14), %r14
jmp 0x3fdbb
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 0x3fe45
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 0x3fe4c
movb 0x4(%rcx), %al
testb %al, %al
je 0x3fe4e
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 0x3fe2e
jmp 0x3fe4e
xorl %edx, %edx
jmp 0x3ff13
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 0x3fee3
testb $0x1, %al
jne 0x3feba
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rax
movl %r12d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x50(%rsp), %rsi
callq 0x4750
movq 0x48(%rsp), %rsi
movl 0x14(%rsp), %edx
testl %eax, %eax
je 0x3fee3
addl %r13d, %r12d
andl %edx, %r12d
incl %r13d
cmpl %ebx, %r12d
jne 0x3fe73
movq 0x18(%rsp), %rbx
movq 0x38(%rsp), %rdi
movl 0x10(%rsp), %r8d
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %rdx
jmp 0x3ff09
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 0x3ff5b
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 0x3fdab
testl %edx, %edx
jle 0x3ff9f
movq 0x88(%rbx), %rsi
xorl %edi, %edi
xorl %ecx, %ecx
movl (%rsi,%rdi,4), %r8d
cmpl $-0x1, %r8d
je 0x3ff90
movslq %ecx, %rdx
incl %ecx
movl %r8d, (%rsi,%rdx,4)
movl 0x68(%rbx), %edx
incq %rdi
movslq %edx, %r8
cmpq %r8, %rdi
jl 0x3ff7a
jmp 0x3ffa1
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, 0x6c(%rbx)
jmp 0x3ffab
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq