// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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;

    // 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) {
        if (hts_verbose >= 1) {
            fprintf(stderr, "[E::%s] deflateInit2 failed: %s\n",
                    __func__, bgzf_zerr(ret, &zs));
        }
        return -1;
    }
    if ((ret = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {
        if (hts_verbose >= 1) {
            fprintf(stderr, "[E::%s] deflate failed: %s\n",
                    __func__, bgzf_zerr(ret, ret == Z_DATA_ERROR ? &zs : NULL));
        }
        return -1;
    }
    if ((ret = deflateEnd(&zs)) != Z_OK) {
        if (hts_verbose >= 1) {
            fprintf(stderr, "[E::%s] deflateEnd failed: %s\n",
                    __func__, 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r15
leaq 0x8(%rsp), %rdi
andq $0x0, 0x30(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rdi)
movq %rdx, (%rdi)
movl %r14d, 0x8(%rdi)
leaq 0x12(%r15), %rax
movq %rax, 0x18(%rdi)
movl (%rsi), %eax
addl $-0x1a, %eax
movl %eax, 0x20(%rdi)
leaq 0xdc4c(%rip), %rax      # 0x44063
pushq $-0xf
popq %rcx
pushq $0x8
popq %rdx
movl %r8d, %esi
movl %edx, %r8d
xorl %r9d, %r9d
pushq $0x70
pushq %rax
callq 0x4080
addq $0x10, %rsp
testl %eax, %eax
je 0x3646b
leaq 0x2110b(%rip), %rcx     # 0x57548
pushq $-0x1
popq %rbx
cmpl $0x0, (%rcx)
jle 0x36560
movq 0x20b80(%rip), %rcx     # 0x56fd0
movq (%rcx), %r14
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x36571
leaq 0x1056e(%rip), %rsi     # 0x469d4
jmp 0x364f4
leaq 0x8(%rsp), %rbp
pushq $0x4
popq %rsi
movq %rbp, %rdi
callq 0x4260
cmpl $0x1, %eax
jne 0x364bd
leaq 0x8(%rsp), %rdi
callq 0x4320
testl %eax, %eax
je 0x3650a
leaq 0x210b3(%rip), %rcx     # 0x57548
pushq $-0x1
popq %rbx
cmpl $0x0, (%rcx)
jle 0x36560
movq 0x20b28(%rip), %rcx     # 0x56fd0
movq (%rcx), %r14
movl %eax, %edi
xorl %esi, %esi
callq 0x36571
leaq 0x10564(%rip), %rsi     # 0x46a1f
jmp 0x364f4
leaq 0x21084(%rip), %rcx     # 0x57548
pushq $-0x1
popq %rbx
cmpl $0x0, (%rcx)
jle 0x36560
movq 0x20af9(%rip), %rcx     # 0x56fd0
movq (%rcx), %r14
xorl %ecx, %ecx
cmpl $-0x3, %eax
cmovneq %rcx, %rbp
movl %eax, %edi
movq %rbp, %rsi
callq 0x36571
leaq 0x1050f(%rip), %rsi     # 0x46a03
leaq 0x104fa(%rip), %rdx     # 0x469f5
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x4770
jmp 0x36560
movq 0x30(%rsp), %rax
addq $0x1a, %rax
movq %rax, (%r12)
movaps 0x103c2(%rip), %xmm0    # 0x468e0
andw $0x0, 0x10(%r15)
movups %xmm0, (%r15)
movl (%r12), %eax
decl %eax
movw %ax, 0x10(%r15)
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x43b0
movq %rax, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x43b0
movq (%r12), %rcx
movl %eax, -0x8(%r15,%rcx)
movq (%r12), %rax
movl %r14d, -0x4(%r15,%rax)
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq