// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %r9, %r12
movq %r8, 0x8(%rsp)
movq %rcx, %r15
movq %rdx, 0x48(%rsp)
movq %rsi, %rbp
movq %rdi, %rsi
movq 0xb0(%rsp), %r13
movq 0xa8(%rsp), %rcx
movq (%rbp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rbp), %rbx
movq 0x10(%rbp), %rax
movq %rax, 0x58(%rsp)
movl 0x18(%rbp), %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x4660
movq %r15, %r14
subq 0x10(%rsi), %r14
movq (%rdi), %rax
movzwl (%rax,%r14,2), %edx
testq %rdx, %rdx
je 0x45c0
movq 0x8(%rdi), %rax
cmpw $0x0, (%rax,%r14,2)
je 0x4662
movb $0x1, %al
cmpq $0x102, %r12            # imm = 0x102
je 0x4605
cmpq %r12, %rdx
jbe 0x4605
cmpq $0x0, 0xa0(%rsp)
je 0x4662
movq %rsi, 0x20(%rsp)
movq %r14, %rsi
callq 0x1c39
movq 0x20(%rsp), %rsi
movq 0xa8(%rsp), %rcx
movl %eax, %eax
cmpq %r12, %rax
setae %al
movq 0x8(%rsi), %rdi
testq %rdi, %rdi
je 0x4662
testb %al, %al
je 0x4662
cmpq $0x0, 0xa0(%rsp)
je 0x4b03
movq (%rdi), %rax
movzwl (%rax,%r14,2), %edx
movq %rdx, 0x38(%rsp)
movq %rsi, %r13
movq %r14, %rsi
callq 0x1c39
movq %r13, %rsi
movq 0x8(%r13), %rdi
cmpl 0x38(%rsp), %eax
jae 0x4afb
movq (%rdi), %rax
movzwl (%rax,%r14,2), %r12d
movq 0xa8(%rsp), %rcx
movq 0xb0(%rsp), %r13
jmp 0x4662
xorl %edi, %edi
cmpq $0x103, %r12            # imm = 0x103
jae 0x4c54
cmpq $0x2, %r12
jbe 0x4c73
movq 0x8(%rsp), %rdx
subq %r15, %rdx
jbe 0x4c92
cmpq $0x2, %rdx
ja 0x469c
andw $0x0, (%r13)
andw $0x0, (%rcx)
jmp 0x4bc9
leaq (%r12,%r15), %rax
cmpq 0x8(%rsp), %rax
cmovbeq %r12, %rdx
movl 0x14(%rsp), %eax
cmpl $0x10000, %eax          # imm = 0x10000
jge 0x4cb1
movq %rsi, 0x20(%rsp)
movq %rdi, 0x60(%rsp)
movl %r15d, %esi
andl $0x7fff, %esi           # imm = 0x7FFF
cltq
movq 0x30(%rsp), %rcx
movzwl (%rcx,%rax,4), %ecx
movq %rsi, 0x50(%rsp)
cmpl %esi, %ecx
jne 0x4cd0
movq %r15, 0x28(%rsp)
movq 0x48(%rsp), %rax
addq %r15, %rax
leaq (%rax,%rdx), %rsi
movq %rdx, (%rsp)
movq %rax, 0x18(%rsp)
leaq -0x8(%rdx,%rax), %rax
movq %rax, 0x38(%rsp)
movzwl (%rbx,%rcx,2), %r8d
movl %ecx, %edx
subl %r8d, %edx
leal 0x8000(%rdx), %r9d
cmoval %edx, %r9d
movl $0x2000, %r10d          # imm = 0x2000
movw $0x1, %r11w
xorl %r12d, %r12d
movl 0x14(%rsp), %r15d
movq %rbp, 0x40(%rsp)
cmpl $0x7fff, %r9d           # imm = 0x7FFF
ja 0x49ec
testw %r8w, %r8w
js 0x4c16
movzwl %cx, %ecx
cmpw (%rbx,%rcx,2), %r8w
movq 0x28(%rsp), %rax
jne 0x4bd8
movzwl %r8w, %r14d
movq 0x58(%rsp), %rcx
cmpl %r15d, (%rcx,%r14,4)
jne 0x4bf7
testl %r9d, %r9d
je 0x4965
movl %r9d, %ecx
movq %rax, %rdx
subq %rcx, %rdx
jb 0x4c35
movq %rbx, %rdi
movl %r15d, %ebx
movq 0x48(%rsp), %rcx
addq %rdx, %rcx
movzwl %r11w, %r15d
leaq (%r15,%rax), %r13
cmpq 0x8(%rsp), %r13
jae 0x47b5
movq 0x18(%rsp), %rax
movb (%rax,%r15), %r13b
cmpb (%rcx,%r15), %r13b
jne 0x4815
movq 0x40(%rbp), %r13
movq 0x50(%rsp), %rax
movzwl (%r13,%rax,2), %r15d
cmpw $0x3, %r15w
jb 0x480e
movq 0x18(%rsp), %rbp
movb (%rbp), %al
cmpb (%rcx), %al
jne 0x4823
andl $0x7fff, %edx           # imm = 0x7FFF
movzwl (%r13,%rdx,2), %eax
cmpw %ax, %r15w
cmovael %eax, %r15d
movl %r15d, %eax
movl $0xffff, %r13d          # imm = 0xFFFF
andl %r13d, %eax
movq (%rsp), %rdx
cmpq %rax, %rdx
cmovbl %edx, %r15d
andl %r13d, %r15d
leaq (%r15,%rbp), %rdx
addq %r15, %rcx
jmp 0x4826
movq 0x18(%rsp), %rdx
jmp 0x482b
movl %r11d, %r13d
movl %ebx, %r15d
movq %rdi, %rbx
jmp 0x4974
movq %rbp, %rdx
movq 0x40(%rsp), %rbp
xorl %r15d, %r15d
leaq (%rdx,%r15), %r13
cmpq 0x38(%rsp), %r13
jae 0x489d
movb (%r13), %al
cmpb (%rcx,%r15), %al
jne 0x489d
movb 0x1(%rdx,%r15), %al
cmpb 0x1(%rcx,%r15), %al
jne 0x48a2
movb 0x2(%rdx,%r15), %al
cmpb 0x2(%rcx,%r15), %al
jne 0x48b1
movb 0x3(%rdx,%r15), %al
cmpb 0x3(%rcx,%r15), %al
jne 0x48c2
movb 0x4(%rdx,%r15), %al
cmpb 0x4(%rcx,%r15), %al
jne 0x48d3
movb 0x5(%rdx,%r15), %al
cmpb 0x5(%rcx,%r15), %al
jne 0x48e4
movb 0x6(%rdx,%r15), %al
cmpb 0x6(%rcx,%r15), %al
jne 0x48f5
movb 0x7(%rdx,%r15), %al
cmpb 0x7(%rcx,%r15), %al
jne 0x4906
addq $0x8, %r15
jmp 0x482e
addq %r15, %rcx
jmp 0x4915
leaq (%rdx,%r15), %r13
incq %r13
addq %r15, %rcx
incq %rcx
jmp 0x4915
leaq (%rdx,%r15), %r13
addq $0x2, %r13
addq %r15, %rcx
addq $0x2, %rcx
jmp 0x4915
leaq (%rdx,%r15), %r13
addq $0x3, %r13
addq %r15, %rcx
addq $0x3, %rcx
jmp 0x4915
leaq (%rdx,%r15), %r13
addq $0x4, %r13
addq %r15, %rcx
addq $0x4, %rcx
jmp 0x4915
leaq (%rdx,%r15), %r13
addq $0x5, %r13
addq %r15, %rcx
addq $0x5, %rcx
jmp 0x4915
leaq (%rdx,%r15), %r13
addq $0x6, %r13
addq %r15, %rcx
addq $0x6, %rcx
jmp 0x4915
leaq (%rdx,%r15), %r13
addq $0x7, %r13
addq %r15, %rcx
addq $0x7, %rcx
movl %ebx, %r15d
movq %rdi, %rbx
cmpq %rsi, %r13
je 0x4930
movb (%r13), %al
cmpb (%rcx), %al
jne 0x4933
incq %r13
incq %rcx
jmp 0x491b
movq %rsi, %r13
subq 0x18(%rsp), %r13
cmpw %r13w, %r11w
jae 0x4965
movl %r9d, %r12d
movq 0xa0(%rsp), %rcx
testq %rcx, %rcx
je 0x496a
incl %r11d
cmpw %r13w, %r11w
ja 0x496a
movzwl %r11w, %eax
movw %r12w, (%rcx,%rax,2)
incl %r11d
jmp 0x4951
movl %r11d, %r13d
jmp 0x4974
movzwl %r13w, %eax
cmpq %rax, (%rsp)
jbe 0x49ef
movq 0x20(%rbp), %rcx
cmpq %rcx, 0x30(%rsp)
je 0x49ad
movq 0x40(%rbp), %rax
movq 0x50(%rsp), %rdx
cmpw (%rax,%rdx,2), %r13w
jb 0x49ad
movl 0x38(%rbp), %edx
movq 0x30(%rbp), %r11
cmpl (%r11,%r14,4), %edx
jne 0x49ad
movq 0x28(%rbp), %rbx
movq %rcx, 0x30(%rsp)
movq %r11, 0x58(%rsp)
movl %edx, %r15d
movzwl (%rbx,%r14,2), %edx
cmpw %r8w, %dx
je 0x49ef
movzwl %dx, %eax
subl %eax, %r14d
leal 0x8000(%r14), %eax
cmpw %r8w, %dx
cmovbl %r14d, %eax
addl %eax, %r9d
leal -0x1(%r10), %eax
movl %r13d, %r11d
movl %r8d, %ecx
movl %edx, %r8d
cmpl $0x1, %r10d
movl %eax, %r10d
ja 0x4734
jmp 0x49ef
movl %r11d, %r13d
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rax
subq 0x10(%rax), %rsi
movq 0x60(%rsp), %r8
testq %r8, %r8
je 0x4a2b
movq (%r8), %rax
cmpw $0x0, (%rax,%rsi,2)
movq 0xa0(%rsp), %rcx
movq (%rsp), %rdx
je 0x4a3b
movq 0x8(%r8), %rax
cmpw $0x0, (%rax,%rsi,2)
setne %al
jmp 0x4a3d
xorl %eax, %eax
movq 0xa0(%rsp), %rcx
movq (%rsp), %rdx
jmp 0x4a3d
movb $0x1, %al
testq %rcx, %rcx
je 0x4aa7
cmpq $0x102, %rdx            # imm = 0x102
jne 0x4aa7
testq %r8, %r8
je 0x4aa7
testb %al, %al
jne 0x4aa7
movq (%r8), %rax
cmpw $0x1, (%rax,%rsi,2)
jne 0x4cef
movq 0x8(%r8), %rcx
cmpw $0x0, (%rcx,%rsi,2)
jne 0x4cef
xorl %edx, %edx
cmpw $0x3, %r13w
movl %r12d, %edi
cmovbl %edx, %edi
movw %di, (%rcx,%rsi,2)
cmovael %r13d, %edx
movw %dx, (%rax,%rsi,2)
movzwl %r13w, %r14d
movq 0xa0(%rsp), %rdi
movq %r14, %rdx
movq %r8, %rcx
callq 0x1b5b
movq (%rsp), %rdx
jmp 0x4aab
movzwl %r13w, %r14d
cmpq %r14, %rdx
movq 0xa8(%rsp), %rax
jb 0x4d0e
movw %r12w, (%rax)
movq 0xb0(%rsp), %rax
movw %r13w, (%rax)
addq 0x28(%rsp), %r14
cmpq 0x8(%rsp), %r14
jbe 0x4bc9
leaq 0x2ebd(%rip), %rdi      # 0x79a0
leaq 0x2e29(%rip), %rsi      # 0x7913
leaq 0x2ec5(%rip), %rcx      # 0x79b6
movl $0x161, %edx            # imm = 0x161
callq 0x10b0
movq 0xb0(%rsp), %r13
movq (%rdi), %rax
movzwl (%rax,%r14,2), %ecx
cmpq %r12, %rcx
movl %r12d, %eax
cmovbel %ecx, %eax
movw %ax, (%r13)
movq 0xa0(%rsp), %rcx
testq %rcx, %rcx
je 0x4b9f
movq %rsi, %rbp
cmpq $0x102, %r12            # imm = 0x102
sete %bl
movzwl %ax, %edx
movq %r14, %rsi
movq %rcx, %r12
callq 0x1c5b
movzwl (%r13), %eax
movzwl (%r12,%rax,2), %eax
movq 0xa8(%rsp), %rcx
movw %ax, (%rcx)
movzwl (%r13), %eax
cmpw $0x3, %ax
setae %cl
andb %bl, %cl
cmpb $0x1, %cl
jne 0x4bb8
movzwl %ax, %ecx
movzwl (%r12,%rcx,2), %ecx
movq 0x8(%rbp), %rdx
movq 0x8(%rdx), %rdx
cmpw (%rdx,%r14,2), %cx
je 0x4bb8
leaq 0x2ff7(%rip), %rdi      # 0x7b7e
leaq 0x2d85(%rip), %rsi      # 0x7913
leaq 0x3011(%rip), %rcx      # 0x7ba6
movl $0xb1, %edx
callq 0x10b0
movq 0x8(%rdi), %rax
movzwl (%rax,%r14,2), %eax
movq 0xa8(%rsp), %rcx
movw %ax, (%rcx)
movzwl (%r13), %eax
movzwl %ax, %eax
addq %r15, %rax
cmpq 0x8(%rsp), %rax
ja 0x4d2d
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2eee(%rip), %rdi      # 0x7acd
leaq 0x2d2d(%rip), %rsi      # 0x7913
leaq 0x2dc9(%rip), %rcx      # 0x79b6
movl $0x118, %edx            # imm = 0x118
callq 0x10b0
leaq 0x2ede(%rip), %rdi      # 0x7adc
leaq 0x2d0e(%rip), %rsi      # 0x7913
leaq 0x2daa(%rip), %rcx      # 0x79b6
movl $0x119, %edx            # imm = 0x119
callq 0x10b0
leaq 0x2e99(%rip), %rdi      # 0x7ab6
leaq 0x2cef(%rip), %rsi      # 0x7913
leaq 0x2d8b(%rip), %rcx      # 0x79b6
movl $0x117, %edx            # imm = 0x117
callq 0x10b0
leaq 0x2eb4(%rip), %rdi      # 0x7af0
leaq 0x2cd0(%rip), %rsi      # 0x7913
leaq 0x2d6c(%rip), %rcx      # 0x79b6
movl $0x11d, %edx            # imm = 0x11D
callq 0x10b0
leaq 0x2e04(%rip), %rdi      # 0x7a5f
leaq 0x2cb1(%rip), %rsi      # 0x7913
leaq 0x2d4d(%rip), %rcx      # 0x79b6
movl $0xf8, %edx
callq 0x10b0
leaq 0x2dff(%rip), %rdi      # 0x7a79
leaq 0x2c92(%rip), %rsi      # 0x7913
leaq 0x2d2e(%rip), %rcx      # 0x79b6
movl $0xf9, %edx
callq 0x10b0
leaq 0x2dfa(%rip), %rdi      # 0x7a93
leaq 0x2c73(%rip), %rsi      # 0x7913
leaq 0x2d0f(%rip), %rcx      # 0x79b6
movl $0xfa, %edx
callq 0x10b0
leaq 0x2de6(%rip), %rdi      # 0x7a9e
leaq 0x2c54(%rip), %rsi      # 0x7913
leaq 0x2cf0(%rip), %rcx      # 0x79b6
movl $0x10a, %edx            # imm = 0x10A
callq 0x10b0
leaq 0x2dd4(%rip), %rdi      # 0x7aab
leaq 0x2c35(%rip), %rsi      # 0x7913
leaq 0x2cd1(%rip), %rcx      # 0x79b6
movl $0x10f, %edx            # imm = 0x10F
callq 0x10b0
leaq 0x2f2c(%rip), %rdi      # 0x7c22
leaq 0x2c16(%rip), %rsi      # 0x7913
leaq 0x2f57(%rip), %rcx      # 0x7c5b
movl $0xd2, %edx
callq 0x10b0
leaq 0x2de7(%rip), %rdi      # 0x7afc
leaq 0x2bf7(%rip), %rsi      # 0x7913
leaq 0x2c93(%rip), %rcx      # 0x79b6
movl $0x15d, %edx            # imm = 0x15D
callq 0x10b0
leaq 0x2c6c(%rip), %rdi      # 0x79a0
leaq 0x2bd8(%rip), %rsi      # 0x7913
leaq 0x2c74(%rip), %rcx      # 0x79b6
movl $0xf3, %edx
callq 0x10b0