// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x70, %rsp
movl %esi, %r15d
movq %rdi, %r14
movl $0x8000, %edi           # imm = 0x8000
callq 0x3f1d
testq %rax, %rax
je 0x1c1e
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 0x1cc9
cmpl $0x8, %eax
jge 0x1b28
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x3f56
testq %r15, %r15
je 0x1c1e
movq %r15, %rdi
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1060
movl %eax, %eax
addq %rax, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movabsq $0xa1a0a0d474e5089, %rax # imm = 0xA1A0A0D474E5089
cmpq %rax, (%rdi)
je 0x1b61
leaq 0x3635(%rip), %rsi      # 0x517d
pushq $0x6
popq %rdx
callq 0x1140
movzwl %r15w, %edx
testl %eax, %eax
je 0x1b6e
leaq 0x35e8(%rip), %rsi      # 0x5147
jmp 0x1b75
movzwl %r15w, %edx
leaq 0x3601(%rip), %rsi      # 0x516d
jmp 0x1b75
leaq 0x360f(%rip), %rsi      # 0x5184
leaq -0x90(%rbp), %rdi
xorl %eax, %eax
callq 0x11b0
leaq 0x35cd(%rip), %rsi      # 0x5157
leaq -0x90(%rbp), %rdi
callq 0x1190
testq %rax, %rax
je 0x1c16
movq %rax, %r15
movq 0x10(%rbx), %rdi
cmpl $0xd9ffd8ff, (%rdi)     # imm = 0xD9FFD8FF
jne 0x1bb2
addq $0x4, %rdi
movq %rdi, 0x10(%rbx)
cmpl $0xd8ffd9ff, (%rdi)     # imm = 0xD8FFD9FF
jne 0x1bc2
addq $0x4, %rdi
movq %rdi, 0x10(%rbx)
movq 0x18(%rbx), %rsi
subq %rdi, %rsi
testl %esi, %esi
jle 0x1bde
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x11c0
pushq $0x1
popq %r12
movq (%rbx), %rsi
movq %rsi, 0x10(%rbx)
movq %rsi, 0x18(%rbx)
movl 0x8(%rbx), %edx
subl %esi, %edx
movq %r14, %rdi
callq 0x1cc9
testl %eax, %eax
je 0x1b03
movq 0x10(%rbx), %rdi
movslq %eax, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
jmp 0x1be2
movq %rbx, %rdi
callq 0x3f56
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop