// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/libsmacker/smacker.c
// Function: smk_render_video
static char smk_render_video(struct smk_video_t * s, unsigned char * p, unsigned int size)
{
	unsigned char * t = s->frame;
	unsigned char s1, s2;
	unsigned short temp;
	unsigned long i, j, k, row, col, skip;
	/* used for video decoding */
	struct smk_bit_t bs;
	/* results from a tree lookup */
	int unpack;
	/* unpack, broken into pieces */
	unsigned char type;
	unsigned char blocklen;
	unsigned char typedata;
	char bit;
	const unsigned short sizetable[64] = {
		1,	 2,	3,	4,	5,	6,	7,	8,
		9,	10,	11,	12,	13,	14,	15,	16,
		17,	18,	19,	20,	21,	22,	23,	24,
		25,	26,	27,	28,	29,	30,	31,	32,
		33,	34,	35,	36,	37,	38,	39,	40,
		41,	42,	43,	44,	45,	46,	47,	48,
		49,	50,	51,	52,	53,	54,	55,	56,
		57,	58,	59,	128,	256,	512,	1024,	2048
	};
	/* null check */
	assert(s);
	assert(p);
	row = 0;
	col = 0;
	/* Set up a bitstream for video unpacking */
	smk_bs_init(&bs, p, size);

	/* Reset the cache on all bigtrees */
	for (i = 0; i < 4; i++)
		memset(&s->tree[i].cache, 0, 3 * sizeof(unsigned short));

	while (row < s->h) {
		if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_TYPE], &bs)) < 0) {
			fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from TYPE tree.\n", stderr);
			return -1;
		}

		type = ((unpack & 0x0003));
		blocklen = ((unpack & 0x00FC) >> 2);
		typedata = ((unpack & 0xFF00) >> 8);

		/* support for v4 full-blocks */
		if (type == 1 && s->v == '4') {
			bit = smk_bs_read_1(&bs);

			if (bit)
				type = 4;
			else {
				bit = smk_bs_read_1(&bs);

				if (bit)
					type = 5;
			}
		}

		for (j = 0; (j < sizetable[blocklen]) && (row < s->h); j ++) {
			skip = (row * s->w) + col;

			switch (type) {
			case 0:
				if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_MCLR], &bs)) < 0) {
					fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from MCLR tree.\n", stderr);
					return -1;
				}

				s1 = (unpack & 0xFF00) >> 8;
				s2 = (unpack & 0x00FF);

				if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_MMAP], &bs)) < 0) {
					fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from MMAP tree.\n", stderr);
					return -1;
				}

				temp = 0x01;

				for (k = 0; k < 4; k ++) {
					for (i = 0; i < 4; i ++) {
						if (unpack & temp)
							t[skip + i] = s1;
						else
							t[skip + i] = s2;

						temp = temp << 1;
					}

					skip += s->w;
				}

				break;

			case 1: /* FULL BLOCK */
				for (k = 0; k < 4; k ++) {
					if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
						fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
						return -1;
					}

					t[skip + 3] = ((unpack & 0xFF00) >> 8);
					t[skip + 2] = (unpack & 0x00FF);

					if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
						fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
						return -1;
					}

					t[skip + 1] = ((unpack & 0xFF00) >> 8);
					t[skip] = (unpack & 0x00FF);
					skip += s->w;
				}

				break;

			case 2: /* VOID BLOCK */
				/* break;
				if (s->frame)
				{
					memcpy(&t[skip], &s->frame[skip], 4);
					skip += s->w;
					memcpy(&t[skip], &s->frame[skip], 4);
					skip += s->w;
					memcpy(&t[skip], &s->frame[skip], 4);
					skip += s->w;
					memcpy(&t[skip], &s->frame[skip], 4);
				} */
				break;

			case 3: /* SOLID BLOCK */
				memset(&t[skip], typedata, 4);
				skip += s->w;
				memset(&t[skip], typedata, 4);
				skip += s->w;
				memset(&t[skip], typedata, 4);
				skip += s->w;
				memset(&t[skip], typedata, 4);
				break;

			case 4: /* V4 DOUBLE BLOCK */
				for (k = 0; k < 2; k ++) {
					if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
						fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
						return -1;
					}

					for (i = 0; i < 2; i ++) {
						memset(&t[skip + 2], (unpack & 0xFF00) >> 8, 2);
						memset(&t[skip], (unpack & 0x00FF), 2);
						skip += s->w;
					}
				}

				break;

			case 5: /* V4 HALF BLOCK */
				for (k = 0; k < 2; k ++) {
					if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
						fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
						return -1;
					}

					t[skip + 3] = ((unpack & 0xFF00) >> 8);
					t[skip + 2] = (unpack & 0x00FF);
					t[skip + s->w + 3] = ((unpack & 0xFF00) >> 8);
					t[skip + s->w + 2] = (unpack & 0x00FF);

					if ((unpack = smk_huff16_lookup(&s->tree[SMK_TREE_FULL], &bs)) < 0) {
						fputs("libsmacker::smk_render_video() - ERROR: failed to lookup from FULL tree.\n", stderr);
						return -1;
					}

					t[skip + 1] = ((unpack & 0xFF00) >> 8);
					t[skip] = (unpack & 0x00FF);
					t[skip + s->w + 1] = ((unpack & 0xFF00) >> 8);
					t[skip + s->w] = (unpack & 0x00FF);
					skip += (s->w << 1);
				}

				break;
			}

			col += 4;

			if (col >= s->w) {
				col = 0;
				row += 4;
			}
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x3a0(%rax), %rax
movq %rax, -0x28(%rbp)
leaq -0x100(%rbp), %rdi
leaq 0xa424d(%rip), %rsi     # 0xed030
movl $0x80, %edx
callq 0x80e0
cmpq $0x0, -0x10(%rbp)
je 0x48df6
jmp 0x48e15
leaq 0xa6952(%rip), %rdi     # 0xef74f
leaq 0xa44bf(%rip), %rsi     # 0xed2c3
movl $0x512, %edx            # imm = 0x512
leaq 0xa644a(%rip), %rcx     # 0xef25a
callq 0x8230
cmpq $0x0, -0x18(%rbp)
je 0x48e1e
jmp 0x48e3d
leaq 0x90c84(%rip), %rdi     # 0xd9aa9
leaq 0xa4497(%rip), %rsi     # 0xed2c3
movl $0x513, %edx            # imm = 0x513
leaq 0xa6422(%rip), %rcx     # 0xef25a
callq 0x8230
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %eax
movl %eax, %edx
leaq -0x78(%rbp), %rdi
callq 0x47720
movq $0x0, -0x38(%rbp)
cmpq $0x4, -0x38(%rbp)
jae 0x48e9c
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
imulq $0x18, -0x38(%rbp), %rax
addq %rax, %rdi
addq $0x10, %rdi
xorl %esi, %esi
movl $0x6, %edx
callq 0x8410
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x48e67
jmp 0x48e9e
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x49560
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
addq $0x48, %rdi
leaq -0x78(%rbp), %rsi
callq 0x49570
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jge 0x48eec
movq 0xd0104(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa63c7(%rip), %rdi     # 0xef2a5
callq 0x8430
movb $-0x1, -0x1(%rbp)
jmp 0x49564
movl -0x7c(%rbp), %eax
andl $0x3, %eax
movb %al, -0x7d(%rbp)
movl -0x7c(%rbp), %eax
andl $0xfc, %eax
sarl $0x2, %eax
movb %al, -0x7e(%rbp)
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, -0x7f(%rbp)
movzbl -0x7d(%rbp), %eax
cmpl $0x1, %eax
jne 0x48f59
movq -0x10(%rbp), %rax
movzbl 0x19(%rax), %eax
cmpl $0x34, %eax
jne 0x48f59
leaq -0x78(%rbp), %rdi
callq 0x47bc0
movb %al, -0x80(%rbp)
cmpb $0x0, -0x80(%rbp)
je 0x48f3f
movb $0x4, -0x7d(%rbp)
jmp 0x48f57
leaq -0x78(%rbp), %rdi
callq 0x47bc0
movb %al, -0x80(%rbp)
cmpb $0x0, -0x80(%rbp)
je 0x48f55
movb $0x5, -0x7d(%rbp)
jmp 0x48f57
jmp 0x48f59
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rcx
movzbl -0x7e(%rbp), %eax
movzwl -0x100(%rbp,%rax,2), %eax
movl %eax, %edx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x101(%rbp)
jae 0x48f95
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
setb %al
movb %al, -0x101(%rbp)
movb -0x101(%rbp), %al
testb $0x1, %al
jne 0x48fa4
jmp 0x4955b
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
imulq %rcx, %rax
movq -0x58(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movzbl -0x7d(%rbp), %eax
movq %rax, -0x110(%rbp)
subq $0x5, %rax
ja 0x4951a
movq -0x110(%rbp), %rax
leaq 0xa3fee(%rip), %rcx     # 0xecfd0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
addq $0x18, %rdi
leaq -0x78(%rbp), %rsi
callq 0x49570
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jge 0x49027
movq 0xcffc9(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa62d6(%rip), %rdi     # 0xef2ef
callq 0x8430
movb $-0x1, -0x1(%rbp)
jmp 0x49564
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, -0x29(%rbp)
movl -0x7c(%rbp), %eax
andl $0xff, %eax
movb %al, -0x2a(%rbp)
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
leaq -0x78(%rbp), %rsi
callq 0x49570
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jge 0x49078
movq 0xcff78(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa62cf(%rip), %rdi     # 0xef339
callq 0x8430
movb $-0x1, -0x1(%rbp)
jmp 0x49564
movw $0x1, -0x2c(%rbp)
movq $0x0, -0x48(%rbp)
cmpq $0x4, -0x48(%rbp)
jae 0x49106
movq $0x0, -0x38(%rbp)
cmpq $0x4, -0x38(%rbp)
jae 0x490e8
movl -0x7c(%rbp), %eax
movzwl -0x2c(%rbp), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x490be
movb -0x29(%rbp), %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
addq -0x38(%rbp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x490d0
movb -0x2a(%rbp), %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
addq -0x38(%rbp), %rcx
movb %dl, (%rax,%rcx)
movzwl -0x2c(%rbp), %eax
shll %eax
movw %ax, -0x2c(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x49095
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x49086
jmp 0x4951a
movq $0x0, -0x48(%rbp)
cmpq $0x4, -0x48(%rbp)
jae 0x49214
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
addq $0x30, %rdi
leaq -0x78(%rbp), %rsi
callq 0x49570
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jge 0x4915a
movq 0xcfe96(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6237(%rip), %rdi     # 0xef383
callq 0x8430
movb $-0x1, -0x1(%rbp)
jmp 0x49564
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movb %dl, 0x3(%rax,%rcx)
movl -0x7c(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movb %dl, 0x2(%rax,%rcx)
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
addq $0x30, %rdi
leaq -0x78(%rbp), %rsi
callq 0x49570
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jge 0x491c5
movq 0xcfe2b(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa61cc(%rip), %rdi     # 0xef383
callq 0x8430
movb $-0x1, -0x1(%rbp)
jmp 0x49564
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx)
movl -0x7c(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x49113
jmp 0x4951a
jmp 0x4951a
movq -0x28(%rbp), %rdi
addq -0x60(%rbp), %rdi
movzbl -0x7f(%rbp), %eax
movl $0x4, %edx
movzbl %al, %esi
callq 0x8410
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rdi
addq -0x60(%rbp), %rdi
movzbl -0x7f(%rbp), %eax
movl $0x4, %edx
movzbl %al, %esi
callq 0x8410
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rdi
addq -0x60(%rbp), %rdi
movzbl -0x7f(%rbp), %eax
movl $0x4, %edx
movzbl %al, %esi
callq 0x8410
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rdi
addq -0x60(%rbp), %rdi
movzbl -0x7f(%rbp), %eax
movl $0x4, %edx
movzbl %al, %esi
callq 0x8410
jmp 0x4951a
movq $0x0, -0x48(%rbp)
cmpq $0x2, -0x48(%rbp)
jae 0x4938a
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
addq $0x30, %rdi
leaq -0x78(%rbp), %rsi
callq 0x49570
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jge 0x49306
movq 0xcfcea(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa608b(%rip), %rdi     # 0xef383
callq 0x8430
movb $-0x1, -0x1(%rbp)
jmp 0x49564
movq $0x0, -0x38(%rbp)
cmpq $0x2, -0x38(%rbp)
jae 0x49377
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rax
addq $0x2, %rax
addq %rax, %rdi
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movl $0x2, %edx
movzbl %al, %esi
callq 0x8410
movq -0x28(%rbp), %rdi
addq -0x60(%rbp), %rdi
movl -0x7c(%rbp), %eax
andl $0xff, %eax
movl $0x2, %edx
movzbl %al, %esi
callq 0x8410
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x4930e
jmp 0x49379
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x492bf
jmp 0x4951a
movq $0x0, -0x48(%rbp)
cmpq $0x2, -0x48(%rbp)
jae 0x49518
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
addq $0x30, %rdi
leaq -0x78(%rbp), %rsi
callq 0x49570
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jge 0x493de
movq 0xcfc12(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa5fb3(%rip), %rdi     # 0xef383
callq 0x8430
movb $-0x1, -0x1(%rbp)
jmp 0x49564
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movb %dl, 0x3(%rax,%rcx)
movl -0x7c(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movb %dl, 0x2(%rax,%rcx)
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rsi
addq 0x8(%rsi), %rcx
movb %dl, 0x3(%rax,%rcx)
movl -0x7c(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rsi
addq 0x8(%rsi), %rcx
movb %dl, 0x2(%rax,%rcx)
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
addq $0x30, %rdi
leaq -0x78(%rbp), %rsi
callq 0x49570
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jge 0x49488
movq 0xcfb68(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa5f09(%rip), %rdi     # 0xef383
callq 0x8430
movb $-0x1, -0x1(%rbp)
jmp 0x49564
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx)
movl -0x7c(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x7c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rsi
addq 0x8(%rsi), %rcx
movb %dl, 0x1(%rax,%rcx)
movl -0x7c(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rsi
addq 0x8(%rsi), %rcx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x49397
jmp 0x4951a
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jb 0x49548
movq $0x0, -0x58(%rbp)
movq -0x50(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x50(%rbp)
jmp 0x4954a
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x48f61
jmp 0x48e9e
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq