// File: /workspace/github_c_makefile_star2_O2/lmcilroy[P]lzm/lzmencode.c
// Function: lzm_encode_fast
static unsigned int
lzm_encode_fast(
    const struct lzm_state * const state,
    const unsigned char * const buffer_in,
    const unsigned int size_in,
    unsigned char * const buffer_out,
    unsigned int * const size_out)
{
	const unsigned char * const end = buffer_in + size_in;
	const unsigned char * const match_end = end - 7;
	const unsigned char * const scan_end = match_end - 7;
	const unsigned char * const out_limit = buffer_out + *size_out;
	const unsigned char *lit_start = buffer_in;
	const unsigned char *curr_in = buffer_in;
	const unsigned char *next_curr;
	const unsigned char *last;
	unsigned char *curr_out = buffer_out;
	struct ht_entry *last_htp;
	unsigned long int token;
	unsigned long int next_token;
	unsigned int last_token;
	unsigned int len;
	unsigned int off;
	unsigned int misses = (1 << MISS_ORDER) + 1;
	unsigned int hashval;
	unsigned int next_hashval;

	lzm_reset(state, buffer_in);

	token = readmem64(curr_in);
	hashval = hash_fast(token);
	next_token = readmem64(curr_in + 1);
	next_hashval = hash_fast(next_token);
	last_htp = &state->last_ht[hashval];
	last_htp->index = curr_in - buffer_in;
	last_htp->token = token;
	curr_in++;

	while (likely(curr_in < scan_end)) {
		token = next_token;
		hashval = next_hashval;
		next_curr = curr_in + (misses >> MISS_ORDER);
		next_token = readmem64(next_curr);
		next_hashval = hash_fast(next_token);
		last_htp = &state->last_ht[hashval];
		last = last_htp->index + buffer_in;
		last_token = last_htp->token;
		last_htp->index = curr_in - buffer_in;
		last_htp->token = token;

		if ((unsigned int)token != last_token ||
		    (curr_in - last) & ~MAX_OFFSET_MASK) {
			misses++;
			curr_in = next_curr;
			continue;
		}
		misses = (1 << MISS_ORDER) + 1;

		len = MIN_MATCH;
		len += matchlen(curr_in + len, last + len, match_end);
		off = matchlen_rev(curr_in, last, lit_start, buffer_in);
		curr_in -= off;
		last -= off;
		len += off;

		curr_out = output_match(curr_out, lit_start,
		    curr_in - lit_start, curr_in - last, len, out_limit);
		if (unlikely(curr_out == NULL))
			return EOVERFLOW;

		curr_in += len;
		lit_start = curr_in;

		token = readmem64(curr_in - 2);
		hashval = hash_fast(token);
		next_token = readmem64(curr_in);
		next_hashval = hash_fast(next_token);
		last_htp = &state->last_ht[hashval];
		last_htp->index = curr_in - 2 - buffer_in;
		last_htp->token = token;
	}

	curr_out = output_literals(curr_out, lit_start, end - lit_start,
	    out_limit);
	if (curr_out == NULL)
		return EOVERFLOW;

	*size_out = curr_out - buffer_out;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rsi, %r13
movq %rdi, %r12
movabsq $-0x53a9a353cb000000, %r15 # imm = 0xAC565CAC35000000
movl %edx, %eax
leaq (%rsi,%rax), %rcx
movq %rcx, 0x30(%rsp)
leaq -0x7(%rsi,%rax), %rcx
movq %rcx, 0x40(%rsp)
leaq (%rsi,%rax), %r14
addq $-0xe, %r14
movq %r8, 0x28(%rsp)
movl (%r8), %eax
addq %rbx, %rax
movq %rax, 0x18(%rsp)
callq 0x3a52
movq (%r13), %rsi
movq 0x1(%r13), %rax
movq %rsi, %rdi
imulq %r15, %rdi
shrq $0x34, %rdi
leaq 0x1(%r13), %rdx
movq %r12, 0x38(%rsp)
movq (%r12), %rcx
leaq (%rcx,%rdi,8), %rdi
andl $0x0, (%rdi)
movq %rbx, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movl %esi, 0x4(%rdi)
pushq $0x41
popq %rsi
cmpq %r14, %rdx
jae 0x32da
movq %rdx, %rbx
movq %rax, %r8
imulq %r15, %r8
shrq $0x34, %r8
movl %esi, %edi
shrl $0x6, %edi
addq %rdi, %rdx
movq (%rbx,%rdi), %rdi
movl (%rcx,%r8,8), %ebp
movl 0x4(%rcx,%r8,8), %r9d
movl %ebx, %r10d
subl %r13d, %r10d
movl %r10d, (%rcx,%r8,8)
movl %eax, 0x4(%rcx,%r8,8)
incl %esi
cmpl %eax, %r9d
movq %rdi, %rax
jne 0x31e1
addq %r13, %rbp
movq %rbx, %r8
subq %rbp, %r8
movq %rdi, %rax
cmpq $0xfffffff, %r8         # imm = 0xFFFFFFF
ja 0x31e1
leaq 0x4(%rbx), %rdi
leaq 0x4(%rbp), %rsi
movq 0x40(%rsp), %rdx
callq 0x3a6f
movl %eax, %r12d
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
movq %r13, %rcx
callq 0x3bc6
movl %eax, %ecx
subq %rcx, %rbx
addl %eax, %r12d
addl $0x4, %r12d
movl %ebx, %edx
movq 0x8(%rsp), %rsi
subl %esi, %edx
subl %ebp, %eax
leal (%rax,%rbx), %ecx
movq 0x10(%rsp), %rdi
movl %r12d, %r8d
movq 0x18(%rsp), %r9
callq 0x3ce8
testq %rax, %rax
je 0x331b
movq %rax, 0x10(%rsp)
movl %r12d, %eax
leaq (%rbx,%rax), %rdx
leaq (%rbx,%rax), %r8
addq $-0x2, %r8
movq (%r8), %rsi
movq 0x2(%r8), %rax
movq %rsi, %rdi
imulq %r15, %rdi
shrq $0x34, %rdi
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
leaq (%rcx,%rdi,8), %rdi
subl %r13d, %r8d
movl %r8d, (%rdi)
movq %rdx, 0x8(%rsp)
jmp 0x31db
movq 0x30(%rsp), %rdx
movq 0x8(%rsp), %rsi
subl %esi, %edx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
callq 0x390b
testq %rax, %rax
pushq $0x4b
popq %rcx
je 0x330a
subl 0x20(%rsp), %eax
movq 0x28(%rsp), %rcx
movl %eax, (%rcx)
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x4b
popq %rcx
jmp 0x330a