// File: karel-brinda[P]ococo/ext/htslib/cram/sam_header.c
// Function: sam_hdr_add_lines
int sam_hdr_add_lines(SAM_hdr *sh, const char *lines, int len) {
    int i, lno, text_offset;
    char *hdr;

    if (!len)
	len = strlen(lines);

    text_offset = ks_len(&sh->text);
    if (EOF == kputsn(lines, len, &sh->text))
	return -1;
    hdr = ks_str(&sh->text) + text_offset;

    for (i = 0, lno = 1; i < len && hdr[i] != '\0'; i++, lno++) {
	khint32_t type;
	khint_t k;

	int l_start = i, new;
	SAM_hdr_type *h_type;
	SAM_hdr_tag *h_tag, *last;

	if (hdr[i] != '@') {
	    int j;
	    for (j = i; j < len && hdr[j] != '\0' && hdr[j] != '\n'; j++)
		;
	    sam_hdr_error("Header line does not start with '@'",
			  &hdr[l_start], len - l_start, lno);
	    return -1;
	}

	type = (hdr[i+1]<<8) | hdr[i+2];
	if (hdr[i+1] < 'A' || hdr[i+1] > 'z' ||
	    hdr[i+2] < 'A' || hdr[i+2] > 'z') {
	    sam_hdr_error("Header line does not have a two character key",
			  &hdr[l_start], len - l_start, lno);
	    return -1;
	}

	i += 3;
	if (hdr[i] == '\n')
	    continue;

	// Add the header line type
	if (!(h_type = pool_alloc(sh->type_pool)))
	    return -1;
	if (-1 == (k = kh_put(sam_hdr, sh->h, type, &new)))
	    return -1;

	// Form the ring, either with self or other lines of this type
	if (!new) {
	    SAM_hdr_type *t = kh_val(sh->h, k), *p;
	    p = t->prev;
	    
	    assert(p->next == t);
	    p->next = h_type;
	    h_type->prev = p;

	    t->prev = h_type;
	    h_type->next = t;
	    h_type->order = p->order+1;
	} else {
	    kh_val(sh->h, k) = h_type;
	    h_type->prev = h_type->next = h_type;
	    h_type->order = 0;
	}

	// Parse the tags on this line
	last = NULL;
	if ((type>>8) == 'C' && (type&0xff) == 'O') {
	    int j;
	    if (hdr[i] != '\t') {
		sam_hdr_error("Missing tab",
			      &hdr[l_start], len - l_start, lno);
		return -1;
	    }

	    for (j = ++i; j < len && hdr[j] != '\0' && hdr[j] != '\n'; j++)
		;

	    if (!(h_type->tag = h_tag = pool_alloc(sh->tag_pool)))
		return -1;
	    h_tag->str = string_ndup(sh->str_pool, &hdr[i], j-i);
	    h_tag->len = j-i;
	    h_tag->next = NULL;
	    if (!h_tag->str)
		return -1;

	    i = j;

	} else {
	    do {
		int j;
		if (hdr[i] != '\t') {
		    sam_hdr_error("Missing tab",
				  &hdr[l_start], len - l_start, lno);
		    return -1;
		}

		for (j = ++i; j < len && hdr[j] != '\0' && hdr[j] != '\n' && hdr[j] != '\t'; j++)
		    ;
	    
		if (!(h_tag = pool_alloc(sh->tag_pool)))
		    return -1;
		h_tag->str = string_ndup(sh->str_pool, &hdr[i], j-i);
		h_tag->len = j-i;
		h_tag->next = NULL;
		if (!h_tag->str)
		    return -1;

		if (h_tag->len < 3 || h_tag->str[2] != ':') {
		    sam_hdr_error("Malformed key:value pair",
				  &hdr[l_start], len - l_start, lno);
		    return -1;
		}
	    
		if (last)
		    last->next = h_tag;
		else
		    h_type->tag = h_tag;

		last = h_tag;
		i = j;
	    } while (i < len && hdr[i] != '\0' && hdr[i] != '\n');
	}

	/* Update RG/SQ hashes */
	if (-1 == sam_hdr_update_hashes(sh, type, h_type))
	    return -1;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x5b298
movq -0x18(%rbp), %rdi
callq 0x4230
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5b930
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x10(%rbp), %rdx
callq 0x5b950
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5b2cb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movq -0x10(%rbp), %rdi
callq 0x5b9e0
movslq -0x28(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x1, -0x24(%rbp)
movl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x79(%rbp)
jge 0x5b30f
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x5b31b
jmp 0x5b919
movl -0x20(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x40, %eax
je 0x5b3b6
movl -0x20(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x7a(%rbp)
jge 0x5b374
movq -0x30(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x7a(%rbp)
je 0x5b374
movq -0x30(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, -0x7a(%rbp)
movb -0x7a(%rbp), %al
testb $0x1, %al
jne 0x5b37d
jmp 0x5b38a
jmp 0x5b37f
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x5b33c
movq -0x30(%rbp), %rsi
movslq -0x3c(%rbp), %rax
addq %rax, %rsi
movl -0x1c(%rbp), %edx
subl -0x3c(%rbp), %edx
movl -0x24(%rbp), %ecx
leaq 0x226e3(%rip), %rdi     # 0x7da88
callq 0x5ba00
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movl -0x20(%rbp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movsbl (%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x41, %eax
jl 0x5b438
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x7a, %eax
jg 0x5b438
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x41, %eax
jl 0x5b438
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x7a, %eax
jle 0x5b464
movq -0x30(%rbp), %rsi
movslq -0x3c(%rbp), %rax
addq %rax, %rsi
movl -0x1c(%rbp), %edx
subl -0x3c(%rbp), %edx
movl -0x24(%rbp), %ecx
leaq 0x22659(%rip), %rdi     # 0x7daac
callq 0x5ba00
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movl -0x20(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x5b483
jmp 0x5b902
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x75ef0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x5b4a6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x34(%rbp), %esi
leaq -0x40(%rbp), %rdx
callq 0x5ba90
movl %eax, %ecx
movl %ecx, -0x38(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5b4d4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
cmpl $0x0, -0x40(%rbp)
jne 0x5b570
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq (%rax), %rax
cmpq -0x68(%rbp), %rax
jne 0x5b510
jmp 0x5b52f
leaq 0x225c3(%rip), %rdi     # 0x7dada
leaq 0x225c9(%rip), %rsi     # 0x7dae7
movl $0x14f, %edx            # imm = 0x14F
leaq 0x225cf(%rip), %rcx     # 0x7daf9
callq 0x4360
movq -0x48(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, (%rax)
movq -0x70(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x70(%rbp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x5b5a5
movq -0x48(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x18(%rax)
movq $0x0, -0x58(%rbp)
movl -0x34(%rbp), %eax
shrl $0x8, %eax
cmpl $0x43, %eax
jne 0x5b6f5
movl -0x34(%rbp), %eax
andl $0xff, %eax
cmpl $0x4f, %eax
jne 0x5b6f5
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x9, %eax
je 0x5b60a
movq -0x30(%rbp), %rsi
movslq -0x3c(%rbp), %rax
addq %rax, %rsi
movl -0x1c(%rbp), %edx
subl -0x3c(%rbp), %edx
movl -0x24(%rbp), %ecx
leaq 0x22534(%rip), %rdi     # 0x7db2d
callq 0x5ba00
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x7b(%rbp)
jge 0x5b64e
movq -0x30(%rbp), %rax
movslq -0x74(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x7b(%rbp)
je 0x5b64e
movq -0x30(%rbp), %rax
movslq -0x74(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, -0x7b(%rbp)
movb -0x7b(%rbp), %al
testb $0x1, %al
jne 0x5b657
jmp 0x5b664
jmp 0x5b659
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x5b616
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x75ef0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rcx
movq %rax, 0x10(%rcx)
cmpq $0x0, %rax
jne 0x5b68f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rsi
movslq -0x20(%rbp), %rax
addq %rax, %rsi
movl -0x74(%rbp), %eax
subl -0x20(%rbp), %eax
movslq %eax, %rdx
callq 0x601b0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x74(%rbp), %ecx
subl -0x20(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5b6ea
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movl -0x74(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x5b8dc
jmp 0x5b6f7
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x9, %eax
je 0x5b734
movq -0x30(%rbp), %rsi
movslq -0x3c(%rbp), %rax
addq %rax, %rsi
movl -0x1c(%rbp), %edx
subl -0x3c(%rbp), %edx
movl -0x24(%rbp), %ecx
leaq 0x2240a(%rip), %rdi     # 0x7db2d
callq 0x5ba00
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x7c(%rbp)
jge 0x5b78e
movq -0x30(%rbp), %rax
movslq -0x78(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x7c(%rbp)
je 0x5b78e
movq -0x30(%rbp), %rax
movslq -0x78(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0xa, %ecx
movb %al, -0x7c(%rbp)
je 0x5b78e
movq -0x30(%rbp), %rax
movslq -0x78(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x9, %eax
setne %al
movb %al, -0x7c(%rbp)
movb -0x7c(%rbp), %al
testb $0x1, %al
jne 0x5b797
jmp 0x5b7a4
jmp 0x5b799
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x5b740
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x75ef0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x5b7c7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rsi
movslq -0x20(%rbp), %rax
addq %rax, %rsi
movl -0x78(%rbp), %eax
subl -0x20(%rbp), %eax
movslq %eax, %rdx
callq 0x601b0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x78(%rbp), %ecx
subl -0x20(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5b822
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
movq -0x50(%rbp), %rax
cmpl $0x3, 0x10(%rax)
jl 0x5b83d
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x2(%rax), %eax
cmpl $0x3a, %eax
je 0x5b869
movq -0x30(%rbp), %rsi
movslq -0x3c(%rbp), %rax
addq %rax, %rsi
movl -0x1c(%rbp), %edx
subl -0x3c(%rbp), %edx
movl -0x24(%rbp), %ecx
leaq 0x222e1(%rip), %rdi     # 0x7db39
callq 0x5ba00
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
cmpq $0x0, -0x58(%rbp)
je 0x5b87d
movq -0x50(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
jmp 0x5b889
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x7d(%rbp)
jge 0x5b8cf
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x7d(%rbp)
je 0x5b8cf
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, -0x7d(%rbp)
movb -0x7d(%rbp), %al
testb $0x1, %al
jne 0x5b6f7
jmp 0x5b8dc
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x48(%rbp), %rdx
callq 0x5bde0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5b900
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b920
jmp 0x5b902
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x5b2ed
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)