// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
xorl %eax, %eax
movq %rax, 0x70(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movl %eax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq %rax, 0x38(%rsp)
movl %eax, 0x40(%rsp)
movq 0x8(%rdi), %rcx
movq 0x18(%rdi), %r15
addq $-0x8, %rcx
movq %rcx, 0x20(%rdi)
cmpq %rcx, %r15
jge 0x4249
movq %rdi, %rbx
leaq 0x27160(%rdi), %rdx
leaq 0x40(%rdi), %r14
movl $0x1, 0x4(%rsp)
movq %rdx, 0x8(%rsp)
movq %r14, 0x18(%rsp)
movq (%rbx), %rcx
movl (%rcx,%r15), %ebp
imull $0x9e3779b1, %ebp, %esi # imm = 0x9E3779B1
shrl $0xd, %esi
andl $-0x20, %esi
movups (%rdx,%rsi), %xmm0
movups 0x10(%rdx,%rsi), %xmm1
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movl 0x8(%rdx,%rsi), %eax
movl %eax, 0x50(%rsp)
movq (%rdx,%rsi), %rax
movq %rax, 0x48(%rsp)
movl 0x18(%rdx,%rsi), %eax
movl %eax, 0x40(%rsp)
movq 0x10(%rdx,%rsi), %rax
movq %rax, 0x38(%rsp)
movq 0x10(%rbx), %rax
movq %r15, %rdi
subq %rax, %rdi
jge 0x3f61
addq %rdx, %rsi
movl %r15d, (%rsi)
movq 0x48(%rsp), %rax
movq %rax, 0x4(%rsi)
movl 0x50(%rsp), %eax
movl %eax, 0xc(%rsi)
movl %ebp, 0x10(%rsi)
movq 0x38(%rsp), %rax
movq %rax, 0x14(%rsi)
movl 0x40(%rsp), %eax
movl %eax, 0x1c(%rsi)
xorl %eax, %eax
testl %eax, %eax
jne 0x4231
movq 0x18(%rbx), %r15
incq %r15
movq %r15, 0x18(%rbx)
cmpq 0x20(%rbx), %r15
jl 0x3eb9
jmp 0x4236
movq %rdi, 0x80(%rsp)
movq %rax, 0x58(%rsp)
movq %rsi, 0x10(%rsp)
leaq (%rcx,%r15), %r8
movq %r15, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
xorl %r9d, %r9d
xorl %eax, %eax
xorl %esi, %esi
cmpl %ebp, 0xb0(%rsp,%r9,4)
jne 0x400b
movslq 0xa0(%rsp,%r9,4), %r10
leaq 0x3fffb(%r10), %rdi
cmpq %rdi, %r15
jg 0x400b
leaq (%rcx,%r10), %r14
movq %rbx, %r12
movl 0x8(%rbx), %r13d
subl %r15d, %r13d
addl $-0x8, %r13d
movl $0x4, %r11d
cmpl %r13d, %r11d
jae 0x3ffd
movl %r11d, %edi
movq (%r14,%rdi), %rbx
movq (%r8,%rdi), %rdx
cmpq %rdx, %rbx
jne 0x3fe5
addl $0x8, %r11d
jmp 0x3ff8
movq %rdx, %rdi
xorq %rbx, %rdi
bsfq %rdi, %rdi
shrl $0x3, %edi
addl %r11d, %edi
movl %edi, %r11d
cmpq %rdx, %rbx
je 0x3fca
cmpl %eax, %r11d
cmovaq %r10, %rsi
cmoval %r11d, %eax
movq %r12, %rbx
incq %r9
cmpq $0x4, %r9
jne 0x3f94
movl %eax, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
testl %eax, %eax
je 0x4068
cmpl $0x3997d, %eax          # imm = 0x3997D
movq 0x58(%rsp), %r9
jb 0x403a
movl $0x3997c, 0x30(%rsp)    # imm = 0x3997C
movl $0x3997c, %eax          # imm = 0x3997C
cmpq %r9, %r15
jle 0x40b8
testq %rsi, %rsi
jle 0x40b8
movq %r15, %rdi
movb -0x1(%rcx,%rsi), %dl
cmpb -0x1(%rcx,%rdi), %dl
jne 0x40c0
decq %rdi
leaq -0x1(%rsi), %r8
cmpq %r9, %rdi
jle 0x40c3
cmpq $0x1, %rsi
movq %r8, %rsi
jg 0x4047
jmp 0x40c3
cmpq $0x9d9, 0x80(%rsp) # imm = 0x9D9
movq 0x18(%rsp), %r14
movq 0x58(%rsp), %rcx
jl 0x41d7
cmpl $0x0, 0x50(%rbx)
je 0x4189
movq %rbx, %rdi
movq %r14, %rsi
callq 0x425b
testl %eax, %eax
jne 0x41c6
movq 0x70(%rsp), %rax
movq %rax, 0x10(%r14)
movaps 0x60(%rsp), %xmm0
movups %xmm0, (%r14)
jmp 0x41d7
movq %rsi, %r8
movq %r15, %rdi
jmp 0x40c3
movq %rsi, %r8
movq %rdi, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movl %r15d, %ecx
subl %edi, %ecx
addl %ecx, %eax
movl %eax, 0x30(%rsp)
cmpl $0x28, %eax
jb 0x4107
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
callq 0x425b
testl %eax, %eax
jne 0x4201
movq 0x70(%rsp), %rax
movq 0x18(%rsp), %r14
movq %rax, 0x10(%r14)
movaps 0x60(%rsp), %xmm0
jmp 0x4176
movl 0x50(%rbx), %ecx
testq %rcx, %rcx
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rsi
je 0x413e
movq (%r14), %rdx
addq %rcx, %rdx
cmpq %rdi, %rdx
jle 0x4155
movq %rbx, %rdi
cmpl %ecx, %eax
jbe 0x41f5
leaq 0x20(%rsp), %rsi
jmp 0x41f8
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0x20(%rsp), %xmm0
movups %xmm0, (%r14)
jmp 0x3f14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x425b
testl %eax, %eax
jne 0x4201
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r14)
movups 0x20(%rsp), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rsi
jmp 0x3f14
leaq 0x13b(%rcx), %rax
movq %rax, 0x88(%rsp)
addq $0x13a, %rcx            # imm = 0x13A
movq %rcx, 0x90(%rsp)
movl $0x0, 0x98(%rsp)
movq %rbx, %rdi
leaq 0x88(%rsp), %rsi
callq 0x425b
testl %eax, %eax
je 0x41d7
xorl %ecx, %ecx
movl $0x11, %eax
movl $0x0, 0x4(%rsp)
jmp 0x41de
movl $0xb, %eax
movb $0x1, %cl
testb %cl, %cl
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rsi
jne 0x3f14
jmp 0x3f3f
movq %r14, %rsi
callq 0x425b
testl %eax, %eax
je 0x4218
movl $0x0, 0x4(%rsp)
movl $0x11, %eax
movq 0x8(%rsp), %rdx
jmp 0x3f3f
xorps %xmm0, %xmm0
movq 0x18(%rsp), %r14
movups %xmm0, (%r14)
movq $0x0, 0x10(%r14)
jmp 0x417a
cmpl $0x11, %eax
jne 0x4249
xorl %eax, %eax
cmpl $0x0, 0x4(%rsp)
setne %al
leal -0x2(,%rax,2), %eax
jmp 0x4249
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq