// File: sbingner[P]kerneldec/lzfse/src/lzvn_encode_base.c
// Function: lzvn_find_match
static inline int lzvn_find_match(const unsigned char *src,
                                  lzvn_offset src_begin,
                                  lzvn_offset src_end, lzvn_offset l_begin,
                                  lzvn_offset m0_begin, lzvn_offset m_begin,
                                  lzvn_match_info *match) {
  lzvn_offset n = nmatch4(src, m_begin, m0_begin);
  if (n < 3)
    return 0; // no match

  lzvn_offset D = m_begin - m0_begin; // actual distance
  if (D <= 0 || D > LZVN_ENCODE_MAX_DISTANCE)
    return 0; // distance out of range

  // Expand forward
  lzvn_offset m_end = m_begin + n;
  while (n == 4 && m_end + 4 < src_end) {
    n = nmatch4(src, m_end, m_end - D);
    m_end += n;
  }

  // Expand backwards over literal
  while (m0_begin > src_begin && m_begin > l_begin &&
         src[m_begin - 1] == src[m0_begin - 1]) {
    m0_begin--;
    m_begin--;
  }

  // OK, we keep it, update MATCH
  lzvn_offset M = m_end - m_begin; // match length
  match->m_begin = m_begin;
  match->m_end = m_end;
  match->K = M - ((D < 0x600) ? 2 : 3);
  match->M = M;
  match->D = D;

  return 1; // OK
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0xb460
movq %rax, -0x40(%rbp)
cmpq $0x3, -0x40(%rbp)
jge 0xaf9c
movl $0x0, -0x4(%rbp)
jmp 0xb0f5
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jle 0xafb9
cmpq $0xffff, -0x48(%rbp)    # imm = 0xFFFF
jle 0xafc5
movl $0x0, -0x4(%rbp)
jmp 0xb0f5
movq -0x38(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
xorl %eax, %eax
cmpq $0x4, -0x40(%rbp)
movb %al, -0x59(%rbp)
jne 0xafef
movq -0x50(%rbp), %rax
addq $0x4, %rax
cmpq -0x20(%rbp), %rax
setl %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0xaff8
jmp 0xb01f
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x50(%rbp), %rdx
subq -0x48(%rbp), %rdx
callq 0xb460
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0xafd1
jmp 0xb021
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq -0x18(%rbp), %rcx
movb %al, -0x5a(%rbp)
jle 0xb067
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq -0x28(%rbp), %rcx
movb %al, -0x5a(%rbp)
jle 0xb067
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
subq $0x1, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x5a(%rbp)
movb -0x5a(%rbp), %al
testb $0x1, %al
jne 0xb070
jmp 0xb08a
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xb021
movq -0x50(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rcx
movq -0x48(%rbp), %rsi
movl $0x3, %eax
movl $0x2, %edx
cmpq $0x600, %rsi            # imm = 0x600
cmovll %edx, %eax
cltq
subq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x58(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop