// File: 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
movq %rsp, %rbp
subq $0xf0, %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)
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 0x17ad0(%rip), %rax     # 0x7b1aa
movl %r8d, %edx
movq %rax, (%rsp)
movl $0x70, 0x8(%rsp)
callq 0x4090
movl %eax, -0xac(%rbp)
cmpl $0x0, -0xac(%rbp)
je 0x63757
leaq 0x26edc(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x6374b
movq 0x268c0(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movl -0xac(%rbp), %edi
leaq -0xa0(%rbp), %rsi
callq 0x63940
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
leaq 0x1a757(%rip), %rsi     # 0x7de94
leaq 0x1a771(%rip), %rdx     # 0x7deb5
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6392e
leaq -0xa0(%rbp), %rdi
movl $0x4, %esi
callq 0x4270
movl %eax, -0xac(%rbp)
cmpl $0x1, %eax
je 0x63801
leaq 0x26e62(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x637f5
movq 0x26846(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0xbc(%rbp)
cmpl $-0x3, -0xac(%rbp)
jne 0x637b9
leaq -0xa0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x637c4
xorl %eax, %eax
movq %rax, -0xd0(%rbp)
jmp 0x637c4
movl -0xbc(%rbp), %edi
movq -0xd0(%rbp), %rsi
callq 0x63940
movq -0xc8(%rbp), %rdi
movq %rax, %rcx
leaq 0x1a6dc(%rip), %rsi     # 0x7dec3
leaq 0x1a6c7(%rip), %rdx     # 0x7deb5
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6392e
leaq -0xa0(%rbp), %rdi
callq 0x4310
movl %eax, -0xac(%rbp)
cmpl $0x0, %eax
je 0x6386f
leaq 0x26dc1(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x63863
movq 0x267a5(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
movl -0xac(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x63940
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
leaq 0x1a68a(%rip), %rsi     # 0x7dedf
leaq 0x1a659(%rip), %rdx     # 0x7deb5
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6392e
movq -0x78(%rbp), %rcx
addq $0x12, %rcx
addq $0x8, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0xa8(%rbp), %rax
movq 0x1a500(%rip), %rcx     # 0x7dd90
movq %rcx, (%rax)
movq 0x1a4fe(%rip), %rcx     # 0x7dd98
movq %rcx, 0x8(%rax)
movw 0x1a4fb(%rip), %cx      # 0x7dda0
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 0x63a20
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
movq %rsi, %rdi
callq 0x43d0
movq %rax, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movl %eax, %edx
callq 0x43d0
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 0x63a50
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 0x63a50
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)