// File: /workspace/github_c_makefile_star2_O0/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
subq $0xb0, %rsp
movw %si, %ax
movq %rdi, -0x8(%rbp)
movw %ax, -0xa(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x8000, %edi           # imm = 0x8000
callq 0x63d0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1ae8
jmp 0x1d1d
movq -0x18(%rbp), %rdi
callq 0x2230
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2260
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movl %eax, %edx
callq 0x27e0
movl %eax, -0x24(%rbp)
cmpl $0x8, -0x24(%rbp)
jge 0x1b37
jmp 0x1d00
movl -0x24(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x18(%rax)
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 0x1b8a
leaq -0x90(%rbp), %rdi
movzwl -0xa(%rbp), %edx
leaq 0x5666(%rip), %rsi      # 0x71e7
movb $0x0, %al
callq 0x11a0
jmp 0x1bde
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x565e(%rip), %rsi      # 0x71f7
movl $0x6, %edx
callq 0x10c0
cmpl $0x0, %eax
jne 0x1bc3
leaq -0x90(%rbp), %rdi
movzwl -0xa(%rbp), %edx
leaq 0x5644(%rip), %rsi      # 0x71fe
movb $0x0, %al
callq 0x11a0
jmp 0x1bdc
leaq -0x90(%rbp), %rdi
movzwl -0xa(%rbp), %edx
leaq 0x55ec(%rip), %rsi      # 0x71c1
movb $0x0, %al
callq 0x11a0
jmp 0x1bde
leaq -0x90(%rbp), %rdi
leaq 0x55e5(%rip), %rsi      # 0x71d1
callq 0x1180
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1c0e
movq -0x18(%rbp), %rdi
callq 0x6430
movq %rax, -0x18(%rbp)
jmp 0x1d1d
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 0x1c38
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 0x1c62
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rdi
callq 0x22b0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x1c8e
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x24(%rbp), %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
jmp 0x1c90
movq -0x18(%rbp), %rdi
callq 0x2230
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2260
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movl %eax, %edx
callq 0x27e0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x1cdc
jmp 0x1cfe
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x24(%rbp), %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
movb $0x1, %al
testb $0x1, %al
jne 0x1c90
jmp 0x1cfe
jmp 0x1d00
movq -0x18(%rbp), %rdi
callq 0x6430
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x1d1d
movq -0x20(%rbp), %rdi
callq 0x1060
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)