// File: rofl0r[P]ascc/util_string.cpp
// Function: AGS::Common::String::FindSection(char, unsigned long, unsigned long, bool, bool, unsigned long&, unsigned long&) const
bool String::FindSection(char separator, size_t first, size_t last, bool exclude_first_sep, bool exclude_last_sep,
                        size_t &from, size_t &to) const
{
    if (!_meta || !separator)
    {
        return false;
    }
    if (first > last)
    {
        return false;
    }

    size_t this_field = 0;
    size_t slice_from = 0;
    size_t slice_to = _meta->Length;
    size_t slice_at = -1;
    do
    {
        slice_at = FindChar(separator, slice_at + 1);
        if (slice_at == -1)
            slice_at = _meta->Length;
        // found where previous field ends
        if (this_field == last)
        {
            // if previous field is the last one to be included,
            // then set the section tail
            slice_to = exclude_last_sep ? slice_at : slice_at + 1;
        }
        if (slice_at != _meta->Length)
        {
            this_field++;
            if (this_field == first)
            {
                // if the new field is the first one to be included,
                // then set the section head
                slice_from = exclude_first_sep ? slice_at + 1 : slice_at;
            }
        }
    }
    while (slice_at < _meta->Length && this_field <= last);

    // the search is a success if at least the first field was found
    if (this_field >= first)
    {
        // correct the indices to stay in the [0; length] range
        assert(slice_from <= slice_to);
        from = Math::Clamp(slice_from, (size_t)0, _meta->Length);
        to   = Math::Clamp(slice_to, (size_t)0, _meta->Length);
        return true;
    }
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl %r8d, -0x84(%rbp)
movq %rcx, %rax
movl -0x84(%rbp), %ecx
movq %rax, -0x80(%rbp)
movq %rdx, %rax
movq -0x80(%rbp), %rdx
movq %rax, -0x78(%rbp)
movl %esi, %eax
movq -0x78(%rbp), %rsi
movl %eax, -0x6c(%rbp)
movq %rdi, %r8
movl -0x6c(%rbp), %edi
movb %r9b, %al
movq 0x18(%rbp), %r9
movq 0x10(%rbp), %r9
movq %r8, -0x10(%rbp)
movb %dil, -0x11(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
andb $0x1, %cl
movb %cl, -0x29(%rbp)
andb $0x1, %al
movb %al, -0x2a(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, (%rax)
je 0x28c73
cmpb $0x0, -0x11(%rbp)
jne 0x28c7c
movb $0x0, -0x1(%rbp)
jmp 0x28e48
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
jbe 0x28c8f
movb $0x0, -0x1(%rbp)
jmp 0x28e48
movq -0x68(%rbp), %rax
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq (%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
movq $-0x1, -0x50(%rbp)
movq -0x68(%rbp), %rdi
movb -0x11(%rbp), %al
movq -0x50(%rbp), %rdx
addq $0x1, %rdx
movsbl %al, %esi
callq 0x289f0
movq %rax, -0x50(%rbp)
cmpq $-0x1, -0x50(%rbp)
jne 0x28ce7
movq -0x68(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x28d1e
testb $0x1, -0x2a(%rbp)
je 0x28d04
movq -0x50(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x28d13
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x68(%rbp), %rcx
movq -0x50(%rbp), %rax
movq (%rcx), %rcx
cmpq 0x10(%rcx), %rax
je 0x28d74
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x28d72
testb $0x1, -0x29(%rbp)
je 0x28d5c
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x28d67
movq -0x50(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x28d74
jmp 0x28d76
movq -0x68(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax), %rdx
xorl %eax, %eax
cmpq 0x10(%rdx), %rcx
movb %al, -0x99(%rbp)
jae 0x28da0
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
setbe %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x28cb6
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jb 0x28e44
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
ja 0x28dc8
jmp 0x28de7
leaq 0xb1f5(%rip), %rdi      # 0x33fc4
leaq 0xb205(%rip), %rsi      # 0x33fdb
movl $0x11f, %edx            # imm = 0x11F
leaq 0xb209(%rip), %rcx      # 0x33feb
callq 0x4290
movq -0x68(%rbp), %rax
movq $0x0, -0x58(%rbp)
movq (%rax), %rdx
addq $0x10, %rdx
leaq -0x40(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x2abd0
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq (%rcx), %rdx
movq 0x10(%rbp), %rcx
movq %rdx, (%rcx)
movq $0x0, -0x60(%rbp)
movq (%rax), %rdx
addq $0x10, %rdx
leaq -0x48(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2abd0
movq (%rax), %rcx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x28e48
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)