// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil-miniz.c
// Function: my_deflate_codec_addbuf
static void my_deflate_codec_addbuf(struct de_dfilter_ctx *dfctx,
	const u8 *buf, i64 buf_len)
{
	struct deflate_pushable_ctx *dc = (struct deflate_pushable_ctx*)dfctx->codec_private;
	int ret;
	i64 nbytes_to_write;

	deark *c = dfctx->c;
	struct de_dfilter_results *dres = dfctx->dres;
	struct de_dfilter_out_params *dcmpro = dfctx->dcmpro;

	if(!dc->stream_open_flag) return;
	if(dres->errcode) return;

	if(!dc->outbuf) {
		if(dcmpro->len_known && dcmpro->expected_len<DE_DEFLATEP_MAX_OUTBUF_SIZE) {
			dc->outbuf_size = (unsigned int)dcmpro->expected_len;
		}
		else {
			dc->outbuf_size = DE_DEFLATEP_MAX_OUTBUF_SIZE;
		}
		dc->outbuf = de_malloc(c, (i64)dc->outbuf_size);
	}

	dc->strm.next_in = buf;
	dc->strm.avail_in = (unsigned int)buf_len;

	while(1) {
		unsigned int old_avail_in;

		// If we have written enough bytes, stop.
		if((dcmpro->len_known) && (dc->nbytes_written_total >= dcmpro->expected_len)) {
			dfctx->finished_flag = 1;
			goto done;
		}

		dc->strm.next_out = dc->outbuf;
		dc->strm.avail_out = dc->outbuf_size;
		old_avail_in = dc->strm.avail_in;

		ret = mz_inflate(&dc->strm, MZ_SYNC_FLUSH);

		if(ret!=MZ_STREAM_END && ret!=MZ_OK && ret!=MZ_BUF_ERROR) {
			de_dfilter_set_errorf(c, dres, dc->modname, "Inflate error (%d)", (int)ret);
			goto done;
		}

		nbytes_to_write = dc->outbuf_size - dc->strm.avail_out;

		if(ret==MZ_BUF_ERROR) {
			if(dc->strm.avail_in==0 && nbytes_to_write==0) {
				// Consumed all the input, can't produce any more output: suspend
				goto done;
			}
		}

		dbuf_write(dcmpro->f, dc->outbuf, nbytes_to_write);
		dc->nbytes_written_total += nbytes_to_write;

		if(ret==MZ_STREAM_END) {
			dfctx->finished_flag = 1;
			goto done;
		}

		if(dc->strm.avail_in==old_avail_in && nbytes_to_write==0) {
			// No progress? Probably shouldn't happen, but we want to defend against
			// infinite loops.
			de_dfilter_set_generic_error(c, dres, dc->modname);
			goto done;
		}
	}

done:
	dc->total_nbytes_processed += buf_len - (i64)dc->strm.avail_in;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq 0x28(%rdi), %rbx
cmpb $0x0, 0x4(%rbx)
je 0x100ae3
movq %rdi, %r13
movq 0x8(%rdi), %rax
cmpl $0x0, (%rax)
jne 0x100ae3
movq %rdx, %r15
movq %rsi, %r14
movq %rax, 0x10(%rsp)
movq (%r13), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%r13), %rbp
cmpq $0x0, 0x20(%rbx)
jne 0x1009c4
cmpb $0x0, 0x8(%rbp)
je 0x1009aa
movq 0x10(%rbp), %rax
cmpq $0xffff, %rax           # imm = 0xFFFF
jg 0x1009aa
movl %eax, 0x18(%rbx)
movl %eax, %esi
jmp 0x1009b6
movl $0x10000, 0x18(%rbx)    # imm = 0x10000
movl $0x10000, %esi          # imm = 0x10000
movq 0x8(%rsp), %rdi
callq 0xdecaf
movq %rax, 0x20(%rbx)
leaq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
movq %r14, 0x30(%rbx)
movl %r15d, 0x38(%rbx)
movq %r15, 0x28(%rsp)
movq %r13, 0x18(%rsp)
movq %rbp, 0x20(%rsp)
cmpb $0x0, 0x8(%rbp)
je 0x1009f8
movq 0x8(%rbx), %rax
cmpq 0x10(%rbp), %rax
jge 0x100aae
movq 0x20(%rbx), %rax
movq %rax, 0x48(%rbx)
movl 0x18(%rbx), %eax
movl %eax, 0x50(%rbx)
movq 0x30(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0xfdf5e
movl %eax, %r12d
leal 0x5(%r12), %eax
cmpl $0x6, %eax
ja 0x100ab5
pushq $0x61
popq %rcx
btl %eax, %ecx
jae 0x100ab5
movl 0x18(%rbx), %r13d
movl 0x50(%rbx), %ebp
cmpl $-0x5, %r12d
jne 0x100a4b
movl %r13d, %eax
xorl %ebp, %eax
orl 0x38(%rbx), %eax
je 0x100ad4
movl %r13d, %r14d
subl %ebp, %r14d
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x20(%rbx), %rsi
movq %r14, %rdx
callq 0xe46ba
addq %r14, 0x8(%rbx)
cmpl $0x1, %r12d
je 0x100aa3
movl 0x38(%rbx), %eax
xorl %eax, %r15d
xorl %ebp, %r13d
orl %r15d, %r13d
movl %eax, %r15d
movq 0x18(%rsp), %r13
movq 0x20(%rsp), %rbp
jne 0x1009e4
movq 0x28(%rbx), %rdx
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0xee91c
jmp 0x100ad4
movq 0x18(%rsp), %rax
movb $0x1, 0x20(%rax)
jmp 0x100ad4
movb $0x1, 0x20(%r13)
jmp 0x100ad4
movq 0x28(%rbx), %rdx
leaq 0x3f8da(%rip), %rcx     # 0x14039a
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %r12d, %r8d
xorl %eax, %eax
callq 0xee823
movl 0x38(%rbx), %eax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
addq %rcx, 0x10(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq