// File: /workspace/github_c_makefile_star2_O1/samtools[P]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(2 * BGZF_MAX_BLOCK_SIZE);
    if (fp->uncompressed_block == NULL) goto mem_fail;
    fp->compressed_block = (char *)fp->uncompressed_block + BGZF_MAX_BLOCK_SIZE;

    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) {
            hts_log_error("Call to deflateInit2 failed: %s", bgzf_zerr(ret, fp->gz_stream));
            goto fail;
        }
    }
    return fp;

mem_fail:
    hts_log_error("%s", strerror(errno));

fail:
    if (fp != NULL) {
        free(fp->uncompressed_block);
        free(fp->gz_stream);
        free(fp);
    }
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
movl $0x1, %edi
movl $0x70, %esi
callq 0x4300
testq %rax, %rax
je 0xaa08
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %r12d
orl $0x20000, %r12d         # imm = 0x20000
movl %r12d, (%rax)
movq %r14, %rax
movzbl (%rax), %r13d
testl %r13d, %r13d
je 0xa8b8
leal -0x30(%r13), %ecx
incq %rax
cmpb $0x9, %cl
ja 0xa8a3
xorl %r15d, %r15d
cmpl $0x1, %r13d
sbbl %r15d, %r15d
movq %r14, %rdi
movl $0x75, %esi
callq 0x4190
testq %rax, %rax
jne 0xaa36
addl $-0x30, %r13d
orl %r13d, %r15d
cmpl $-0x2, %r15d
je 0xaa36
movl %ebp, %r12d
orl $0x40020000, %r12d      # imm = 0x40020000
movl %r12d, (%rbx)
movl $0x20000, %edi          # imm = 0x20000
callq 0x4680
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0xaa45
addq $0x10000, %rax          # imm = 0x10000
movq %rax, 0x30(%rbx)
testl %r15d, %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnsl %r15d, %eax
shll $0x14, %eax
andl $0x1ff00000, %eax       # imm = 0x1FF00000
andl $0xe00fffff, %r12d      # imm = 0xE00FFFFF
orl %eax, %r12d
orl $0x5ff20000, %ebp       # imm = 0x5FF20000
cmpl $0xa, %r15d
cmovll %r12d, %ebp
movl %ebp, (%rbx)
movq %r14, %rdi
movl $0x67, %esi
callq 0x4190
testq %rax, %rax
je 0xaa40
movl %ebp, %eax
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
movl $0x1, %edi
movl $0x70, %esi
callq 0x4300
movq %rax, 0x60(%rbx)
testq %rax, %rax
je 0xaa45
movq %rax, %r15
movq $0x0, 0x30(%rax)
shll $0x3, %ebp
sarl $0x17, %ebp
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rax)
leaq 0x8cfe1(%rip), %rax     # 0x97980
movq %r15, %rdi
movl %ebp, %esi
movl $0x8, %edx
movl $0x1f, %ecx
movl $0x8, %r8d
xorl %r9d, %r9d
pushq $0x70
pushq %rax
callq 0x4080
addq $0x10, %rsp
movl %eax, %ebp
movq %rbx, %r14
testl %eax, %eax
je 0xa9f6
movl %ebp, %edi
movq %r15, %rsi
callq 0xadd0
leaq 0x8d727(%rip), %rsi     # 0x98104
leaq 0x8cfb1(%rip), %rdx     # 0x97995
xorl %r14d, %r14d
movl $0x1, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x14378
testq %rbx, %rbx
je 0xaa93
testl %ebp, %ebp
jne 0xaa76
jmp 0xaa93
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x8d6e9(%rip), %rsi     # 0x98104
leaq 0x90aa5(%rip), %rdx     # 0x9b4c7
xorl %r14d, %r14d
movl $0x1, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x14378
jmp 0xaa93
andl $0xbfffffff, %r12d      # imm = 0xBFFFFFFF
movl %r12d, (%rbx)
movq %rbx, %r14
jmp 0xaa93
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x8d6ac(%rip), %rsi     # 0x98104
leaq 0x90a68(%rip), %rdx     # 0x9b4c7
xorl %r14d, %r14d
movl $0x1, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x14378
testq %rbx, %rbx
je 0xaa93
movq 0x28(%rbx), %rdi
callq 0x45c0
movq 0x60(%rbx), %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq