// File: /workspace/github_cpp_makefile_star2_O3/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
jne 0x3d9e8
movl %esi, 0x28(%rsp)
movl %edx, 0x20(%rsp)
movl 0x38(%r13), %r14d
movq %r13, %rbx
movslq %r14d, %r13
movq 0x40(%rbx), %rdi
shlq $0x5, %r13
leaq 0x20(%r13), %rsi
callq 0x48d0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x3de56
movl %r14d, 0x10(%rsp)
movq %rax, 0x40(%rbx)
movq 0x10(%r15), %rbp
movq $0x0, (%rax,%r13)
movl $0x0, 0x8(%rax,%r13)
movq %r15, 0x10(%rax,%r13)
movq %rbp, 0x18(%rax,%r13)
testq %rbp, %rbp
je 0x3de51
movq %rbx, %r14
movq %r15, 0x18(%rsp)
movq 0x8(%rbp), %r15
movzbl (%r15), %eax
cmpl $0x4c, %eax
je 0x3d969
cmpl $0x53, %eax
jne 0x3d985
cmpb $0x4e, 0x1(%r15)
jne 0x3d985
movslq 0x10(%rbp), %r12
movq %r12, %rdi
callq 0x4700
movq 0x40(%r14), %rbx
movq %rax, (%rbx,%r13)
testq %rax, %rax
je 0x3de51
addq %r13, %rbx
addq $0x3, %r15
addq $-0x3, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x42b0
movq (%rbx), %rax
movslq 0x10(%rbp), %rcx
movb $0x0, -0x3(%rax,%rcx)
jmp 0x3d985
cmpb $0x4e, 0x1(%r15)
jne 0x3d985
addq $0x3, %r15
movq %r15, %rdi
callq 0x4840
movq 0x40(%r14), %rcx
movl %eax, 0x8(%rcx,%r13)
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x3d90c
movq 0x40(%r14), %rax
movq (%rax,%r13), %rsi
testq %rsi, %rsi
je 0x3de51
movq 0x48(%r14), %rdi
leaq 0x64(%rsp), %r15
movq %r15, %rdx
callq 0x40942
cmpl $-0x1, (%r15)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
je 0x3de56
movq %r14, %r13
movq 0x48(%r14), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl 0x10(%rsp), %edx
movl %edx, (%rcx,%rax,4)
incl 0x38(%r14)
movq 0x18(%rsp), %r15
movl 0x20(%rsp), %edx
movl 0x28(%rsp), %esi
movl %esi, %eax
xorl $0x52, %eax
xorl $0x47, %edx
orl %edx, %eax
jne 0x3db24
movl %esi, 0x28(%rsp)
movl %edx, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movl 0x50(%r13), %r14d
movslq %r14d, %rbp
movq 0x58(%r13), %rdi
shlq $0x5, %rbp
leaq 0x20(%rbp), %rsi
callq 0x48d0
testq %rax, %rax
je 0x3de51
movq %rax, %r15
movq %rax, 0x58(%r13)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rbx
movq $0x0, (%r15,%rbp)
movl $0x0, 0x18(%r15,%rbp)
movq %rax, 0x8(%r15,%rbp)
movq %rbx, 0x10(%r15,%rbp)
movl %r14d, 0x1c(%r15,%rbp)
testq %rbx, %rbx
je 0x3de51
movl %r14d, 0x10(%rsp)
movq 0x8(%rbx), %r12
cmpb $0x49, (%r12)
jne 0x3dacd
cmpb $0x44, 0x1(%r12)
jne 0x3dacd
movq %r13, %r14
movslq 0x10(%rbx), %r13
movq %r13, %rdi
callq 0x4700
movq %rax, (%r15,%rbp)
testq %rax, %rax
je 0x3de51
addq %rbp, %r15
addq $0x3, %r12
addq $-0x3, %r13
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x42b0
movq (%r15), %rax
movslq 0x10(%rbx), %rcx
movb $0x0, -0x3(%rax,%rcx)
movq %r14, %r13
movq 0x58(%r14), %r15
movq (%r15,%rbp), %rdi
callq 0x4230
movl %eax, 0x18(%r15,%rbp)
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x3da64
movq (%r15,%rbp), %rsi
testq %rsi, %rsi
je 0x3de51
movq 0x60(%r13), %rdi
leaq 0x60(%rsp), %r15
movq %r15, %rdx
callq 0x40942
cmpl $-0x1, (%r15)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
je 0x3de56
movq 0x60(%r13), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl 0x10(%rsp), %edx
movl %edx, (%rcx,%rax,4)
incl 0x50(%r13)
movq 0x18(%rsp), %r15
movl 0x20(%rsp), %edx
movl 0x28(%rsp), %esi
xorl $0x50, %esi
xorl %ebp, %ebp
orl %edx, %esi
jne 0x3de56
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 0x3de56
movq %rax, 0x78(%r13)
movq 0x10(%r15), %rcx
movq $0x0, (%rax,%r14,8)
movl $0x0, 0x18(%rax,%r14,8)
movq %r15, 0x8(%rax,%r14,8)
movq %rcx, 0x10(%rax,%r14,8)
movl %ebx, 0x1c(%rax,%r14,8)
movq %r14, 0x10(%rsp)
movl $0xffffffff, 0x20(%rax,%r14,8) # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x3de56
movq %rcx, %r15
movl %ebx, 0x34(%rsp)
movq %r13, 0x38(%rsp)
movq 0x8(%r15), %r14
movzbl (%r14), %eax
cmpl $0x50, %eax
je 0x3dc2c
cmpl $0x49, %eax
jne 0x3dde9
cmpb $0x44, 0x1(%r14)
jne 0x3dde9
movq %r15, %r12
movslq 0x10(%r15), %r15
movq %r15, %rdi
callq 0x4700
movq 0x78(%r13), %rcx
movq 0x10(%rsp), %rbp
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x3de51
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)
movq %r12, %r15
jmp 0x3dde9
cmpb $0x50, 0x1(%r14)
jne 0x3dde9
movslq 0x10(%r15), %rax
movb (%r14,%rax), %dl
movb $0x0, (%r14,%rax)
movq 0x80(%r13), %rax
movq 0x8(%r15), %rdi
movq %rax, 0x20(%rsp)
movl (%rax), %esi
testl %esi, %esi
je 0x3dc9a
leaq 0x3(%rdi), %rax
movq %rax, 0x28(%rsp)
movsbl (%rax), %ebp
testl %ebp, %ebp
movb %dl, 0xf(%rsp)
movq %rdi, 0x48(%rsp)
movq %r15, 0x40(%rsp)
je 0x3dca1
movb 0x4(%rdi), %al
testb %al, %al
je 0x3dca3
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 0x3dc83
jmp 0x3dca3
xorl %ecx, %ecx
jmp 0x3dd46
xorl %ebp, %ebp
movq %rsi, 0x50(%rsp)
leal -0x1(%rsi), %r12d
andl %r12d, %ebp
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rsi
movl $0x1, %r14d
movl %ebp, %ebx
movq %rsi, 0x18(%rsp)
movl %ebx, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %r13d
leal (%rbx,%rbx), %r15d
andl $0x1e, %r15d
movl %r13d, %eax
movl %r15d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x3dd21
testb $0x1, %al
jne 0x3dd08
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rdi
movq 0x28(%rsp), %rsi
callq 0x4750
movq 0x18(%rsp), %rsi
testl %eax, %eax
je 0x3dd21
addl %r14d, %ebx
andl %r12d, %ebx
incl %r14d
cmpl %ebp, %ebx
jne 0x3dcc5
movq 0x38(%rsp), %r13
movq 0x50(%rsp), %rcx
jmp 0x3dd38
movl %r15d, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x50(%rsp), %rcx
cmovel %ebx, %ecx
movq 0x38(%rsp), %r13
movb 0xf(%rsp), %dl
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %r15
movslq 0x10(%r15), %rax
movb %dl, (%rdi,%rax)
movq 0x80(%r13), %rax
cmpl (%rax), %ecx
movq 0x10(%rsp), %rdx
jne 0x3dd6b
movq 0x78(%r13), %rax
movl $0xffffffff, 0x20(%rax,%rdx,8) # imm = 0xFFFFFFFF
jmp 0x3dde9
movq 0x20(%rax), %rax
movl %ecx, %ecx
movslq (%rax,%rcx,4), %rbx
movq 0x78(%r13), %rax
leaq (%rbx,%rbx,4), %rcx
movl 0x1c(%rax,%rcx,8), %ecx
movl %ecx, 0x20(%rax,%rdx,8)
movslq 0x6c(%r13), %rax
testq %rax, %rax
jle 0x3dde9
movq 0x88(%r13), %rcx
cmpl %ebx, -0x4(%rcx,%rax,4)
jne 0x3dda3
decl %eax
movl %eax, 0x6c(%r13)
jmp 0x3dde9
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq 0x88(%r13), %rcx
cmpl %ebx, (%rcx,%r14,4)
jne 0x3dddc
leaq (%rcx,%r14,4), %rdi
leaq (%rcx,%r14,4), %rsi
addq $0x4, %rsi
addl %ebp, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
callq 0x48c0
movl 0x6c(%r13), %eax
decl %eax
movl %eax, 0x6c(%r13)
incq %r14
movslq %eax, %rcx
decl %ebp
cmpq %rcx, %r14
jl 0x3ddab
movq (%r15), %r15
testq %r15, %r15
jne 0x3dba6
movq 0x78(%r13), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
testq %rsi, %rsi
je 0x3de51
movq 0x80(%r13), %rdi
leaq 0x5c(%rsp), %r14
movq %r14, %rdx
callq 0x40942
cmpl $-0x1, (%r14)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
je 0x3de56
movq 0x80(%r13), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movl 0x34(%rsp), %r14d
movl %r14d, (%rcx,%rax,4)
movl 0x6c(%r13), %ecx
movl 0x70(%r13), %eax
cmpl %eax, %ecx
jge 0x3de67
movq 0x88(%r13), %rax
jmp 0x3de9b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
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 0x3de56
movl %ebx, 0x70(%r13)
movq %rax, 0x88(%r13)
movl 0x6c(%r13), %ecx
leal 0x1(%rcx), %edx
movl %edx, 0x6c(%r13)
movslq %ecx, %rcx
movl %r14d, (%rax,%rcx,4)
incl 0x68(%r13)
xorl %ebp, %ebp
jmp 0x3de56