// File: karel-brinda[P]ococo/ext/htslib/bgzf.c
// Function: bgzf_write_init
static BGZF *bgzf_write_init(const char *mode)
{
    BGZF *fp;
    fp = (BGZF*)calloc(1, sizeof(BGZF));
    if (fp == NULL) goto mem_fail;
    fp->is_write = 1;
    int compress_level = mode2level(mode);
    if ( compress_level==-2 )
    {
        fp->is_compressed = 0;
        return fp;
    }
    fp->is_compressed = 1;

    fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
    if (fp->uncompressed_block == NULL) goto mem_fail;
    fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
    if (fp->compressed_block == NULL) goto mem_fail;

    fp->compress_level = compress_level < 0? Z_DEFAULT_COMPRESSION : compress_level; // Z_DEFAULT_COMPRESSION==-1
    if (fp->compress_level > 9) fp->compress_level = Z_DEFAULT_COMPRESSION;
    if ( strchr(mode,'g') )
    {
        // gzip output
        fp->is_gzip = 1;
        fp->gz_stream = (z_stream*)calloc(1,sizeof(z_stream));
        if (fp->gz_stream == NULL) goto mem_fail;
        fp->gz_stream->zalloc = NULL;
        fp->gz_stream->zfree  = NULL;
        fp->gz_stream->msg    = NULL;

        int ret = deflateInit2(fp->gz_stream, fp->compress_level, Z_DEFLATED, 15|16, 8, Z_DEFAULT_STRATEGY);
        if (ret!=Z_OK) {
            if (hts_verbose >= 1) {
                fprintf(stderr, "[E::%s] deflateInit2 failed: %s\n",
                        __func__, bgzf_zerr(ret, fp->gz_stream));
            }
            goto fail;
        }
    }
    return fp;

 mem_fail:
    if (hts_verbose >= 1) {
        fprintf(stderr, "[E::%s] %s\n", __func__, strerror(errno));
    }
 fail:
    if (fp != NULL) {
        free(fp->uncompressed_block);
        free(fp->compressed_block);
        free(fp->gz_stream);
        free(fp);
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x1, %edi
movl $0x60, %esi
callq 0x4370
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x630cb
jmp 0x632e7
movq -0x18(%rbp), %rax
movl (%rax), %ecx
andl $0xfffcffff, %ecx       # imm = 0xFFFCFFFF
orl $0x10000, %ecx          # imm = 0x10000
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
callq 0x66f90
movl %eax, -0x1c(%rbp)
cmpl $-0x2, -0x1c(%rbp)
jne 0x6310f
movq -0x18(%rbp), %rax
movl (%rax), %ecx
andl $0x9fffffff, %ecx       # imm = 0x9FFFFFFF
orl $0x0, %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x6336a
movq -0x18(%rbp), %rax
movl (%rax), %ecx
andl $0x9fffffff, %ecx       # imm = 0x9FFFFFFF
orl $0x20000000, %ecx       # imm = 0x20000000
movl %ecx, (%rax)
movl $0x10000, %edi          # imm = 0x10000
callq 0x47a0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x63148
jmp 0x632e7
movl $0x10000, %edi          # imm = 0x10000
callq 0x47a0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x6316d
jmp 0x632e7
cmpl $0x0, -0x1c(%rbp)
jge 0x6317d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x24(%rbp)
jmp 0x63183
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edx
movq -0x18(%rbp), %rax
movl (%rax), %ecx
andl $0x1ff, %edx            # imm = 0x1FF
shll $0x14, %edx
andl $0xe00fffff, %ecx       # imm = 0xE00FFFFF
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
shll $0x3, %eax
sarl $0x17, %eax
cmpl $0x9, %eax
jle 0x631c4
movq -0x18(%rbp), %rax
movl (%rax), %ecx
andl $0xe00fffff, %ecx       # imm = 0xE00FFFFF
orl $0x1ff00000, %ecx       # imm = 0x1FF00000
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movl $0x67, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x632da
movq -0x18(%rbp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, (%rax)
movl $0x1, %edi
movl $0x70, %esi
callq 0x4370
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x58(%rax)
jne 0x6321a
jmp 0x632e7
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq $0x0, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq $0x0, 0x48(%rax)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq $0x0, 0x30(%rax)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
shll $0x3, %esi
sarl $0x17, %esi
movl $0x8, %r8d
movl $0x1f, %ecx
xorl %r9d, %r9d
leaq 0x17f37(%rip), %rax     # 0x7b1aa
movl %r8d, %edx
movq %rax, (%rsp)
movl $0x70, 0x8(%rsp)
callq 0x4090
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x632d8
leaq 0x27349(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x632d6
movq 0x26d2d(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl -0x20(%rbp), %edi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rsi
callq 0x63940
movq -0x30(%rbp), %rdi
movq %rax, %rcx
leaq 0x1abcc(%rip), %rsi     # 0x7de94
leaq 0x1af0d(%rip), %rdx     # 0x7e1dc
movb $0x0, %al
callq 0x4830
jmp 0x6332b
jmp 0x632da
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x6336a
leaq 0x272f2(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x63329
movq 0x26cd6(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x38(%rbp)
callq 0x4070
movl (%rax), %edi
callq 0x4190
movq -0x38(%rbp), %rdi
movq %rax, %rcx
leaq 0x1aed1(%rip), %rsi     # 0x7e1ec
leaq 0x1aeba(%rip), %rdx     # 0x7e1dc
movb $0x0, %al
callq 0x4830
jmp 0x6332b
cmpq $0x0, -0x18(%rbp)
je 0x63362
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)