// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/samtools-0.1.18/bgzf.c
// Function: deflate_block
static
int
deflate_block(BGZF* fp, int block_length)
{
    // Deflate the block in fp->uncompressed_block into fp->compressed_block.
    // Also adds an extra field that stores the compressed block length.

    bgzf_byte_t* buffer = fp->compressed_block;
    int buffer_size = fp->compressed_block_size;

    // Init gzip header
    buffer[0] = GZIP_ID1;
    buffer[1] = GZIP_ID2;
    buffer[2] = CM_DEFLATE;
    buffer[3] = FLG_FEXTRA;
    buffer[4] = 0; // mtime
    buffer[5] = 0;
    buffer[6] = 0;
    buffer[7] = 0;
    buffer[8] = 0;
    buffer[9] = OS_UNKNOWN;
    buffer[10] = BGZF_XLEN;
    buffer[11] = 0;
    buffer[12] = BGZF_ID1;
    buffer[13] = BGZF_ID2;
    buffer[14] = BGZF_LEN;
    buffer[15] = 0;
    buffer[16] = 0; // placeholder for block length
    buffer[17] = 0;

    // loop to retry for blocks that do not compress enough
    int input_length = block_length;
    int compressed_length = 0;
    while (1) {
        z_stream zs;
        zs.zalloc = NULL;
        zs.zfree = NULL;
        zs.next_in = fp->uncompressed_block;
        zs.avail_in = input_length;
        zs.next_out = (void*)&buffer[BLOCK_HEADER_LENGTH];
        zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;

        int status = deflateInit2(&zs, fp->compress_level, Z_DEFLATED,
                                  GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY);
        if (status != Z_OK) {
            report_error(fp, "deflate init failed");
            return -1;
        }
        status = deflate(&zs, Z_FINISH);
        if (status != Z_STREAM_END) {
            deflateEnd(&zs);
            if (status == Z_OK) {
                // Not enough space in buffer.
                // Can happen in the rare case the input doesn't compress enough.
                // Reduce the amount of input until it fits.
                input_length -= 1024;
                if (input_length <= 0) {
                    // should never happen
                    report_error(fp, "input reduction failed");
                    return -1;
                }
                continue;
            }
            report_error(fp, "deflate failed");
            return -1;
        }
        status = deflateEnd(&zs);
        if (status != Z_OK) {
            report_error(fp, "deflate end failed");
            return -1;
        }
        compressed_length = zs.total_out;
        compressed_length += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
        if (compressed_length > MAX_BLOCK_SIZE) {
            // should never happen
            report_error(fp, "deflate overflow");
            return -1;
        }
        break;
    }

    packInt16((uint8_t*)&buffer[16], compressed_length-1);
    uint32_t crc = crc32(0L, NULL, 0L);
    crc = crc32(crc, fp->uncompressed_block, input_length);
    packInt32((uint8_t*)&buffer[compressed_length-8], crc);
    packInt32((uint8_t*)&buffer[compressed_length-4], input_length);

    int remaining = block_length - input_length;
    if (remaining > 0) {
        if (remaining > input_length) {
            // should never happen (check so we can use memcpy)
            report_error(fp, "remainder too large");
            return -1;
        }
        memcpy(fp->uncompressed_block,
               fp->uncompressed_block + input_length,
               remaining);
    }
    fp->block_offset = remaining;
    return compressed_length;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %r13
movq 0x28(%rdi), %rdx
movl 0x1c(%rdi), %r9d
movq $0x4088b1f, (%rdx)      # imm = 0x4088B1F
xorl %ecx, %ecx
movb %cl, 0x8(%rdx)
movabsq $0x243420006ff, %rax    # imm = 0x243420006FF
movq %rax, 0x9(%rdx)
movq $0x0, 0x28(%rsp)
movb %cl, 0x11(%rdx)
leaq 0x78(%rsp), %r12
movq %rdx, 0x30(%rsp)
leaq 0x12(%rdx), %rbp
addl $-0x1a, %r9d
leaq 0x98e38(%rip), %r15     # 0x3d7f14
leaq 0x38(%rsp), %rbx
movl %esi, 0x24(%rsp)
movl %esi, %edx
movq %rdi, 0x10(%rsp)
movl %r9d, 0xc(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq 0x20(%r13), %rax
movq %rax, 0x38(%rsp)
movq %rdx, 0x18(%rsp)
movl %edx, 0x40(%rsp)
movq %rbp, 0x50(%rsp)
movl %r9d, 0x58(%rsp)
movswl 0x8(%r13), %esi
movq %rbx, %rdi
movl $0x8, %edx
movl $0xfffffff1, %ecx       # imm = 0xFFFFFFF1
movl $0x8, %r8d
movl %r9d, %r14d
xorl %r9d, %r9d
pushq $0x70
pushq %r15
callq 0x273807
addq $0x10, %rsp
testl %eax, %eax
je 0x33f161
leaq 0xb1b33(%rip), %rax     # 0x3f0c7e
movq %rax, 0x48(%r13)
movl $0x1, %eax
movq 0x18(%rsp), %rdx
movl %r14d, %r9d
jmp 0x33f1f0
movq %rbx, %rdi
movl $0x4, %esi
callq 0x27445a
movl %eax, %r13d
movq %rbx, %rdi
callq 0x273add
cmpl $0x1, %r13d
je 0x33f1bf
testl %r13d, %r13d
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r13
jne 0x33f1d6
leal -0x400(%rdx), %ecx
movl $0x2, %eax
cmpl $0x400, %edx            # imm = 0x400
leaq 0x38(%rsp), %rbx
jg 0x33f1b6
leaq 0xb1ae5(%rip), %rax     # 0x3f0c92
movq %rax, 0x48(%r13)
movl $0x1, %eax
movl %ecx, %edx
movl 0xc(%rsp), %r9d
jmp 0x33f1f0
testl %eax, %eax
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r13
je 0x33f1fb
leaq 0xb1ae4(%rip), %rax     # 0x3f0cb8
jmp 0x33f1dd
leaq 0xb1acc(%rip), %rax     # 0x3f0ca9
movq %rax, 0x48(%r13)
movl $0x1, %eax
movl 0xc(%rsp), %r9d
leaq 0x38(%rsp), %rbx
cmpl $0x2, %eax
je 0x33f0f1
jmp 0x33f22b
movl 0x60(%rsp), %ecx
leal 0x1a(%rcx), %eax
movq %rax, 0x28(%rsp)
movl $0x3, %eax
cmpl $0xffe7, %ecx           # imm = 0xFFE7
leaq 0x38(%rsp), %rbx
jl 0x33f1b8
leaq 0xb1aab(%rip), %rax     # 0x3f0ccb
movq %rax, 0x48(%r13)
movl $0x1, %eax
jmp 0x33f1b8
cmpl $0x3, %eax
jne 0x33f2ae
movq 0x28(%rsp), %r15
leal -0x1(%r15), %eax
movq 0x30(%rsp), %rbp
movw %ax, 0x10(%rbp)
xorl %edi, %edi
xorl %esi, %esi
movq %rdx, %rbx
xorl %edx, %edx
callq 0x348883
movl %eax, %edi
movq 0x20(%r13), %rsi
movl %ebx, %edx
callq 0x348883
movslq %r15d, %rcx
movb %al, -0x8(%rbp,%rcx)
movb %ah, -0x7(%rbp,%rcx)
movl %eax, %edx
shrl $0x10, %edx
movb %dl, -0x6(%rbp,%rcx)
shrl $0x18, %eax
movb %al, -0x5(%rbp,%rcx)
movb %bl, -0x4(%rbp,%rcx)
movb %bh, -0x3(%rbp,%rcx)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, -0x2(%rbp,%rcx)
movl %ebx, %eax
shrl $0x18, %eax
movb %al, -0x1(%rbp,%rcx)
movl 0x24(%rsp), %ebp
subl %ebx, %ebp
jle 0x33f2cb
cmpl %ebx, %ebp
jle 0x33f2b6
leaq 0xb1a37(%rip), %rax     # 0x3f0cdc
movq 0x10(%rsp), %rcx
movq %rax, 0x48(%rcx)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x33f2cf
movq 0x10(%rsp), %r13
movq 0x20(%r13), %rdi
movl %ebx, %esi
addq %rdi, %rsi
movl %ebp, %edx
callq 0x17660
movl %ebp, 0x3c(%r13)
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq