// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/bgzf.c
// Function: bgzf_compress
int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level)
{
    uint32_t crc;
    z_stream zs;
    uint8_t *dst = (uint8_t*)_dst;

    if (level == 0) {
    uncomp:
        // Uncompressed data
        if (*dlen < slen+5 + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH) return -1;
        dst[BLOCK_HEADER_LENGTH] = 1; // BFINAL=1, BTYPE=00; see RFC1951
        u16_to_le(slen,  &dst[BLOCK_HEADER_LENGTH+1]); // length
        u16_to_le(~slen, &dst[BLOCK_HEADER_LENGTH+3]); // ones-complement length
        memcpy(dst + BLOCK_HEADER_LENGTH+5, src, slen);
        *dlen = slen+5 + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
    } else {
        // compress the body
        zs.zalloc = NULL; zs.zfree = NULL;
        zs.msg = NULL;
        zs.next_in  = (Bytef*)src;
        zs.avail_in = slen;
        zs.next_out = dst + BLOCK_HEADER_LENGTH;
        zs.avail_out = *dlen - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;
        int ret = deflateInit2(&zs, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); // -15 to disable zlib header/footer
        if (ret!=Z_OK) {
            hts_log_error("Call to deflateInit2 failed: %s", bgzf_zerr(ret, &zs));
            return -1;
        }
        if ((ret = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {
            if (ret == Z_OK && zs.avail_out == 0) {
                deflateEnd(&zs);
                goto uncomp;
            } else {
                hts_log_error("Deflate operation failed: %s", bgzf_zerr(ret, ret == Z_DATA_ERROR ? &zs : NULL));
            }
            return -1;
        }
        // If we used up the entire output buffer, then we either ran out of
        // room or we *just* fitted, but either way we may as well store
        // uncompressed for faster decode.
        if (zs.avail_out == 0) {
            deflateEnd(&zs);
            goto uncomp;
        }
        if ((ret = deflateEnd(&zs)) != Z_OK) {
            hts_log_error("Call to deflateEnd failed: %s", bgzf_zerr(ret, NULL));
            return -1;
        }
        *dlen = zs.total_out + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
    }

    // write the header
    memcpy(dst, g_magic, BLOCK_HEADER_LENGTH); // the last two bytes are a place holder for the length of the block
    packInt16(&dst[16], *dlen - 1); // write the compressed length; -1 to fit 2 bytes
    // write the footer
    crc = crc32(crc32(0L, NULL, 0L), (Bytef*)src, slen);
    packInt32((uint8_t*)&dst[*dlen - 8], crc);
    packInt32((uint8_t*)&dst[*dlen - 4], slen);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0xca73
jmp 0xc9d6
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
addq $0x5, %rcx
addq $0x12, %rcx
addq $0x8, %rcx
cmpq %rcx, %rax
jae 0xc9fe
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xcd11
movq -0xa8(%rbp), %rax
movb $0x1, 0x12(%rax)
movq -0x28(%rbp), %rax
movq -0xa8(%rbp), %rsi
addq $0x13, %rsi
movzwl %ax, %edi
callq 0xcd20
movq -0x28(%rbp), %rax
xorq $-0x1, %rax
movq -0xa8(%rbp), %rsi
addq $0x15, %rsi
movzwl %ax, %edi
callq 0xcd20
movq -0xa8(%rbp), %rdi
addq $0x12, %rdi
addq $0x5, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x43b0
movq -0x28(%rbp), %rcx
addq $0x5, %rcx
addq $0x12, %rcx
addq $0x8, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0xcc65
movq $0x0, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
movl %eax, -0x98(%rbp)
movq -0xa8(%rbp), %rax
addq $0x12, %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
subq $0x12, %rax
subq $0x8, %rax
movl %eax, -0x80(%rbp)
movl -0x2c(%rbp), %esi
leaq -0xa0(%rbp), %rdi
movl $0x8, %r8d
movl $0xfffffff1, %ecx       # imm = 0xFFFFFFF1
xorl %r9d, %r9d
leaq 0xe20a2(%rip), %rax     # 0xeeb85
movl %r8d, %edx
movq %rax, (%rsp)
movl $0x70, 0x8(%rsp)
callq 0x4080
movl %eax, -0xac(%rbp)
cmpl $0x0, -0xac(%rbp)
je 0xcb41
movl -0xac(%rbp), %edi
leaq -0xa0(%rbp), %rsi
callq 0xcd40
movq %rax, %rcx
movl $0x1, %edi
leaq 0xe2065(%rip), %rsi     # 0xeeb8c
leaq 0xe206c(%rip), %rdx     # 0xeeb9a
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xcd11
leaq -0xa0(%rbp), %rdi
movl $0x4, %esi
callq 0x41e0
movl %eax, -0xac(%rbp)
cmpl $0x1, %eax
je 0xcbec
cmpl $0x0, -0xac(%rbp)
jne 0xcb81
cmpl $0x0, -0x80(%rbp)
jne 0xcb81
leaq -0xa0(%rbp), %rdi
callq 0x4290
jmp 0xc9d6
movl -0xac(%rbp), %eax
movl %eax, -0xb0(%rbp)
cmpl $-0x3, -0xac(%rbp)
jne 0xcba6
leaq -0xa0(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0xcbb1
xorl %eax, %eax
movq %rax, -0xb8(%rbp)
jmp 0xcbb1
movl -0xb0(%rbp), %edi
movq -0xb8(%rbp), %rsi
callq 0xcd40
movq %rax, %rcx
movl $0x1, %edi
leaq 0xe1fba(%rip), %rsi     # 0xeeb8c
leaq 0xe1fe1(%rip), %rdx     # 0xeebba
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xcd11
cmpl $0x0, -0x80(%rbp)
jne 0xcc03
leaq -0xa0(%rbp), %rdi
callq 0x4290
jmp 0xc9d6
leaq -0xa0(%rbp), %rdi
callq 0x4290
movl %eax, -0xac(%rbp)
cmpl $0x0, %eax
je 0xcc52
movl -0xac(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
callq 0xcd40
movq %rax, %rcx
movl $0x1, %edi
leaq 0xe1f54(%rip), %rsi     # 0xeeb8c
leaq 0xe1f98(%rip), %rdx     # 0xeebd7
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xcd11
movq -0x78(%rbp), %rcx
addq $0x12, %rcx
addq $0x8, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0xa8(%rbp), %rax
movq 0xe1edd(%rip), %rcx     # 0xeeb50
movq %rcx, (%rax)
movq 0xe1edb(%rip), %rcx     # 0xeeb58
movq %rcx, 0x8(%rax)
movw 0xe1ed8(%rip), %cx      # 0xeeb60
movw %cx, 0x10(%rax)
movq -0xa8(%rbp), %rdi
addq $0x10, %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
subq $0x1, %rax
movzwl %ax, %esi
callq 0xce30
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
movq %rsi, %rdi
callq 0x4310
movq %rax, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movl %eax, %edx
callq 0x4310
movl %eax, -0x30(%rbp)
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
subq $0x8, %rax
addq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0xce60
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
subq $0x4, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rax
movl %eax, %esi
callq 0xce60
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)