// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/libsmacker/smacker.c
// Function: smk_render_audio
static char smk_render_audio(struct smk_audio_t * s, unsigned char * p, unsigned long size)
{
	unsigned int j, k;
	unsigned char * t = s->buffer;
	struct smk_bit_t bs;
	char bit;
	short unpack, unpack2;
	/* used for audio decoding */
	struct smk_huff8_t aud_tree[4];
	/* null check */
	assert(s);
	assert(p);

	if (!s->compress) {
		/* Raw PCM data, update buffer size and perform copy */
		s->buffer_size = size;
		memcpy(t, p, size);
	} else if (s->compress == 1) {
		/* SMACKER DPCM compression */
		/* need at least 4 bytes to process */
		if (size < 4) {
			fputs("libsmacker::smk_render_audio() - ERROR: need 4 bytes to get unpacked output buffer size.\n", stderr);
			goto error;
		}

		/* chunk is compressed (huff-compressed dpcm), retrieve unpacked buffer size */
		s->buffer_size = ((unsigned int) p[3] << 24) |
			((unsigned int) p[2] << 16) |
			((unsigned int) p[1] << 8) |
			((unsigned int) p[0]);
		p += 4;
		size -= 4;
		/* Compressed audio: must unpack here */
		/*  Set up a bitstream */
		smk_bs_init(&bs, p, size);
		bit = smk_bs_read_1(&bs);

		if (!bit) {
			fputs("libsmacker::smk_render_audio - ERROR: initial get_bit returned 0\n", stderr);
			goto error;
		}

		bit = smk_bs_read_1(&bs);

		if (s->channels != (bit == 1 ? 2 : 1))
			fputs("libsmacker::smk_render - ERROR: mono/stereo mismatch\n", stderr);

		bit = smk_bs_read_1(&bs);

		if (s->bitdepth != (bit == 1 ? 16 : 8))
			fputs("libsmacker::smk_render - ERROR: 8-/16-bit mismatch\n", stderr);

		/* build the trees */
		smk_huff8_build(&aud_tree[0], &bs);
		j = 1;
		k = 1;

		if (s->bitdepth == 16) {
			smk_huff8_build(&aud_tree[1], &bs);
			k = 2;
		}

		if (s->channels == 2) {
			smk_huff8_build(&aud_tree[2], &bs);
			j = 2;
			k = 2;

			if (s->bitdepth == 16) {
				smk_huff8_build(&aud_tree[3], &bs);
				k = 4;
			}
		}

		/* read initial sound level */
		if (s->channels == 2) {
			unpack = smk_bs_read_8(&bs);

			if (s->bitdepth == 16) {
				((short *)t)[1] = smk_bs_read_8(&bs);
				((short *)t)[1] |= (unpack << 8);
			} else
				((unsigned char *)t)[1] = (unsigned char)unpack;
		}

		unpack = smk_bs_read_8(&bs);

		if (s->bitdepth == 16) {
			((short *)t)[0] = smk_bs_read_8(&bs);
			((short *)t)[0] |= (unpack << 8);
		} else
			((unsigned char *)t)[0] = (unsigned char)unpack;

		/* All set: let's read some DATA! */
		while (k < s->buffer_size) {
			if (s->bitdepth == 8) {
				unpack = smk_huff8_lookup(&aud_tree[0], &bs);
				((unsigned char *)t)[j] = (char)unpack + ((unsigned char *)t)[j - s->channels];
				j ++;
				k++;
			} else {
				unpack = smk_huff8_lookup(&aud_tree[0], &bs);
				unpack2 = smk_huff8_lookup(&aud_tree[1], &bs);
				((short *)t)[j] = (short)(unpack | (unpack2 << 8)) + ((short *)t)[j - s->channels];
				j ++;
				k += 2;
			}

			if (s->channels == 2) {
				if (s->bitdepth == 8) {
					unpack = smk_huff8_lookup(&aud_tree[2], &bs);
					((unsigned char *)t)[j] = (char)unpack + ((unsigned char *)t)[j - 2];
					j ++;
					k++;
				} else {
					unpack = smk_huff8_lookup(&aud_tree[2], &bs);
					unpack2 = smk_huff8_lookup(&aud_tree[3], &bs);
					((short *)t)[j] = (short)(unpack | (unpack2 << 8)) + ((short *)t)[j - 2];
					j ++;
					k += 2;
				}
			}
		}
	}

	return 0;
error:
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1070, %rsp           # imm = 0x1070
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x488ac
jmp 0x488cb
leaq 0xa6e9c(%rip), %rdi     # 0xef74f
leaq 0xa4a09(%rip), %rsi     # 0xed2c3
movl $0x5c8, %edx            # imm = 0x5C8
leaq 0xa6842(%rip), %rcx     # 0xef108
callq 0x8230
cmpq $0x0, -0x18(%rbp)
je 0x488d4
jmp 0x488f3
leaq 0x911ce(%rip), %rdi     # 0xd9aa9
leaq 0xa49e1(%rip), %rsi     # 0xed2c3
movl $0x5c9, %edx            # imm = 0x5C9
leaq 0xa681a(%rip), %rcx     # 0xef108
callq 0x8230
movq -0x10(%rbp), %rax
cmpb $0x0, 0x18(%rax)
jne 0x4891f
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x80e0
jmp 0x48d9a
movq -0x10(%rbp), %rax
movzbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x48d98
cmpq $0x4, -0x20(%rbp)
jae 0x48952
movq 0xd069a(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa680c(%rip), %rdi     # 0xef154
callq 0x8430
jmp 0x48da0
movq -0x18(%rbp), %rax
movzbl 0x3(%rax), %eax
shll $0x18, %eax
movq -0x18(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x18(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x18(%rbp), %rcx
movzbl (%rcx), %ecx
orl %ecx, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
subq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x47720
leaq -0x48(%rbp), %rdi
callq 0x47bc0
movb %al, -0x49(%rbp)
cmpb $0x0, -0x49(%rbp)
jne 0x489e2
movq 0xd060a(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa67d6(%rip), %rdi     # 0xef1ae
callq 0x8430
jmp 0x48da0
leaq -0x48(%rbp), %rdi
callq 0x47bc0
movb %al, -0x49(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
movsbl -0x49(%rbp), %esi
movl $0x1, %ecx
movl $0x2, %edx
cmpl $0x1, %esi
cmovel %edx, %ecx
cmpl %ecx, %eax
je 0x48a24
movq 0xd05c3(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa67d1(%rip), %rdi     # 0xef1f0
callq 0x8430
leaq -0x48(%rbp), %rdi
callq 0x47bc0
movb %al, -0x49(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
movsbl -0x49(%rbp), %esi
movl $0x8, %ecx
movl $0x10, %edx
cmpl $0x1, %esi
cmovel %edx, %ecx
cmpl %ecx, %eax
je 0x48a66
movq 0xd0581(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa67c5(%rip), %rdi     # 0xef226
callq 0x8430
leaq -0x1070(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x47c80
movl $0x1, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x10, %eax
jne 0x48aaf
leaq -0x1070(%rbp), %rdi
addq $0x408, %rdi            # imm = 0x408
leaq -0x48(%rbp), %rsi
callq 0x47c80
movl $0x2, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x2, %eax
jne 0x48b0e
leaq -0x1070(%rbp), %rdi
addq $0x810, %rdi            # imm = 0x810
leaq -0x48(%rbp), %rsi
callq 0x47c80
movl $0x2, -0x24(%rbp)
movl $0x2, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x10, %eax
jne 0x48b0c
leaq -0x1070(%rbp), %rdi
addq $0xc18, %rdi            # imm = 0xC18
leaq -0x48(%rbp), %rsi
callq 0x47c80
movl $0x4, -0x28(%rbp)
jmp 0x48b0e
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x2, %eax
jne 0x48b6f
leaq -0x48(%rbp), %rdi
callq 0x47dd0
movw %ax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x10, %eax
jne 0x48b60
leaq -0x48(%rbp), %rdi
callq 0x47dd0
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, 0x2(%rax)
movswl -0x4c(%rbp), %edx
shll $0x8, %edx
movq -0x30(%rbp), %rax
movswl 0x2(%rax), %ecx
orl %edx, %ecx
movw %cx, 0x2(%rax)
jmp 0x48b6d
movw -0x4c(%rbp), %ax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x1(%rax)
jmp 0x48b6f
leaq -0x48(%rbp), %rdi
callq 0x47dd0
movw %ax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x10, %eax
jne 0x48bb1
leaq -0x48(%rbp), %rdi
callq 0x47dd0
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, (%rax)
movswl -0x4c(%rbp), %edx
shll $0x8, %edx
movq -0x30(%rbp), %rax
movswl (%rax), %ecx
orl %edx, %ecx
movw %cx, (%rax)
jmp 0x48bbd
movw -0x4c(%rbp), %ax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
jmp 0x48bbf
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jae 0x48d96
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x8, %eax
jne 0x48c31
leaq -0x1070(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x483f0
movw %ax, -0x4c(%rbp)
movw -0x4c(%rbp), %ax
movsbl %al, %eax
movq -0x30(%rbp), %rcx
movl -0x24(%rbp), %edx
movq -0x10(%rbp), %rsi
movzbl 0x2(%rsi), %esi
subl %esi, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
addl %ecx, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x48ca7
leaq -0x1070(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x483f0
movw %ax, -0x4c(%rbp)
leaq -0x1070(%rbp), %rdi
addq $0x408, %rdi            # imm = 0x408
leaq -0x48(%rbp), %rsi
callq 0x483f0
movw %ax, -0x4e(%rbp)
movswl -0x4c(%rbp), %eax
movswl -0x4e(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
cwtl
movq -0x30(%rbp), %rcx
movl -0x24(%rbp), %edx
movq -0x10(%rbp), %rsi
movzbl 0x2(%rsi), %esi
subl %esi, %edx
movl %edx, %edx
movswl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
movw %ax, %dx
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
movw %dx, (%rax,%rcx,2)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x2, %eax
jne 0x48d91
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x8, %eax
jne 0x48d19
leaq -0x1070(%rbp), %rdi
addq $0x810, %rdi            # imm = 0x810
leaq -0x48(%rbp), %rsi
callq 0x483f0
movw %ax, -0x4c(%rbp)
movw -0x4c(%rbp), %ax
movsbl %al, %eax
movq -0x30(%rbp), %rcx
movl -0x24(%rbp), %edx
subl $0x2, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
addl %ecx, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x48d8f
leaq -0x1070(%rbp), %rdi
addq $0x810, %rdi            # imm = 0x810
leaq -0x48(%rbp), %rsi
callq 0x483f0
movw %ax, -0x4c(%rbp)
leaq -0x1070(%rbp), %rdi
addq $0xc18, %rdi            # imm = 0xC18
leaq -0x48(%rbp), %rsi
callq 0x483f0
movw %ax, -0x4e(%rbp)
movswl -0x4c(%rbp), %eax
movswl -0x4e(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
cwtl
movq -0x30(%rbp), %rcx
movl -0x24(%rbp), %edx
subl $0x2, %edx
movl %edx, %edx
movswl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
movw %ax, %dx
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
movw %dx, (%rax,%rcx,2)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x28(%rbp)
jmp 0x48d91
jmp 0x48bbf
jmp 0x48d98
jmp 0x48d9a
movb $0x0, -0x1(%rbp)
jmp 0x48da4
movb $-0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x1070, %rsp           # imm = 0x1070
popq %rbp
retq