// File: /workspace/github_c_makefile_star2_O3/icebert[P]pblat/htslib/cram/cram_io.c
// Function: cram_close
int cram_close(cram_fd *fd) {
    spare_bams *bl, *next;
    int i;
	
    if (!fd)
	return -1;

    if (fd->mode == 'w' && fd->ctr) {
	if(fd->ctr->slice)
	    fd->ctr->curr_slice++;
	if (-1 == cram_flush_container_mt(fd, fd->ctr))
	    return -1;
    }

    if (fd->pool && fd->eof >= 0) {
	t_pool_flush(fd->pool);

	if (0 != cram_flush_result(fd))
	    return -1;

	pthread_mutex_destroy(&fd->metrics_lock);
	pthread_mutex_destroy(&fd->ref_lock);
	pthread_mutex_destroy(&fd->bam_list_lock);

	fd->ctr = NULL; // prevent double freeing

	//fprintf(stderr, "CRAM: destroy queue %p\n", fd->rqueue);

	t_results_queue_destroy(fd->rqueue);
    }

    if (fd->mode == 'w') {
	/* Write EOF block */
	if (CRAM_MAJOR_VERS(fd->version) == 3) {
	    if (38 != hwrite(fd->fp,
			     "\x0f\x00\x00\x00\xff\xff\xff\xff" // Cont HDR
			     "\x0f\xe0\x45\x4f\x46\x00\x00\x00" // Cont HDR
			     "\x00\x01\x00"                     // Cont HDR
			     "\x05\xbd\xd9\x4f"                 // CRC32
			     "\x00\x01\x00\x06\x06"             // Comp.HDR blk
			     "\x01\x00\x01\x00\x01\x00"         // Comp.HDR blk
			     "\xee\x63\x01\x4b",                // CRC32
			     38))
		return -1;
	} else {
	    if (30 != hwrite(fd->fp,
			     "\x0b\x00\x00\x00\xff\xff\xff\xff"
			     "\x0f\xe0\x45\x4f\x46\x00\x00\x00"
			     "\x00\x01\x00\x00\x01\x00\x06\x06"
			     "\x01\x00\x01\x00\x01\x00", 30))
		return -1;
	}
    }

    for (bl = fd->bl; bl; bl = next) {
	int i, max_rec = fd->seqs_per_slice * fd->slices_per_container;

	next = bl->next;
	for (i = 0; i < max_rec; i++) {
	    if (bl->bams[i])
		bam_free(bl->bams[i]);
	}
	free(bl->bams);
	free(bl);
    }

    if (hclose(fd->fp) != 0)
	return -1;

    if (fd->file_def)
	cram_free_file_def(fd->file_def);

    if (fd->header)
	sam_hdr_free(fd->header);

    free(fd->prefix);

    if (fd->ctr)
	cram_free_container(fd->ctr);

    if (fd->refs)
	refs_free(fd->refs);
    if (fd->ref_free)
        free(fd->ref_free);

    for (i = 0; i < DS_END; i++)
	if (fd->m[i])
	    free(fd->m[i]);

    if (fd->index)
	cram_index_free(fd);

    if (fd->own_pool && fd->pool)
	t_pool_destroy(fd->pool, 0);

    free(fd);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x5cfcd
movq %rdi, %rbx
cmpl $0x77, 0x8(%rdi)
jne 0x5cda1
movq 0x38(%rbx), %rsi
testq %rsi, %rsi
je 0x5cda1
cmpq $0x0, 0x80(%rsi)
je 0x5cd90
incl 0x54(%rsi)
movq %rbx, %rdi
callq 0x5af18
cmpl $-0x1, %eax
je 0x5cfcd
movq 0x8860(%rbx), %rdi
testq %rdi, %rdi
je 0x5ce03
cmpl $0x0, 0x8848(%rbx)
js 0x5ce03
callq 0x659ec
movq %rbx, %rdi
callq 0x5af83
testl %eax, %eax
jne 0x5cfcd
leaq 0x8870(%rbx), %rdi
callq 0x6860
leaq 0x8898(%rbx), %rdi
callq 0x6860
leaq 0x88c8(%rbx), %rdi
callq 0x6860
movq $0x0, 0x38(%rbx)
movq 0x8868(%rbx), %rdi
callq 0x65405
cmpl $0x77, 0x8(%rbx)
jne 0x5cec1
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rbx), %eax
movq (%rbx), %r14
movq 0x8(%r14), %rdi
movq 0x18(%r14), %r12
subq %rdi, %r12
cmpl $0x300, %eax            # imm = 0x300
jne 0x5ce76
cmpq $0x26, %r12
movl $0x26, %r15d
cmovbq %r12, %r15
leaq 0x3bda1(%rip), %rsi     # 0x98be0
movq %r15, %rdx
callq 0x6570
addq %r15, 0x8(%r14)
cmpq $0x25, %r12
ja 0x5ce6b
leaq 0x3bd88(%rip), %rsi     # 0x98be0
movl $0x26, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x6b241
movq %rax, %r15
cmpq $0x26, %r15
je 0x5cec1
jmp 0x5cfcd
cmpq $0x1e, %r12
movl $0x1e, %r15d
cmovbq %r12, %r15
leaq 0x3bd7c(%rip), %rsi     # 0x98c07
movq %r15, %rdx
callq 0x6570
addq %r15, 0x8(%r14)
cmpq $0x1d, %r12
ja 0x5ceb7
leaq 0x3bd63(%rip), %rsi     # 0x98c07
movl $0x1e, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x6b241
movq %rax, %r15
cmpq $0x1e, %r15
jne 0x5cfcd
movq 0x88c0(%rbx), %r14
testq %r14, %r14
je 0x5cf19
movl 0x204(%rbx), %eax
imull 0x200(%rbx), %eax
movq 0x8(%r14), %r15
testl %eax, %eax
jle 0x5cf01
movl %eax, %r12d
xorl %r13d, %r13d
movq (%r14), %rax
movq (%rax,%r13,8), %rdi
testq %rdi, %rdi
je 0x5cef9
callq 0x6d2dc
incq %r13
cmpq %r13, %r12
jne 0x5cee8
movq (%r14), %rdi
callq 0x68a0
movq %r14, %rdi
callq 0x68a0
movq %r15, %r14
testq %r15, %r15
jne 0x5cecd
movq (%rbx), %rdi
callq 0x6b393
testl %eax, %eax
jne 0x5cfcd
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x5cf37
callq 0x68a0
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x5cf45
callq 0x63d6d
movq 0x20(%rbx), %rdi
callq 0x68a0
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x5cf5c
callq 0x59605
movq 0x48(%rbx), %rdi
testq %rdi, %rdi
je 0x5cf6a
callq 0x576f8
movq 0x58(%rbx), %rdi
testq %rdi, %rdi
je 0x5cf78
callq 0x68a0
movl $0x10, %r14d
movq (%rbx,%r14,8), %rdi
testq %rdi, %rdi
je 0x5cf8c
callq 0x68a0
incq %r14
cmpq $0x3f, %r14
jne 0x5cf7e
cmpq $0x0, 0x8838(%rbx)
je 0x5cfa7
movq %rbx, %rdi
callq 0x53c5d
cmpl $0x0, 0x885c(%rbx)
je 0x5cfc3
movq 0x8860(%rbx), %rdi
testq %rdi, %rdi
je 0x5cfc3
xorl %esi, %esi
callq 0x65a79
movq %rbx, %rdi
callq 0x68a0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq