// File: sbingner[P]kerneldec/lzfse/src/lzfse_encode_base.c
// Function: lzfse_encode_base
int lzfse_encode_base(lzfse_encoder_state *s) {
  lzfse_history_set *history_table = s->history_table;
  lzfse_history_set *hashLine = 0;
  lzfse_history_set newH;
  const lzfse_match NO_MATCH = {0};
  int ok = 1;

  memset(&newH, 0x00, sizeof(newH));

  // 8 byte padding at end of buffer
  s->src_encode_end = s->src_end - 8;
  for (; s->src_encode_i < s->src_encode_end; s->src_encode_i++) {
    lzfse_offset pos = s->src_encode_i; // pos >= 0

    // Load 4 byte value and get hash line
    uint32_t x = load4(s->src + pos);
    hashLine = history_table + hashX(x);
    lzfse_history_set h = *hashLine;

    // Prepare next hash line (component 0 is the most recent) to prepare new
    // entries (stored later)
    {
      newH.pos[0] = (int32_t)pos;
      for (int k = 0; k < LZFSE_ENCODE_HASH_WIDTH - 1; k++)
        newH.pos[k + 1] = h.pos[k];
      newH.value[0] = x;
      for (int k = 0; k < LZFSE_ENCODE_HASH_WIDTH - 1; k++)
        newH.value[k + 1] = h.value[k];
    }

    // Do not look for a match if we are still covered by a previous match
    if (pos < s->src_literal)
      goto END_POS;

    // Search best incoming match
    lzfse_match incoming = {.pos = pos, .ref = 0, .length = 0};

    // Check for matches.  We consider matches of length >= 4 only.
    for (int k = 0; k < LZFSE_ENCODE_HASH_WIDTH; k++) {
      uint32_t d = h.value[k] ^ x;
      if (d)
        continue; // no 4 byte match
      int32_t ref = h.pos[k];
      if (ref + LZFSE_ENCODE_MAX_D_VALUE < pos)
        continue; // too far

      const uint8_t *src_ref = s->src + ref;
      const uint8_t *src_pos = s->src + pos;
      uint32_t length = 4;
      uint32_t maxLength =
        (uint32_t)(s->src_end - pos - 8); // ensure we don't hit the end of SRC
      while (length < maxLength) {
        uint64_t d = load8(src_ref + length) ^ load8(src_pos + length);
        if (d == 0) {
          length += 8;
          continue;
        }

        length +=
            (__builtin_ctzll(d) >> 3); // ctzll must be called only with D != 0
        break;
      }
      if (length > incoming.length) {
        incoming.length = length;
        incoming.ref = ref;
      } // keep if longer
    }

    // No incoming match?
    if (incoming.length == 0) {
      // We may still want to emit some literals here, to not lag too far behind
      // the current search point, and avoid
      // ending up with a literal block not fitting in the state.
      lzfse_offset n_literals = pos - s->src_literal;
      // The threshold here should be larger than a couple of MAX_L_VALUE, and
      // much smaller than LITERALS_PER_BLOCK
      if (n_literals > 8 * LZFSE_ENCODE_MAX_L_VALUE) {
        // Here, we need to consume some literals. Emit pending match if there
        // is one
        if (s->pending.length > 0) {
          if (lzfse_backend_match(s, &s->pending) != LZFSE_STATUS_OK) {
            ok = 0;
            goto END;
          }
          s->pending = NO_MATCH;
        } else {
          // No pending match, emit a full LZFSE_ENCODE_MAX_L_VALUE block of
          // literals
          if (lzfse_backend_literals(s, LZFSE_ENCODE_MAX_L_VALUE) !=
              LZFSE_STATUS_OK) {
            ok = 0;
            goto END;
          }
        }
      }
      goto END_POS; // no incoming match
    }

    // Limit match length (it may still be expanded backwards, but this is
    // bounded by the limit on literals we tested before)
    if (incoming.length > LZFSE_ENCODE_MAX_MATCH_LENGTH) {
      incoming.length = LZFSE_ENCODE_MAX_MATCH_LENGTH;
    }

    // Expand backwards (since this is expensive, we do this for the best match
    // only)
    while (incoming.pos > s->src_literal && incoming.ref > 0 &&
           s->src[incoming.ref - 1] == s->src[incoming.pos - 1]) {
      incoming.pos--;
      incoming.ref--;
    }
    incoming.length += pos - incoming.pos; // update length after expansion

    // Match filtering heuristic (from LZVN). INCOMING is always defined here.

    // Incoming is 'good', emit incoming
    if (incoming.length >= LZFSE_ENCODE_GOOD_MATCH) {
      if (lzfse_backend_match(s, &incoming) != LZFSE_STATUS_OK) {
        ok = 0;
        goto END;
      }
      s->pending = NO_MATCH;
      goto END_POS;
    }

    // No pending, keep incoming
    if (s->pending.length == 0) {
      s->pending = incoming;
      goto END_POS;
    }

    // No overlap, emit pending, keep incoming
    if (s->pending.pos + s->pending.length <= incoming.pos) {
      if (lzfse_backend_match(s, &s->pending) != LZFSE_STATUS_OK) {
        ok = 0;
        goto END;
      }
      s->pending = incoming;
      goto END_POS;
    }

    // Overlap: emit longest
    if (incoming.length > s->pending.length) {
      if (lzfse_backend_match(s, &incoming) != LZFSE_STATUS_OK) {
        ok = 0;
        goto END;
      }
    } else {
      if (lzfse_backend_match(s, &s->pending) != LZFSE_STATUS_OK) {
        ok = 0;
        goto END;
      }
    }
    s->pending = NO_MATCH;

  END_POS:
    // We are done with this src_encode_i.
    // Update state now (s->pending has already been updated).
    *hashLine = newH;
  }

END:
  return ok ? LZFSE_STATUS_OK : LZFSE_STATUS_DST_FULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x27160, %rax          # imm = 0x27160
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3080
movl $0x1, -0x84(%rbp)
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3080
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
subq $0x8, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jge 0x4bbd
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x118(%rbp)
movl -0x94(%rbp), %edi
callq 0x4be0
movl %eax, %ecx
movq -0x118(%rbp), %rax
movl %ecx, %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0xb4(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0xac(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0xa4(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x9c(%rbp)
movq -0x90(%rbp), %rax
movl %eax, -0x68(%rbp)
movl $0x0, -0xb8(%rbp)
cmpl $0x3, -0xb8(%rbp)
jge 0x46a2
movslq -0xb8(%rbp), %rax
movl -0xb4(%rbp,%rax,4), %ecx
movl -0xb8(%rbp), %eax
addl $0x1, %eax
cltq
movl %ecx, -0x68(%rbp,%rax,4)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x466b
movl -0x94(%rbp), %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0xbc(%rbp)
cmpl $0x3, -0xbc(%rbp)
jge 0x46ec
movslq -0xbc(%rbp), %rax
movl -0xa4(%rbp,%rax,4), %ecx
movl -0xbc(%rbp), %eax
addl $0x1, %eax
cltq
movl %ecx, -0x58(%rbp,%rax,4)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x46b5
movq -0x90(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x4702
jmp 0x4b85
movq -0x90(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq $0x0, -0xd0(%rbp)
movl $0x0, -0xc8(%rbp)
movl $0x0, -0xdc(%rbp)
cmpl $0x4, -0xdc(%rbp)
jge 0x48b6
movslq -0xdc(%rbp), %rax
movl -0xa4(%rbp,%rax,4), %eax
xorl -0x94(%rbp), %eax
movl %eax, -0xe0(%rbp)
cmpl $0x0, -0xe0(%rbp)
je 0x4764
jmp 0x48a2
movslq -0xdc(%rbp), %rax
movl -0xb4(%rbp,%rax,4), %eax
movl %eax, -0xe4(%rbp)
movl -0xe4(%rbp), %eax
addl $0x3fffb, %eax          # imm = 0x3FFFB
cltq
cmpq -0x90(%rbp), %rax
jge 0x4793
jmp 0x48a2
movq -0x38(%rbp), %rax
movq (%rax), %rax
movslq -0xe4(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xf0(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
addq -0x90(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl $0x4, -0xfc(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x90(%rbp), %rax
subq $0x8, %rax
movl %eax, -0x100(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0x100(%rbp), %eax
jae 0x4878
movq -0xf0(%rbp), %rax
movl -0xfc(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq -0xf8(%rbp), %rcx
movl -0xfc(%rbp), %edx
addq %rdx, %rcx
movq %rcx, -0x18(%rbp)
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, -0x20(%rbp)
xorq -0x20(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, -0x108(%rbp)
jne 0x485d
movl -0xfc(%rbp), %eax
addl $0x8, %eax
movl %eax, -0xfc(%rbp)
jmp 0x47e3
movq -0x108(%rbp), %rax
tzcntq %rax, %rax
sarl $0x3, %eax
addl -0xfc(%rbp), %eax
movl %eax, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jbe 0x48a0
movl -0xfc(%rbp), %eax
movl %eax, -0xc8(%rbp)
movslq -0xe4(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x48a2
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x472f
cmpl $0x0, -0xc8(%rbp)
jne 0x495e
movq -0x90(%rbp), %rax
movq -0x38(%rbp), %rcx
subq 0x10(%rcx), %rax
movq %rax, -0x110(%rbp)
cmpq $0x9d8, -0x110(%rbp) # imm = 0x9D8
jle 0x4959
movq -0x38(%rbp), %rax
cmpl $0x0, 0x50(%rax)
jbe 0x4933
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x40, %rsi
callq 0x4c00
cmpl $0x0, %eax
je 0x4915
movl $0x0, -0x84(%rbp)
jmp 0x4bbf
movq -0x38(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, 0x40(%rax)
movq -0x78(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x50(%rax)
jmp 0x4957
movq -0x38(%rbp), %rdi
movl $0x13b, %esi            # imm = 0x13B
callq 0x4c60
cmpl $0x0, %eax
je 0x4955
movl $0x0, -0x84(%rbp)
jmp 0x4bbf
jmp 0x4957
jmp 0x4959
jmp 0x4b85
cmpl $0x3997c, -0xc8(%rbp)   # imm = 0x3997C
jbe 0x4974
movl $0x3997c, -0xc8(%rbp)   # imm = 0x3997C
jmp 0x4976
movq -0xd8(%rbp), %rcx
movq -0x38(%rbp), %rdx
xorl %eax, %eax
cmpq 0x10(%rdx), %rcx
movb %al, -0x119(%rbp)
jle 0x49d8
xorl %eax, %eax
cmpq $0x0, -0xd0(%rbp)
movb %al, -0x119(%rbp)
jle 0x49d8
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0xd0(%rbp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0xd8(%rbp), %rdx
subq $0x1, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x119(%rbp)
movb -0x119(%rbp), %al
testb $0x1, %al
jne 0x49e4
jmp 0x4a0d
movq -0xd8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x4976
movq -0x90(%rbp), %rcx
subq -0xd8(%rbp), %rcx
movl -0xc8(%rbp), %eax
addq %rcx, %rax
movl %eax, -0xc8(%rbp)
cmpl $0x28, -0xc8(%rbp)
jb 0x4a78
movq -0x38(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x4c00
cmpl $0x0, %eax
je 0x4a57
movl $0x0, -0x84(%rbp)
jmp 0x4bbf
movq -0x38(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, 0x40(%rax)
movq -0x78(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x50(%rax)
jmp 0x4b85
movq -0x38(%rbp), %rax
cmpl $0x0, 0x50(%rax)
jne 0x4aac
movq -0x38(%rbp), %rax
movq -0xd8(%rbp), %rcx
movq %rcx, 0x40(%rax)
movq -0xd0(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0xc8(%rbp), %rcx
movq %rcx, 0x50(%rax)
jmp 0x4b85
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
movl 0x50(%rcx), %ecx
addq %rcx, %rax
cmpq -0xd8(%rbp), %rax
jg 0x4b13
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x40, %rsi
callq 0x4c00
cmpl $0x0, %eax
je 0x4aec
movl $0x0, -0x84(%rbp)
jmp 0x4bbf
movq -0x38(%rbp), %rax
movq -0xd8(%rbp), %rcx
movq %rcx, 0x40(%rax)
movq -0xd0(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0xc8(%rbp), %rcx
movq %rcx, 0x50(%rax)
jmp 0x4b85
movl -0xc8(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jbe 0x4b45
movq -0x38(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x4c00
cmpl $0x0, %eax
je 0x4b43
movl $0x0, -0x84(%rbp)
jmp 0x4bbf
jmp 0x4b69
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x40, %rsi
callq 0x4c00
cmpl $0x0, %eax
je 0x4b67
movl $0x0, -0x84(%rbp)
jmp 0x4bbf
jmp 0x4b69
movq -0x38(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, 0x40(%rax)
movq -0x78(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, (%rax)
movq -0x60(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
jmp 0x45b3
jmp 0x4bbf
movl -0x84(%rbp), %edx
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax,%rax)