// File: /workspace/github_c_makefile_star2_O3/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 %rdx, %rbx
movq 0x28(%rdi), %rdx
cmpb $0x0, 0x4(%rdx)
je 0x10dc3d
movq %rdi, %r15
movq 0x8(%rdi), %rax
cmpl $0x0, (%rax)
jne 0x10dc3d
movq %rsi, %rbp
movq %rax, 0x18(%rsp)
movq (%r15), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%r15), %r12
cmpq $0x0, 0x20(%rdx)
jne 0x10db35
cmpb $0x0, 0x8(%r12)
je 0x10db15
movq 0x10(%r12), %rax
cmpq $0xffff, %rax           # imm = 0xFFFF
jg 0x10db15
movl %eax, 0x18(%rdx)
movl %eax, %esi
jmp 0x10db21
movl $0x10000, 0x18(%rdx)    # imm = 0x10000
movl $0x10000, %esi          # imm = 0x10000
movq %rdx, %r14
movq 0x10(%rsp), %rdi
callq 0xe8b2b
movq %rax, 0x20(%r14)
movq %r14, %rdx
movq %r15, 0x20(%rsp)
leaq 0x30(%rdx), %rax
movq %rax, 0x30(%rsp)
movq %rbp, 0x30(%rdx)
movl %ebx, 0x38(%rdx)
movq %rbx, 0x28(%rsp)
movl %ebx, %r15d
movq %rdx, 0x8(%rsp)
cmpb $0x0, 0x8(%r12)
je 0x10db6e
movq 0x8(%rdx), %rax
cmpq 0x10(%r12), %rax
jge 0x10dc25
movq 0x20(%rdx), %rax
movq %rax, 0x48(%rdx)
movl 0x18(%rdx), %eax
movl %eax, 0x50(%rdx)
movq 0x30(%rsp), %rdi
movl $0x2, %esi
callq 0x10a1ee
movl %eax, %ebp
leal 0x5(%rbp), %eax
cmpl $0x6, %eax
ja 0x10dc4c
movl $0x61, %ecx
btl %eax, %ecx
jae 0x10dc4c
movq 0x8(%rsp), %rdx
movl 0x18(%rdx), %r13d
movl 0x50(%rdx), %ecx
cmpl $-0x5, %ebp
jne 0x10dbc2
movl %r13d, %eax
xorl %ecx, %eax
orl 0x38(%rdx), %eax
je 0x10dc2e
movq %r12, %rax
movl %r13d, %r12d
movl %ecx, %r14d
subl %ecx, %r12d
movq %rax, %rbx
movq (%rax), %rdi
movq 0x20(%rdx), %rsi
movq %r12, %rdx
callq 0xef547
movq 0x8(%rsp), %rdx
addq %r12, 0x8(%rdx)
cmpl $0x1, %ebp
je 0x10dc25
movl 0x38(%rdx), %eax
xorl %eax, %r15d
xorl %r14d, %r13d
orl %r15d, %r13d
movl %eax, %r15d
movq %rbx, %r12
jne 0x10db57
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rdx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xfa311
movq 0x8(%rsp), %rdx
jmp 0x10dc2e
movq 0x20(%rsp), %rax
movb $0x1, 0x20(%rax)
movl 0x38(%rdx), %eax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
addq %rcx, 0x10(%rdx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rbx
movq 0x28(%rbx), %rdx
leaq 0x3fc4e(%rip), %rcx     # 0x14d8aa
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %ebp, %r8d
xorl %eax, %eax
callq 0xfa21a
movq %rbx, %rdx
jmp 0x10dc2e