// File: /workspace/github_c_makefile_star2_O0/joncampbell123[P]swfhax/swf_lzma_reader.c
// Function: swf_reader_lzma_read
int swf_reader_lzma_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));

			/* and then we need to translate Adobe's LZMA header format to the one
			 * the LZMA library expects... because apparently Adobe wanted to make
			 * an incompatible variant or something? */
			if (r->raw_current_pos == 8) {
				/* LZMA header format:
				 *
				 * bytes 0-4: LZMA properties
				 * bytes 5-12: Uncompressed length
				 * bytes 13+: compressed data
				 *
				 * Adobe SWF format:
				 *
				 * bytes 0-3: ZWS header
				 * bytes 4-7: Uncompressed length
				 * bytes 8-11: Compressed length
				 * bytes 12-16: LZMA properties
				 * bytes 17+: compressed data */
				sliding_window_v4_empty(r->decom_buf);
				assert(r->decom_buf->data == r->decom_buf->buffer);
				if (swf_reader_raw_seek(r,0)) return -1;
				if ((cc=sliding_window_v4_can_write(r->decom_buf)) >= 16) {
					rd = swf_reader_raw_read((swf_reader*)io,r->decom_buf->end,cc - 16);
					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);

					/* sigh... alright, now we need to modify the buffer to apply this transformation */
					/* SWF 12-16 LZMA props -> LZMA 0-4 props */
					memmove(r->decom_buf->data+4,r->decom_buf->data+12,5);
					/* dummy uncompressed length LZMA 5-12 */
					memset(r->decom_buf->data+4+5,0xFF,8);
					/* move data ptr forward 4 bytes. our storing LZMA (5) + len (8)
					 * becomes 5+8 = 13 bytes to compressed data */
					r->decom_buf->data += 4;
				}
				else {
					fprintf(stderr,"LZMA: Unable to head check\n");
					return -1;
				}
			}
			else {
				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->lzma.next_in = r->decom_buf->data;
			r->lzma.avail_in = sliding_window_v4_data_available(r->decom_buf);
			r->lzma.next_out = r->out_buf->end;
			r->lzma.avail_out = sliding_window_v4_can_write(r->out_buf);
			rd = lzma_code(&r->lzma,LZMA_RUN);
			if (rd == LZMA_STREAM_END) {
				r->zlib_eof = 1;
			}
			else if (rd != LZMA_OK) {
				fprintf(stderr,"LZMA error %d\n",rd);
				r->zlib_eof = 1;
				break;
			}
			r->decom_buf->data = (unsigned char*)r->lzma.next_in;
			r->out_buf->end = r->lzma.next_out;
			assert(sliding_window_v4_is_sane(r->decom_buf));
		}
	}

	return accum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x1c(%rbp)
movb %al, -0x41(%rbp)
jle 0x5c94
movq -0x30(%rbp), %rax
cmpq $0x8, 0x8(%rax)
setb %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x5ca0
jmp 0x5d74
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x30d0
cmpl $0x0, %eax
je 0x5cc2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6287
movq -0x30(%rbp), %rcx
movl $0x8, %eax
subq 0x8(%rcx), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x5ce8
movslq -0x1c(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rax
movl %eax, %edx
callq 0x37b0
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5d25
cmpl $0x0, -0x34(%rbp)
jle 0x5d25
movq -0x28(%rbp), %rax
movb $0x46, (%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x34(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl %ecx, %eax
jge 0x5d4a
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6287
movl -0x34(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x34(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
jmp 0x5c7a
jmp 0x5d76
cmpl $0x0, -0x1c(%rbp)
jle 0x6281
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x7390
cmpl $0x0, %eax
je 0x5d94
jmp 0x5db3
leaq 0x3a35(%rip), %rdi      # 0x97d0
leaq 0x3a56(%rip), %rsi      # 0x97f8
movl $0x5e, %edx
leaq 0x3a5c(%rip), %rcx      # 0x980a
callq 0x1080
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x7390
cmpl $0x0, %eax
je 0x5dca
jmp 0x5de9
leaq 0x3a67(%rip), %rdi      # 0x9838
leaq 0x3a20(%rip), %rsi      # 0x97f8
movl $0x5f, %edx
leaq 0x3a26(%rip), %rcx      # 0x980a
callq 0x1080
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x7280
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x7280
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x62c0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jbe 0x5edf
movq -0x40(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x5e39
movslq -0x1c(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x10(%rax), %rsi
movq -0x40(%rbp), %rdx
callq 0x10d0
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rcx
movslq -0x38(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rcx
movslq -0x1c(%rbp), %rax
subq %rcx, %rax
movl %eax, -0x1c(%rbp)
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x7390
cmpl $0x0, %eax
je 0x5ebb
jmp 0x5eda
leaq 0x3976(%rip), %rdi      # 0x9838
leaq 0x392f(%rip), %rsi      # 0x97f8
movl $0x6b, %edx
leaq 0x3935(%rip), %rcx      # 0x980a
callq 0x1080
jmp 0x627c
movq -0x30(%rbp), %rax
cmpb $0x0, 0x70(%rax)
je 0x5ef6
movq -0x30(%rbp), %rax
movb $0x1, 0x71(%rax)
jmp 0x6281
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x7390
cmpl $0x0, %eax
je 0x5f0a
jmp 0x5f29
leaq 0x38bf(%rip), %rdi      # 0x97d0
leaq 0x38e0(%rip), %rsi      # 0x97f8
movl $0x73, %edx
leaq 0x38e6(%rip), %rcx      # 0x980a
callq 0x1080
movq -0x30(%rbp), %rax
cmpq $0x8, (%rax)
jne 0x60c4
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x6290
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x78(%rcx), %rcx
cmpq (%rcx), %rax
jne 0x5f5f
jmp 0x5f7e
leaq 0x38f8(%rip), %rdi      # 0x985e
leaq 0x388b(%rip), %rsi      # 0x97f8
movl $0x87, %edx
leaq 0x3891(%rip), %rcx      # 0x980a
callq 0x1080
movq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x30d0
cmpl $0x0, %eax
je 0x5f9c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6287
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x62e0
movq %rax, -0x40(%rbp)
cmpq $0x10, %rax
jb 0x609b
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x18(%rax), %rsi
movq -0x40(%rbp), %rax
subq $0x10, %rax
movl %eax, %edx
callq 0x37b0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x5ff8
movl -0x34(%rbp), %edx
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x18(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x7390
cmpl $0x0, %eax
je 0x600c
jmp 0x602b
leaq 0x37bd(%rip), %rdi      # 0x97d0
leaq 0x37de(%rip), %rsi      # 0x97f8
movl $0x8c, %edx
leaq 0x37e4(%rip), %rcx      # 0x980a
callq 0x1080
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x7280
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x10(%rax), %rax
addq $0x4, %rax
movq -0x30(%rbp), %rcx
movq 0x78(%rcx), %rcx
movq 0x10(%rcx), %rdx
addq $0xc, %rdx
movl (%rdx), %ecx
movb 0x4(%rdx), %dl
movb %dl, 0x4(%rax)
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x10(%rax), %rdi
addq $0x4, %rdi
addq $0x5, %rdi
movl $0xff, %esi
movl $0x8, %edx
callq 0x1090
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x10(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0x10(%rax)
jmp 0x60bf
movq 0x5f56(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x37dd(%rip), %rsi      # 0x9889
movb $0x0, %al
callq 0x10c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6287
jmp 0x615a
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x62e0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jbe 0x6158
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x18(%rax), %rsi
movq -0x40(%rbp), %rax
movl %eax, %edx
callq 0x37b0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x6118
movl -0x34(%rbp), %edx
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x18(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x7390
cmpl $0x0, %eax
je 0x612c
jmp 0x614b
leaq 0x369d(%rip), %rdi      # 0x97d0
leaq 0x36be(%rip), %rsi      # 0x97f8
movl $0xa1, %edx
leaq 0x36c4(%rip), %rcx      # 0x980a
callq 0x1080
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x7280
jmp 0x615a
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x10(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xf8(%rax)
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x62c0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x100(%rax)
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x110(%rax)
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x62e0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x118(%rax)
movq -0x30(%rbp), %rdi
addq $0xf8, %rdi
xorl %esi, %esi
callq 0x1040
movl %eax, -0x34(%rbp)
cmpl $0x1, -0x34(%rbp)
jne 0x61e9
movq -0x30(%rbp), %rax
movb $0x1, 0x70(%rax)
jmp 0x6216
cmpl $0x0, -0x34(%rbp)
je 0x6214
movq 0x5e02(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x34(%rbp), %edx
leaq 0x36a2(%rip), %rsi      # 0x98a5
movb $0x0, %al
callq 0x10c0
movq -0x30(%rbp), %rax
movb $0x1, 0x70(%rax)
jmp 0x6281
jmp 0x6216
movq -0x30(%rbp), %rax
movq 0xf8(%rax), %rcx
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x110(%rax), %rcx
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x7390
cmpl $0x0, %eax
je 0x625b
jmp 0x627a
leaq 0x356e(%rip), %rdi      # 0x97d0
leaq 0x358f(%rip), %rsi      # 0x97f8
movl $0xb5, %edx
leaq 0x3595(%rip), %rcx      # 0x980a
callq 0x1080
jmp 0x627c
jmp 0x5d76
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq