// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq 0x38(%rdi), %r15
pushq $-0x1
popq %rbp
testq %r15, %r15
jne 0x74371
movl 0x200(%rbx), %edi
movl 0x204(%rbx), %esi
callq 0x507fa
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x748bd
movq %rax, %r15
movq 0x28(%rbx), %rax
movq %rax, 0x10(%r15)
movq 0x80(%r15), %rax
testq %rax, %rax
je 0x7439f
movl 0x5c(%r15), %ecx
cmpl 0x58(%r15), %ecx
je 0x7439f
movl 0x6c(%r15), %ecx
cmpl %ecx, (%r14)
sete %dl
cmpl $-0x1, %ecx
setl %cl
orb %dl, %cl
jne 0x74809
movl 0x8850(%rbx), %ecx
cmpl $0x1, %ecx
sete %r8b
leaq 0x6c(%r15), %rdx
testq %rax, %rax
cmoveq %r14, %rdx
movl (%rdx), %r12d
cmpl $-0x1, %ecx
jne 0x743f5
movl 0x58(%r15), %eax
movl 0x5c(%r15), %ecx
pushq $0x4
popq %rsi
cltd
idivl %esi
addl $0xa, %eax
cmpl %eax, %ecx
jge 0x743f2
movl 0x884c(%rbx), %ecx
xorl %r8d, %r8d
testl %ecx, %ecx
je 0x743f5
cmpl %eax, %ecx
jge 0x743f5
cmpl $0x0, 0x208(%rbx)
je 0x74742
xorl %r8d, %r8d
movl 0x5c(%r15), %r13d
movl 0x68(%r15), %eax
movl %eax, 0x4(%rsp)
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rbx), %eax
cmpl $0x100, %eax            # imm = 0x100
je 0x74430
cmpl 0x58(%r15), %r13d
je 0x74430
cmpl $0x1, 0x8850(%rbx)
jne 0x74430
cmpq $0x0, 0x80(%r15)
movl %r13d, %eax
jne 0x74631
movq 0x38(%rbx), %r15
movl 0x6c(%r15), %eax
cmpl $-0x2, %eax
jne 0x74444
movl (%r14), %eax
movl %eax, 0x6c(%r15)
movq 0x80(%r15), %rcx
testq %rcx, %rcx
je 0x74466
cmpl $0x0, 0x94(%r15)
movq (%rcx), %rcx
je 0x7446c
xorl %esi, %esi
pushq $-0x2
popq %rdx
xorl %edi, %edi
jmp 0x74480
movl 0x54(%r15), %ecx
jmp 0x744b3
movl 0xa0(%r15), %esi
movl 0xa4(%r15), %edi
subl %esi, %edi
incl %edi
movl %eax, %edx
movl %edx, 0x4(%rcx)
movl %esi, 0x8(%rcx)
movl %edi, 0xc(%rcx)
movl 0x5c(%r15), %esi
movl %esi, 0x10(%rcx)
movl 0x54(%r15), %ecx
testl %ecx, %ecx
jne 0x744ad
cmpl %edx, 0x4(%r15)
je 0x744a2
movl %edx, 0x4(%r15)
movl 0xa0(%r15), %edx
movl %edx, 0x8(%r15)
incl %ecx
movl %ecx, 0x54(%r15)
cmpl 0x50(%r15), %ecx
jne 0x744fb
movl %r8d, %ebp
movl 0x44(%rbx), %r8d
movl 0x8(%r15), %ecx
movl %r8d, %eax
subl %ecx, %eax
incl %eax
movl %eax, 0xc(%r15)
cmpl $0x0, 0x1fc(%rbx)
jne 0x74784
cmpq $0x0, 0x8860(%rbx)
je 0x74512
movq %rbx, %rdi
movq %r15, %rsi
callq 0x520d6
cmpl $-0x1, %eax
jne 0x7456a
jmp 0x745f1
cmpl %eax, (%r14)
je 0x7450a
cmpl $0x0, 0x94(%r15)
je 0x744b9
movl %r8d, %ebp
jmp 0x7459a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x51fbe
cmpl $-0x1, %eax
je 0x745f1
movl %r12d, (%rsp)
xorl %r12d, %r12d
movslq 0x50(%r15), %rax
cmpq %rax, %r12
jge 0x74551
movq 0x78(%r15), %rax
movq (%rax,%r12,8), %rdi
callq 0x50b17
movq 0x78(%r15), %rax
andq $0x0, (%rax,%r12,8)
incq %r12
jmp 0x7452d
andq $0x0, 0x80(%r15)
andl $0x0, 0x54(%r15)
movq %r15, %rdi
callq 0x5093a
movl (%rsp), %r12d
movl 0x200(%rbx), %edi
movl 0x204(%rbx), %esi
callq 0x507fa
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x7473a
movq %rax, %r15
movq 0x28(%rbx), %rax
movq %rax, 0x10(%r15)
movl (%r14), %eax
movl %eax, 0x6c(%r15)
movl 0x4(%r14), %eax
incl %eax
movl %eax, 0xa4(%r15)
movl %eax, 0xa0(%r15)
movl %eax, 0x70(%r15)
movl 0x58(%r15), %esi
pushq $0x2
popq %rdi
callq 0x52261
movq 0x78(%r15), %rcx
movslq 0x54(%r15), %rdx
movq %rax, (%rcx,%rdx,8)
movq %rax, 0x80(%r15)
testq %rax, %rax
je 0x745f1
cmpl $0x0, 0x94(%r15)
je 0x74610
movq (%rax), %rcx
movl $0xfffffffe, 0x4(%rcx)  # imm = 0xFFFFFFFE
xorl %edx, %edx
pushq $0x1
popq %rsi
jmp 0x74621
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
pushq $-0x1
popq %rbp
je 0x748bd
callq 0x5093a
andq $0x0, 0x38(%rbx)
jmp 0x748bd
movl (%r14), %edx
movq (%rax), %rcx
movl %edx, 0x4(%rcx)
movl 0x4(%r14), %edx
incl %edx
movl %edx, %esi
movl %ebp, %r8d
movl %edx, 0x8(%rcx)
movl %esi, 0x20(%rax)
andl $0x0, 0x5c(%r15)
xorl %eax, %eax
testb %r8b, %r8b
je 0x746a6
pushq $0x1
popq %rcx
movl %ecx, 0x8850(%rbx)
movl %ecx, 0x94(%r15)
andl $0x0, 0x88(%r15)
cmpq $0x0, 0x240(%r15)
jne 0x746a6
movl %r8d, %ebp
movl %r12d, (%rsp)
leaq 0x8898(%rbx), %r12
movq %r12, %rdi
callq 0x6ae0
movq 0x48(%rbx), %rax
movslq 0x18(%rax), %rdi
pushq $0x4
popq %rsi
callq 0x6470
movq %rax, 0x240(%r15)
movq %r12, %rdi
callq 0x64f0
cmpq $0x0, 0x240(%r15)
je 0x7473a
movl 0x5c(%r15), %eax
movl (%rsp), %r12d
movl %ebp, %r8d
subl 0x4(%rsp), %r13d
movl %r13d, 0x884c(%rbx)
movl %eax, 0x68(%r15)
movslq (%r14), %rax
testq %rax, %rax
pushq $-0x1
popq %rbp
js 0x747e1
cmpl %r12d, %eax
je 0x747e1
cmpl $0x0, 0x208(%rbx)
jne 0x747e1
cmpl $0x0, 0x8854(%rbx)
setne %cl
xorb $0x1, %r8b
orb %cl, %r8b
jne 0x747e1
movq 0x240(%r15), %rcx
testq %rcx, %rcx
je 0x747a5
cmpl $0x0, (%rcx,%rax,4)
je 0x747e1
leaq 0x8898(%rbx), %r12
movq %r12, %rdi
callq 0x6ae0
pushq $0x1
popq %r13
movl %r13d, 0x8854(%rbx)
movq %r12, %rdi
callq 0x64f0
movl %r13d, 0x8850(%rbx)
jmp 0x747e1
pushq $-0x1
popq %rbp
jmp 0x748bd
movb $0x1, %r8b
cmpl $0x0, 0x1fc(%rbx)
je 0x743f5
cmpl $0x0, 0x94(%r15)
jne 0x743f5
movq 0x3e871(%rip), %rax     # 0xb2fd8
movq (%rax), %rcx
leaq 0x15b5f(%rip), %rdi     # 0x8a2d0
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x6e70
movb $0x1, %r8b
jmp 0x743f5
movq 0x3e84d(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
movl 0x4(%r15), %edx
leaq 0x15d20(%rip), %rsi     # 0x8a4b9
xorl %eax, %eax
callq 0x6a90
jmp 0x744dc
leaq 0x8898(%rbx), %r12
movq %r12, %rdi
callq 0x6ae0
movq 0x48(%rbx), %rax
movslq 0x18(%rax), %rdi
pushq $0x4
popq %rsi
callq 0x6470
movq %rax, 0x240(%r15)
movq %r12, %rdi
callq 0x64f0
cmpq $0x0, 0x240(%r15)
je 0x748bd
movl (%r14), %eax
movl %eax, 0x6c(%r15)
movq 0x240(%r15), %rcx
testq %rcx, %rcx
setne %dl
testl %eax, %eax
setns %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x74809
movl %eax, %eax
incl (%rcx,%rax,4)
movq 0xb8(%r15), %rax
testq %rax, %rax
jne 0x74873
leaq 0x88c8(%rbx), %r12
movq %r12, %rdi
callq 0x6ae0
movq 0x88c0(%rbx), %rdi
testq %rdi, %rdi
je 0x7484c
movq (%rdi), %rax
movq %rax, 0xb8(%r15)
movq 0x8(%rdi), %rax
movq %rax, 0x88c0(%rbx)
callq 0x68a0
jmp 0x74864
movslq 0x60(%r15), %rdi
pushq $0x8
popq %rsi
callq 0x6470
movq %rax, 0xb8(%r15)
testq %rax, %rax
je 0x748bd
movq %r12, %rdi
callq 0x64f0
movq 0xb8(%r15), %rax
movslq 0x64(%r15), %rcx
movq (%rax,%rcx,8), %rdi
testq %rdi, %rdi
je 0x7488e
movq %r14, %rsi
callq 0x61926
movl 0x64(%r15), %ecx
jmp 0x748ad
callq 0x618ff
movq %rax, %rdi
movq %r14, %rsi
callq 0x61926
movq 0xb8(%r15), %rdx
movslq 0x64(%r15), %rcx
movq %rax, (%rdx,%rcx,8)
incl 0x5c(%r15)
incl %ecx
movl %ecx, 0x64(%r15)
incq 0x28(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq