// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %rbx
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rdi), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x37050
leaq 0x14(%rsp), %rsi
movq %rbx, %rdi
callq 0x31676
cmpl $-0x1, %eax
je 0x3708a
movl 0x14(%rsp), %r15d
testl %r15d, %r15d
js 0x3708a
leaq 0x1(%r15), %rdi
callq 0x4700
testq %rax, %rax
je 0x3708a
movq %rax, %r12
movq (%rbx), %r13
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rbp
subq %rsi, %rbp
cmpq %r15, %rbp
movq %r15, %r14
cmovbq %rbp, %r14
movq %rax, %rdi
movq %r14, %rdx
callq 0x4420
addq %r14, 0x8(%r13)
cmpq %r15, %rbp
jae 0x37016
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x4716b
movq %rax, %r14
cmpq %r15, %r14
jne 0x3708a
movb $0x0, (%r12,%r15)
movq 0x8840(%rbx), %rax
addq %r15, %rax
addq $0x4, %rax
movq %rax, 0x8840(%rbx)
movq %r12, %rdi
movl %r15d, %esi
callq 0x3f91e
movq %rax, %rbx
movq %r12, %rdi
callq 0x4610
movq %rbx, %rax
jmp 0x3708c
movq %rbx, %rdi
callq 0x3530f
testq %rax, %rax
je 0x3708a
movq %rax, %r14
cmpl $0x0, 0x24(%rax)
jle 0x37082
movq %rbx, %rdi
callq 0x31aab
testq %rax, %rax
je 0x37082
movq %rax, %r13
movq %rax, %rdi
callq 0x321d5
testl %eax, %eax
je 0x3709b
movq %r14, %rdi
callq 0x34f02
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%r13), %eax
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge %dl
movl $0x1, %esi
movl $0x1, %edi
cmpl $0x80, %eax
jb 0x370df
movl $0x2, %edi
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x370df
movl $0x3, %edi
cmpl $0x200000, %eax         # imm = 0x200000
jb 0x370df
cmpl $0x10000000, %eax       # imm = 0x10000000
movl $0x5, %edi
sbbl $0x0, %edi
movl 0x10(%r13), %ebp
movslq 0x14(%r13), %rax
cmpq $0x80, %rax
jb 0x37114
movl $0x2, %esi
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x37114
movl $0x3, %esi
cmpl $0x200000, %eax         # imm = 0x200000
jb 0x37114
cmpl $0x10000000, %eax       # imm = 0x10000000
movl $0x5, %esi
sbbl $0x0, %esi
movl $0x1, %r8d
cmpl $0x80, %ebp
jb 0x3714e
movl $0x2, %r8d
cmpl $0x4000, %ebp           # imm = 0x4000
jb 0x3714e
movl $0x3, %r8d
cmpl $0x200000, %ebp         # imm = 0x200000
jb 0x3714e
cmpl $0x10000000, %ebp       # imm = 0x10000000
movl $0x5, %r8d
sbbl $0x0, %r8d
movq 0x30(%r13), %r9
movq %rax, %rcx
subq %r9, %rcx
cmpq $0x4, %rcx
jae 0x37173
movq %r14, %rdi
callq 0x34f02
movq %r13, %rdi
callq 0x321b4
jmp 0x3708a
movq 0x20(%r13), %rcx
movl (%rcx,%r9), %r15d
addq $0x4, %r9
movq %r9, 0x30(%r13)
testl %r15d, %r15d
js 0x3715e
addl $-0x4, %eax
cmpl %r15d, %eax
jl 0x3715e
movq %rcx, 0x18(%rsp)
movq %r9, 0x20(%rsp)
movl %r8d, 0x8(%rsp)
movl %edi, 0xc(%rsp)
movl %esi, 0x10(%rsp)
movb %dl, 0x7(%rsp)
leaq 0x1(%r15), %rdi
callq 0x4700
testq %rax, %rax
je 0x3715e
movq %rax, %r12
xorl %eax, %eax
movb 0x7(%rsp), %cl
movb %cl, %al
leal 0x2(%rbp,%rax,4), %ebp
addl 0xc(%rsp), %ebp
addl 0x10(%rsp), %ebp
addl 0x8(%rsp), %ebp
movq 0x18(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x4420
movb $0x0, (%r12,%r15)
movq %r13, %rdi
callq 0x321b4
cmpl $0x2, 0x24(%r14)
jl 0x3730b
movl $0x1, %r13d
movq %rbx, %rdi
callq 0x31aab
testq %rax, %rax
je 0x37082
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 0x37267
movl $0x2, %edi
cmpl $0x4000, %r8d           # imm = 0x4000
jb 0x37267
movl $0x3, %edi
cmpl $0x200000, %r8d         # imm = 0x200000
jb 0x37267
cmpl $0x10000000, %r8d       # imm = 0x10000000
movl $0x5, %edi
sbbl $0x0, %edi
movl 0x14(%rax), %r8d
cmpl $0x80, %r8d
jb 0x3729f
movl $0x2, %edx
cmpl $0x4000, %r8d           # imm = 0x4000
jb 0x3729f
movl $0x3, %edx
cmpl $0x200000, %r8d         # imm = 0x200000
jb 0x3729f
cmpl $0x10000000, %r8d       # imm = 0x10000000
movl $0x5, %edx
sbbl $0x0, %edx
movl 0x10(%rax), %r9d
movl $0x1, %r8d
cmpl $0x80, %r9d
jb 0x372e1
movl $0x2, %r8d
cmpl $0x4000, %r9d           # imm = 0x4000
jb 0x372e1
movl $0x3, %r8d
cmpl $0x200000, %r9d         # imm = 0x200000
jb 0x372e1
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 0x321b4
incl %r13d
cmpl 0x24(%r14), %r13d
jl 0x37207
movl (%r14), %eax
testl %eax, %eax
jle 0x37350
testl %ebp, %ebp
jle 0x37350
subl %ebp, %eax
jle 0x37350
movslq %eax, %r13
movq %r13, %rdi
callq 0x4700
testq %rax, %rax
je 0x37082
movq %rax, %rbp
movq (%rbx), %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x316fb
cmpq %r13, %rax
jne 0x37082
movq %rbp, %rdi
callq 0x4610
movq %r14, %rdi
callq 0x34f02
jmp 0x37035