// File: /workspace/github_c_makefile_star2_O1/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 0x47df
testq %rax, %rax
je 0x1fdb
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 0x2087
cmpl $0x8, %eax
jge 0x1ec8
xorl %r15d, %r15d
jmp 0x1fbc
movl %eax, %eax
addq %rax, 0x18(%rbx)
movq 0x10(%rbx), %rax
movabsq $0xa1a0a0d474e5089, %rcx # imm = 0xA1A0A0D474E5089
cmpq %rcx, (%rax)
je 0x1f02
movl $0x38464947, %ecx       # imm = 0x38464947
xorl (%rax), %ecx
movzwl 0x4(%rax), %eax
xorl $0x6139, %eax           # imm = 0x6139
orl %ecx, %eax
movzwl %r15w, %edx
je 0x1f0f
leaq 0x3249(%rip), %rsi      # 0x5149
jmp 0x1f16
movzwl %r15w, %edx
leaq 0x3262(%rip), %rsi      # 0x516f
jmp 0x1f16
leaq 0x3270(%rip), %rsi      # 0x5186
leaq -0x80(%rbp), %rdi
xorl %eax, %eax
callq 0x1190
leaq 0x3231(%rip), %rsi      # 0x5159
leaq -0x80(%rbp), %rdi
callq 0x1170
movq %rax, %r15
testq %rax, %rax
jne 0x1f44
movq %rbx, %rdi
callq 0x4838
movq %rax, %rbx
testq %r15, %r15
je 0x1fdb
movq 0x10(%rbx), %rax
cmpl $0xd9ffd8ff, (%rax)     # imm = 0xD9FFD8FF
jne 0x1f61
addq $0x4, %rax
movq %rax, 0x10(%rbx)
movq 0x10(%rbx), %rax
cmpl $0xd8ffd9ff, (%rax)     # imm = 0xD8FFD9FF
jne 0x1f75
addq $0x4, %rax
movq %rax, 0x10(%rbx)
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
subq %rdi, %rsi
testl %esi, %esi
jle 0x1fa0
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
jmp 0x1f93
movq 0x10(%rbx), %rdi
movslq %eax, %rsi
movl $0x1, %edx
movq %r15, %rcx
callq 0x11a0
movq (%rbx), %rsi
movq %rsi, 0x10(%rbx)
movq %rsi, 0x18(%rbx)
movl 0x8(%rbx), %edx
subl %esi, %edx
movq %r14, %rdi
callq 0x2087
testl %eax, %eax
jne 0x1f8c
movq %rbx, %rdi
callq 0x4838
testq %r15, %r15
je 0x1fdb
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1060
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop