// File: sbingner[P]kerneldec/lzfse/src/lzvn_encode_base.c
// Function: lzvn_find_matchN
static inline int lzvn_find_matchN(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_offset n, lzvn_match_info *match) {
  // We can skip the first comparison on 4 bytes
  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 0x18(%rbp), %rax
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)
cmpq $0x3, 0x10(%rbp)
jge 0xadeb
movl $0x0, -0x4(%rbp)
jmp 0xaf44
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jle 0xae08
cmpq $0xffff, -0x40(%rbp)    # imm = 0xFFFF
jle 0xae14
movl $0x0, -0x4(%rbp)
jmp 0xaf44
movq -0x38(%rbp), %rax
addq 0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
xorl %eax, %eax
cmpq $0x4, 0x10(%rbp)
movb %al, -0x51(%rbp)
jne 0xae3e
movq -0x48(%rbp), %rax
addq $0x4, %rax
cmpq -0x20(%rbp), %rax
setl %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0xae47
jmp 0xae6e
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x48(%rbp), %rdx
subq -0x40(%rbp), %rdx
callq 0xb460
movq %rax, 0x10(%rbp)
movq 0x10(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0xae20
jmp 0xae70
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq -0x18(%rbp), %rcx
movb %al, -0x52(%rbp)
jle 0xaeb6
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq -0x28(%rbp), %rcx
movb %al, -0x52(%rbp)
jle 0xaeb6
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, -0x52(%rbp)
movb -0x52(%rbp), %al
testb $0x1, %al
jne 0xaebf
jmp 0xaed9
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xae70
movq -0x48(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rsi
movl $0x3, %eax
movl $0x2, %edx
cmpq $0x600, %rsi            # imm = 0x600
cmovll %edx, %eax
cltq
subq %rax, %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x50(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)