// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %r14
movl $0x1, %edi
movl $0x60, %esi
callq 0x4360
movq %rax, %rbx
testq %rax, %rax
je 0x43a65
movl $0xfffcffff, %ebp       # imm = 0xFFFCFFFF
andl (%rbx), %ebp
orl $0x10000, %ebp          # imm = 0x10000
movl %ebp, (%rbx)
movq %r14, %rax
movzbl (%rax), %r12d
testl %r12d, %r12d
je 0x43929
leal -0x30(%r12), %ecx
incq %rax
cmpb $0x9, %cl
ja 0x43913
xorl %r15d, %r15d
cmpl $0x1, %r12d
sbbl %r15d, %r15d
movq %r14, %rdi
movl $0x75, %esi
callq 0x4200
andl $0x9ffdffff, %ebp       # imm = 0x9FFDFFFF
testq %rax, %rax
jne 0x43a9d
addl $-0x30, %r12d
orl %r12d, %r15d
cmpl $-0x2, %r15d
je 0x43a9d
movl %ebp, %r12d
orl $0x20000000, %r12d      # imm = 0x20000000
movl %r12d, (%rbx)
movl $0x10000, %edi          # imm = 0x10000
callq 0x4700
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x43a65
movl $0x10000, %edi          # imm = 0x10000
callq 0x4700
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x43a65
testl %r15d, %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnsl %r15d, %eax
shll $0x14, %eax
andl $0x1ff00000, %eax       # imm = 0x1FF00000
andl $0xa00dffff, %r12d      # imm = 0xA00DFFFF
orl %eax, %r12d
orl $0x3ff00000, %ebp       # imm = 0x3FF00000
cmpl $0xa, %r15d
cmovll %r12d, %ebp
movl %ebp, (%rbx)
movq %r14, %rdi
movl $0x67, %esi
callq 0x4200
testq %rax, %rax
je 0x43a9f
movl %ebp, %eax
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
movl $0x1, %edi
movl $0x70, %esi
callq 0x4360
movq %rax, 0x58(%rbx)
testq %rax, %rax
je 0x43a65
movq %rax, %r14
movq $0x0, 0x30(%rax)
shll $0x3, %ebp
sarl $0x17, %ebp
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rax)
leaq 0xf5d4(%rip), %rax      # 0x52ff3
movq %r14, %rdi
movl %ebp, %esi
movl $0x8, %edx
movl $0x1f, %ecx
movl $0x8, %r8d
xorl %r9d, %r9d
pushq $0x70
pushq %rax
callq 0x4090
addq $0x10, %rsp
movl %eax, %ebp
movq %rbx, %rax
testl %ebp, %ebp
je 0x43a5a
leaq 0x20afd(%rip), %rax     # 0x64550
cmpl $0x0, (%rax)
jg 0x43ab2
xorl %eax, %eax
testq %rbx, %rbx
je 0x43aa2
testl %ebp, %ebp
jne 0x43a76
jmp 0x43aa2
leaq 0x20ae4(%rip), %rax     # 0x64550
cmpl $0x0, (%rax)
jg 0x43aab
testq %rbx, %rbx
je 0x43a99
movq 0x20(%rbx), %rdi
callq 0x4610
movq 0x28(%rbx), %rdi
callq 0x4610
movq 0x58(%rbx), %rdi
callq 0x4610
movq %rbx, %rdi
callq 0x4610
xorl %eax, %eax
jmp 0x43aa2
movl %ebp, (%rbx)
movq %rbx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x5a93
jmp 0x43a71
movl %ebp, %edi
movq %r14, %rsi
callq 0x5a67
jmp 0x43a58