// File: karel-brinda[P]ococo/ext/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)
	    cram_update_curr_slice(fd->ctr);

	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 0x5269f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x529f0
movq -0x10(%rbp), %rax
cmpl $0x77, 0x8(%rax)
jne 0x526fd
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x526fd
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq $0x0, 0x80(%rax)
je 0x526d3
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x39a00
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
callq 0x4f3e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x526fb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x529f0
jmp 0x526fd
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8860(%rax)
je 0x52795
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8848(%rax)
jl 0x52795
movq -0x10(%rbp), %rax
movq 0x8860(%rax), %rdi
callq 0x60c40
movq -0x10(%rbp), %rdi
callq 0x4f480
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x52749
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x529f0
movq -0x10(%rbp), %rdi
addq $0x8870, %rdi           # imm = 0x8870
callq 0x4670
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4670
movq -0x10(%rbp), %rdi
addq $0x88c8, %rdi           # imm = 0x88C8
callq 0x4670
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x8868(%rax), %rdi
callq 0x60580
movq -0x10(%rbp), %rax
cmpl $0x77, 0x8(%rax)
jne 0x52816
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x3, %eax
jne 0x527e1
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x2a444(%rip), %rsi     # 0x7cc00
movl $0x26, %edx
callq 0x43620
movq %rax, %rcx
movl $0x26, %eax
cmpq %rcx, %rax
je 0x527df
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x529f0
jmp 0x52814
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x2a438(%rip), %rsi     # 0x7cc27
movl $0x1e, %edx
callq 0x43620
movq %rax, %rcx
movl $0x1e, %eax
cmpq %rcx, %rax
je 0x52812
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x529f0
jmp 0x52814
jmp 0x52816
movq -0x10(%rbp), %rax
movq 0x88c0(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x528b8
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 0x52896
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x52889
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1b610
jmp 0x5288b
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5285b
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rdi
callq 0x46a0
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x52825
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6ad60
cmpl $0x0, %eax
je 0x528d5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x529f0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x528ed
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x4fe40
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x52905
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x5e3a0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x5292a
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x4b5e0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x52942
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x48510
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x5295a
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x46a0
movl $0x0, -0x24(%rbp)
cmpl $0x2f, -0x24(%rbp)
jge 0x5299c
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, 0x80(%rax,%rcx,8)
je 0x5298f
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq 0x80(%rax,%rcx,8), %rdi
callq 0x46a0
jmp 0x52991
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x52961
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8838(%rax)
je 0x529b3
movq -0x10(%rbp), %rdi
callq 0x423f0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x885c(%rax)
je 0x529e0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8860(%rax)
je 0x529e0
movq -0x10(%rbp), %rax
movq 0x8860(%rax), %rdi
xorl %esi, %esi
callq 0x60d10
movq -0x10(%rbp), %rdi
callq 0x46a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)