// File: /workspace/github_c_makefile_star2_O1/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 0x10dd8a
movq %rdi, %r12
movq 0x8(%rdi), %rax
cmpl $0x0, (%rax)
jne 0x10dd8a
movq %rsi, %rbp
movq %rax, 0x18(%rsp)
movq (%r12), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%r12), %r13
cmpq $0x0, 0x20(%rdx)
movq %rdx, 0x8(%rsp)
jne 0x10dc56
cmpb $0x0, 0x8(%r13)
je 0x10dc39
movq 0x10(%r13), %rcx
movl $0x10000, %eax          # imm = 0x10000
cmpq %rax, %rcx
cmovlq %rcx, %rax
jmp 0x10dc3e
movl $0x10000, %eax          # imm = 0x10000
movl %eax, 0x18(%rdx)
movl %eax, %esi
movq 0x10(%rsp), %rdi
callq 0xe969b
movq 0x8(%rsp), %rdx
movq %rax, 0x20(%rdx)
leaq 0x30(%rdx), %rax
movq %rax, 0x30(%rsp)
movq %rbp, 0x30(%rdx)
movq %rbx, 0x28(%rsp)
movl %ebx, 0x38(%rdx)
movq %r13, 0x20(%rsp)
cmpb $0x0, 0x8(%r13)
je 0x10dc85
movq 0x8(%rdx), %rax
cmpq 0x10(%r13), %rax
jge 0x10dd4c
movq 0x20(%rdx), %rax
movq %rax, 0x48(%rdx)
movl 0x18(%rdx), %eax
movl %eax, 0x50(%rdx)
movl 0x38(%rdx), %r15d
movq 0x30(%rsp), %rdi
movl $0x2, %esi
callq 0x10a729
movl %eax, %ebp
leal 0x5(%rbp), %eax
cmpl $0x6, %eax
ja 0x10dd54
movl $0x61, %ecx
btl %eax, %ecx
jae 0x10dd54
movq 0x8(%rsp), %rdx
movl 0x18(%rdx), %r13d
movl 0x50(%rdx), %ecx
cmpl $-0x5, %ebp
jne 0x10dce1
movl %r13d, %eax
xorl %ecx, %eax
orl 0x38(%rdx), %eax
je 0x10dd7b
movq %r12, %rbx
movl %r13d, %r12d
movl %ecx, %r14d
subl %ecx, %r12d
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x20(%rdx), %rsi
movq %r12, %rdx
callq 0xefb82
movq 0x8(%rsp), %rdx
addq %r12, 0x8(%rdx)
cmpl $0x1, %ebp
je 0x10dd46
xorl 0x38(%rdx), %r15d
xorl %r14d, %r13d
orl %r15d, %r13d
movq %rbx, %r12
movq 0x20(%rsp), %r13
jne 0x10dc70
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rdx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xfa815
movq 0x8(%rsp), %rdx
jmp 0x10dd7b
movb $0x1, 0x20(%rbx)
jmp 0x10dd7b
movb $0x1, 0x20(%r12)
jmp 0x10dd7b
movq 0x8(%rsp), %rbx
movq 0x28(%rbx), %rdx
leaq 0x3f526(%rip), %rcx     # 0x14d28a
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %ebp, %r8d
xorl %eax, %eax
callq 0xfa71e
movq %rbx, %rdx
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