// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rdi, %r15
leaq 0x27160(%rdi), %rax
movq %rax, 0x38(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%rsp)
movups %xmm0, 0x44(%rsp)
movq 0x8(%rdi), %rcx
movq 0x18(%rdi), %rax
addq $-0x8, %rcx
movq %rcx, 0x20(%rdi)
leaq 0x40(%rdi), %rsi
movq %rsi, 0x10(%rsp)
cmpq %rcx, %rax
jge 0x3f8e
movq (%r15), %rcx
leaq (%rcx,%rax), %rbx
movl (%rcx,%rax), %edi
imull $0x9e3779b1, %edi, %edx # imm = 0x9E3779B1
shrl $0xd, %edx
andl $-0x20, %edx
movq 0x38(%rsp), %r8
leaq (%r8,%rdx), %r9
movups (%r8,%rdx), %xmm0
movups 0x10(%r8,%rdx), %xmm1
movaps %xmm1, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movl %eax, 0x40(%rsp)
pushq $0x1
popq %rdx
cmpq $0x4, %rdx
je 0x3d8e
movl 0x5c(%rsp,%rdx,4), %r8d
movl %r8d, 0x40(%rsp,%rdx,4)
incq %rdx
jmp 0x3d79
movl %edi, 0x50(%rsp)
pushq $0x5
popq %rdx
cmpq $0x8, %rdx
je 0x3daa
movl 0x5c(%rsp,%rdx,4), %r8d
movl %r8d, 0x40(%rsp,%rdx,4)
incq %rdx
jmp 0x3d95
movq 0x10(%r15), %r8
movq %rax, %rdx
subq %r8, %rdx
jl 0x3f0b
movq %rdx, 0x30(%rsp)
movq %r9, 0x8(%rsp)
xorl %r11d, %r11d
xorl %r9d, %r9d
xorl %edx, %edx
cmpq $0x4, %r11
je 0x3e3f
cmpl %edi, 0x70(%rsp,%r11,4)
jne 0x3e3a
movslq 0x60(%rsp,%r11,4), %rbp
leaq 0x3fffb(%rbp), %r10
cmpq %r10, %rax
jg 0x3e3a
leaq (%rcx,%rbp), %r14
movq %r15, %rsi
movl 0x8(%r15), %r12d
subl %eax, %r12d
addl $-0x8, %r12d
pushq $0x4
popq %r15
cmpl %r12d, %r15d
jae 0x3e2c
movl %r15d, %r10d
movq (%r14,%r10), %r13
movq (%rbx,%r10), %r10
cmpq %r10, %r13
jne 0x3e1b
addl $0x8, %r15d
jmp 0x3e00
xorq %r13, %r10
bsfq %r10, %r10
shrl $0x3, %r10d
addl %r15d, %r10d
movl %r10d, %r15d
cmpl %edx, %r15d
cmovaq %rbp, %r9
cmoval %r15d, %edx
movq %rsi, %r15
incq %r11
jmp 0x3dcc
movl %edx, 0x28(%rsp)
movq %r9, 0x20(%rsp)
testl %edx, %edx
je 0x3e9f
movl $0x3997c, %esi          # imm = 0x3997C
cmpl %esi, %edx
cmovael %esi, %edx
cmpq %r8, %rax
jle 0x3e76
testq %r9, %r9
jle 0x3e76
movb -0x1(%rcx,%r9), %sil
cmpb -0x1(%rcx,%rax), %sil
jne 0x3e76
decq %rax
decq %r9
incl %edx
jmp 0x3e56
movq %rax, 0x18(%rsp)
movq %r9, 0x20(%rsp)
movl %edx, 0x28(%rsp)
cmpl $0x28, %edx
jb 0x3ec4
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3fa2
testl %eax, %eax
movq 0x10(%rsp), %rsi
jmp 0x3ef5
cmpq $0x9d9, 0x30(%rsp)      # imm = 0x9D9
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r9
jl 0x3f0b
cmpl $0x0, 0x50(%r15)
je 0x3f73
movq %r15, %rdi
jmp 0x3ee9
movl 0x50(%r15), %ecx
testq %rcx, %rcx
movq 0x10(%rsp), %rsi
je 0x3f32
movq (%rsi), %rdi
addq %rcx, %rdi
cmpq %rax, %rdi
jle 0x3f4a
movq %r15, %rdi
cmpl %ecx, %edx
jbe 0x3ee9
leaq 0x18(%rsp), %rsi
callq 0x3fa2
movq 0x10(%rsp), %rsi
testl %eax, %eax
movq 0x8(%rsp), %r9
jne 0x3f89
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movups 0x40(%rsp), %xmm0
movups 0x50(%rsp), %xmm1
movups %xmm1, 0x10(%r9)
movups %xmm0, (%r9)
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
incq %rax
movq %rax, 0x18(%r15)
jmp 0x3d35
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x18(%rsp), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rsp), %r9
jmp 0x3f0b
movq %r15, %rdi
callq 0x3fa2
movq 0x10(%rsp), %rsi
testl %eax, %eax
movq 0x8(%rsp), %r9
jne 0x3f89
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x18(%rsp), %xmm0
movups %xmm0, (%rsi)
jmp 0x3f0b
movl $0x13b, %esi            # imm = 0x13B
movq %r15, %rdi
callq 0x3fe2
testl %eax, %eax
movq 0x8(%rsp), %r9
je 0x3f0b
pushq $-0x2
popq %rax
jmp 0x3f90
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq