// File: /workspace/github_c_makefile_star2_O1/joncampbell123[P]swfhax/swf_zlib_reader.c
// Function: swf_reader_zlib_read
int swf_reader_zlib_read(void *io,void *buf,int len) {
	unsigned char *cbuf = (unsigned char*)buf;
	swf_reader *r = (swf_reader*)io;
	int rd,accum=0;
	size_t cc;

	/* pass back the first 8 bytes uncompressed */
	while (len > 0 && r->current_pos < 8) {
		if (swf_reader_raw_seek((swf_reader*)io,r->current_pos)) return -1;
		cc = (size_t)((uint64_t)8 - r->current_pos);
		if (cc > (size_t)len) cc = (size_t)len;
		r->current_pos = r->raw_current_pos;
		rd = swf_reader_raw_read((swf_reader*)io,cbuf,(int)cc);

		/* need to patch first byte at offset 0 to "FWS" instead of "CWS" */
		if (r->current_pos == 0 && rd > 0) cbuf[0] = 'F';

		r->current_pos = r->raw_current_pos;
		if (rd < (int)cc) return rd;
		accum += rd;
		cbuf += rd;
		len -= rd;
	}

	while (len > 0) {
		assert(sliding_window_v4_is_sane(r->decom_buf));
		assert(sliding_window_v4_is_sane(r->out_buf));
		sliding_window_v4_lazy_flush(r->out_buf);
		sliding_window_v4_lazy_flush(r->decom_buf);
		if ((cc=sliding_window_v4_data_available(r->out_buf)) > 0) {
			if (cc > (size_t)len) cc = (size_t)len;
			memcpy(cbuf,r->out_buf->data,cc);
			r->out_buf->data += cc;
			r->current_pos += cc;
			accum += cc;
			cbuf += cc;
			len -= cc;

			assert(sliding_window_v4_is_sane(r->out_buf));
		}
		else {
			if (r->zlib_eof) {
				r->eof = 1;
				break;
			}

			assert(sliding_window_v4_is_sane(r->decom_buf));
			if ((cc=sliding_window_v4_can_write(r->decom_buf)) > 0) {
				rd = swf_reader_raw_read((swf_reader*)io,r->decom_buf->end,cc);
				if (rd > 0) r->decom_buf->end += rd;
				assert(sliding_window_v4_is_sane(r->decom_buf));
				sliding_window_v4_lazy_flush(r->decom_buf);
			}

			r->zlib.next_in = r->decom_buf->data;
			r->zlib.avail_in = sliding_window_v4_data_available(r->decom_buf);
			r->zlib.next_out = r->out_buf->end;
			r->zlib.avail_out = sliding_window_v4_can_write(r->out_buf);
			rd = inflate(&r->zlib,Z_NO_FLUSH);
			if (rd == Z_STREAM_END) {
				r->zlib_eof = 1;
			}
			else if (rd != Z_OK) {
				fprintf(stderr,"ZLIB error %d\n",rd);
				r->zlib_eof = 1;
				break;
			}
			r->decom_buf->data = r->zlib.next_in;
			r->out_buf->end = r->zlib.next_out;
			assert(sliding_window_v4_is_sane(r->decom_buf));
		}
	}

	return accum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %r14
xorl %ebx, %ebx
testl %edx, %edx
jle 0x4db2
movq 0x8(%r14), %rsi
cmpq $0x7, %rsi
ja 0x4db2
movq %r14, %rdi
callq 0x2a13
testl %eax, %eax
jne 0x4f3d
movl $0x8, %r13d
subq 0x8(%r14), %r13
movl %r15d, %eax
cmpq %rax, %r13
cmovaeq %rax, %r13
movq (%r14), %rax
movq %rax, 0x8(%r14)
movq %r14, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x2e88
cmpq $0x0, 0x8(%r14)
jne 0x4d96
testl %eax, %eax
jle 0x4d96
movb $0x46, (%r12)
movq (%r14), %rcx
movq %rcx, 0x8(%r14)
cmpl %r13d, %eax
jl 0x4f44
addl %eax, %ebx
movl %eax, %ecx
addq %rcx, %r12
subl %eax, %r15d
jg 0x4d43
testl %r15d, %r15d
jle 0x4f4d
leaq 0x88(%r14), %rax
movq %rax, -0x30(%rbp)
movq 0x78(%r14), %rdi
callq 0x5478
testl %eax, %eax
je 0x4f9e
movq 0x80(%r14), %rdi
callq 0x5478
testl %eax, %eax
je 0x4f7f
movq 0x80(%r14), %rdi
callq 0x5407
movq 0x78(%r14), %rdi
callq 0x5407
movq 0x80(%r14), %rax
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rax
subq %rsi, %rax
je 0x4e53
movl %r15d, %r13d
cmpq %r13, %rax
cmovbq %rax, %r13
movq %r12, %rdi
movq %r13, %rdx
callq 0x10f0
movq 0x80(%r14), %rdi
addq %r13, 0x10(%rdi)
addq %r13, 0x8(%r14)
callq 0x5478
testl %eax, %eax
je 0x4fbd
addl %r13d, %ebx
addq %r13, %r12
subl %r13d, %r15d
jmp 0x4f32
cmpb $0x0, 0x70(%r14)
jne 0x4f48
movq 0x78(%r14), %rdi
callq 0x5478
testl %eax, %eax
je 0x4fdc
movq 0x78(%r14), %rax
movq 0x8(%rax), %rdx
movq 0x18(%rax), %rsi
cmpq %rsi, %rdx
je 0x4eb2
subl %esi, %edx
movq %r14, %rdi
callq 0x2e88
testl %eax, %eax
jle 0x4e98
movq 0x78(%r14), %rcx
movl %eax, %eax
addq %rax, 0x18(%rcx)
movq 0x78(%r14), %rdi
callq 0x5478
testl %eax, %eax
je 0x501a
movq 0x78(%r14), %rdi
callq 0x5407
movq 0x78(%r14), %rax
movq 0x80(%r14), %rcx
movq 0x10(%rax), %rdx
movq %rdx, 0x88(%r14)
movl 0x18(%rax), %eax
subl %edx, %eax
movl %eax, 0x90(%r14)
movq 0x18(%rcx), %rax
movq %rax, 0xa0(%r14)
movl 0x8(%rcx), %ecx
subl %eax, %ecx
movl %ecx, 0xa8(%r14)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x1060
testl %eax, %eax
je 0x4f04
cmpl $0x1, %eax
jne 0x4f5e
movb $0x1, 0x70(%r14)
movq 0x78(%r14), %rdi
movq 0x88(%r14), %rax
movq %rax, 0x10(%rdi)
movq 0x80(%r14), %rax
movq 0xa0(%r14), %rcx
movq %rcx, 0x18(%rax)
callq 0x5478
testl %eax, %eax
je 0x4ffb
testl %r15d, %r15d
jg 0x4dc6
jmp 0x4f4d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x4f4d
movl %eax, %ebx
jmp 0x4f4d
movb $0x1, 0x71(%r14)
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5093(%rip), %rcx      # 0x9ff8
movq (%rcx), %rdi
leaq 0x2739(%rip), %rsi      # 0x76a8
movl %eax, %edx
xorl %eax, %eax
callq 0x10e0
movb $0x1, 0x70(%r14)
jmp 0x4f4d
leaq 0x25a2(%rip), %rdi      # 0x7528
leaq 0x2618(%rip), %rsi      # 0x75a5
leaq 0x26e6(%rip), %rcx      # 0x767a
movl $0x6d, %edx
callq 0x10a0
leaq 0x251b(%rip), %rdi      # 0x74c0
leaq 0x25f9(%rip), %rsi      # 0x75a5
leaq 0x26c7(%rip), %rcx      # 0x767a
movl $0x6c, %edx
callq 0x10a0
leaq 0x2564(%rip), %rdi      # 0x7528
leaq 0x25da(%rip), %rsi      # 0x75a5
leaq 0x26a8(%rip), %rcx      # 0x767a
movl $0x79, %edx
callq 0x10a0
leaq 0x24dd(%rip), %rdi      # 0x74c0
leaq 0x25bb(%rip), %rsi      # 0x75a5
leaq 0x2689(%rip), %rcx      # 0x767a
movl $0x81, %edx
callq 0x10a0
leaq 0x24be(%rip), %rdi      # 0x74c0
leaq 0x259c(%rip), %rsi      # 0x75a5
leaq 0x266a(%rip), %rcx      # 0x767a
movl $0x98, %edx
callq 0x10a0
leaq 0x249f(%rip), %rdi      # 0x74c0
leaq 0x257d(%rip), %rsi      # 0x75a5
leaq 0x264b(%rip), %rcx      # 0x767a
movl $0x85, %edx
callq 0x10a0
nopl (%rax)