// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq 0x8(%rdi), %rcx
movq 0x18(%rdi), %r12
addq $-0x8, %rcx
movq %rcx, 0x20(%rdi)
xorl %eax, %eax
cmpq %rcx, %r12
jge 0x412a
movq %rdi, %r14
leaq 0x27160(%rdi), %rdx
leaq 0x40(%rdi), %rsi
movq %rdx, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movq (%r14), %rcx
movl (%rcx,%r12), %r13d
imull $0x9e3779b1, %r13d, %ebx # imm = 0x9E3779B1
shrl $0xd, %ebx
andl $-0x20, %ebx
movdqu (%rdx,%rbx), %xmm0
movups 0x10(%rdx,%rbx), %xmm1
movaps %xmm1, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movl 0x8(%rdx,%rbx), %eax
movl %eax, 0x50(%rsp)
movq (%rdx,%rbx), %rax
movq %rax, 0x48(%rsp)
movl 0x18(%rdx,%rbx), %eax
movl %eax, 0x40(%rsp)
movq 0x10(%rdx,%rbx), %rax
movq %rax, 0x38(%rsp)
movq 0x10(%r14), %rax
movq %r12, %rdi
subq %rax, %rdi
jl 0x404d
movq %rdi, 0x58(%rsp)
movq %rax, 0x30(%rsp)
leaq (%rcx,%r12), %r8
xorl %r9d, %r9d
xorl %eax, %eax
xorl %esi, %esi
cmpl %r13d, 0x70(%rsp,%r9,4)
jne 0x3f2f
movslq 0x60(%rsp,%r9,4), %r10
leaq 0x3fffb(%r10), %rdi
cmpq %rdi, %r12
jg 0x3f2f
movq %r14, %rdx
movl 0x8(%r14), %r15d
subl %r12d, %r15d
addl $-0x8, %r15d
movl $0x4, %r11d
cmpl $0x5, %r15d
jb 0x3f21
leaq (%rcx,%r10), %rbp
movl %r11d, %r14d
movq (%rbp,%r14), %rdi
movq (%r8,%r14), %r14
cmpq %r14, %rdi
jne 0x3f11
addl $0x8, %r11d
cmpl %r15d, %r11d
jb 0x3ef5
jmp 0x3f21
xorq %rdi, %r14
bsfq %r14, %rdi
shrl $0x3, %edi
addl %r11d, %edi
movl %edi, %r11d
cmpl %eax, %r11d
cmovaq %r10, %rsi
cmoval %r11d, %eax
movq %rdx, %r14
incq %r9
cmpq $0x4, %r9
jne 0x3ebf
movl %eax, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
testl %eax, %eax
je 0x3f84
movl $0x3997c, %edx          # imm = 0x3997C
cmpl %edx, %eax
cmovael %edx, %eax
movq 0x30(%rsp), %rdx
cmpq %rdx, %r12
jle 0x3fad
testq %rsi, %rsi
jle 0x3fad
movq %r12, %rdi
movb -0x1(%rcx,%rsi), %r8b
cmpb -0x1(%rcx,%rdi), %r8b
jne 0x3fb5
decq %rdi
leaq -0x1(%rsi), %r8
cmpq %rdx, %rdi
jle 0x3fb8
cmpq $0x1, %rsi
movq %r8, %rsi
jg 0x3f61
jmp 0x3fb8
cmpq $0x9d9, 0x58(%rsp)      # imm = 0x9D9
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %rax
jl 0x404d
cmpl $0x0, 0x50(%r14)
je 0x40de
movq %r14, %rdi
jmp 0x4021
movq %rsi, %r8
movq %r12, %rdi
jmp 0x3fb8
movq %rsi, %r8
movq %rdi, 0x18(%rsp)
movq %r8, 0x20(%rsp)
movl %r12d, %ecx
subl %edi, %ecx
addl %ecx, %eax
movl %eax, 0x28(%rsp)
cmpl $0x28, %eax
jb 0x3ff2
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
callq 0x413c
testl %eax, %eax
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rsi
je 0x403d
jmp 0x4121
movl 0x50(%r14), %ecx
testq %rcx, %rcx
movq 0x8(%rsp), %rsi
je 0x4091
movq (%rsi), %rdx
addq %rcx, %rdx
cmpq %rdi, %rdx
jle 0x40ab
movq %r14, %rdi
cmpl %ecx, %eax
jbe 0x4021
leaq 0x18(%rsp), %rsi
jmp 0x4026
movq 0x8(%rsp), %rsi
callq 0x413c
movq 0x8(%rsp), %rsi
testl %eax, %eax
movq 0x10(%rsp), %rdx
jne 0x4121
pxor %xmm0, %xmm0
movdqu %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
addq %rdx, %rbx
movl %r12d, (%rbx)
movq 0x48(%rsp), %rax
movq %rax, 0x4(%rbx)
movl 0x50(%rsp), %eax
movl %eax, 0xc(%rbx)
movl %r13d, 0x10(%rbx)
movq 0x38(%rsp), %rax
movq %rax, 0x14(%rbx)
movl 0x40(%rsp), %eax
movl %eax, 0x1c(%rbx)
movq 0x18(%r14), %r12
incq %r12
movq %r12, 0x18(%r14)
cmpq 0x20(%r14), %r12
jl 0x3e4e
jmp 0x4128
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsi)
movdqu 0x18(%rsp), %xmm0
movdqu %xmm0, (%rsi)
movq 0x10(%rsp), %rdx
jmp 0x404d
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x413c
movq 0x8(%rsp), %rsi
testl %eax, %eax
movq 0x10(%rsp), %rdx
jne 0x4121
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsi)
movdqu 0x18(%rsp), %xmm0
movdqu %xmm0, (%rsi)
jmp 0x404d
movq %rax, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
paddq 0x59c0(%rip), %xmm0     # 0x9ab0
movdqa %xmm0, 0x80(%rsp)
movl $0x0, 0x90(%rsp)
movq %r14, %rdi
leaq 0x80(%rsp), %rsi
callq 0x413c
testl %eax, %eax
movq 0x10(%rsp), %rdx
je 0x404d
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x412a
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq