// File: karel-brinda[P]ococo/ext/htslib/cram/cram_encode.c
// Function: cram_next_container
static cram_container *cram_next_container(cram_fd *fd, bam_seq_t *b) {
    cram_container *c = fd->ctr;
    int i;

    /* First occurence */
    if (c->curr_ref == -2)
	c->curr_ref = bam_ref(b);

    if (c->slice)
	cram_update_curr_slice(c);

    /* Flush container */
    if (c->curr_slice == c->max_slice ||
	(bam_ref(b) != c->curr_ref && !c->multi_seq)) {
	c->ref_seq_span = fd->last_base - c->ref_seq_start + 1;
	if (fd->verbose)
	    fprintf(stderr, "Flush container %d/%d..%d\n",
		    c->ref_seq_id, c->ref_seq_start,
		    c->ref_seq_start + c->ref_seq_span -1);

	/* Encode slices */
	if (fd->pool) {
	    if (-1 == cram_flush_container_mt(fd, c))
		return NULL;
	} else {
	    if (-1 == cram_flush_container(fd, c))
		return NULL;

	    // Move to sep func, as we need cram_flush_container for
	    // the closing phase to flush the partial container.
	    for (i = 0; i < c->max_slice; i++) {
		cram_free_slice(c->slices[i]);
		c->slices[i] = NULL;
	    }

	    c->slice = NULL;
	    c->curr_slice = 0;

	    /* Easy approach for purposes of freeing stats */
	    cram_free_container(c);
	}

	c = fd->ctr = cram_new_container(fd->seqs_per_slice,
					 fd->slices_per_container);
	if (!c)
	    return NULL;
	c->record_counter = fd->record_counter;
	c->curr_ref = bam_ref(b);
    }

    c->last_pos = c->first_base = c->last_base = bam_pos(b)+1;

    /* New slice */
    c->slice = c->slices[c->curr_slice] =
	cram_new_slice(MAPPED_SLICE, c->max_rec);
    if (!c->slice)
	return NULL;

    if (c->multi_seq) {
	c->slice->hdr->ref_seq_id = -2;
	c->slice->hdr->ref_seq_start = 0;
	c->slice->last_apos = 1;
    } else {
	c->slice->hdr->ref_seq_id = bam_ref(b);
	// wrong for unsorted data, will fix during encoding.
	c->slice->hdr->ref_seq_start = bam_pos(b)+1;
	c->slice->last_apos = bam_pos(b)+1;
    }

    c->curr_rec = 0;

    return c;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpl $-0x2, 0x6c(%rax)
jne 0x3a0f3
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x3a10a
movq -0x20(%rbp), %rdi
callq 0x39a00
movq -0x20(%rbp), %rax
movl 0x54(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x50(%rcx), %eax
je 0x3a13e
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x6c(%rcx), %eax
je 0x3a2ba
movq -0x20(%rbp), %rax
cmpl $0x0, 0x94(%rax)
jne 0x3a2ba
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
movq -0x20(%rbp), %rax
subl 0x8(%rax), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1fc(%rax)
je 0x3a19d
movq 0x4fe66(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x20(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x20(%rbp), %rax
movl 0x8(%rax), %r8d
movq -0x20(%rbp), %rax
addl 0xc(%rax), %r8d
subl $0x1, %r8d
leaq 0x42698(%rip), %rsi     # 0x7c82e
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8860(%rax)
je 0x3a1d5
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4f3e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x3a1d0
movq $0x0, -0x8(%rbp)
jmp 0x3a3d5
jmp 0x3a264
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4f1d0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x3a1fa
movq $0x0, -0x8(%rbp)
jmp 0x3a3d5
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jge 0x3a241
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x4b920
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x24(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3a201
movq -0x20(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x54(%rax)
movq -0x20(%rbp), %rdi
callq 0x4b5e0
movq -0x10(%rbp), %rax
movl 0x200(%rax), %edi
movq -0x10(%rbp), %rax
movl 0x204(%rax), %esi
callq 0x4b310
movq -0x10(%rbp), %rcx
movq %rax, 0x38(%rcx)
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3a29d
movq $0x0, -0x8(%rbp)
jmp 0x3a3d5
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xa4(%rax)
movq -0x20(%rbp), %rax
movl %ecx, 0xa0(%rax)
movq -0x20(%rbp), %rax
movl %ecx, 0x70(%rax)
movq -0x20(%rbp), %rax
movl 0x58(%rax), %esi
movl $0x2, %edi
callq 0x4f6c0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x20(%rbp), %rdx
movslq 0x54(%rdx), %rdx
movq %rcx, (%rax,%rdx,8)
movq -0x20(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x80(%rax)
jne 0x3a32d
movq $0x0, -0x8(%rbp)
jmp 0x3a3d5
movq -0x20(%rbp), %rax
cmpl $0x0, 0x94(%rax)
je 0x3a378
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movq (%rax), %rax
movl $0xfffffffe, 0x4(%rax)  # imm = 0xFFFFFFFE
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movq (%rax), %rax
movl $0x0, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movl $0x1, 0x20(%rax)
jmp 0x3a3c2
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movq (%rax), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movq (%rax), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movl %ecx, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x5c(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nop