// File: ivan-tkatchev[P]yalz77/./lz77.h
// Function: lz77::offsets_dict_t::operator()(unsigned short, unsigned char const*, unsigned char const*, unsigned char const*, unsigned long&, unsigned long&, unsigned long&)
void operator()(uint16_t packed, const unsigned char* i0, const unsigned char* i, const unsigned char* e,
                    size_t& maxrun, size_t& maxoffset, size_t& maxgain) {

        // Select a range of values representing a circular buffer.
        // The first value is the index of the buffer head, the rest are
        // the values of the buffer itself. 

        size_t* cb_start = &offsets[packed * (searchlen + 1)];

        size_t* cb_beg = (cb_start + 1);
        size_t* cb_end = (cb_start + 1 + searchlen);
        size_t* cb_head = cb_beg + *cb_start;

        size_t* cb_i = cb_head;

        while (1) {

            cb_i = prev(cb_beg, cb_end, cb_i);

            if (*cb_i == 0)
                break;

            // The stored value is position + 1 to allow 0 to mean 'uninitialized offset'.
            size_t pos = *cb_i - 1;

            size_t offset = i - i0 - pos;
            size_t run = substr_run(i, e, i0 + pos, e);
            size_t gain = gains(run, offset);

            if (gain > maxgain) {
                maxrun = run;
                maxoffset = offset;
                maxgain = gain;
            }

            if (cb_i == cb_head)
                break;
        }

        *cb_start = push_back(cb_beg, cb_end, cb_head, i - i0 + 1);
    }

// Assembly:
subq $0x88, %rsp
movw %si, %ax
movq 0x98(%rsp), %rsi
movq 0x90(%rsp), %rsi
movq %rdi, 0x80(%rsp)
movw %ax, 0x7e(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movq %r9, 0x58(%rsp)
movq 0x80(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movzwl 0x7e(%rsp), %eax
movl %eax, %esi
movq 0x18(%rdi), %rax
addq $0x1, %rax
imulq %rax, %rsi
callq 0x4f80
movq 0x8(%rsp), %rcx
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rax
addq $0x8, %rax
movq 0x18(%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movq (%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x4fa0
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x46bd
jmp 0x476e
movq 0x30(%rsp), %rax
movq (%rax), %rax
subq $0x1, %rax
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rax
movq 0x70(%rsp), %rcx
subq %rcx, %rax
subq 0x28(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq 0x28(%rsp), %rdx
movq 0x60(%rsp), %rcx
callq 0x4fe0
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x5060
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x98(%rsp), %rcx
cmpq (%rcx), %rax
jbe 0x475b
movq 0x18(%rsp), %rcx
movq 0x58(%rsp), %rax
movq %rcx, (%rax)
movq 0x20(%rsp), %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rsp), %rcx
movq 0x98(%rsp), %rax
movq %rcx, (%rax)
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0x4769
jmp 0x476e
jmp 0x4694
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %rax
subq %rax, %rcx
addq $0x1, %rcx
callq 0x50f0
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, (%rax)
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)