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

	buf = sliding_window_v4_create_buffer(32768);
	if (buf == NULL) return;
	sliding_window_v4_empty(buf);
	rd = swf_reader_read(swf,buf->data,sliding_window_v4_can_write(buf));
	if (rd < 8) goto end;
	buf->end += 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);

		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 = swf_reader_read(swf,buf->data,sliding_window_v4_can_write(buf));
		if (rd == 0) break;
		fwrite(buf->data,(size_t)rd,1,fp);
	} while (1);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x68, %rsp
movl %esi, %r15d
movq %rdi, %r14
movl $0x8000, %edi           # imm = 0x8000
callq 0x47ae
testq %rax, %rax
je 0x1f95
movq %rax, %rbx
movq (%rax), %rsi
movq %rsi, 0x10(%rax)
movq %rsi, 0x18(%rax)
movl 0x8(%rax), %edx
subl %esi, %edx
movq %r14, %rdi
callq 0x208b
cmpl $0x7, %eax
jg 0x1ee5
movq %rbx, %rdi
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x47f1
movl %eax, %eax
addq %rax, 0x18(%rbx)
movq 0x10(%rbx), %rax
movabsq $0xa1a0a0d474e5089, %rcx # imm = 0xA1A0A0D474E5089
cmpq %rcx, (%rax)
je 0x1f1f
movl $0x38464947, %ecx       # imm = 0x38464947
xorl (%rax), %ecx
movzwl 0x4(%rax), %eax
xorl $0x6139, %eax           # imm = 0x6139
orl %ecx, %eax
movzwl %r15w, %edx
je 0x1f2c
leaq 0x322a(%rip), %rsi      # 0x5147
jmp 0x1f33
movzwl %r15w, %edx
leaq 0x3243(%rip), %rsi      # 0x516d
jmp 0x1f33
leaq 0x3251(%rip), %rsi      # 0x5184
leaq -0x80(%rbp), %rdi
xorl %eax, %eax
callq 0x11a0
leaq 0x3212(%rip), %rsi      # 0x5157
leaq -0x80(%rbp), %rdi
callq 0x1180
testq %rax, %rax
je 0x1f8d
movq %rax, %r15
movq 0x10(%rbx), %rdi
cmpl $0xd9ffd8ff, (%rdi)     # imm = 0xD9FFD8FF
jne 0x1f6a
addq $0x4, %rdi
movq %rdi, 0x10(%rbx)
cmpl $0xd8ffd9ff, (%rdi)     # imm = 0xD8FFD9FF
jne 0x1f7a
addq $0x4, %rdi
movq %rdi, 0x10(%rbx)
movq 0x18(%rbx), %rsi
subq %rdi, %rsi
testl %esi, %esi
jle 0x1fb4
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
jmp 0x1fa7
movq %rbx, %rdi
callq 0x47f1
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rbx), %rdi
movslq %eax, %rsi
movl $0x1, %edx
movq %r15, %rcx
callq 0x11b0
movq (%rbx), %rsi
movq %rsi, 0x10(%rbx)
movq %rsi, 0x18(%rbx)
movl 0x8(%rbx), %edx
subl %esi, %edx
movq %r14, %rdi
callq 0x208b
testl %eax, %eax
jne 0x1fa0
movq %rbx, %rdi
callq 0x47f1
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1060
nop