// File: /workspace/github_c_makefile_star2_O0/joncampbell123[P]swfhax/c4_swfjpegs.c
// Function: do_definebits_jpeg3
static void do_definebits_jpeg3(swf_reader *swf,swf_UI16 id,swf_UI32 len) {
	sliding_window_v4 *buf;
	FILE *fp=NULL;
	int jpeg=0;
	int rd;

	if (len < 8) return;
	buf = sliding_window_v4_create_buffer(32768);
	if (buf == NULL) return;
	sliding_window_v4_empty(buf);
	rd = sliding_window_v4_can_write(buf);
	if ((swf_UI32)rd > len) rd = len;
	rd = swf_reader_read(swf,buf->data,rd);
	if (rd < 8) goto end;
	buf->end += rd;
	len -= rd;

	/* it can be JPEG, PNG, or GIF. We have to look at the first 8 bytes */
	{
		char tmp[100];

		if (!memcmp(buf->data,"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",8))
			sprintf(tmp,"swf.jpeg.%u.png",id);
		else if (!memcmp(buf->data,"GIF89a",6))
			sprintf(tmp,"swf.jpeg.%u.gif",id);
		else {
			sprintf(tmp,"swf.jpeg.%u.jpg",id);
			jpeg = 1;
		}

		fp = fopen(tmp,"wb");
		if (fp == NULL) {
			buf = sliding_window_v4_destroy(buf);
			return;
		}
	}

	/* Macromedia Flash prior to 8 stuck in an extra SOI/EOI for some reason. Discard */
	if (!memcmp(buf->data,"\xFF\xD8\xFF\xD9",4)) buf->data += 4;
	if (!memcmp(buf->data,"\xFF\xD9\xFF\xD8",4)) buf->data += 4;

	/* write out the rest of the buffer */
	rd = (int)sliding_window_v4_data_available(buf);
	if (rd > 0) fwrite(buf->data,(size_t)rd,1,fp);

	/* and then copy the rest of the file */
	do {
		sliding_window_v4_empty(buf);
		rd = sliding_window_v4_can_write(buf);
		if ((swf_UI32)rd > len) rd = len;
		rd = swf_reader_read(swf,buf->data,rd);
		if (rd == 0) break;
		fwrite(buf->data,(size_t)rd,1,fp);
		len -= rd;
	} while (1);

	sliding_window_v4_empty(buf);
	if (fp) fclose(fp); fp = NULL;

	/* and then... the JPEG alpha channel */
	if (jpeg) {
		unsigned char out[4096];
		char tmp[100];
		z_stream z;

		sprintf(tmp,"swf.jpeg.%u.jpg.alpha.data",id);
		fp = fopen(tmp,"wb");
		if (fp == NULL) goto end;

		rd = sliding_window_v4_can_write(buf);
		rd = swf_reader_read(swf,buf->end,rd);
		if (rd > 0) buf->end += rd;
		memset(&z,0,sizeof(z));
		z.next_in = buf->data;
		z.avail_in = sliding_window_v4_data_available(buf);
		z.next_out = out;
		z.avail_out = sizeof(out);
		if (inflateInit(&z) == Z_OK) {
			int eof=0;

			do {
				sliding_window_v4_lazy_flush(buf);
				rd = sliding_window_v4_can_write(buf);
				if (rd > 0) {
					rd = swf_reader_read(swf,buf->end,rd);
					if (rd > 0) buf->end += rd;
					else eof=1;
				}

				z.next_in = buf->data;
				z.avail_in = sliding_window_v4_data_available(buf);
				z.next_out = out;
				z.avail_out = sizeof(out);
				rd = inflate(&z,Z_NO_FLUSH);
				if (rd == Z_STREAM_END) eof=1;
				buf->data = z.next_in;
				if (z.next_out == out && eof) break;
				fwrite(out,(size_t)(z.next_out-out),1,fp);
				if (rd != Z_OK) break;
			} while (1);

			inflateEnd(&z);
		}
	}

end:	buf = sliding_window_v4_destroy(buf);
	if (fp) fclose(fp); fp = NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1180, %rsp           # imm = 0x1180
movw %si, %ax
movq %rdi, -0x8(%rbp)
movw %ax, -0xa(%rbp)
movl %edx, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x8, -0x10(%rbp)
jae 0x1d63
jmp 0x2205
movl $0x8000, %edi           # imm = 0x8000
callq 0x63d0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1d7d
jmp 0x2205
movq -0x18(%rbp), %rdi
callq 0x2230
movq -0x18(%rbp), %rdi
callq 0x2260
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x10(%rbp), %eax
jbe 0x1da0
movl -0x10(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x28(%rbp), %edx
callq 0x27e0
movl %eax, -0x28(%rbp)
cmpl $0x8, -0x28(%rbp)
jge 0x1dc2
jmp 0x21e0
movl -0x28(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x18(%rax)
movl -0x28(%rbp), %ecx
movl -0x10(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x10(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movabsq $0xa1a0a0d474e5089, %rcx # imm = 0xA1A0A0D474E5089
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1e20
leaq -0x90(%rbp), %rdi
movzwl -0xa(%rbp), %edx
leaq 0x53d0(%rip), %rsi      # 0x71e7
movb $0x0, %al
callq 0x11a0
jmp 0x1e7b
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x53c8(%rip), %rsi      # 0x71f7
movl $0x6, %edx
callq 0x10c0
cmpl $0x0, %eax
jne 0x1e59
leaq -0x90(%rbp), %rdi
movzwl -0xa(%rbp), %edx
leaq 0x53ae(%rip), %rsi      # 0x71fe
movb $0x0, %al
callq 0x11a0
jmp 0x1e79
leaq -0x90(%rbp), %rdi
movzwl -0xa(%rbp), %edx
leaq 0x5356(%rip), %rsi      # 0x71c1
movb $0x0, %al
callq 0x11a0
movl $0x1, -0x24(%rbp)
jmp 0x1e7b
leaq -0x90(%rbp), %rdi
leaq 0x5348(%rip), %rsi      # 0x71d1
callq 0x1180
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1eab
movq -0x18(%rbp), %rdi
callq 0x6430
movq %rax, -0x18(%rbp)
jmp 0x2205
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %eax
subl $0xd9ffd8ff, %eax       # imm = 0xD9FFD8FF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1ed5
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %eax
subl $0xd8ffd9ff, %eax       # imm = 0xD8FFD9FF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1eff
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rdi
callq 0x22b0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jle 0x1f2b
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
jmp 0x1f2d
movq -0x18(%rbp), %rdi
callq 0x2230
movq -0x18(%rbp), %rdi
callq 0x2260
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x10(%rbp), %eax
jbe 0x1f50
movl -0x10(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x28(%rbp), %edx
callq 0x27e0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x1f6f
jmp 0x1f9c
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
movl -0x28(%rbp), %ecx
movl -0x10(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x10(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0x1f2d
jmp 0x1f9c
movq -0x18(%rbp), %rdi
callq 0x2230
cmpq $0x0, -0x20(%rbp)
je 0x1fb5
movq -0x20(%rbp), %rdi
callq 0x1060
movq $0x0, -0x20(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x21de
leaq -0x1100(%rbp), %rdi
movzwl -0xa(%rbp), %edx
leaq 0x5235(%rip), %rsi      # 0x720e
movb $0x0, %al
callq 0x11a0
leaq -0x1100(%rbp), %rdi
leaq 0x51e3(%rip), %rsi      # 0x71d1
callq 0x1180
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x2003
jmp 0x21e0
movq -0x18(%rbp), %rdi
callq 0x2260
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movl -0x28(%rbp), %edx
callq 0x27e0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jle 0x2041
movl -0x28(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x18(%rax)
leaq -0x1170(%rbp), %rdi
xorl %esi, %esi
movl $0x70, %edx
callq 0x1090
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1170(%rbp)
movq -0x18(%rbp), %rdi
callq 0x22b0
movl %eax, -0x1168(%rbp)
leaq -0x1090(%rbp), %rax
movq %rax, -0x1158(%rbp)
movl $0x1000, -0x1150(%rbp)  # imm = 0x1000
leaq -0x1170(%rbp), %rdi
leaq 0x5191(%rip), %rsi      # 0x7229
movl $0x70, %edx
callq 0x1150
cmpl $0x0, %eax
jne 0x21dc
movl $0x0, -0x1174(%rbp)
movq -0x18(%rbp), %rdi
callq 0x68d0
movq -0x18(%rbp), %rdi
callq 0x2260
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jle 0x2110
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movl -0x28(%rbp), %edx
callq 0x27e0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jle 0x2104
movl -0x28(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x18(%rax)
jmp 0x210e
movl $0x1, -0x1174(%rbp)
jmp 0x2110
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1170(%rbp)
movq -0x18(%rbp), %rdi
callq 0x22b0
movl %eax, -0x1168(%rbp)
leaq -0x1090(%rbp), %rax
movq %rax, -0x1158(%rbp)
movl $0x1000, -0x1150(%rbp)  # imm = 0x1000
leaq -0x1170(%rbp), %rdi
xorl %esi, %esi
callq 0x1050
movl %eax, -0x28(%rbp)
cmpl $0x1, -0x28(%rbp)
jne 0x2167
movl $0x1, -0x1174(%rbp)
movq -0x1170(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x1158(%rbp), %rax
leaq -0x1090(%rbp), %rcx
cmpq %rcx, %rax
jne 0x2194
cmpl $0x0, -0x1174(%rbp)
je 0x2194
jmp 0x21d0
leaq -0x1090(%rbp), %rdi
movq -0x1158(%rbp), %rsi
leaq -0x1090(%rbp), %rax
subq %rax, %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
cmpl $0x0, -0x28(%rbp)
je 0x21c2
jmp 0x21d0
jmp 0x21c4
movb $0x1, %al
testb $0x1, %al
jne 0x20b5
jmp 0x21d0
leaq -0x1170(%rbp), %rdi
callq 0x1100
jmp 0x21de
jmp 0x21e0
movq -0x18(%rbp), %rdi
callq 0x6430
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x21fd
movq -0x20(%rbp), %rdi
callq 0x1060
movq $0x0, -0x20(%rbp)
addq $0x1180, %rsp           # imm = 0x1180
popq %rbp
retq
nop