// File: /workspace/github_c_makefile_star2_O0/joncampbell123[P]swfhax/c4_swfjpegs.c
// Function: do_definebits
static void do_definebits(swf_reader *swf,swf_UI16 id) {
	sliding_window_v4 *buf;
	unsigned char *s;
	FILE *fp;
	int rd;

	if (JPEGTables == NULL || JPEGTables_size == 0) {
		/* Flash 8+ compatibility where JPEGTables tags are zero length */
		do_definebits_jpeg2(swf,id);
		return;
	}

	buf = sliding_window_v4_create_buffer(32768);
	if (buf == NULL) return;

	{
		char tmp[100];

		sprintf(tmp,"swf.jpeg.%u.jpg",id);
		fp = fopen(tmp,"wb");
		if (fp == NULL) {
			buf = sliding_window_v4_destroy(buf);
			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;

	/* OK. We have to do two things here:
	 *
	 *    1) Skip the extra 0xFF 0xD9 0xFF 0xD8 that Macromedia Flash prior to version 8
	 *       inserted into the stream for some reason
	 *    2) Scan and copy up to the Start of Frame marker.
	 *       At that point, before continuing to copy the stream, we need to take the
	 *       JPEGTables and splice it into the stream to make a valid whole JPEG file */
	if (!memcmp(buf->data,"\xFF\xD8\xFF\xD9",4)) buf->data += 4;
	else if (!memcmp(buf->data,"\xFF\xD9\xFF\xD8",4)) buf->data += 4;
	s = buf->data;
	while ((s+2) <= buf->end) {
		if (*s == 0xFF) {
			if (s[1] == 0xC0 || s[1] == 0xC2) {
				/* Stop here. It's the Start of Frame */
				break;
			}
			else if (s[1] == 0xE0) { /* APP0 marker. speed scan by reading length field */
				unsigned int l = __be_u16(s+2);
				s += 2 + l; if (s > buf->end) s = buf->end;
			}
			else {
				s += 2;
			}
		}
		else {
			s++;
		}
	}

	/* write what we found */
	fwrite(buf->data,(size_t)(s-buf->data),1,fp);
	buf->data = s;

	/* now splice in the JPEGTables. Note the JPEGTables might have SOI and EOI
	 * around the quant tables, so we have to check for and remove those */
	{
		unsigned char *s = JPEGTables;
		unsigned char *e = JPEGTables + JPEGTables_size;

		if (s[0] == 0xFF && s[1] == 0xD8) s += 2;
		if ((s+2) <= e && e[-2] == 0xFF && e[-1] == 0xD9) e -= 2;
		if (s < e) fwrite(s,(size_t)(e-s),1,fp);
	}

	/* 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);
	fclose(fp);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movw %si, %ax
movq %rdi, -0x8(%rbp)
movw %ax, -0xa(%rbp)
cmpq $0x0, 0x9aa2(%rip)      # 0xb180
je 0x16ea
cmpq $0x0, 0x9aa0(%rip)      # 0xb188
jne 0x16fc
movq -0x8(%rbp), %rdi
movzwl -0xa(%rbp), %esi
callq 0x1ab0
jmp 0x1aa4
movl $0x8000, %edi           # imm = 0x8000
callq 0x63d0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1716
jmp 0x1aa4
leaq -0x90(%rbp), %rdi
movzwl -0xa(%rbp), %edx
leaq 0x5a99(%rip), %rsi      # 0x71c1
movb $0x0, %al
callq 0x11a0
leaq -0x90(%rbp), %rdi
leaq 0x5a94(%rip), %rsi      # 0x71d1
callq 0x1180
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x175f
movq -0x18(%rbp), %rdi
callq 0x6430
movq %rax, -0x18(%rbp)
jmp 0x1aa4
movq -0x18(%rbp), %rdi
callq 0x2230
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2260
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movl %eax, %edx
callq 0x27e0
movl %eax, -0x2c(%rbp)
cmpl $0x8, -0x2c(%rbp)
jge 0x17ae
jmp 0x1a8e
movl -0x2c(%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
movl (%rax), %eax
subl $0xd9ffd8ff, %eax       # imm = 0xD9FFD8FF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x17ef
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0x10(%rax)
jmp 0x181b
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 0x1819
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0x10(%rax)
jmp 0x181b
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
ja 0x18e7
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xff, %eax
jne 0x18d6
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0xc0, %eax
je 0x186d
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0xc2, %eax
jne 0x186f
jmp 0x18e7
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0xe0, %eax
jne 0x18c6
movq -0x20(%rbp), %rdi
addq $0x2, %rdi
callq 0x2280
movzwl %ax, %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %ecx
addl $0x2, %ecx
movq -0x20(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jbe 0x18c4
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x18d2
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
jmp 0x18d4
jmp 0x18e2
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1827
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
subq %rax, %rsi
movq -0x28(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x9861(%rip), %rax      # 0xb180
movq %rax, -0xa0(%rbp)
movq 0x9853(%rip), %rax      # 0xb180
addq 0x9854(%rip), %rax      # 0xb188
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xff, %eax
jne 0x1970
movq -0xa0(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0xd8, %eax
jne 0x1970
movq -0xa0(%rbp), %rax
addq $0x2, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
addq $0x2, %rax
cmpq -0xa8(%rbp), %rax
ja 0x19ba
movq -0xa8(%rbp), %rax
movzbl -0x2(%rax), %eax
cmpl $0xff, %eax
jne 0x19ba
movq -0xa8(%rbp), %rax
movzbl -0x1(%rax), %eax
cmpl $0xd9, %eax
jne 0x19ba
movq -0xa8(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jae 0x19f0
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rax
subq %rax, %rsi
movq -0x28(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
movq -0x18(%rbp), %rdi
callq 0x22b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x1a1c
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x2c(%rbp), %rsi
movq -0x28(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
jmp 0x1a1e
movq -0x18(%rbp), %rdi
callq 0x2230
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2260
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl %eax, %edx
callq 0x27e0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x1a6a
jmp 0x1a8c
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x2c(%rbp), %rsi
movq -0x28(%rbp), %rcx
movl $0x1, %edx
callq 0x11b0
movb $0x1, %al
testb $0x1, %al
jne 0x1a1e
jmp 0x1a8c
jmp 0x1a8e
movq -0x18(%rbp), %rdi
callq 0x6430
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rdi
callq 0x1060
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)