// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xa8, %rsp
movq 0xc0(%rsp), %rax
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rax
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdx, 0x90(%rsp)
movq %rcx, 0x88(%rsp)
movq %r8, 0x80(%rsp)
movq %r9, 0x78(%rsp)
movq 0x88(%rsp), %rax
andq $0x7fff, %rax           # imm = 0x7FFF
movw %ax, 0x76(%rsp)
movw $0x0, 0x70(%rsp)
movw $0x1, 0x6e(%rsp)
movl $0x2000, 0x44(%rsp)     # imm = 0x2000
movl $0x0, 0x40(%rsp)
movq 0x98(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x98(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x98(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x98(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x24(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0xb0(%rsp), %rcx
movq 0xb8(%rsp), %r8
movq 0xc0(%rsp), %r9
leaq 0x78(%rsp), %rdx
callq 0x83e0
cmpl $0x0, %eax
je 0x7ceb
movq 0x88(%rsp), %rax
movq 0xc0(%rsp), %rcx
movzwl (%rcx), %ecx
addq %rcx, %rax
cmpq 0x80(%rsp), %rax
ja 0x7cc7
jmp 0x7ce6
leaq 0x5ce6(%rip), %rdi      # 0xd9b4
leaq 0x5c52(%rip), %rsi      # 0xd927
movl $0xf3, %edx
leaq 0x5ce9(%rip), %rcx      # 0xd9ca
callq 0x10b0
jmp 0x83ce
cmpq $0x102, 0x78(%rsp)      # imm = 0x102
ja 0x7cf8
jmp 0x7d17
leaq 0x5d74(%rip), %rdi      # 0xda73
leaq 0x5c21(%rip), %rsi      # 0xd927
movl $0xf8, %edx
leaq 0x5cb8(%rip), %rcx      # 0xd9ca
callq 0x10b0
cmpq $0x3, 0x78(%rsp)
jb 0x7d21
jmp 0x7d40
leaq 0x5d65(%rip), %rdi      # 0xda8d
leaq 0x5bf8(%rip), %rsi      # 0xd927
movl $0xf9, %edx
leaq 0x5c8f(%rip), %rcx      # 0xd9ca
callq 0x10b0
movq 0x88(%rsp), %rax
cmpq 0x80(%rsp), %rax
jae 0x7d54
jmp 0x7d73
leaq 0x5d4c(%rip), %rdi      # 0xdaa7
leaq 0x5bc5(%rip), %rsi      # 0xd927
movl $0xfa, %edx
leaq 0x5c5c(%rip), %rcx      # 0xd9ca
callq 0x10b0
movq 0x80(%rsp), %rax
subq 0x88(%rsp), %rax
cmpq $0x3, %rax
jae 0x7da8
movq 0xc0(%rsp), %rax
movw $0x0, (%rax)
movq 0xb8(%rsp), %rax
movw $0x0, (%rax)
jmp 0x83ce
movq 0x88(%rsp), %rax
addq 0x78(%rsp), %rax
cmpq 0x80(%rsp), %rax
jbe 0x7dd4
movq 0x80(%rsp), %rax
subq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x90(%rsp), %rax
addq 0x88(%rsp), %rax
addq 0x78(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
addq $-0x8, %rax
movq %rax, 0x48(%rsp)
cmpl $0x10000, 0x24(%rsp)    # imm = 0x10000
jge 0x7e08
jmp 0x7e27
leaq 0x5ca3(%rip), %rdi      # 0xdab2
leaq 0x5b11(%rip), %rsi      # 0xd927
movl $0x10a, %edx            # imm = 0x10A
leaq 0x5ba8(%rip), %rcx      # 0xd9ca
callq 0x10b0
movq 0x38(%rsp), %rax
movslq 0x24(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movw %ax, 0x72(%rsp)
movq 0x30(%rsp), %rax
movzwl 0x72(%rsp), %ecx
movw (%rax,%rcx,2), %ax
movw %ax, 0x74(%rsp)
movzwl 0x72(%rsp), %eax
movzwl 0x76(%rsp), %ecx
cmpl %ecx, %eax
jne 0x7e5c
jmp 0x7e7b
leaq 0x5c5c(%rip), %rdi      # 0xdabf
leaq 0x5abd(%rip), %rsi      # 0xd927
movl $0x10f, %edx            # imm = 0x10F
leaq 0x5b54(%rip), %rcx      # 0xd9ca
callq 0x10b0
movzwl 0x74(%rsp), %eax
movzwl 0x72(%rsp), %ecx
cmpl %ecx, %eax
jge 0x7e9b
movzwl 0x72(%rsp), %eax
movzwl 0x74(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x14(%rsp)
jmp 0x7eb2
movzwl 0x74(%rsp), %ecx
movl $0x8000, %eax           # imm = 0x8000
subl %ecx, %eax
movzwl 0x72(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x40(%rsp)
cmpl $0x8000, 0x40(%rsp)     # imm = 0x8000
jae 0x830f
movw $0x0, 0x22(%rsp)
movzwl 0x74(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jge 0x7edd
jmp 0x7efc
leaq 0x5be6(%rip), %rdi      # 0xdaca
leaq 0x5a3c(%rip), %rsi      # 0xd927
movl $0x117, %edx            # imm = 0x117
leaq 0x5ad3(%rip), %rcx      # 0xd9ca
callq 0x10b0
movzwl 0x74(%rsp), %eax
movq 0x30(%rsp), %rcx
movzwl 0x72(%rsp), %edx
movzwl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jne 0x7f15
jmp 0x7f34
leaq 0x5bc5(%rip), %rdi      # 0xdae1
leaq 0x5a04(%rip), %rsi      # 0xd927
movl $0x118, %edx            # imm = 0x118
leaq 0x5a9b(%rip), %rcx      # 0xd9ca
callq 0x10b0
movq 0x28(%rsp), %rax
movzwl 0x74(%rsp), %ecx
movl (%rax,%rcx,4), %eax
cmpl 0x24(%rsp), %eax
jne 0x7f49
jmp 0x7f68
leaq 0x5ba0(%rip), %rdi      # 0xdaf0
leaq 0x59d0(%rip), %rsi      # 0xd927
movl $0x119, %edx            # imm = 0x119
leaq 0x5a67(%rip), %rcx      # 0xd9ca
callq 0x10b0
cmpl $0x0, 0x40(%rsp)
jbe 0x81ee
movq 0x88(%rsp), %rax
cmpq 0x80(%rsp), %rax
jae 0x7f87
jmp 0x7fa6
leaq 0x5b19(%rip), %rdi      # 0xdaa7
leaq 0x5992(%rip), %rsi      # 0xd927
movl $0x11c, %edx            # imm = 0x11C
leaq 0x5a29(%rip), %rcx      # 0xd9ca
callq 0x10b0
movl 0x40(%rsp), %eax
cmpq 0x88(%rsp), %rax
ja 0x7fb6
jmp 0x7fd5
leaq 0x5b47(%rip), %rdi      # 0xdb04
leaq 0x5963(%rip), %rsi      # 0xd927
movl $0x11d, %edx            # imm = 0x11D
leaq 0x59fa(%rip), %rcx      # 0xd9ca
callq 0x10b0
movq 0x90(%rsp), %rax
addq 0x88(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x90(%rsp), %rax
movq 0x88(%rsp), %rcx
movl 0x40(%rsp), %edx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
movq 0x88(%rsp), %rax
movzwl 0x6e(%rsp), %ecx
addq %rcx, %rax
cmpq 0x80(%rsp), %rax
jae 0x804d
movq 0x60(%rsp), %rax
movzwl 0x6e(%rsp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq 0x58(%rsp), %rcx
movzwl 0x6e(%rsp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x8168
movq 0x98(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x88(%rsp), %rcx
andq $0x7fff, %rcx           # imm = 0x7FFF
movw (%rax,%rcx,2), %ax
movw %ax, 0x20(%rsp)
movzwl 0x20(%rsp), %eax
cmpl $0x2, %eax
jle 0x812d
movq 0x60(%rsp), %rax
movzbl (%rax), %eax
movq 0x58(%rsp), %rcx
movzbl (%rcx), %ecx
cmpl %ecx, %eax
jne 0x812d
movq 0x98(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x88(%rsp), %rcx
movl 0x40(%rsp), %edx
subq %rdx, %rcx
andq $0x7fff, %rcx           # imm = 0x7FFF
movw (%rax,%rcx,2), %ax
movw %ax, 0x1e(%rsp)
movzwl 0x20(%rsp), %eax
movzwl 0x1e(%rsp), %ecx
cmpl %ecx, %eax
jge 0x80db
movzwl 0x20(%rsp), %eax
movl %eax, 0x10(%rsp)
jmp 0x80e4
movzwl 0x1e(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movw %ax, 0x1c(%rsp)
movzwl 0x1c(%rsp), %eax
cmpq 0x78(%rsp), %rax
jbe 0x8103
movq 0x78(%rsp), %rax
movw %ax, 0x1c(%rsp)
movzwl 0x1c(%rsp), %ecx
movq 0x60(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movzwl 0x1c(%rsp), %ecx
movq 0x58(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rcx
callq 0x8710
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq 0x90(%rsp), %rcx
addq 0x88(%rsp), %rcx
subq %rcx, %rax
movw %ax, 0x22(%rsp)
movzwl 0x22(%rsp), %eax
movzwl 0x6e(%rsp), %ecx
cmpl %ecx, %eax
jle 0x81ec
cmpq $0x0, 0xb0(%rsp)
je 0x81c6
movzwl 0x6e(%rsp), %eax
addl $0x1, %eax
movw %ax, 0x1a(%rsp)
movzwl 0x1a(%rsp), %eax
movzwl 0x22(%rsp), %ecx
cmpl %ecx, %eax
jg 0x81c4
movl 0x40(%rsp), %eax
movw %ax, %dx
movq 0xb0(%rsp), %rax
movzwl 0x1a(%rsp), %ecx
movw %dx, (%rax,%rcx,2)
movw 0x1a(%rsp), %ax
addw $0x1, %ax
movw %ax, 0x1a(%rsp)
jmp 0x818e
jmp 0x81c6
movl 0x40(%rsp), %eax
movw %ax, 0x70(%rsp)
movw 0x22(%rsp), %ax
movw %ax, 0x6e(%rsp)
movzwl 0x22(%rsp), %eax
cmpq 0x78(%rsp), %rax
jb 0x81ea
jmp 0x830f
jmp 0x81ec
jmp 0x81ee
movq 0x38(%rsp), %rax
movq 0x98(%rsp), %rcx
cmpq 0x20(%rcx), %rax
je 0x8286
movzwl 0x6e(%rsp), %eax
movq 0x98(%rsp), %rcx
movq 0x40(%rcx), %rcx
movzwl 0x76(%rsp), %edx
movzwl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jl 0x8286
movq 0x98(%rsp), %rax
movl 0x38(%rax), %eax
movq 0x98(%rsp), %rcx
movq 0x30(%rcx), %rcx
movzwl 0x74(%rsp), %edx
cmpl (%rcx,%rdx,4), %eax
jne 0x8286
movq 0x98(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x98(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x98(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x98(%rsp), %rax
movl 0x38(%rax), %eax
movl %eax, 0x24(%rsp)
movw 0x74(%rsp), %ax
movw %ax, 0x72(%rsp)
movq 0x30(%rsp), %rax
movzwl 0x74(%rsp), %ecx
movw (%rax,%rcx,2), %ax
movw %ax, 0x74(%rsp)
movzwl 0x74(%rsp), %eax
movzwl 0x72(%rsp), %ecx
cmpl %ecx, %eax
jne 0x82b3
jmp 0x830f
movzwl 0x74(%rsp), %eax
movzwl 0x72(%rsp), %ecx
cmpl %ecx, %eax
jge 0x82d3
movzwl 0x72(%rsp), %eax
movzwl 0x74(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0xc(%rsp)
jmp 0x82ea
movzwl 0x74(%rsp), %ecx
movl $0x8000, %eax           # imm = 0x8000
subl %ecx, %eax
movzwl 0x72(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
addl 0x40(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x44(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x44(%rsp)
jg 0x830a
jmp 0x830f
jmp 0x7eba
movq 0xa0(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0xb0(%rsp), %rcx
movw 0x70(%rsp), %ax
movzwl %ax, %r8d
movzwl 0x6e(%rsp), %r9d
callq 0x8960
movzwl 0x6e(%rsp), %eax
cmpq 0x78(%rsp), %rax
ja 0x834e
jmp 0x836d
leaq 0x57bb(%rip), %rdi      # 0xdb10
leaq 0x55cb(%rip), %rsi      # 0xd927
movl $0x15d, %edx            # imm = 0x15D
leaq 0x5662(%rip), %rcx      # 0xd9ca
callq 0x10b0
movw 0x70(%rsp), %cx
movq 0xb8(%rsp), %rax
movw %cx, (%rax)
movw 0x6e(%rsp), %cx
movq 0xc0(%rsp), %rax
movw %cx, (%rax)
movq 0x88(%rsp), %rax
movq 0xc0(%rsp), %rcx
movzwl (%rcx), %ecx
addq %rcx, %rax
cmpq 0x80(%rsp), %rax
ja 0x83af
jmp 0x83ce
leaq 0x55fe(%rip), %rdi      # 0xd9b4
leaq 0x556a(%rip), %rsi      # 0xd927
movl $0x161, %edx            # imm = 0x161
leaq 0x5601(%rip), %rcx      # 0xd9ca
callq 0x10b0
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)