// File: karel-brinda[P]ococo/ext/htslib/cram/cram_io.c
// Function: cram_read_slice
cram_slice *cram_read_slice(cram_fd *fd) {
    cram_block *b = cram_read_block(fd);
    cram_slice *s = calloc(1, sizeof(*s));
    int i, n, max_id, min_id;

    if (!b || !s)
	goto err;

    s->hdr_block = b;
    switch (b->content_type) {
    case MAPPED_SLICE:
    case UNMAPPED_SLICE:
	if (!(s->hdr = cram_decode_slice_header(fd, b)))
	    goto err;
	break;

    default:
	fprintf(stderr, "Unexpected block of type %s\n",
		cram_content_type2str(b->content_type));
	goto err;
    }

    if (s->hdr->num_blocks < 1) {
        fprintf(stderr, "Slice does not include any data blocks.\n");
	goto err;
    }

    s->block = calloc(n = s->hdr->num_blocks, sizeof(*s->block));
    if (!s->block)
	goto err;

    for (max_id = i = 0, min_id = INT_MAX; i < n; i++) {
	if (!(s->block[i] = cram_read_block(fd)))
	    goto err;

	if (s->block[i]->content_type == EXTERNAL) {
	    if (max_id < s->block[i]->content_id)
		max_id = s->block[i]->content_id;
	    if (min_id > s->block[i]->content_id)
		min_id = s->block[i]->content_id;
	}
    }
    if (min_id >= 0 && max_id < 1024) {
	if (!(s->block_by_id = calloc(1024, sizeof(s->block[0]))))
	    goto err;

	for (i = 0; i < n; i++) {
	    if (s->block[i]->content_type != EXTERNAL)
		continue;
	    s->block_by_id[s->block[i]->content_id] = s->block[i];
	}
    }

    /* Initialise encoding/decoding tables */
    s->cigar = NULL;
    s->cigar_alloc = 0;
    s->ncigar = 0;

    if (!(s->seqs_blk = cram_new_block(EXTERNAL, 0)))      goto err;
    if (!(s->qual_blk = cram_new_block(EXTERNAL, DS_QS)))  goto err;
    if (!(s->name_blk = cram_new_block(EXTERNAL, DS_RN)))  goto err;
    if (!(s->aux_blk  = cram_new_block(EXTERNAL, DS_aux))) goto err;
    if (!(s->base_blk = cram_new_block(EXTERNAL, DS_IN)))  goto err;
    if (!(s->soft_blk = cram_new_block(EXTERNAL, DS_SC)))  goto err;

    s->crecs = NULL;

    s->last_apos = s->hdr->ref_seq_start;
    
    return s;

 err:
    if (b)
	cram_free_block(b);
    if (s) {
	s->hdr_block = NULL;
	cram_free_slice(s);
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x45bc0
movq %rax, -0x18(%rbp)
movl $0x1, %edi
movl $0x100, %esi            # imm = 0x100
callq 0x4370
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x4f95a
cmpq $0x0, -0x20(%rbp)
jne 0x4f95f
jmp 0x4fca5
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
addl $-0x2, %eax
subl $0x1, %eax
ja 0x4f99d
jmp 0x4f97c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x27560
movq -0x20(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x4f99b
jmp 0x4fca5
jmp 0x4f9d1
movq 0x3a62c(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x48480
movq -0x38(%rbp), %rdi
movq %rax, %rdx
leaq 0x2d63c(%rip), %rsi     # 0x7d001
movb $0x0, %al
callq 0x4830
jmp 0x4fca5
movq -0x20(%rbp), %rax
movq (%rax), %rax
cmpl $0x1, 0x20(%rax)
jge 0x4f9fb
movq 0x3a5eb(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x2d62f(%rip), %rsi     # 0x7d01e
movb $0x0, %al
callq 0x4830
jmp 0x4fca5
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, -0x28(%rbp)
movslq %eax, %rdi
movl $0x8, %esi
callq 0x4370
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x4fa30
jmp 0x4fca5
movl $0x0, -0x24(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x7fffffff, -0x30(%rbp) # imm = 0x7FFFFFFF
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x4faf9
movq -0x10(%rbp), %rdi
callq 0x45bc0
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x24(%rbp), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0x4fa75
jmp 0x4fca5
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x4, 0x8(%rax)
jne 0x4fae9
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x24(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl 0xc(%rcx), %eax
jge 0x4fab9
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0xc(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x24(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl 0xc(%rcx), %eax
jle 0x4fae7
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0xc(%rax), %eax
movl %eax, -0x30(%rbp)
jmp 0x4fae9
jmp 0x4faeb
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4fa45
cmpl $0x0, -0x30(%rbp)
jl 0x4fb96
cmpl $0x400, -0x2c(%rbp)     # imm = 0x400
jge 0x4fb96
movl $0x400, %edi            # imm = 0x400
movl $0x8, %esi
callq 0x4370
movq -0x20(%rbp), %rcx
movq %rax, 0x18(%rcx)
cmpq $0x0, %rax
jne 0x4fb32
jmp 0x4fca5
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x4fb94
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x4, 0x8(%rax)
je 0x4fb59
jmp 0x4fb89
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x24(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movslq 0xc(%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4fb39
jmp 0x4fb96
movq -0x20(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x38(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x3c(%rax)
movl $0x4, %edi
xorl %esi, %esi
callq 0x45b10
movq -0x20(%rbp), %rcx
movq %rax, 0x68(%rcx)
cmpq $0x0, %rax
jne 0x4fbd7
jmp 0x4fca5
movl $0x4, %edi
movl $0xc, %esi
callq 0x45b10
movq -0x20(%rbp), %rcx
movq %rax, 0x70(%rcx)
cmpq $0x0, %rax
jne 0x4fbf9
jmp 0x4fca5
movl $0x4, %edi
movl $0xb, %esi
callq 0x45b10
movq -0x20(%rbp), %rcx
movq %rax, 0x60(%rcx)
cmpq $0x0, %rax
jne 0x4fc1b
jmp 0x4fca5
movl $0x4, %edi
movl $0x1, %esi
callq 0x45b10
movq -0x20(%rbp), %rcx
movq %rax, 0x88(%rcx)
cmpq $0x0, %rax
jne 0x4fc3d
jmp 0x4fca5
movl $0x4, %edi
movl $0xd, %esi
callq 0x45b10
movq -0x20(%rbp), %rcx
movq %rax, 0x78(%rcx)
cmpq $0x0, %rax
jne 0x4fc5c
jmp 0x4fca5
movl $0x4, %edi
movl $0xe, %esi
callq 0x45b10
movq -0x20(%rbp), %rcx
movq %rax, 0x80(%rcx)
cmpq $0x0, %rax
jne 0x4fc7e
jmp 0x4fca5
movq -0x20(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4fcd9
cmpq $0x0, -0x18(%rbp)
je 0x4fcb5
movq -0x18(%rbp), %rdi
callq 0x470c0
cmpq $0x0, -0x20(%rbp)
je 0x4fcd1
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x20(%rbp), %rdi
callq 0x4b920
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)