// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/libsmacker/smacker.c
// Function: smk_huff16_build
static int smk_huff16_build(struct smk_huff16_t * const t, struct smk_bit_t * const bs, const unsigned int alloc_size)
{
	struct smk_huff8_t low8, hi8;
	size_t limit;
	int value, i, bit;
	/* null check */
	assert(t);
	assert(bs);

	/* Smacker huff trees begin with a set-bit. */
	if ((bit = smk_bs_read_1(bs)) < 0) {
		fputs("libsmacker::smk_huff16_build() - ERROR: initial get_bit returned -1\n", stderr);
		return 0;
	}

	t->size = 0;

	/* First bit indicates whether a tree is present or not. */
	/*  Very small or audio-only files may have no tree. */
	if (bit) {
		/* build low-8-bits tree */
		if (! smk_huff8_build(&low8, bs)) {
			fputs("libsmacker::smk_huff16_build() - ERROR: failed to build LOW tree\n", stderr);
			return 0;
		}

		/* build hi-8-bits tree */
		if (! smk_huff8_build(&hi8, bs)) {
			fputs("libsmacker::smk_huff16_build() - ERROR: failed to build HIGH tree\n", stderr);
			return 0;
		}

		/* Init the escape code cache. */
		for (i = 0; i < 3; i ++) {
			if ((value = smk_bs_read_8(bs)) < 0) {
				fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: get LOW value for cache %d returned -1\n", i);
				return 0;
			}

			t->cache[i] = value;

			/* now read HIGH value */
			if ((value = smk_bs_read_8(bs)) < 0) {
				fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: get HIGH value for cache %d returned -1\n", i);
				return 0;
			}

			t->cache[i] |= (value << 8);
		}

		/* Everything looks OK so far. Time to malloc structure. */
		if (alloc_size < 12 || alloc_size % 4) {
			fprintf(stderr, "libsmacker::smk_huff16_build() - ERROR: illegal value %u for alloc_size\n", alloc_size);
			return 0;
		}

		limit = (alloc_size - 12) / 4;

		if ((t->tree = malloc(limit * sizeof(unsigned int))) == NULL) {
			perror("libsmacker::smk_huff16_build() - ERROR: failed to malloc() huff16 tree");
			return 0;
		}

		/* Finally, call recursive function to retrieve the Bigtree. */
		if (! _smk_huff16_build_rec(t, bs, &low8, &hi8, limit)) {
			fputs("libsmacker::smk_huff16_build() - ERROR: failed to build huff16 tree\n", stderr);
			free(t->tree);
			t->tree = NULL;
			return 0;
		}

		/* check that we completely filled the tree */
		if (limit != t->size) {
			fputs("libsmacker::smk_huff16_build() - ERROR: failed to completely decode huff16 tree\n", stderr);
			free(t->tree);
			t->tree = NULL;
			return 0;
		}
	} else {
		if ((t->tree = malloc(sizeof(unsigned int))) == NULL) {
			perror("libsmacker::smk_huff16_build() - ERROR: failed to malloc() huff16 tree");
			return 0;
		}

		t->tree[0] = 0;
		//t->cache[0] = t->cache[1] = t->cache[2] = 0;
	}

	/* Check final end tag. */
	if ((bit = smk_bs_read_1(bs)) < 0) {
		fputs("libsmacker::smk_huff16_build() - ERROR: final get_bit returned -1\n", stderr);
		free(t->tree);
		t->tree = NULL;
		return 0;
	}

	/* a 0 is expected here, a 1 generally indicates a problem! */
	if (bit) {
		fputs("libsmacker::smk_huff16_build() - ERROR: final get_bit returned 1\n", stderr);
		free(t->tree);
		t->tree = NULL;
		return 0;
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x850, %rsp            # imm = 0x850
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x477cf
jmp 0x477ee
leaq 0x934df(%rip), %rdi     # 0xdacb5
leaq 0xa5ae6(%rip), %rsi     # 0xed2c3
movl $0x158, %edx            # imm = 0x158
leaq 0xa6a5b(%rip), %rcx     # 0xee244
callq 0x8230
cmpq $0x0, -0x18(%rbp)
je 0x477f7
jmp 0x47816
leaq 0xa7cdc(%rip), %rdi     # 0xef4da
leaq 0xa5abe(%rip), %rsi     # 0xed2c3
movl $0x159, %edx            # imm = 0x159
leaq 0xa6a33(%rip), %rcx     # 0xee244
callq 0x8230
movq -0x18(%rbp), %rdi
callq 0x47bc0
movl %eax, -0x844(%rbp)
cmpl $0x0, %eax
jge 0x4784c
movq 0xd17a7(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a67(%rip), %rdi     # 0xee2a2
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
cmpl $0x0, -0x844(%rbp)
je 0x47ade
movq -0x18(%rbp), %rsi
leaq -0x428(%rbp), %rdi
callq 0x47c80
cmpl $0x0, %eax
jne 0x4789c
movq 0xd1757(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a5c(%rip), %rdi     # 0xee2e7
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movq -0x18(%rbp), %rsi
leaq -0x830(%rbp), %rdi
callq 0x47c80
cmpl $0x0, %eax
jne 0x478d3
movq 0xd1720(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a67(%rip), %rdi     # 0xee329
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movl $0x0, -0x840(%rbp)
cmpl $0x3, -0x840(%rbp)
jge 0x479b3
movq -0x18(%rbp), %rdi
callq 0x47dd0
movl %eax, -0x83c(%rbp)
cmpl $0x0, %eax
jge 0x47928
movq 0xd16d3(%rip), %rax     # 0x118fd8
movq (%rax), %rdi
movl -0x840(%rbp), %edx
leaq 0xa6a57(%rip), %rsi     # 0xee36c
movb $0x0, %al
callq 0x8280
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movl -0x83c(%rbp), %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0x840(%rbp), %rcx
movw %dx, 0x10(%rax,%rcx,2)
movq -0x18(%rbp), %rdi
callq 0x47dd0
movl %eax, -0x83c(%rbp)
cmpl $0x0, %eax
jge 0x4797f
movq 0xd167c(%rip), %rax     # 0x118fd8
movq (%rax), %rdi
movl -0x840(%rbp), %edx
leaq 0xa6a50(%rip), %rsi     # 0xee3bc
movb $0x0, %al
callq 0x8280
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movl -0x83c(%rbp), %esi
shll $0x8, %esi
movq -0x10(%rbp), %rax
movslq -0x840(%rbp), %rcx
movzwl 0x10(%rax,%rcx,2), %edx
orl %esi, %edx
movw %dx, 0x10(%rax,%rcx,2)
movl -0x840(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x840(%rbp)
jmp 0x478dd
cmpl $0xc, -0x1c(%rbp)
jb 0x479c4
movl -0x1c(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x479eb
movq 0xd160d(%rip), %rax     # 0x118fd8
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0xa6a35(%rip), %rsi     # 0xee40d
movb $0x0, %al
callq 0x8280
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movl -0x1c(%rbp), %eax
subl $0xc, %eax
shrl $0x2, %eax
movl %eax, %eax
movq %rax, -0x838(%rbp)
movq -0x838(%rbp), %rdi
shlq $0x2, %rdi
callq 0x8ca0
movq -0x10(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x47a32
leaq 0xa6a35(%rip), %rdi     # 0xee456
callq 0x83d0
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x838(%rbp), %r8
leaq -0x428(%rbp), %rdx
leaq -0x830(%rbp), %rcx
callq 0x47ed0
cmpl $0x0, %eax
jne 0x47a92
movq 0xd1578(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a33(%rip), %rdi     # 0xee49d
callq 0x8430
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x8260
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movq -0x838(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
je 0x47adc
movq 0xd152e(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a2e(%rip), %rdi     # 0xee4e2
callq 0x8430
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x8260
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x47baa
jmp 0x47b1a
movl $0x4, %edi
callq 0x8ca0
movq -0x10(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x47b0d
leaq 0xa695a(%rip), %rdi     # 0xee456
callq 0x83d0
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rdi
callq 0x47bc0
movl %eax, -0x844(%rbp)
cmpl $0x0, %eax
jge 0x47b64
movq 0xd14a3(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa69f4(%rip), %rdi     # 0xee533
callq 0x8430
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x8260
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x47baa
cmpl $0x0, -0x844(%rbp)
je 0x47ba3
movq 0xd1464(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa69f8(%rip), %rdi     # 0xee576
callq 0x8430
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x8260
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x47baa
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x850, %rsp            # imm = 0x850
popq %rbp
retq
nopw %cs:(%rax,%rax)