// File: /workspace/github_c_makefile_star2_O0/albinoloverats[P]hide/src/jpeg-load.c
// Function: ParseJFIF
static int ParseJFIF(stJpegData *jdata, const uint8_t *stream)
{
	int chuck_len;
	int marker;
	int sos_marker_found = 0;
	int dht_marker_found = 0;

	// Parse marker
	while (!sos_marker_found)
	{
		if (*stream++ != 0xff)
			goto bogus_jpeg_format;

		// Skip any padding ff byte (this is normal)
		while (*stream == 0xff)
			stream++;

		marker = *stream++;
		chuck_len = BYTE_TO_WORD(stream);

		switch (marker)
		{
			case SOF:
				if (ParseSOF(jdata, stream) < 0)
					return -1;
				break;

			case DQT:
				if (ParseDQT(jdata, stream) < 0)
					return -1;
				break;

			case SOS:
				if (ParseSOS(jdata, stream) < 0)
					return -1;
				sos_marker_found = 1;
				break;

			case DHT:
				if (ParseDHT(jdata, stream) < 0)
					return -1;
				dht_marker_found = 1;
				break;

				// The reason I added these additional skips here, is because for
				// certain jpg compressions, like swf, it splits the encoding
				// and image data with SOI & EOI extra tags, so we need to skip
				// over them here and decode the whole image
			case SOI:
			case EOI:
				chuck_len = 0;
				break;

			case 0xDD: //DRI: Restart_markers=1;
				jdata->m_restart_interval = BYTE_TO_WORD(stream);
				break;

			case APP0:
				break;

			default:
				break;
		}

		stream += chuck_len;
	}

	if (!dht_marker_found)
	{
		fprintf(stderr, "Fail @ %s:%d\n", __FILE__, __LINE__);
		exit(-1);
	}

	return 0;

bogus_jpeg_format:
	fprintf(stderr, "Fail @ %s:%d\n", __FILE__, __LINE__);
	exit(-1);
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x0, -0x24(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1ee0
jmp 0x203d
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movzbl (%rax), %eax
cmpl $0xff, %eax
je 0x1efe
jmp 0x207a
jmp 0x1f00
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xff, %eax
jne 0x1f1c
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x1f00
movq -0x18(%rbp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, -0x18(%rbp)
movzbl (%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movw (%rax), %ax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
addl $0xffffff40, %eax       # imm = 0xFFFFFF40
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x20, %eax
ja 0x2025
movq -0x30(%rbp), %rax
leaq 0x50cd(%rip), %rcx      # 0x7030
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x20c0
cmpl $0x0, %eax
jge 0x1f8a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x20a8
jmp 0x2027
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x21e0
cmpl $0x0, %eax
jge 0x1fad
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x20a8
jmp 0x2027
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2350
cmpl $0x0, %eax
jge 0x1fcd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x20a8
movl $0x1, -0x24(%rbp)
jmp 0x2027
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x24f0
cmpl $0x0, %eax
jge 0x1ff4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x20a8
movl $0x1, -0x28(%rbp)
jmp 0x2027
movl $0x0, -0x1c(%rbp)
jmp 0x2027
movq -0x18(%rbp), %rax
movzbl (%rax), %ecx
shll $0x8, %ecx
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
orl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x2027
jmp 0x2027
jmp 0x2027
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
jmp 0x1ece
cmpl $0x0, -0x28(%rbp)
jne 0x2071
movq 0x6fae(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x509c(%rip), %rsi      # 0x70f0
leaq 0x50a3(%rip), %rdx      # 0x70fe
movl $0x22e, %ecx            # imm = 0x22E
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1150
movl $0x0, -0x4(%rbp)
jmp 0x20a8
movq 0x6f77(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x5065(%rip), %rsi      # 0x70f0
leaq 0x506c(%rip), %rdx      # 0x70fe
movl $0x235, %ecx            # imm = 0x235
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1150
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)