// File: /workspace/github_c_makefile_star2_O1/imaya[P]zopfli-for-emscripten/lz77.c
// Function: ZopfliFindLongestMatch
void ZopfliFindLongestMatch(ZopfliBlockState* s, const ZopfliHash* h,
    const unsigned char* array,
    size_t pos, size_t size, size_t limit,
    unsigned short* sublen, unsigned short* distance, unsigned short* length) {
  unsigned short hpos = pos & ZOPFLI_WINDOW_MASK, p, pp;
  unsigned short bestdist = 0;
  unsigned short bestlength = 1;
  const unsigned char* scan;
  const unsigned char* match;
  const unsigned char* arrayend;
  const unsigned char* arrayend_safe;
#if ZOPFLI_MAX_CHAIN_HITS < ZOPFLI_WINDOW_SIZE
  int chain_counter = ZOPFLI_MAX_CHAIN_HITS;  /* For quitting early. */
#endif

  unsigned dist = 0;  /* Not unsigned short on purpose. */

  int* hhead = h->head;
  unsigned short* hprev = h->prev;
  int* hhashval = h->hashval;
  int hval = h->val;

#ifdef ZOPFLI_LONGEST_MATCH_CACHE
  if (TryGetFromLongestMatchCache(s, pos, &limit, sublen, distance, length)) {
    assert(pos + *length <= size);
    return;
  }
#endif

  assert(limit <= ZOPFLI_MAX_MATCH);
  assert(limit >= ZOPFLI_MIN_MATCH);
  assert(pos < size);

  if (size - pos < ZOPFLI_MIN_MATCH) {
    /* The rest of the code assumes there are at least ZOPFLI_MIN_MATCH bytes to
       try. */
    *length = 0;
    *distance = 0;
    return;
  }

  if (pos + limit > size) {
    limit = size - pos;
  }
  arrayend = &array[pos] + limit;
  arrayend_safe = arrayend - 8;

  assert(hval < 65536);

  pp = hhead[hval];  /* During the whole loop, p == hprev[pp]. */
  p = hprev[pp];

  assert(pp == hpos);

  dist = p < pp ? pp - p : ((ZOPFLI_WINDOW_SIZE - p) + pp);

  /* Go through all distances. */
  while (dist < ZOPFLI_WINDOW_SIZE) {
    unsigned short currentlength = 0;

    assert(p < ZOPFLI_WINDOW_SIZE);
    assert(p == hprev[pp]);
    assert(hhashval[p] == hval);

    if (dist > 0) {
      assert(pos < size);
      assert(dist <= pos);
      scan = &array[pos];
      match = &array[pos - dist];

      /* Testing the byte at position bestlength first, goes slightly faster. */
      if (pos + bestlength >= size
          || *(scan + bestlength) == *(match + bestlength)) {

#ifdef ZOPFLI_HASH_SAME
        unsigned short same0 = h->same[pos & ZOPFLI_WINDOW_MASK];
        if (same0 > 2 && *scan == *match) {
          unsigned short same1 = h->same[(pos - dist) & ZOPFLI_WINDOW_MASK];
          unsigned short same = same0 < same1 ? same0 : same1;
          if (same > limit) same = limit;
          scan += same;
          match += same;
        }
#endif
        scan = GetMatch(scan, match, arrayend, arrayend_safe);
        currentlength = scan - &array[pos];  /* The found length. */
      }

      if (currentlength > bestlength) {
        if (sublen) {
          unsigned short j;
          for (j = bestlength + 1; j <= currentlength; j++) {
            sublen[j] = dist;
          }
        }
        bestdist = dist;
        bestlength = currentlength;
        if (currentlength >= limit) break;
      }
    }


#ifdef ZOPFLI_HASH_SAME_HASH
    /* Switch to the other hash once this will be more efficient. */
    if (hhead != h->head2 && bestlength >= h->same[hpos] &&
        h->val2 == h->hashval2[p]) {
      /* Now use the hash that encodes the length and first byte. */
      hhead = h->head2;
      hprev = h->prev2;
      hhashval = h->hashval2;
      hval = h->val2;
    }
#endif

    pp = p;
    p = hprev[p];
    if (p == pp) break;  /* Uninited prev value. */

    dist += p < pp ? pp - p : ((ZOPFLI_WINDOW_SIZE - p) + pp);

#if ZOPFLI_MAX_CHAIN_HITS < ZOPFLI_WINDOW_SIZE
    chain_counter--;
    if (chain_counter <= 0) break;
#endif
  }

#ifdef ZOPFLI_LONGEST_MATCH_CACHE
  StoreInLongestMatchCache(s, pos, limit, sublen, bestdist, bestlength);
#endif

  assert(bestlength <= limit);

  *distance = bestdist;
  *length = bestlength;
  assert(pos + *length <= size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %rbp
movq %r8, 0x20(%rsp)
movq %rcx, %r14
movq %rcx, 0x10(%rsp)
movq %rdx, 0x50(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rsi
subq 0x10(%rdi), %r14
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x68a1
movq (%rdi), %rax
cmpw $0x0, (%rax,%r14,2)
je 0x68a6
movq 0x8(%rdi), %rax
cmpw $0x0, (%rax,%r14,2)
setne %r12b
jmp 0x68a9
xorl %r12d, %r12d
jmp 0x68a9
movb $0x1, %r12b
movq 0x18(%rsp), %rcx
movl 0x18(%rcx), %r10d
movq 0xb0(%rsp), %r15
movq (%rcx), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x70(%rsp)
movq 0x10(%rcx), %rax
movq %rax, 0x68(%rsp)
testb %r12b, %r12b
je 0x691a
movb $0x1, %al
cmpq $0x102, %rbp            # imm = 0x102
je 0x691c
movq (%rdi), %rcx
movzwl (%rcx,%r14,2), %edx
cmpq %rbp, %rdx
jbe 0x691c
testq %r15, %r15
je 0x691a
movq %rsi, %rbx
movq %r14, %rsi
movq %r15, %r13
movl %r10d, %r15d
callq 0x2103
movl %r15d, %r10d
movq %r13, %r15
movq %rbx, %rsi
movl %eax, %eax
cmpq %rbp, %rax
setae %al
jmp 0x691c
xorl %eax, %eax
movq 0xc0(%rsp), %r8
movq 0xb8(%rsp), %r9
movq 0x8(%rsi), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %r12b
andb %cl, %r12b
movb $0x1, %bl
cmpb $0x1, %r12b
jne 0x6a3e
testq %r15, %r15
je 0x69a2
movq (%rdi), %rax
movzwl (%rax,%r14,2), %r12d
movq %rsi, %r13
movq %r14, %rsi
movq %r12, %rdx
movq %rbp, 0x8(%rsp)
movq %r15, %rbp
movl %r10d, %r15d
callq 0x2103
movl %r15d, %r10d
movq %rbp, %r15
movq 0x8(%rsp), %rbp
movq %r13, %rsi
movq 0xb8(%rsp), %r9
movq 0xc0(%rsp), %r8
cmpl %r12d, %eax
jae 0x69a2
movq 0x8(%rsi), %rax
movq (%rax), %rax
movzwl (%rax,%r14,2), %ebp
jmp 0x6a3e
movq 0x8(%rsi), %rdi
movq (%rdi), %rax
movzwl (%rax,%r14,2), %eax
cmpq %rbp, %rax
movl %ebp, %ecx
cmovbel %eax, %ecx
movw %cx, (%r8)
testq %r15, %r15
je 0x6a2f
movl %r10d, %r13d
movq %rsi, 0x28(%rsp)
movzwl (%r8), %edx
movq %r14, %rsi
movq %r15, %rcx
movq %r8, %rbx
movq %r9, %r12
callq 0x2129
movq %r12, %r9
movq %rbx, %r8
movzwl (%rbx), %eax
movzwl (%r15,%rax,2), %eax
movw %ax, (%r12)
cmpq $0x102, %rbp            # imm = 0x102
movq 0x10(%rsp), %rdx
jne 0x6e88
movzwl (%r8), %eax
cmpq $0x3, %rax
movq 0x28(%rsp), %rsi
movl %r13d, %r10d
jb 0x6a2b
movzwl (%r15,%rax,2), %eax
movq 0x8(%rsi), %rcx
movq 0x8(%rcx), %rcx
cmpw (%rcx,%r14,2), %ax
jne 0x70c7
xorl %ebx, %ebx
jmp 0x6a43
movq 0x8(%rdi), %rax
movzwl (%rax,%r14,2), %eax
movw %ax, (%r9)
xorl %ebx, %ebx
movq 0x10(%rsp), %rdx
testb %bl, %bl
je 0x6a86
cmpq $0x103, %rbp            # imm = 0x103
jae 0x6fcf
cmpq $0x2, %rbp
movq 0x70(%rsp), %rbx
jbe 0x6fee
movq 0x20(%rsp), %rdi
subq %rdx, %rdi
jbe 0x700d
cmpq $0x2, %rdi
ja 0x6ab7
xorl %eax, %eax
movw %ax, (%r8)
movw %ax, (%r9)
jmp 0x6f44
movzwl (%r8), %eax
addq %rdx, %rax
cmpq 0x20(%rsp), %rax
jbe 0x6f44
leaq 0x4017(%rip), %rdi      # 0xaab6
leaq 0x3f83(%rip), %rsi      # 0xaa29
leaq 0x401f(%rip), %rcx      # 0xaacc
movl $0xf3, %edx
callq 0x10b0
leaq (%rdx,%rbp), %rax
cmpq 0x20(%rsp), %rax
cmovbeq %rbp, %rdi
cmpl $0x10000, %r10d         # imm = 0x10000
jge 0x702c
movl %edx, %r8d
andl $0x7fff, %r8d           # imm = 0x7FFF
movslq %r10d, %rax
movq 0x40(%rsp), %rcx
movzwl (%rcx,%rax,4), %ebp
movq %r8, 0x58(%rsp)
cmpl %r8d, %ebp
jne 0x704b
movq %rsi, 0x28(%rsp)
movq 0x50(%rsp), %rax
addq %rdx, %rax
leaq (%rax,%rdi), %rdx
movq %rdi, 0x38(%rsp)
movq %rax, 0x8(%rsp)
leaq -0x8(%rdi,%rax), %rax
movq %rax, 0x60(%rsp)
movzwl (%rbx,%rbp,2), %edi
movl %ebp, %eax
subl %edi, %eax
leal 0x8000(%rax), %r8d
cmoval %eax, %r8d
movl $0x2000, %eax           # imm = 0x2000
movq %rax, 0x48(%rsp)
movw $0x1, %r13w
movq $0x0, 0x30(%rsp)
cmpl $0x7fff, %r8d           # imm = 0x7FFF
ja 0x6e41
testw %di, %di
js 0x6f91
movzwl %bp, %eax
cmpw (%rbx,%rax,2), %di
movq 0x10(%rsp), %rsi
jne 0x6f72
movzwl %di, %r14d
movq 0x68(%rsp), %rax
cmpl %r10d, (%rax,%r14,4)
jne 0x6f53
testl %r8d, %r8d
je 0x6d95
movl %r8d, %r9d
movq %rsi, %rax
subq %r9, %rax
jb 0x6fb0
movl %r10d, %ecx
movq %rbx, %r10
movq 0x50(%rsp), %r9
addq %rax, %r9
movzwl %r13w, %ebx
leaq (%rbx,%rsi), %r12
cmpq 0x20(%rsp), %r12
jae 0x6bc4
movq 0x8(%rsp), %rsi
movb (%rsi,%rbx), %r12b
cmpb (%r9,%rbx), %r12b
jne 0x6c26
movq 0x18(%rsp), %rsi
movq 0x40(%rsi), %r12
movq 0x58(%rsp), %rsi
movzwl (%r12,%rsi,2), %ebx
cmpw $0x3, %bx
jb 0x6c1c
movq 0x8(%rsp), %r11
movb (%r11), %sil
cmpb (%r9), %sil
jne 0x6c34
andl $0x7fff, %eax           # imm = 0x7FFF
movzwl (%r12,%rax,2), %eax
cmpw %ax, %bx
cmovael %eax, %ebx
movl %ebx, %eax
movl $0xffff, %r12d          # imm = 0xFFFF
andl %r12d, %eax
movq 0x38(%rsp), %rsi
cmpq %rax, %rsi
cmovbl %esi, %ebx
andl %r12d, %ebx
leaq (%r11,%rbx), %r12
addq %rbx, %r9
jmp 0x6c37
movq 0x8(%rsp), %rax
movq %rax, %r12
jmp 0x6c3a
xorl %r12d, %r12d
movq %r10, %rbx
movl %ecx, %r10d
jmp 0x6d5c
movq %r11, %r12
movq %r11, %rax
cmpq 0x60(%rsp), %r12
movq %r10, %rbx
jae 0x6cc2
xorl %eax, %eax
movl %ecx, %r10d
movb (%r12,%rax), %sil
cmpb (%r9,%rax), %sil
jne 0x6cba
movb 0x1(%r12,%rax), %sil
cmpb 0x1(%r9,%rax), %sil
jne 0x6cc7
movb 0x2(%r12,%rax), %sil
cmpb 0x2(%r9,%rax), %sil
jne 0x6cd5
movb 0x3(%r12,%rax), %sil
cmpb 0x3(%r9,%rax), %sil
jne 0x6ce5
movb 0x4(%r12,%rax), %sil
cmpb 0x4(%r9,%rax), %sil
jne 0x6cf5
movb 0x5(%r12,%rax), %sil
cmpb 0x5(%r9,%rax), %sil
jne 0x6d05
movb 0x6(%r12,%rax), %sil
cmpb 0x6(%r9,%rax), %sil
jne 0x6d15
movb 0x7(%r12,%rax), %sil
cmpb 0x7(%r9,%rax), %sil
jne 0x6d25
leaq (%r12,%rax), %rsi
addq $0x8, %rsi
addq $0x8, %rax
cmpq 0x60(%rsp), %rsi
jb 0x6c49
addq %rax, %r12
addq %rax, %r9
jmp 0x6d33
movl %ecx, %r10d
jmp 0x6d38
addq %rax, %r12
incq %r12
addq %rax, %r9
incq %r9
jmp 0x6d33
addq %rax, %r12
addq $0x2, %r12
addq %rax, %r9
addq $0x2, %r9
jmp 0x6d33
addq %rax, %r12
addq $0x3, %r12
addq %rax, %r9
addq $0x3, %r9
jmp 0x6d33
addq %rax, %r12
addq $0x4, %r12
addq %rax, %r9
addq $0x4, %r9
jmp 0x6d33
addq %rax, %r12
addq $0x5, %r12
addq %rax, %r9
addq $0x5, %r9
jmp 0x6d33
addq %rax, %r12
addq $0x6, %r12
addq %rax, %r9
addq $0x6, %r9
jmp 0x6d33
addq %rax, %r12
addq $0x7, %r12
addq %rax, %r9
addq $0x7, %r9
movq 0x8(%rsp), %rax
cmpq %rdx, %r12
je 0x6d59
movb (%r12), %al
cmpb (%r9), %al
jne 0x6d54
incq %r12
incq %r9
cmpq %rdx, %r12
jne 0x6d3d
movq %rdx, %r12
movq 0x8(%rsp), %rax
subl %eax, %r12d
cmpw %r13w, %r12w
jbe 0x6d95
testq %r15, %r15
je 0x6d7c
incl %r13d
movzwl %r13w, %eax
movw %r8w, (%r15,%rax,2)
incl %r13d
cmpw %r12w, %r13w
jbe 0x6d6a
movzwl %r12w, %eax
cmpq %rax, 0x38(%rsp)
jbe 0x6e35
movl %r8d, %eax
movq %rax, 0x30(%rsp)
jmp 0x6d98
movl %r13d, %r12d
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
cmpq %rax, 0x40(%rsp)
je 0x6de6
movq 0x18(%rsp), %rcx
movq 0x40(%rcx), %rsi
movq 0x58(%rsp), %rcx
cmpw (%rsi,%rcx,2), %r12w
jb 0x6de6
movq 0x18(%rsp), %rcx
movl 0x38(%rcx), %r9d
movq 0x30(%rcx), %r11
cmpl (%r11,%r14,4), %r9d
jne 0x6de6
movq 0x18(%rsp), %rcx
movq 0x28(%rcx), %rbx
movq %rax, 0x40(%rsp)
movq %r11, 0x68(%rsp)
movl %r9d, %r10d
movzwl (%rbx,%r14,2), %r9d
cmpw %di, %r9w
jne 0x6df5
xorl %eax, %eax
jmp 0x6e23
movzwl %r9w, %eax
subl %eax, %r14d
leal 0x8000(%r14), %eax
cmpw %di, %r9w
cmovbl %r14d, %eax
addl %eax, %r8d
movq 0x48(%rsp), %rax
cmpl $0x2, %eax
leal -0x1(%rax), %esi
setge %al
movl %esi, %ecx
movq %rcx, 0x48(%rsp)
movl %edi, %ebp
movl %r9d, %edi
movl %r12d, %r13d
testb %al, %al
jne 0x6b45
jmp 0x6e44
xorl %eax, %eax
movl %r8d, %ecx
movq %rcx, 0x30(%rsp)
jmp 0x6e28
movl %r13d, %r12d
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rax
subq 0x10(%rax), %rsi
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x6e97
movq (%rcx), %rax
cmpw $0x0, (%rax,%rsi,2)
movq 0xc0(%rsp), %rdx
movq 0xb8(%rsp), %rdi
movq 0x30(%rsp), %rbx
je 0x6eb0
movq 0x8(%rcx), %rax
cmpw $0x0, (%rax,%rsi,2)
setne %al
jmp 0x6eb2
xorl %ebx, %ebx
movq 0x28(%rsp), %rsi
movl %r13d, %r10d
jmp 0x6a43
xorl %eax, %eax
movq 0xc0(%rsp), %rdx
movq 0xb8(%rsp), %rdi
movq 0x30(%rsp), %rbx
jmp 0x6eb2
movb $0x1, %al
movzwl %r12w, %r14d
testq %r15, %r15
je 0x6f22
cmpq $0x102, 0x38(%rsp)      # imm = 0x102
jne 0x6f22
testq %rcx, %rcx
je 0x6f22
testb %al, %al
jne 0x6f22
movq (%rcx), %rax
cmpw $0x1, (%rax,%rsi,2)
jne 0x706a
movq 0x8(%rcx), %rdx
cmpw $0x0, (%rdx,%rsi,2)
jne 0x706a
xorl %edi, %edi
cmpw $0x3, %r14w
movl %ebx, %r8d
cmovbl %edi, %r8d
movw %r8w, (%rdx,%rsi,2)
cmovael %r14d, %edi
movw %di, (%rax,%rsi,2)
movq %r15, %rdi
movq %r14, %rdx
callq 0x1ff7
movq 0xc0(%rsp), %rdx
movq 0xb8(%rsp), %rdi
cmpq %r14, 0x38(%rsp)
jb 0x7089
movw %bx, (%rdi)
movw %r12w, (%rdx)
addq 0x10(%rsp), %r14
cmpq 0x20(%rsp), %r14
ja 0x70a8
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3c98(%rip), %rdi      # 0xabf2
leaq 0x3ac8(%rip), %rsi      # 0xaa29
leaq 0x3b64(%rip), %rcx      # 0xaacc
movl $0x119, %edx            # imm = 0x119
callq 0x10b0
leaq 0x3c6a(%rip), %rdi      # 0xabe3
leaq 0x3aa9(%rip), %rsi      # 0xaa29
leaq 0x3b45(%rip), %rcx      # 0xaacc
movl $0x118, %edx            # imm = 0x118
callq 0x10b0
leaq 0x3c34(%rip), %rdi      # 0xabcc
leaq 0x3a8a(%rip), %rsi      # 0xaa29
leaq 0x3b26(%rip), %rcx      # 0xaacc
movl $0x117, %edx            # imm = 0x117
callq 0x10b0
leaq 0x3c4f(%rip), %rdi      # 0xac06
leaq 0x3a6b(%rip), %rsi      # 0xaa29
leaq 0x3b07(%rip), %rcx      # 0xaacc
movl $0x11d, %edx            # imm = 0x11D
callq 0x10b0
leaq 0x3b9f(%rip), %rdi      # 0xab75
leaq 0x3a4c(%rip), %rsi      # 0xaa29
leaq 0x3ae8(%rip), %rcx      # 0xaacc
movl $0xf8, %edx
callq 0x10b0
leaq 0x3b9a(%rip), %rdi      # 0xab8f
leaq 0x3a2d(%rip), %rsi      # 0xaa29
leaq 0x3ac9(%rip), %rcx      # 0xaacc
movl $0xf9, %edx
callq 0x10b0
leaq 0x3b95(%rip), %rdi      # 0xaba9
leaq 0x3a0e(%rip), %rsi      # 0xaa29
leaq 0x3aaa(%rip), %rcx      # 0xaacc
movl $0xfa, %edx
callq 0x10b0
leaq 0x3b81(%rip), %rdi      # 0xabb4
leaq 0x39ef(%rip), %rsi      # 0xaa29
leaq 0x3a8b(%rip), %rcx      # 0xaacc
movl $0x10a, %edx            # imm = 0x10A
callq 0x10b0
leaq 0x3b6f(%rip), %rdi      # 0xabc1
leaq 0x39d0(%rip), %rsi      # 0xaa29
leaq 0x3a6c(%rip), %rcx      # 0xaacc
movl $0x10f, %edx            # imm = 0x10F
callq 0x10b0
leaq 0x3cc7(%rip), %rdi      # 0xad38
leaq 0x39b1(%rip), %rsi      # 0xaa29
leaq 0x3cf2(%rip), %rcx      # 0xad71
movl $0xd2, %edx
callq 0x10b0
leaq 0x3b82(%rip), %rdi      # 0xac12
leaq 0x3992(%rip), %rsi      # 0xaa29
leaq 0x3a2e(%rip), %rcx      # 0xaacc
movl $0x15d, %edx            # imm = 0x15D
callq 0x10b0
leaq 0x3a07(%rip), %rdi      # 0xaab6
leaq 0x3973(%rip), %rsi      # 0xaa29
leaq 0x3a0f(%rip), %rcx      # 0xaacc
movl $0x161, %edx            # imm = 0x161
callq 0x10b0
leaq 0x3bc6(%rip), %rdi      # 0xac94
leaq 0x3954(%rip), %rsi      # 0xaa29
leaq 0x3be0(%rip), %rcx      # 0xacbc
movl $0xb1, %edx
callq 0x10b0