// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/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 $0x28, %rsp
movq %rdx, %r14
movq %rdi, %rbx
movzbl %sil, %edx
sarl $0x8, %esi
movl %esi, %eax
xorl $0x53, %eax
movl %edx, %ecx
xorl $0x51, %ecx
orl %eax, %ecx
jne 0x58486
movl %esi, 0x8(%rsp)
movl %edx, 0xc(%rsp)
movl 0x38(%rbx), %eax
movl %eax, 0x4(%rsp)
movslq %eax, %r13
movq 0x40(%rbx), %rdi
shlq $0x5, %r13
leaq 0x20(%r13), %rsi
callq 0x6c80
testq %rax, %rax
je 0x587f3
movq %rax, 0x40(%rbx)
movq %r14, %rcx
movq 0x10(%r14), %r14
andq $0x0, (%rax,%r13)
andl $0x0, 0x8(%rax,%r13)
movq %rcx, 0x10(%rsp)
movq %rcx, 0x10(%rax,%r13)
movq %r14, 0x18(%rax,%r13)
testq %r14, %r14
je 0x58439
movq 0x8(%r14), %r15
movzbl (%r15), %eax
cmpl $0x4c, %eax
je 0x58415
cmpl $0x53, %eax
jne 0x58431
cmpb $0x4e, 0x1(%r15)
jne 0x58431
movslq 0x10(%r14), %r12
movq %r12, %rdi
callq 0x69d0
movq 0x40(%rbx), %rbp
movq %rax, (%rbp,%r13)
testq %rax, %rax
je 0x587f3
addq %r13, %rbp
addq $0x3, %r15
addq $-0x3, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x6380
movq (%rbp), %rax
movslq 0x10(%r14), %rcx
movb $0x0, -0x3(%rax,%rcx)
jmp 0x58431
cmpb $0x4e, 0x1(%r15)
jne 0x58431
addq $0x3, %r15
movq %r15, %rdi
callq 0x6bd0
movq 0x40(%rbx), %rcx
movl %eax, 0x8(%rcx,%r13)
movq (%r14), %r14
jmp 0x583ad
movq 0x40(%rbx), %rax
movq (%rax,%r13), %rsi
testq %rsi, %rsi
je 0x587f3
movq 0x48(%rbx), %rdi
leaq 0x24(%rsp), %r15
movq %r15, %rdx
callq 0x5a02d
cmpl $-0x1, (%r15)
je 0x587f3
movq 0x48(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl 0x4(%rsp), %edx
movl %edx, (%rcx,%rax,4)
incl 0x38(%rbx)
movq 0x10(%rsp), %r14
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %esi
movl %esi, %eax
xorl $0x52, %eax
xorl $0x47, %edx
orl %edx, %eax
jne 0x585aa
movl %esi, 0x8(%rsp)
movl %edx, 0xc(%rsp)
movq %r14, 0x10(%rsp)
movl 0x50(%rbx), %ebp
movslq %ebp, %r14
movq 0x58(%rbx), %rdi
shlq $0x5, %r14
leaq 0x20(%r14), %rsi
callq 0x6c80
testq %rax, %rax
je 0x587f3
movq %rax, %r15
movq %rax, 0x58(%rbx)
movq 0x10(%rsp), %rax
movl %ebp, %ecx
movq 0x10(%rax), %rbp
andq $0x0, (%r15,%r14)
andl $0x0, 0x18(%r15,%r14)
movq %rax, 0x8(%r15,%r14)
movq %rbp, 0x10(%r15,%r14)
movl %ecx, 0x4(%rsp)
movl %ecx, 0x1c(%r15,%r14)
testq %rbp, %rbp
je 0x58561
movq 0x8(%rbp), %r12
cmpb $0x49, (%r12)
jne 0x5855b
cmpb $0x44, 0x1(%r12)
jne 0x5855b
movslq 0x10(%rbp), %r13
movq %r13, %rdi
callq 0x69d0
movq %rax, (%r15,%r14)
testq %rax, %rax
je 0x587f3
addq %r14, %r15
addq $0x3, %r12
addq $-0x3, %r13
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x6380
movq (%r15), %rax
movslq 0x10(%rbp), %rcx
movb $0x0, -0x3(%rax,%rcx)
movq 0x58(%rbx), %r15
movq (%r15,%r14), %rdi
callq 0x62d0
movl %eax, 0x18(%r15,%r14)
movq (%rbp), %rbp
jmp 0x584f3
movq (%r15,%r14), %rsi
testq %rsi, %rsi
je 0x587f3
movq 0x60(%rbx), %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rdx
callq 0x5a02d
cmpl $-0x1, (%r15)
je 0x587f3
movq 0x60(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl 0x4(%rsp), %edx
movl %edx, (%rcx,%rax,4)
incl 0x50(%rbx)
movq 0x10(%rsp), %r14
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %esi
xorl $0x50, %esi
xorl %r12d, %r12d
orl %edx, %esi
jne 0x587f7
movl 0x68(%rbx), %r15d
movslq %r15d, %rax
movq 0x78(%rbx), %rdi
imulq $0x28, %rax, %rbp
leaq 0x28(%rbp), %rsi
callq 0x6c80
pushq $-0x1
popq %r12
testq %rax, %rax
je 0x587f7
movq %rax, 0x78(%rbx)
movq 0x10(%r14), %r12
andq $0x0, (%rax,%rbp)
andl $0x0, 0x18(%rax,%rbp)
movq %r14, 0x8(%rax,%rbp)
movq %r12, 0x10(%rax,%rbp)
movl %r15d, 0x10(%rsp)
movl %r15d, 0x1c(%rax,%rbp)
orl $-0x1, 0x20(%rax,%rbp)
testq %r12, %r12
je 0x5875c
movq 0x8(%r12), %r14
movzbl (%r14), %eax
cmpl $0x50, %eax
je 0x5868d
cmpl $0x49, %eax
jne 0x5870c
cmpb $0x44, 0x1(%r14)
jne 0x5870c
movslq 0x10(%r12), %r15
movq %r15, %rdi
callq 0x69d0
movq 0x78(%rbx), %r13
movq %rax, (%r13,%rbp)
testq %rax, %rax
je 0x587f3
addq %rbp, %r13
addq $0x3, %r14
addq $-0x3, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6380
movq (%r13), %rax
movslq 0x10(%r12), %rcx
movb $0x0, -0x3(%rax,%rcx)
movq 0x78(%rbx), %r14
movq (%r14,%rbp), %rdi
callq 0x62d0
movl %eax, 0x18(%r14,%rbp)
jmp 0x5870c
cmpb $0x50, 0x1(%r14)
jne 0x5870c
movslq 0x10(%r12), %rax
movb (%r14,%rax), %r15b
movb $0x0, (%r14,%rax)
movq 0x80(%rbx), %rdi
movq 0x8(%r12), %r14
leaq 0x3(%r14), %rsi
callq 0x58f17
movslq 0x10(%r12), %rcx
movb %r15b, (%r14,%rcx)
movq 0x80(%rbx), %rcx
cmpl (%rcx), %eax
jne 0x586d6
movq 0x78(%rbx), %rax
orl $-0x1, 0x20(%rax,%rbp)
jmp 0x5870c
movq 0x20(%rcx), %rcx
movl %eax, %eax
movslq (%rcx,%rax,4), %r14
movq 0x78(%rbx), %rax
imulq $0x28, %r14, %rcx
movl 0x1c(%rax,%rcx), %ecx
movl %ecx, 0x20(%rax,%rbp)
movslq 0x6c(%rbx), %rax
testq %rax, %rax
jle 0x58715
movq 0x88(%rbx), %rcx
cmpl %r14d, -0x4(%rcx,%rax,4)
jne 0x58715
decl %eax
movl %eax, 0x6c(%rbx)
movq (%r12), %r12
jmp 0x58608
pushq $-0x1
popq %r15
xorl %r13d, %r13d
movslq %eax, %rcx
cmpq %rcx, %r13
jge 0x5870c
movq 0x88(%rbx), %rcx
cmpl %r14d, (%rcx,%r13,4)
jne 0x58754
leaq (%rcx,%r13,4), %rdi
leaq (%rcx,%r13,4), %rsi
addq $0x4, %rsi
addl %r15d, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
callq 0x6c60
movl 0x6c(%rbx), %eax
decl %eax
movl %eax, 0x6c(%rbx)
incq %r13
decl %r15d
jmp 0x5871c
movq 0x78(%rbx), %rax
movq (%rax,%rbp), %rsi
testq %rsi, %rsi
je 0x587f3
movq 0x80(%rbx), %rdi
leaq 0x1c(%rsp), %r14
movq %r14, %rdx
callq 0x5a02d
cmpl $-0x1, (%r14)
pushq $-0x1
popq %r12
je 0x587f7
movq 0x80(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl 0x10(%rsp), %r14d
movl %r14d, (%rcx,%rax,4)
movl 0x6c(%rbx), %ecx
movl 0x70(%rbx), %edx
movq 0x88(%rbx), %rax
cmpl %edx, %ecx
jl 0x587de
leal (%rdx,%rdx), %ecx
testl %edx, %edx
pushq $0x4
popq %rbp
cmovnel %ecx, %ebp
movslq %ebp, %rsi
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x6c80
testq %rax, %rax
je 0x587f7
movl %ebp, 0x70(%rbx)
movq %rax, 0x88(%rbx)
movl 0x6c(%rbx), %ecx
leal 0x1(%rcx), %edx
movl %edx, 0x6c(%rbx)
movslq %ecx, %rcx
movl %r14d, (%rax,%rcx,4)
incl 0x68(%rbx)
xorl %r12d, %r12d
jmp 0x587f7
pushq $-0x1
popq %r12
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq