// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/htslib/cram/sam_header.c
// Function: sam_hdr_update_hashes
static int sam_hdr_update_hashes(SAM_hdr *sh,
				 int type,
				 SAM_hdr_type *h_type) {
    /* Add to reference hash? */
    if ((type>>8) == 'S' && (type&0xff) == 'Q') {
	SAM_hdr_tag *tag;
	SAM_SQ *new_ref;
	int nref = sh->nref;

	new_ref = realloc(sh->ref, (sh->nref+1)*sizeof(*sh->ref));
	if (!new_ref)
	    return -1;
	sh->ref = new_ref;

	tag = h_type->tag;
	sh->ref[nref].name = NULL;
	sh->ref[nref].len  = 0;
	sh->ref[nref].ty = h_type;
	sh->ref[nref].tag  = tag;

	while (tag) {
	    if (tag->str[0] == 'S' && tag->str[1] == 'N') {
		if (!(sh->ref[nref].name = malloc(tag->len)))
		    return -1;
		strncpy(sh->ref[nref].name, tag->str+3, tag->len-3);
		sh->ref[nref].name[tag->len-3] = 0;
	    } else if (tag->str[0] == 'L' && tag->str[1] == 'N') {
		sh->ref[nref].len = atoi(tag->str+3);
	    }
	    tag = tag->next;
	}

	if (sh->ref[nref].name) {
	    khint_t k;
	    int r;
	    k = kh_put(m_s2i, sh->ref_hash, sh->ref[nref].name, &r);
	    if (-1 == r) return -1;
	    kh_val(sh->ref_hash, k) = nref;
	} else {
	    return -1; // SN should be present, according to spec.
	}

	sh->nref++;
    }

    /* Add to read-group hash? */
    if ((type>>8) == 'R' && (type&0xff) == 'G') {
	SAM_hdr_tag *tag;
	SAM_RG *new_rg;
	int nrg = sh->nrg;

	new_rg = realloc(sh->rg, (sh->nrg+1)*sizeof(*sh->rg));
	if (!new_rg)
	    return -1;
	sh->rg = new_rg;

	tag = h_type->tag;
	sh->rg[nrg].name = NULL;
	sh->rg[nrg].name_len = 0;
	sh->rg[nrg].ty   = h_type;
	sh->rg[nrg].tag  = tag;
	sh->rg[nrg].id   = nrg;

	while (tag) {
	    if (tag->str[0] == 'I' && tag->str[1] == 'D') {
		if (!(sh->rg[nrg].name = malloc(tag->len)))
		    return -1;
		strncpy(sh->rg[nrg].name, tag->str+3, tag->len-3);
		sh->rg[nrg].name[tag->len-3] = 0;
		sh->rg[nrg].name_len = strlen(sh->rg[nrg].name);
	    }
	    tag = tag->next;
	}

	if (sh->rg[nrg].name) {
	    khint_t k;
	    int r;
	    k = kh_put(m_s2i, sh->rg_hash, sh->rg[nrg].name, &r);
	    if (-1 == r) return -1;
	    kh_val(sh->rg_hash, k) = nrg;
	} else {
	    return -1; // ID should be present, according to spec.
	}

	sh->nrg++;
    }

    /* Add to program hash? */
    if ((type>>8) == 'P' && (type&0xff) == 'G') {
	SAM_hdr_tag *tag;
	SAM_PG *new_pg;
	int npg = sh->npg;

	new_pg = realloc(sh->pg, (sh->npg+1)*sizeof(*sh->pg));
	if (!new_pg)
	    return -1;
	sh->pg = new_pg;

	tag = h_type->tag;
	sh->pg[npg].name = NULL;
	sh->pg[npg].name_len = 0;
	sh->pg[npg].ty  = h_type;
	sh->pg[npg].tag  = tag;
	sh->pg[npg].id   = npg;
	sh->pg[npg].prev_id = -1;

	while (tag) {
	    if (tag->str[0] == 'I' && tag->str[1] == 'D') {
		if (!(sh->pg[npg].name = malloc(tag->len)))
		    return -1;
		strncpy(sh->pg[npg].name, tag->str+3, tag->len-3);
		sh->pg[npg].name[tag->len-3] = 0;
		sh->pg[npg].name_len = strlen(sh->pg[npg].name);
	    } else if (tag->str[0] == 'P' && tag->str[1] == 'P') {
		// Resolve later if needed
		khint_t k;
		char tmp = tag->str[tag->len]; tag->str[tag->len] = 0;
		k = kh_get(m_s2i, sh->pg_hash, tag->str+3);
		tag->str[tag->len] = tmp;

		if (k != kh_end(sh->pg_hash)) {
		    int p_id = kh_val(sh->pg_hash, k);
		    sh->pg[npg].prev_id = sh->pg[p_id].id;

		    /* Unmark previous entry as a PG termination */
		    if (sh->npg_end > 0 &&
			sh->pg_end[sh->npg_end-1] == p_id) {
			sh->npg_end--;
		    } else {
			int i;
			for (i = 0; i < sh->npg_end; i++) {
			    if (sh->pg_end[i] == p_id) {
				memmove(&sh->pg_end[i], &sh->pg_end[i+1],
					(sh->npg_end-i-1)*sizeof(*sh->pg_end));
				sh->npg_end--;
			    }
			}
		    }
		} else {
		    sh->pg[npg].prev_id = -1;
		}
	    }
	    tag = tag->next;
	}

	if (sh->pg[npg].name) {
	    khint_t k;
	    int r;
	    k = kh_put(m_s2i, sh->pg_hash, sh->pg[npg].name, &r);
	    if (-1 == r) return -1;
	    kh_val(sh->pg_hash, k) = npg;
	} else {
	    return -1; // ID should be present, according to spec.
	}

	/* Add to npg_end[] array. Remove later if we find a PP line */
	if (sh->npg_end >= sh->npg_end_alloc) {
	    int *new_pg_end;
	    int  new_alloc = sh->npg_end_alloc ? sh->npg_end_alloc*2 : 4;

	    new_pg_end = realloc(sh->pg_end, new_alloc * sizeof(int));
	    if (!new_pg_end)
		return -1;
	    sh->npg_end_alloc = new_alloc;
	    sh->pg_end = new_pg_end;
	}
	sh->pg_end[sh->npg_end++] = npg;

	sh->npg++;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r15
movq %rdi, %r13
movzbl %sil, %edx
sarl $0x8, %esi
movl %esi, %eax
xorl $0x53, %eax
movl %edx, %ecx
xorl $0x51, %ecx
orl %eax, %ecx
movq %rdi, 0x10(%rsp)
jne 0x3e868
movl %esi, 0x28(%rsp)
movl %edx, 0x18(%rsp)
movl 0x38(%r13), %eax
movl %eax, 0x20(%rsp)
movslq %eax, %r14
movq 0x40(%r13), %rdi
shlq $0x5, %r14
leaq 0x20(%r14), %rsi
callq 0x48d0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x3ed21
movq %rax, 0x40(%r13)
movq 0x10(%r15), %r13
movq $0x0, (%rax,%r14)
movl $0x0, 0x8(%rax,%r14)
movq %r15, %rbp
movq %r15, 0x10(%rax,%r14)
movq %r13, 0x18(%rax,%r14)
testq %r13, %r13
je 0x3e817
movq 0x8(%r13), %r15
movzbl (%r15), %eax
cmpl $0x4c, %eax
je 0x3e7ed
cmpl $0x53, %eax
jne 0x3e80e
cmpb $0x4e, 0x1(%r15)
jne 0x3e7e9
movslq 0x10(%r13), %r12
movq %r12, %rdi
callq 0x4700
movq 0x10(%rsp), %rcx
movq 0x40(%rcx), %rbx
movq %rax, (%rbx,%r14)
testq %rax, %rax
je 0x3ed1c
addq %r14, %rbx
addq $0x3, %r15
addq $-0x3, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x42b0
movq (%rbx), %rax
movslq 0x10(%r13), %rcx
movb $0x0, -0x3(%rax,%rcx)
jmp 0x3e80e
cmpb $0x4c, %al
jne 0x3e80e
cmpb $0x4e, 0x1(%r15)
jne 0x3e80e
addq $0x3, %r15
movq %r15, %rdi
callq 0x4840
movq 0x10(%rsp), %rcx
movq 0x40(%rcx), %rcx
movl %eax, 0x8(%rcx,%r14)
movq (%r13), %r13
jmp 0x3e77e
movq 0x10(%rsp), %r13
movq 0x40(%r13), %rax
movq (%rax,%r14), %rsi
testq %rsi, %rsi
je 0x3ed1c
movq 0x48(%r13), %rdi
leaq 0x64(%rsp), %r15
movq %r15, %rdx
callq 0x41924
cmpl $-0x1, (%r15)
je 0x3ed1c
movq 0x48(%r13), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl 0x20(%rsp), %edx
movl %edx, (%rcx,%rax,4)
incl 0x38(%r13)
movq %rbp, %r15
movl 0x18(%rsp), %edx
movl 0x28(%rsp), %esi
movl %esi, %eax
xorl $0x52, %eax
xorl $0x47, %edx
orl %edx, %eax
jne 0x3e9a2
movl %esi, 0x28(%rsp)
movl %edx, 0x18(%rsp)
movl 0x50(%r13), %ebx
movslq %ebx, %r14
movq 0x58(%r13), %rdi
shlq $0x5, %r14
leaq 0x20(%r14), %rsi
callq 0x48d0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x3ed21
movq %rax, 0x58(%r13)
movq 0x10(%r15), %r13
movq $0x0, (%rax,%r14)
movl $0x0, 0x18(%rax,%r14)
movq %r15, 0x30(%rsp)
movq %r15, 0x8(%rax,%r14)
movq %r13, 0x10(%rax,%r14)
movl %ebx, 0x1c(%rax,%r14)
testq %r13, %r13
je 0x3e953
movl %ebx, 0x20(%rsp)
movq 0x10(%rsp), %rbp
movq 0x8(%r13), %r15
cmpb $0x49, (%r15)
jne 0x3e94a
cmpb $0x44, 0x1(%r15)
jne 0x3e94a
movslq 0x10(%r13), %r12
movq %r12, %rdi
callq 0x4700
movq 0x58(%rbp), %rbx
movq %rax, (%rbx,%r14)
testq %rax, %rax
je 0x3ed1c
addq %r14, %rbx
addq $0x3, %r15
addq $-0x3, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x42b0
movq (%rbx), %rax
movslq 0x10(%r13), %rcx
movb $0x0, -0x3(%rax,%rcx)
movq 0x58(%rbp), %rbx
movq (%rbx,%r14), %rdi
callq 0x4230
movl %eax, 0x18(%rbx,%r14)
movl 0x20(%rsp), %ebx
movq (%r13), %r13
testq %r13, %r13
jne 0x3e8e1
movq 0x10(%rsp), %r13
movq 0x58(%r13), %rax
movq (%rax,%r14), %rsi
testq %rsi, %rsi
je 0x3ed1c
movq 0x60(%r13), %rdi
leaq 0x60(%rsp), %r15
movq %r15, %rdx
callq 0x41924
cmpl $-0x1, (%r15)
je 0x3ed1c
movq 0x60(%r13), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl %ebx, (%rcx,%rax,4)
incl 0x50(%r13)
movq 0x30(%rsp), %r15
movl 0x18(%rsp), %edx
movl 0x28(%rsp), %esi
xorl $0x50, %esi
xorl %ebp, %ebp
orl %edx, %esi
jne 0x3ed21
movl 0x68(%r13), %ebx
movslq %ebx, %rax
movq 0x78(%r13), %rdi
leaq (%rax,%rax,4), %r14
leaq 0x28(,%r14,8), %rsi
callq 0x48d0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x3ed21
movq %rax, 0x78(%r13)
movq 0x10(%r15), %r12
movq $0x0, (%rax,%r14,8)
movl $0x0, 0x18(%rax,%r14,8)
movq %r15, 0x8(%rax,%r14,8)
movq %r12, 0x10(%rax,%r14,8)
movl %ebx, 0x3c(%rsp)
movl %ebx, 0x1c(%rax,%r14,8)
movl $0xffffffff, 0x20(%rax,%r14,8) # imm = 0xFFFFFFFF
testq %r12, %r12
movq %r14, %rbp
je 0x3ec7e
movq %rbp, 0x40(%rsp)
movq 0x8(%r12), %r14
movzbl (%r14), %eax
cmpl $0x50, %eax
je 0x3eaa0
cmpl $0x49, %eax
jne 0x3ec71
cmpb $0x44, 0x1(%r14)
jne 0x3ea98
movslq 0x10(%r12), %r15
movq %r15, %rdi
callq 0x4700
movq 0x78(%r13), %rcx
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x3ed1c
leaq (%rcx,%rbp,8), %rbx
addq $0x3, %r14
addq $-0x3, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x42b0
movq (%rbx), %rax
movslq 0x10(%r12), %rcx
movb $0x0, -0x3(%rax,%rcx)
movq 0x78(%r13), %rbx
movq (%rbx,%rbp,8), %rdi
callq 0x4230
movl %eax, 0x18(%rbx,%rbp,8)
jmp 0x3ec71
cmpb $0x50, %al
jne 0x3ec71
cmpb $0x50, 0x1(%r14)
jne 0x3ec71
movslq 0x10(%r12), %rax
movb (%r14,%rax), %dl
movb $0x0, (%r14,%rax)
movq 0x80(%r13), %rax
movq 0x8(%r12), %rdi
movq %rax, 0x18(%rsp)
movl (%rax), %esi
testl %esi, %esi
je 0x3eb0b
leaq 0x3(%rdi), %rax
movq %rax, 0x28(%rsp)
movsbl (%rax), %ebp
testl %ebp, %ebp
movb %dl, 0xf(%rsp)
movq %rdi, 0x48(%rsp)
je 0x3eb12
movb 0x4(%rdi), %al
testb %al, %al
je 0x3eb14
leaq 0x5(%rdi), %rcx
movl %ebp, %edx
shll $0x5, %edx
subl %ebp, %edx
movsbl %al, %ebp
addl %edx, %ebp
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x3eaf4
jmp 0x3eb14
xorl %ecx, %ecx
jmp 0x3ebcb
xorl %ebp, %ebp
movq %rsi, 0x50(%rsp)
leal -0x1(%rsi), %edx
andl %edx, %ebp
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rsi
movl $0x1, %r13d
movl %ebp, %r14d
movq %r12, 0x20(%rsp)
movq %rsi, 0x30(%rsp)
movl %r14d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %ebx
leal (%r14,%r14), %r15d
andl $0x1e, %r15d
movl %ebx, %eax
movl %r15d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x3eba7
testb $0x1, %al
jne 0x3eb88
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x28(%rsp), %rsi
movl %edx, %r12d
callq 0x4750
movq 0x30(%rsp), %rsi
movl %r12d, %edx
movq 0x20(%rsp), %r12
testl %eax, %eax
je 0x3eba7
addl %r13d, %r14d
andl %edx, %r14d
incl %r13d
cmpl %ebp, %r14d
jne 0x3eb3a
movq 0x10(%rsp), %r13
movq 0x40(%rsp), %rbp
movq 0x50(%rsp), %rcx
jmp 0x3ebc2
movl %r15d, %ecx
shrl %cl, %ebx
testb $0x3, %bl
movq 0x50(%rsp), %rcx
cmovel %r14d, %ecx
movq 0x10(%rsp), %r13
movq 0x40(%rsp), %rbp
movb 0xf(%rsp), %dl
movq 0x48(%rsp), %rdi
movslq 0x10(%r12), %rax
movb %dl, (%rdi,%rax)
movq 0x80(%r13), %rax
cmpl (%rax), %ecx
jne 0x3ebef
movq 0x78(%r13), %rax
movl $0xffffffff, 0x20(%rax,%rbp,8) # imm = 0xFFFFFFFF
jmp 0x3ec71
movq 0x20(%rax), %rax
movl %ecx, %ecx
movslq (%rax,%rcx,4), %r14
movq 0x78(%r13), %rax
leaq (%r14,%r14,4), %rcx
movl 0x1c(%rax,%rcx,8), %ecx
movl %ecx, 0x20(%rax,%rbp,8)
movslq 0x6c(%r13), %rax
testq %rax, %rax
jle 0x3ec28
movq 0x88(%r13), %rcx
cmpl %r14d, -0x4(%rcx,%rax,4)
jne 0x3ec28
decl %eax
movl %eax, 0x6c(%r13)
jmp 0x3ec71
movl 0x6c(%r13), %eax
testl %eax, %eax
jle 0x3ec71
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movq 0x88(%r13), %rcx
cmpl %r14d, (%rcx,%r15,4)
jne 0x3ec63
leaq (%rcx,%r15,4), %rdi
leaq (%rcx,%r15,4), %rsi
addq $0x4, %rsi
addl %ebx, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
callq 0x48c0
decl 0x6c(%r13)
incq %r15
movslq 0x6c(%r13), %rax
decl %ebx
cmpq %rax, %r15
jl 0x3ec38
movq (%r12), %r12
testq %r12, %r12
jne 0x3ea1f
movq 0x78(%r13), %rax
movq (%rax,%rbp,8), %rsi
testq %rsi, %rsi
je 0x3ed1c
movq 0x80(%r13), %rdi
leaq 0x5c(%rsp), %r14
movq %r14, %rdx
callq 0x41924
cmpl $-0x1, (%r14)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
je 0x3ed21
movq 0x80(%r13), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl 0x3c(%rsp), %r14d
movl %r14d, (%rcx,%rax,4)
movl 0x70(%r13), %eax
cmpl %eax, 0x6c(%r13)
jl 0x3ecfe
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x4, %ebx
cmovnel %ecx, %ebx
movq 0x88(%r13), %rdi
movslq %ebx, %rsi
shlq $0x2, %rsi
callq 0x48d0
testq %rax, %rax
je 0x3ed21
movl %ebx, 0x70(%r13)
movq %rax, 0x88(%r13)
movq 0x88(%r13), %rax
movslq 0x6c(%r13), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x6c(%r13)
movl %r14d, (%rax,%rcx,4)
incl 0x68(%r13)
xorl %ebp, %ebp
jmp 0x3ed21
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq