// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x8ac1f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8af74
movq -0x10(%rbp), %rax
cmpl $0x77, 0x8(%rax)
jne 0x8ac81
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x8ac81
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq $0x0, 0x80(%rax)
je 0x8ac57
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl 0x54(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x54(%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
callq 0x87970
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x8ac7f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8af74
jmp 0x8ac81
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8860(%rax)
je 0x8ad19
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8848(%rax)
jl 0x8ad19
movq -0x10(%rbp), %rax
movq 0x8860(%rax), %rdi
callq 0x98e10
movq -0x10(%rbp), %rdi
callq 0x87a10
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x8accd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8af74
movq -0x10(%rbp), %rdi
addq $0x8870, %rdi           # imm = 0x8870
callq 0x6840
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x6840
movq -0x10(%rbp), %rdi
addq $0x88c8, %rdi           # imm = 0x88C8
callq 0x6840
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x8868(%rax), %rdi
callq 0x985c0
movq -0x10(%rbp), %rax
cmpl $0x77, 0x8(%rax)
jne 0x8ad9a
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x3, %eax
jne 0x8ad65
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x57e30(%rip), %rsi     # 0xe2b70
movl $0x26, %edx
callq 0x7bcb0
movq %rax, %rcx
movl $0x26, %eax
cmpq %rcx, %rax
je 0x8ad63
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8af74
jmp 0x8ad98
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x57e24(%rip), %rsi     # 0xe2b97
movl $0x1e, %edx
callq 0x7bcb0
movq %rax, %rcx
movl $0x1e, %eax
cmpq %rcx, %rax
je 0x8ad96
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8af74
jmp 0x8ad98
jmp 0x8ad9a
movq -0x10(%rbp), %rax
movq 0x88c0(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x8ae3c
movq -0x10(%rbp), %rax
movl 0x200(%rax), %eax
movq -0x10(%rbp), %rcx
imull 0x204(%rcx), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x8ae1a
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x8ae0d
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xa5e80
jmp 0x8ae0f
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x8addf
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x6880
movq -0x18(%rbp), %rdi
callq 0x6880
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x8ada9
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xa23e0
cmpl $0x0, %eax
je 0x8ae59
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8af74
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x8ae71
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x883d0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x8ae89
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x964f0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x6880
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x8aeae
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x83b70
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x8aec6
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x80c40
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x8aede
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x6880
movl $0x0, -0x24(%rbp)
cmpl $0x2f, -0x24(%rbp)
jge 0x8af20
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, 0x80(%rax,%rcx,8)
je 0x8af13
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq 0x80(%rax,%rcx,8), %rdi
callq 0x6880
jmp 0x8af15
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x8aee5
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8838(%rax)
je 0x8af37
movq -0x10(%rbp), %rdi
callq 0x7ab70
movq -0x10(%rbp), %rax
cmpl $0x0, 0x885c(%rax)
je 0x8af64
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8860(%rax)
je 0x8af64
movq -0x10(%rbp), %rax
movq 0x8860(%rax), %rdi
xorl %esi, %esi
callq 0x98ee0
movq -0x10(%rbp), %rdi
callq 0x6880
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)