// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/htslib/cram/cram_io.c
// Function: cram_read_SAM_hdr
SAM_hdr *cram_read_SAM_hdr(cram_fd *fd) {
    int32_t header_len;
    char *header;
    SAM_hdr *hdr;

    /* 1.1 onwards stores the header in the first block of a container */
    if (CRAM_MAJOR_VERS(fd->version) == 1) {
	/* Length */
	if (-1 == int32_decode(fd, &header_len))
	    return NULL;

	/* Alloc and read */
	if (header_len < 0 || NULL == (header = malloc((size_t) header_len+1)))
	    return NULL;

	if (header_len != hread(fd->fp, header, header_len))
	    return NULL;
	header[header_len] = '\0';

	fd->first_container += 4 + header_len;
    } else {
	cram_container *c = cram_read_container(fd);
	cram_block *b;
	int i, len;

	if (!c)
	    return NULL;

	if (c->num_blocks < 1) {
	    cram_free_container(c);
	    return NULL;
	}

	if (!(b = cram_read_block(fd))) {
	    cram_free_container(c);
	    return NULL;
	}
	if (cram_uncompress_block(b) != 0) {
	    cram_free_container(c);
	    return NULL;
	}

	len = b->comp_size + 2 + 4*(CRAM_MAJOR_VERS(fd->version) >= 3) +
	    itf8_size(b->content_id) + 
	    itf8_size(b->uncomp_size) + 
	    itf8_size(b->comp_size);

	/* Extract header from 1st block */
	if (-1 == int32_get_blk(b, &header_len) ||
            header_len < 0 || /* Spec. says signed...  why? */
	    b->uncomp_size - 4 < header_len) {
	    cram_free_container(c);
	    cram_free_block(b);
	    return NULL;
	}
	if (NULL == (header = malloc((size_t) header_len+1))) {
	    cram_free_container(c);
	    cram_free_block(b);
	    return NULL;
	}
	memcpy(header, BLOCK_END(b), header_len);
	header[header_len]='\0';
	cram_free_block(b);

	/* Consume any remaining blocks */
	for (i = 1; i < c->num_blocks; i++) {
	    if (!(b = cram_read_block(fd))) {
		cram_free_container(c);
		return NULL;
	    }
	    len += b->comp_size + 2 + 4*(CRAM_MAJOR_VERS(fd->version) >= 3) +
		itf8_size(b->content_id) + 
		itf8_size(b->uncomp_size) + 
		itf8_size(b->comp_size);
	    cram_free_block(b);
	}

	if (c->length > 0 && len > 0 && c->length > len) {
	    // Consume padding
	    char *pads = malloc(c->length - len);
	    if (!pads) {
		cram_free_container(c);
		return NULL;
	    }

	    if (c->length - len != hread(fd->fp, pads, c->length - len)) {
		cram_free_container(c);
		return NULL;
	    }
	    free(pads);
	}

	cram_free_container(c);
    }

    /* Parse */
    hdr = sam_hdr_parse_(header, header_len);
    free(header);

    return hdr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rdi), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x37e40
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x321c7
cmpl $-0x1, %eax
je 0x37e9c
movl 0x4(%rsp), %r14d
testl %r14d, %r14d
js 0x37e9c
leaq 0x1(%r14), %rdi
callq 0x4700
testq %rax, %rax
je 0x37e9c
movq %rax, %r15
movq (%rbx), %r13
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rbp
subq %rsi, %rbp
cmpq %r14, %rbp
movq %r14, %r12
cmovbq %rbp, %r12
movq %rax, %rdi
movq %r12, %rdx
callq 0x4420
addq %r12, 0x8(%r13)
cmpq %r14, %rbp
jae 0x37e1f
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x48187
movq %rax, %r12
cmpq %r14, %r12
jne 0x37e9c
movb $0x0, (%r15,%r14)
movq 0x8840(%rbx), %rax
addq %r14, %rax
addq $0x4, %rax
movq %rax, 0x8840(%rbx)
jmp 0x37e80
movq %rbx, %rdi
callq 0x360e5
testq %rax, %rax
je 0x37e7a
movq %rax, %r14
cmpl $0x0, 0x24(%rax)
jle 0x37e72
movq %rbx, %rdi
callq 0x32631
testq %rax, %rax
je 0x37e72
movq %rax, %r12
movq %rax, %rdi
callq 0x32ec9
testl %eax, %eax
je 0x37ead
movq %r14, %rdi
callq 0x35cde
xorl %eax, %eax
testb %al, %al
je 0x37e9c
movl 0x4(%rsp), %esi
movq %r15, %rdi
callq 0x4095d
movq %rax, %rbx
movq %r15, %rdi
callq 0x4610
movq %rbx, %rax
jmp 0x37e9e
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%r12), %eax
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge 0x3(%rsp)
movl $0x1, %ecx
movl $0x1, %edx
cmpl $0x80, %eax
jb 0x37ef4
movl $0x2, %edx
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x37ef4
movl $0x3, %edx
cmpl $0x200000, %eax         # imm = 0x200000
jb 0x37ef4
cmpl $0x10000000, %eax       # imm = 0x10000000
movl $0x5, %edx
sbbl $0x0, %edx
movl 0x10(%r12), %ebp
movl 0x14(%r12), %eax
cmpl $0x80, %eax
jb 0x37f2a
movl $0x2, %ecx
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x37f2a
movl $0x3, %ecx
cmpl $0x200000, %eax         # imm = 0x200000
jb 0x37f2a
cmpl $0x10000000, %eax       # imm = 0x10000000
movl $0x5, %ecx
sbbl $0x0, %ecx
movl %edx, 0x8(%rsp)
movl %ecx, 0xc(%rsp)
movl $0x1, %r13d
cmpl $0x80, %ebp
jb 0x37f6c
movl $0x2, %r13d
cmpl $0x4000, %ebp           # imm = 0x4000
jb 0x37f6c
movl $0x3, %r13d
cmpl $0x200000, %ebp         # imm = 0x200000
jb 0x37f6c
cmpl $0x10000000, %ebp       # imm = 0x10000000
movl $0x5, %r13d
sbbl $0x0, %r13d
leaq 0x4(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x3231e
cmpl $-0x1, %eax
sete %cl
movl (%r15), %eax
testl %eax, %eax
sets %dl
orb %cl, %dl
jne 0x37f9c
movl %eax, %ecx
movl 0x14(%r12), %eax
addl $-0x4, %eax
cmpl %ecx, %eax
jge 0x37fb1
movq %r14, %rdi
callq 0x35cde
movq %r12, %rdi
callq 0x32ea8
jmp 0x37e7a
movq %rcx, 0x10(%rsp)
leaq 0x1(%rcx), %rdi
callq 0x4700
testq %rax, %rax
je 0x3816a
movq %rax, %r15
xorl %eax, %eax
movb 0x3(%rsp), %cl
movb %cl, %al
leal 0x2(%rbp,%rax,4), %ebp
addl 0x8(%rsp), %ebp
addl 0xc(%rsp), %ebp
addl %r13d, %ebp
movq 0x20(%r12), %rsi
addq 0x30(%r12), %rsi
movq %r15, %rdi
movq 0x10(%rsp), %r13
movq %r13, %rdx
callq 0x4420
movb $0x0, (%r15,%r13)
movq %r12, %rdi
callq 0x32ea8
cmpl $0x2, 0x24(%r14)
jl 0x3811e
movl $0x1, %r12d
movq %rbx, %rdi
callq 0x32631
testq %rax, %rax
je 0x38184
movl 0xc(%rax), %r8d
xorl %ecx, %ecx
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge %sil
movl $0x1, %edx
movl $0x1, %edi
cmpl $0x80, %r8d
jb 0x3807a
movl $0x2, %edi
cmpl $0x4000, %r8d           # imm = 0x4000
jb 0x3807a
movl $0x3, %edi
cmpl $0x200000, %r8d         # imm = 0x200000
jb 0x3807a
cmpl $0x10000000, %r8d       # imm = 0x10000000
movl $0x5, %edi
sbbl $0x0, %edi
movl 0x14(%rax), %r8d
cmpl $0x80, %r8d
jb 0x380b2
movl $0x2, %edx
cmpl $0x4000, %r8d           # imm = 0x4000
jb 0x380b2
movl $0x3, %edx
cmpl $0x200000, %r8d         # imm = 0x200000
jb 0x380b2
cmpl $0x10000000, %r8d       # imm = 0x10000000
movl $0x5, %edx
sbbl $0x0, %edx
movl 0x10(%rax), %r9d
movl $0x1, %r8d
cmpl $0x80, %r9d
jb 0x380f4
movl $0x2, %r8d
cmpl $0x4000, %r9d           # imm = 0x4000
jb 0x380f4
movl $0x3, %r8d
cmpl $0x200000, %r9d         # imm = 0x200000
jb 0x380f4
cmpl $0x10000000, %r9d       # imm = 0x10000000
movl $0x5, %r8d
sbbl $0x0, %r8d
movb %sil, %cl
shll $0x2, %ecx
addl %r9d, %ebp
addl %edi, %ebp
addl %edx, %ebp
addl %ecx, %ebp
addl %r8d, %ebp
addl $0x2, %ebp
movq %rax, %rdi
callq 0x32ea8
incl %r12d
cmpl 0x24(%r14), %r12d
jl 0x3801a
movl (%r14), %eax
testl %eax, %eax
jle 0x3815b
testl %ebp, %ebp
jle 0x3815b
subl %ebp, %eax
jle 0x3815b
movslq %eax, %r12
movq %r12, %rdi
callq 0x4700
testq %rax, %rax
je 0x38184
movq %rax, %r13
movq (%rbx), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x3224c
cmpq %r12, %rax
jne 0x38184
movq %r13, %rdi
callq 0x4610
movq %r14, %rdi
callq 0x35cde
movb $0x1, %al
jmp 0x37e7c
movq %r14, %rdi
callq 0x35cde
movq %r12, %rdi
callq 0x32ea8
xorl %eax, %eax
xorl %r15d, %r15d
jmp 0x37e7c
movq %r14, %rdi
callq 0x35cde
xorl %eax, %eax
jmp 0x37e7c