// File: /workspace/github_c_makefile_star2_O0/zhou-lab[P]tbmate/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 0x36b0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x398bb
jmp 0x39ad7
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 0x3d780
movl %eax, -0x1c(%rbp)
cmpl $-0x2, -0x1c(%rbp)
jne 0x398ff
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 0x39b5a
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 0x3250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x39938
jmp 0x39ad7
movl $0x10000, %edi          # imm = 0x10000
callq 0x3250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x3995d
jmp 0x39ad7
cmpl $0x0, -0x1c(%rbp)
jge 0x3996d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x24(%rbp)
jmp 0x39973
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 0x399b4
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 0x3580
cmpq $0x0, %rax
je 0x39aca
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 0x36b0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x58(%rax)
jne 0x39a0a
jmp 0x39ad7
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 0x3cff3(%rip), %rax     # 0x76a56
movl %r8d, %edx
movq %rax, (%rsp)
movl $0x70, 0x8(%rsp)
callq 0x3690
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x39ac8
leaq 0x4ba09(%rip), %rax     # 0x85490
cmpl $0x1, (%rax)
jl 0x39ac6
movq 0x4b555(%rip), %rax     # 0x84fe8
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl -0x20(%rbp), %edi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rsi
callq 0x3a130
movq -0x30(%rbp), %rdi
movq %rax, %rcx
leaq 0x3e6ec(%rip), %rsi     # 0x781a4
leaq 0x3ea2a(%rip), %rdx     # 0x784e9
movb $0x0, %al
callq 0x37f0
jmp 0x39b1b
jmp 0x39aca
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x39b5a
leaq 0x4b9b2(%rip), %rax     # 0x85490
cmpl $0x1, (%rax)
jl 0x39b19
movq 0x4b4fe(%rip), %rax     # 0x84fe8
movq (%rax), %rax
movq %rax, -0x38(%rbp)
callq 0x35d0
movl (%rax), %edi
callq 0x34e0
movq -0x38(%rbp), %rdi
movq %rax, %rcx
leaq 0x3e9ee(%rip), %rsi     # 0x784f9
leaq 0x3e9d7(%rip), %rdx     # 0x784e9
movb $0x0, %al
callq 0x37f0
jmp 0x39b1b
cmpq $0x0, -0x18(%rbp)
je 0x39b52
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x3370
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x3370
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x3370
movq -0x18(%rbp), %rdi
callq 0x3370
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)