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

    if (!fd->ctr) {
	fd->ctr = cram_new_container(fd->seqs_per_slice,
				     fd->slices_per_container);
	if (!fd->ctr)
	    return -1;
	fd->ctr->record_counter = fd->record_counter;
    }
    c = fd->ctr;

    if (!c->slice || c->curr_rec == c->max_rec ||
	(bam_ref(b) != c->curr_ref && c->curr_ref >= -1)) {
	int slice_rec, curr_rec, multi_seq = fd->multi_seq == 1;
	int curr_ref = c->slice ? c->curr_ref : bam_ref(b);


	/*
	 * Start packing slices when we routinely have under 1/4tr full.
	 *
	 * This option isn't available if we choose to embed references
	 * since we can only have one per slice.
	 */
	if (fd->multi_seq == -1 && c->curr_rec < c->max_rec/4+10 &&
	    fd->last_slice && fd->last_slice < c->max_rec/4+10 &&
	    !fd->embed_ref) {
	    if (fd->verbose && !c->multi_seq)
		fprintf(stderr, "Multi-ref enabled for this container\n");
	    multi_seq = 1;
	}

	slice_rec = c->slice_rec;
	curr_rec  = c->curr_rec;

	if (CRAM_MAJOR_VERS(fd->version) == 1 ||
	    c->curr_rec == c->max_rec || fd->multi_seq != 1 || !c->slice) {
	    if (NULL == (c = cram_next_container(fd, b))) {
		if (fd->ctr) {
		    // prevent cram_close attempting to flush
		    cram_free_container(fd->ctr);
		    fd->ctr = NULL;
		}
		return -1;
	    }
	}

	/*
	 * Due to our processing order, some things we've already done we
	 * cannot easily undo. So when we first notice we should be packing
	 * multiple sequences per container we emit the small partial
	 * container as-is and then start a fresh one in a different mode.
	 */
	if (multi_seq) {
	    fd->multi_seq = 1;
	    c->multi_seq = 1;
	    c->pos_sorted = 0; // required atm for multi_seq slices

	    if (!c->refs_used) {
		pthread_mutex_lock(&fd->ref_lock);
		c->refs_used = calloc(fd->refs->nref, sizeof(int));
		pthread_mutex_unlock(&fd->ref_lock);
		if (!c->refs_used)
		    return -1;
	    }
	}

	fd->last_slice = curr_rec - slice_rec;
	c->slice_rec = c->curr_rec;

	// Have we seen this reference before?
	if (bam_ref(b) >= 0 && bam_ref(b) != curr_ref && !fd->embed_ref &&
	    !fd->unsorted && multi_seq) {
	    
	    if (!c->refs_used) {
		pthread_mutex_lock(&fd->ref_lock);
		c->refs_used = calloc(fd->refs->nref, sizeof(int));
		pthread_mutex_unlock(&fd->ref_lock);
		if (!c->refs_used)
		    return -1;
	    } else if (c->refs_used && c->refs_used[bam_ref(b)]) {
		pthread_mutex_lock(&fd->ref_lock);
		fd->unsorted = 1;
		pthread_mutex_unlock(&fd->ref_lock);
		fd->multi_seq = 1;
	    }
	}

	c->curr_ref = bam_ref(b);
	if (c->refs_used && c->curr_ref >= 0) c->refs_used[c->curr_ref]++;
    }

    if (!c->bams) {
	/* First time through, allocate a set of bam pointers */
	pthread_mutex_lock(&fd->bam_list_lock);
	if (fd->bl) {
	    spare_bams *spare = fd->bl;
	    c->bams = spare->bams;
	    fd->bl = spare->next;
	    free(spare);
	} else {
	    c->bams = calloc(c->max_c_rec, sizeof(bam_seq_t *));
	    if (!c->bams)
		return -1;
	}
	pthread_mutex_unlock(&fd->bam_list_lock);
    }

    /* Copy or alloc+copy the bam record, for later encoding */
    if (c->bams[c->curr_c_rec])
	bam_copy1(c->bams[c->curr_c_rec], b);
    else
	c->bams[c->curr_c_rec] = bam_dup(b);

    c->curr_rec++;
    c->curr_c_rec++;
    fd->record_counter++;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x39b6a
movq -0x10(%rbp), %rax
movl 0x200(%rax), %edi
movq -0x10(%rbp), %rax
movl 0x204(%rax), %esi
callq 0x4b310
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x39b56
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3a0b4
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x39bb5
movq -0x20(%rbp), %rax
movl 0x5c(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x58(%rcx), %eax
je 0x39bb5
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x6c(%rcx), %eax
je 0x39f61
movq -0x20(%rbp), %rax
cmpl $-0x1, 0x6c(%rax)
jl 0x39f61
movq -0x10(%rbp), %rax
cmpl $0x1, 0x8850(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x39be5
movq -0x20(%rbp), %rax
movl 0x6c(%rax), %eax
movl %eax, -0x3c(%rbp)
jmp 0x39bee
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $-0x1, 0x8850(%rax)
jne 0x39ca5
movq -0x20(%rbp), %rax
movl 0x5c(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movl 0x58(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x40(%rbp), %eax
addl $0xa, %ecx
cmpl %ecx, %eax
jge 0x39ca5
movq -0x10(%rbp), %rax
cmpl $0x0, 0x884c(%rax)
je 0x39ca5
movq -0x10(%rbp), %rax
movl 0x884c(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rax
movl 0x58(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x44(%rbp), %eax
addl $0xa, %ecx
cmpl %ecx, %eax
jge 0x39ca5
movq -0x10(%rbp), %rax
cmpl $0x0, 0x208(%rax)
jne 0x39ca5
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1fc(%rax)
je 0x39c9e
movq -0x20(%rbp), %rax
cmpl $0x0, 0x94(%rax)
jne 0x39c9e
movq 0x50343(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x429ab(%rip), %rsi     # 0x7c642
movb $0x0, %al
callq 0x4830
movl $0x1, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movl 0x68(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl 0x5c(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x1, %eax
je 0x39cf3
movq -0x20(%rbp), %rax
movl 0x5c(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x58(%rcx), %eax
je 0x39cf3
movq -0x10(%rbp), %rax
cmpl $0x1, 0x8850(%rax)
jne 0x39cf3
movq -0x20(%rbp), %rax
cmpq $0x0, 0x80(%rax)
jne 0x39d40
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3a0c0
movq %rax, %rcx
movq %rcx, -0x20(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x39d3e
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x39d32
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x4b5e0
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3a0b4
jmp 0x39d40
cmpl $0x0, -0x2c(%rbp)
je 0x39de4
movq -0x10(%rbp), %rax
movl $0x1, 0x8850(%rax)
movq -0x20(%rbp), %rax
movl $0x1, 0x94(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x88(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x240(%rax)
jne 0x39de2
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4880
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x4370
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x240(%rax)
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq -0x20(%rbp), %rax
cmpq $0x0, 0x240(%rax)
jne 0x39de0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3a0b4
jmp 0x39de2
jmp 0x39de4
movl -0x28(%rbp), %ecx
subl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x884c(%rax)
movq -0x20(%rbp), %rax
movl 0x5c(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x68(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jl 0x39f1e
movq -0x18(%rbp), %rax
movl (%rax), %eax
cmpl -0x30(%rbp), %eax
je 0x39f1e
movq -0x10(%rbp), %rax
cmpl $0x0, 0x208(%rax)
jne 0x39f1e
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8854(%rax)
jne 0x39f1e
cmpl $0x0, -0x2c(%rbp)
je 0x39f1e
movq -0x20(%rbp), %rax
cmpq $0x0, 0x240(%rax)
jne 0x39eb8
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4880
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x4370
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x240(%rax)
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq -0x20(%rbp), %rax
cmpq $0x0, 0x240(%rax)
jne 0x39eb6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3a0b4
jmp 0x39f1c
movq -0x20(%rbp), %rax
cmpq $0x0, 0x240(%rax)
je 0x39f1a
movq -0x20(%rbp), %rax
movq 0x240(%rax), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x39f1a
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4880
movq -0x10(%rbp), %rax
movl $0x1, 0x8854(%rax)
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq -0x10(%rbp), %rax
movl $0x1, 0x8850(%rax)
jmp 0x39f1c
jmp 0x39f1e
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x240(%rax)
je 0x39f5f
movq -0x20(%rbp), %rax
cmpl $0x0, 0x6c(%rax)
jl 0x39f5f
movq -0x20(%rbp), %rax
movq 0x240(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x6c(%rcx), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x39f61
movq -0x20(%rbp), %rax
cmpq $0x0, 0xb8(%rax)
jne 0x3a01c
movq -0x10(%rbp), %rdi
addq $0x88c8, %rdi           # imm = 0x88C8
callq 0x4880
movq -0x10(%rbp), %rax
cmpq $0x0, 0x88c0(%rax)
je 0x39fd0
movq -0x10(%rbp), %rax
movq 0x88c0(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88c0(%rax)
movq -0x38(%rbp), %rdi
callq 0x46a0
jmp 0x3a00c
movq -0x20(%rbp), %rax
movslq 0x60(%rax), %rdi
movl $0x8, %esi
callq 0x4370
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0xb8(%rax)
jne 0x3a00a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3a0b4
jmp 0x3a00c
movq -0x10(%rbp), %rdi
addq $0x88c8, %rdi           # imm = 0x88C8
callq 0x4420
movq -0x20(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x64(%rcx), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x3a058
movq -0x20(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x64(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
callq 0x1b650
jmp 0x3a083
callq 0x1b5f0
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x1b650
movq %rax, %rdx
movq -0x20(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x64(%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movl 0x5c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x5c(%rax)
movq -0x20(%rbp), %rax
movl 0x64(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x64(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)