// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %rcx, %r15
movq %rdx, 0x40(%rsp)
movq %rsi, %rbp
movq %rdi, %r8
movq 0xb0(%rsp), %rcx
movq 0xa8(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq (%rbp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rbp), %rbx
movq 0x10(%rbp), %rax
movq %rax, 0x58(%rsp)
movl 0x18(%rbp), %r9d
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x6b4c
movq %r15, %r14
subq 0x10(%r8), %r14
movq (%rdi), %rax
movzwl (%rax,%r14,2), %edx
testq %rdx, %rdx
je 0x6a78
movq 0x8(%rdi), %rax
cmpw $0x0, (%rax,%r14,2)
je 0x6b50
movb $0x1, %al
cmpq $0x102, %r12            # imm = 0x102
je 0x6acf
cmpq %r12, %rdx
jbe 0x6acf
cmpq $0x0, 0xa0(%rsp)
je 0x6b50
movq %r14, %rsi
movq %r8, 0x18(%rsp)
movl %r9d, 0xc(%rsp)
callq 0x2116
movl 0xc(%rsp), %r9d
movq 0x18(%rsp), %r8
movq 0xa8(%rsp), %rsi
movq 0xb0(%rsp), %rcx
movl %eax, %eax
cmpq %r12, %rax
setae %al
movq 0x8(%r8), %rdi
testq %rdi, %rdi
movq 0xa0(%rsp), %rdx
je 0x6b58
testb %al, %al
je 0x6b58
movq %r13, 0x10(%rsp)
movq (%rdi), %rax
movzwl (%rax,%r14,2), %r13d
testq %rdx, %rdx
je 0x6fbd
movl %r9d, 0xc(%rsp)
movl %r13d, %edx
movq %r14, %rsi
movq %r8, 0x18(%rsp)
callq 0x2116
movq 0x18(%rsp), %r8
movq 0x8(%r8), %rdi
movq (%rdi), %rcx
movzwl (%rcx,%r14,2), %ecx
cmpl %r13d, %eax
jae 0x6fe1
movq %rcx, %r12
movq 0x10(%rsp), %r13
movq 0xb0(%rsp), %rcx
movq 0xa8(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movl 0xc(%rsp), %r9d
jmp 0x6b58
xorl %edi, %edi
jmp 0x6b58
movq 0xa0(%rsp), %rdx
cmpq $0x103, %r12            # imm = 0x103
jae 0x70fe
cmpq $0x2, %r12
jbe 0x711d
movq %r13, %r10
subq %r15, %r10
jbe 0x713c
cmpq $0x2, %r10
ja 0x6b8e
xorl %eax, %eax
movw %ax, (%rcx)
movw %ax, (%rsi)
jmp 0x7073
leaq (%r12,%r15), %rax
cmpq %r13, %rax
cmovbeq %r12, %r10
cmpl $0x10000, %r9d          # imm = 0x10000
jge 0x715b
movq %r8, 0x18(%rsp)
movq %rdi, 0x60(%rsp)
movl %r15d, %esi
andl $0x7fff, %esi           # imm = 0x7FFF
movl %r9d, 0xc(%rsp)
movslq %r9d, %rax
movq 0x30(%rsp), %rcx
movzwl (%rcx,%rax,4), %ecx
movq %rsi, 0x48(%rsp)
cmpl %esi, %ecx
jne 0x717a
movq 0x40(%rsp), %rax
addq %r15, %rax
leaq (%rax,%r10), %rsi
movq %r10, 0x20(%rsp)
movq %rax, 0x10(%rsp)
leaq -0x8(%r10,%rax), %rax
movq %rax, 0x50(%rsp)
movzwl (%rbx,%rcx,2), %r8d
movl %ecx, %eax
subl %r8d, %eax
leal 0x8000(%rax), %r9d
cmoval %eax, %r9d
movl $0x2000, %r10d          # imm = 0x2000
movw $0x1, %r11w
movq $0x0, 0x38(%rsp)
movq %rbp, 0x28(%rsp)
cmpl $0x7fff, %r9d           # imm = 0x7FFF
ja 0x6ee6
testw %r8w, %r8w
js 0x70c0
movzwl %cx, %eax
cmpw (%rbx,%rax,2), %r8w
jne 0x7082
movzwl %r8w, %r14d
movq 0x58(%rsp), %rax
movl 0xc(%rsp), %ecx
cmpl %ecx, (%rax,%r14,4)
jne 0x70a1
testl %r9d, %r9d
je 0x6e6a
movl %r9d, %eax
movq %r15, %rdi
subq %rax, %rdi
jb 0x70df
movq 0x40(%rsp), %rax
leaq (%rax,%rdi), %rcx
movzwl %r11w, %eax
leaq (%rax,%r15), %r12
cmpq %r13, %r12
jae 0x6cad
movq %rbx, %rbp
movq 0x10(%rsp), %rbx
movb (%rbx,%rax), %r12b
movq %rbp, %rbx
movq 0x28(%rsp), %rbp
cmpb (%rcx,%rax), %r12b
jne 0x6e6a
movq 0x40(%rbp), %r12
movq 0x48(%rsp), %rax
movzwl (%r12,%rax,2), %eax
cmpw $0x3, %ax
jb 0x6d0b
movq %rbx, %rbp
movq %r15, %rdx
movq %r13, %r15
movq 0x10(%rsp), %rbx
movb (%rbx), %r13b
cmpb (%rcx), %r13b
jne 0x6d12
andl $0x7fff, %edi           # imm = 0x7FFF
movzwl (%r12,%rdi,2), %edi
cmpw %di, %ax
cmovael %edi, %eax
movl %eax, %edi
movl $0xffff, %r13d          # imm = 0xFFFF
andl %r13d, %edi
movq 0x20(%rsp), %r12
cmpq %rdi, %r12
cmovbl %r12d, %eax
andl %r13d, %eax
leaq (%rbx,%rax), %r12
addq %rax, %rcx
jmp 0x6d15
movq 0x10(%rsp), %r12
jmp 0x6d2b
movq %rbx, %r12
movq %r15, %r13
movq %rdx, %r15
movq 0xa0(%rsp), %rdx
movq %rbp, %rbx
movq 0x28(%rsp), %rbp
cmpq 0x50(%rsp), %r12
jae 0x6e15
xorl %edi, %edi
movb (%r12,%rdi), %al
cmpb (%rcx,%rdi), %al
jne 0x6da1
movb 0x1(%r12,%rdi), %al
cmpb 0x1(%rcx,%rdi), %al
jne 0x6da9
movb 0x2(%r12,%rdi), %al
cmpb 0x2(%rcx,%rdi), %al
jne 0x6db7
movb 0x3(%r12,%rdi), %al
cmpb 0x3(%rcx,%rdi), %al
jne 0x6dc7
movb 0x4(%r12,%rdi), %al
cmpb 0x4(%rcx,%rdi), %al
jne 0x6dd7
movb 0x5(%r12,%rdi), %al
cmpb 0x5(%rcx,%rdi), %al
jne 0x6de7
movb 0x6(%r12,%rdi), %al
cmpb 0x6(%rcx,%rdi), %al
jne 0x6df7
movb 0x7(%r12,%rdi), %al
cmpb 0x7(%rcx,%rdi), %al
jne 0x6e07
leaq (%r12,%rdi), %rax
addq $0x8, %rax
addq $0x8, %rdi
cmpq 0x50(%rsp), %rax
jb 0x6d38
addq %rdi, %r12
addq %rdi, %rcx
jmp 0x6e15
addq %rdi, %r12
incq %r12
addq %rdi, %rcx
incq %rcx
jmp 0x6e15
addq %rdi, %r12
addq $0x2, %r12
addq %rdi, %rcx
addq $0x2, %rcx
jmp 0x6e15
addq %rdi, %r12
addq $0x3, %r12
addq %rdi, %rcx
addq $0x3, %rcx
jmp 0x6e15
addq %rdi, %r12
addq $0x4, %r12
addq %rdi, %rcx
addq $0x4, %rcx
jmp 0x6e15
addq %rdi, %r12
addq $0x5, %r12
addq %rdi, %rcx
addq $0x5, %rcx
jmp 0x6e15
addq %rdi, %r12
addq $0x6, %r12
addq %rdi, %rcx
addq $0x6, %rcx
jmp 0x6e15
addq %rdi, %r12
addq $0x7, %r12
addq %rdi, %rcx
addq $0x7, %rcx
cmpq %rsi, %r12
je 0x6e2a
movb (%r12), %al
cmpb (%rcx), %al
jne 0x6e2d
incq %r12
incq %rcx
jmp 0x6e15
movq %rsi, %r12
movq 0x10(%rsp), %rax
subq %rax, %r12
cmpw %r12w, %r11w
jae 0x6e6a
testq %rdx, %rdx
je 0x6e55
incl %r11d
movzwl %r11w, %eax
movw %r9w, (%rdx,%rax,2)
incl %r11d
cmpw %r12w, %r11w
jbe 0x6e43
movzwl %r12w, %eax
movl %r9d, %ecx
movq %rcx, 0x38(%rsp)
cmpq %rax, 0x20(%rsp)
ja 0x6e6d
jmp 0x6ee9
movl %r11d, %r12d
movq 0x20(%rbp), %rax
cmpq %rax, 0x30(%rsp)
je 0x6ea7
movq 0x40(%rbp), %rcx
movq 0x48(%rsp), %rdi
cmpw (%rcx,%rdi,2), %r12w
jb 0x6ea7
movl 0x38(%rbp), %ecx
movq 0x30(%rbp), %rdi
cmpl (%rdi,%r14,4), %ecx
jne 0x6ea7
movq 0x28(%rbp), %rbx
movq %rax, 0x30(%rsp)
movq %rdi, 0x58(%rsp)
movl %ecx, 0xc(%rsp)
movzwl (%rbx,%r14,2), %eax
cmpw %r8w, %ax
je 0x6ee9
movzwl %ax, %ecx
subl %ecx, %r14d
leal 0x8000(%r14), %ecx
cmpw %r8w, %ax
cmovbl %r14d, %ecx
addl %ecx, %r9d
leal -0x1(%r10), %edi
movl %r12d, %r11d
movl %r8d, %ecx
movl %eax, %r8d
cmpl $0x1, %r10d
movl %edi, %r10d
ja 0x6c25
jmp 0x6ee9
movl %r11d, %r12d
movzwl %r12w, %r14d
movq 0x60(%rsp), %rcx
testq %rcx, %rcx
movq 0x20(%rsp), %r8
movq 0x38(%rsp), %rbx
je 0x6f72
movq %r15, %rsi
movq 0x18(%rsp), %rax
subq 0x10(%rax), %rsi
movq (%rcx), %rax
movzwl (%rax,%rsi,2), %edi
testw %di, %di
je 0x6f72
cmpq $0x0, 0xa0(%rsp)
je 0x6f72
cmpq $0x102, %r8             # imm = 0x102
jne 0x6f72
movq 0x8(%rcx), %rdx
cmpw $0x0, (%rdx,%rsi,2)
jne 0x6f72
cmpw $0x1, %di
jne 0x71d7
xorl %r8d, %r8d
cmpw $0x3, %r14w
movl %ebx, %edi
cmovbl %r8d, %edi
movw %di, (%rdx,%rsi,2)
cmovael %r14d, %r8d
movw %r8w, (%rax,%rsi,2)
movq 0xa0(%rsp), %rdi
movq %r14, %rdx
callq 0x200b
movq 0x20(%rsp), %r8
cmpq %r14, %r8
jb 0x7199
movq 0xa8(%rsp), %rax
movw %bx, (%rax)
movq 0xb0(%rsp), %rax
movw %r12w, (%rax)
addq %r15, %r14
cmpq %r13, %r14
jbe 0x7073
leaq 0x3b01(%rip), %rdi      # 0xaaa6
leaq 0x3a6d(%rip), %rsi      # 0xaa19
leaq 0x3b09(%rip), %rcx      # 0xaabc
movl $0x161, %edx            # imm = 0x161
callq 0x10b0
cmpq %r12, %r13
cmoval %r12d, %r13d
movw %r13w, (%rcx)
movq 0x8(%rdi), %rax
movzwl (%rax,%r14,2), %eax
movw %ax, (%rsi)
movzwl (%rcx), %eax
movq 0x10(%rsp), %rsi
jmp 0x7064
cmpq $0x102, %r12            # imm = 0x102
sete %bl
cmpq %r12, %rcx
cmoval %r12d, %ecx
movq 0xb0(%rsp), %r12
movw %cx, (%r12)
movzwl %cx, %edx
movq %r14, %rsi
movq 0xa0(%rsp), %r13
movq %r13, %rcx
callq 0x213c
movzwl (%r12), %eax
movzwl (%r13,%rax,2), %eax
movq 0xa8(%rsp), %rcx
movw %ax, (%rcx)
movzwl (%r12), %eax
cmpw $0x3, %ax
setae %cl
andb %bl, %cl
cmpb $0x1, %cl
movq 0x10(%rsp), %rsi
jne 0x7064
movzwl %ax, %ecx
movzwl (%r13,%rcx,2), %ecx
movq 0x18(%rsp), %rdx
movq 0x8(%rdx), %rdx
movq 0x8(%rdx), %rdx
cmpw (%rdx,%r14,2), %cx
jne 0x71f6
movzwl %ax, %eax
addq %r15, %rax
cmpq %rsi, %rax
ja 0x71b8
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3b4a(%rip), %rdi      # 0xabd3
leaq 0x3989(%rip), %rsi      # 0xaa19
leaq 0x3a25(%rip), %rcx      # 0xaabc
movl $0x118, %edx            # imm = 0x118
callq 0x10b0
leaq 0x3b3a(%rip), %rdi      # 0xabe2
leaq 0x396a(%rip), %rsi      # 0xaa19
leaq 0x3a06(%rip), %rcx      # 0xaabc
movl $0x119, %edx            # imm = 0x119
callq 0x10b0
leaq 0x3af5(%rip), %rdi      # 0xabbc
leaq 0x394b(%rip), %rsi      # 0xaa19
leaq 0x39e7(%rip), %rcx      # 0xaabc
movl $0x117, %edx            # imm = 0x117
callq 0x10b0
leaq 0x3b10(%rip), %rdi      # 0xabf6
leaq 0x392c(%rip), %rsi      # 0xaa19
leaq 0x39c8(%rip), %rcx      # 0xaabc
movl $0x11d, %edx            # imm = 0x11D
callq 0x10b0
leaq 0x3a60(%rip), %rdi      # 0xab65
leaq 0x390d(%rip), %rsi      # 0xaa19
leaq 0x39a9(%rip), %rcx      # 0xaabc
movl $0xf8, %edx
callq 0x10b0
leaq 0x3a5b(%rip), %rdi      # 0xab7f
leaq 0x38ee(%rip), %rsi      # 0xaa19
leaq 0x398a(%rip), %rcx      # 0xaabc
movl $0xf9, %edx
callq 0x10b0
leaq 0x3a56(%rip), %rdi      # 0xab99
leaq 0x38cf(%rip), %rsi      # 0xaa19
leaq 0x396b(%rip), %rcx      # 0xaabc
movl $0xfa, %edx
callq 0x10b0
leaq 0x3a42(%rip), %rdi      # 0xaba4
leaq 0x38b0(%rip), %rsi      # 0xaa19
leaq 0x394c(%rip), %rcx      # 0xaabc
movl $0x10a, %edx            # imm = 0x10A
callq 0x10b0
leaq 0x3a30(%rip), %rdi      # 0xabb1
leaq 0x3891(%rip), %rsi      # 0xaa19
leaq 0x392d(%rip), %rcx      # 0xaabc
movl $0x10f, %edx            # imm = 0x10F
callq 0x10b0
leaq 0x3a62(%rip), %rdi      # 0xac02
leaq 0x3872(%rip), %rsi      # 0xaa19
leaq 0x390e(%rip), %rcx      # 0xaabc
movl $0x15d, %edx            # imm = 0x15D
callq 0x10b0
leaq 0x38e7(%rip), %rdi      # 0xaaa6
leaq 0x3853(%rip), %rsi      # 0xaa19
leaq 0x38ef(%rip), %rcx      # 0xaabc
movl $0xf3, %edx
callq 0x10b0
leaq 0x3b4a(%rip), %rdi      # 0xad28
leaq 0x3834(%rip), %rsi      # 0xaa19
leaq 0x3b75(%rip), %rcx      # 0xad61
movl $0xd2, %edx
callq 0x10b0
leaq 0x3a87(%rip), %rdi      # 0xac84
leaq 0x3815(%rip), %rsi      # 0xaa19
leaq 0x3aa1(%rip), %rcx      # 0xacac
movl $0xb1, %edx
callq 0x10b0