// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
cmpb $0x0, 0x4(%rax)
jne 0x197d62
jmp 0x197f3c
movq -0x40(%rbp), %rax
cmpl $0x0, (%rax)
je 0x197d70
jmp 0x197f3c
movq -0x20(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x197dd1
movq -0x48(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
je 0x197da9
movq -0x48(%rbp), %rax
cmpq $0x10000, 0x10(%rax)    # imm = 0x10000
jge 0x197da9
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x197db4
movq -0x20(%rbp), %rax
movl $0x10000, 0x18(%rax)    # imm = 0x10000
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, %esi
callq 0x15b780
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x48(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
je 0x197e16
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x48(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0x197e16
movq -0x8(%rbp), %rax
movb $0x1, 0x20(%rax)
jmp 0x197f22
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x50(%rax)
movq -0x20(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
movl $0x2, %esi
callq 0x1914e0
movl %eax, -0x24(%rbp)
cmpl $0x1, -0x24(%rbp)
je 0x197e8c
cmpl $0x0, -0x24(%rbp)
je 0x197e8c
cmpl $-0x5, -0x24(%rbp)
je 0x197e8c
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
movl -0x24(%rbp), %r8d
leaq 0x4162c(%rip), %rcx     # 0x1d94ac
movb $0x0, %al
callq 0x177450
jmp 0x197f22
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x20(%rbp), %rcx
subl 0x50(%rcx), %eax
movl %eax, %eax
movq %rax, -0x30(%rbp)
cmpl $-0x5, -0x24(%rbp)
jne 0x197ebb
movq -0x20(%rbp), %rax
cmpl $0x0, 0x38(%rax)
jne 0x197eb9
cmpq $0x0, -0x30(%rbp)
jne 0x197eb9
jmp 0x197f22
jmp 0x197ebb
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x166170
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
cmpl $0x1, -0x24(%rbp)
jne 0x197ef3
movq -0x8(%rbp), %rax
movb $0x1, 0x20(%rax)
jmp 0x197f22
movq -0x20(%rbp), %rax
movl 0x38(%rax), %eax
cmpl -0x4c(%rbp), %eax
jne 0x197f1d
cmpq $0x0, -0x30(%rbp)
jne 0x197f1d
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x177560
jmp 0x197f22
jmp 0x197dea
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movl 0x38(%rax), %eax
subq %rax, %rcx
movq -0x20(%rbp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)