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

	/* Make sure we aren't running out of bounds */
	if (t->size >= limit) {
		fputs("libsmacker::_smk_huff16_build_rec() - ERROR: size exceeded\n", stderr);
		return 0;
	}

	/* Read the first bit */
	if ((bit = smk_bs_read_1(bs)) < 0) {
		fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get_bit returned -1\n", stderr);
		return 0;
	}

	if (bit) {
		/* See tree-in-array explanation for HUFF8 above */
		/* track the current index */
		value = t->size ++;

		/* go build the left branch */
		if (! _smk_huff16_build_rec(t, bs, low8, hi8, limit)) {
			fputs("libsmacker::_smk_huff16_build_rec() - ERROR: failed to build left sub-tree\n", stderr);
			return 0;
		}

		/* now go back to our current location, and
			mark our location as a "jump" */
		t->tree[value] = SMK_HUFF16_BRANCH | t->size;

		/* continue building the right side */
		if (! _smk_huff16_build_rec(t, bs, low8, hi8, limit)) {
			fputs("libsmacker::_smk_huff16_build_rec() - ERROR: failed to build right sub-tree\n", stderr);
			return 0;
		}
	} else {
		/* Bit unset signifies a Leaf node. */
		/* Attempt to read LOW value */
		if ((value = smk_huff8_lookup(low8, bs)) < 0) {
			fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get LOW value returned -1\n", stderr);
			return 0;
		}

		t->tree[t->size] = value;

		/* now read HIGH value */
		if ((value = smk_huff8_lookup(hi8, bs)) < 0) {
			fputs("libsmacker::_smk_huff16_build_rec() - ERROR: get HIGH value returned -1\n", stderr);
			return 0;
		}

		/* Looks OK: we got low and hi values. Return a new LEAF */
		t->tree[t->size] |= (value << 8);

		/* Last: when building the tree, some Values may correspond to cache positions.
			Identify these values and set the Escape code byte accordingly. */
		if (t->tree[t->size] == t->cache[0])
			t->tree[t->size] = SMK_HUFF16_CACHE;
		else if (t->tree[t->size] == t->cache[1])
			t->tree[t->size] = SMK_HUFF16_CACHE | 1;
		else if (t->tree[t->size] == t->cache[2])
			t->tree[t->size] = SMK_HUFF16_CACHE | 2;

		t->size ++;
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x47ef5
jmp 0x47f14
leaq 0x92db9(%rip), %rdi     # 0xdacb5
leaq 0xa53c0(%rip), %rsi     # 0xed2c3
movl $0x10a, %edx            # imm = 0x10A
leaq 0xa6a5e(%rip), %rcx     # 0xee96d
callq 0x8230
cmpq $0x0, -0x18(%rbp)
je 0x47f1d
jmp 0x47f3c
leaq 0xa75b6(%rip), %rdi     # 0xef4da
leaq 0xa5398(%rip), %rsi     # 0xed2c3
movl $0x10b, %edx            # imm = 0x10B
leaq 0xa6a36(%rip), %rcx     # 0xee96d
callq 0x8230
cmpq $0x0, -0x20(%rbp)
je 0x47f45
jmp 0x47f64
leaq 0xa6ac0(%rip), %rdi     # 0xeea0c
leaq 0xa5370(%rip), %rsi     # 0xed2c3
movl $0x10c, %edx            # imm = 0x10C
leaq 0xa6a0e(%rip), %rcx     # 0xee96d
callq 0x8230
cmpq $0x0, -0x28(%rbp)
je 0x47f6d
jmp 0x47f8c
leaq 0xa6a9d(%rip), %rdi     # 0xeea11
leaq 0xa5348(%rip), %rsi     # 0xed2c3
movl $0x10d, %edx            # imm = 0x10D
leaq 0xa69e6(%rip), %rcx     # 0xee96d
callq 0x8230
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x30(%rbp), %rax
jb 0x47fbc
movq 0xd1037(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a6a(%rip), %rdi     # 0xeea15
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x4820f
movq -0x18(%rbp), %rdi
callq 0x47bc0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jge 0x47fef
movq 0xd1004(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a73(%rip), %rdi     # 0xeea51
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x4820f
cmpl $0x0, -0x34(%rbp)
je 0x480b6
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x8(%rcx)
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x47ed0
cmpl $0x0, %eax
jne 0x4804f
movq 0xd0fa4(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a55(%rip), %rdi     # 0xeea93
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x4820f
movq -0x10(%rbp), %rcx
movabsq $0x80000000, %rax       # imm = 0x80000000
orq 0x8(%rcx), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x47ed0
cmpl $0x0, %eax
jne 0x480b1
movq 0xd0f42(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a3f(%rip), %rdi     # 0xeeadf
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x4820f
jmp 0x48208
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x483f0
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
jge 0x480ed
movq 0xd0f06(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a50(%rip), %rdi     # 0xeeb2c
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x4820f
movl -0x38(%rbp), %edx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x483f0
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
jge 0x48139
movq 0xd0eba(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6a4c(%rip), %rdi     # 0xeeb74
callq 0x8430
movl $0x0, -0x4(%rbp)
jmp 0x4820f
movl -0x38(%rbp), %edx
shll $0x8, %edx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movzwl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jne 0x4818a
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl $0x40000000, (%rax,%rcx,4) # imm = 0x40000000
jmp 0x481f8
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
cmpl %ecx, %eax
jne 0x481c0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl $0x40000001, (%rax,%rcx,4) # imm = 0x40000001
jmp 0x481f6
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movzwl 0x14(%rcx), %ecx
cmpl %ecx, %eax
jne 0x481f4
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl $0x40000002, (%rax,%rcx,4) # imm = 0x40000002
jmp 0x481f6
jmp 0x481f8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)