// File: /workspace/github_cpp_makefile_star2_O3/rhoot[P]sp/tests/../include/sp.hpp
// Function: void sp::do_format<Foo>(sp::IWriter&, sp::StringView const&, int*, Foo&&)
void do_format(IWriter& writer, const StringView& fmt, int32_t* prevIndex, Args&&... args)
    {
        enum State {
            STATE_OPENER,
            STATE_INDEX,
            STATE_MARKER,
            STATE_FLAGS,
            STATE_CLOSER,
        };

        auto state = STATE_OPENER;
        auto next = fmt.ptr;
        auto start = fmt.ptr;
        auto term = fmt.ptr + fmt.length;

        const char* formatStart = nullptr;
        auto index = -1;
        auto opened = 0;
        auto nested = false;

        while (next < term) {
            const auto ptr = next++;
            const auto ch = *ptr;

            switch (state) {
            case STATE_OPENER:
                if (ch == '{') {
                    writer.write(int32_t(ptr - start), start);

                    if (next < term) {
                        if (*next != '{') {
                            index = -1;
                            start = ptr;
                            nested = false;
                            formatStart = nullptr;
                            state = STATE_INDEX;
                        } else {
                            start = next++;
                        }
                    }
                } else if (ch == '}') {
                    writer.write(int32_t(next - start), start);
                    if (next < term && *next == '}') {
                        ++next;
                    }
                    start = next;
                }
                break;

            case STATE_INDEX:
                if (ch >= '0' && ch <= '9') {
                    index = (index < 0)
                        ? (ch - '0')
                        : (index * 10) + (ch - '0');
                } else {
                    if (index < 0) {
                        index = *prevIndex + 1;
                    }
                    *prevIndex = index;
                    state = STATE_MARKER;
                    --next;
                }
                break;

            case STATE_MARKER:
                if (ch == ':') {
                    state = STATE_FLAGS;
                } else {
                    state = STATE_CLOSER;
                    --next;
                }
                break;

            case STATE_FLAGS:
                if (!formatStart) {
                    formatStart = ptr;
                }

                switch (ch) {
                case '{':
                    ++opened;
                    nested = true;
                    break;
                case '}':
                    if (opened > 0) {
                        --opened;
                    }
                    else {
                        state = STATE_CLOSER;
                        --next;
                    }
                    break;
                }

                break;

            case STATE_CLOSER:
                if (ch == '}') {
                    StringView format = formatStart
                        ? StringView(formatStart, int32_t(ptr - formatStart))
                        : StringView();

                    // handle nested format specifiers
                    char buffer[64];

                    if (nested) {
                        StringWriter nestedWriter(buffer, sizeof(buffer));
                        sp::do_format(nestedWriter, format, prevIndex, std::forward<Args>(args)...);
                        const auto fullLen = nestedWriter.result();
                        const auto realLen = std::min(size_t(fullLen), sizeof(buffer));
                        format = StringView(buffer, int32_t(realLen));
                    }

                    if (format_index(writer, format, index, std::forward<Args>(args)...)) {
                        start = next;
                    }
                }
                state = STATE_OPENER;
                break;
            }
        }

        // Print remaining data
        if (start != term) {
            writer.write(int32_t(term - start), start);
        }
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, -0x78(%rbp)
movq %rdx, -0x50(%rbp)
movq %rdi, -0x40(%rbp)
movq (%rsi), %r12
movslq 0x8(%rsi), %rax
leaq (%r12,%rax), %r13
testq %rax, %rax
jle 0x1003f
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
leaq 0x92bf(%rip), %rsi      # 0x19048
xorl %r14d, %r14d
movq %r12, %r9
xorl %r8d, %r8d
movq $0x0, -0x48(%rbp)
xorl %edi, %edi
movq %r13, -0x68(%rbp)
leaq 0x1(%r12), %rbx
cmpl $0x4, %r14d
ja 0x10031
movzbl (%r12), %eax
movl %r14d, %ecx
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
xorl %r14d, %r14d
cmpl $0x7d, %eax
je 0xff91
cmpl $0x7b, %eax
jne 0x10031
movq %r8, -0x38(%rbp)
movl %edi, -0x2c(%rbp)
movl %r12d, %eax
subl %r9d, %eax
movslq %eax, %rsi
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
movq %r9, -0x70(%rbp)
movq %r9, %rdx
callq *(%rax)
cmpq %r13, %rbx
jae 0xffd3
xorl %r14d, %r14d
cmpb $0x7b, (%rbx)
leaq 0x923d(%rip), %rsi      # 0x19048
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %r8
jne 0xffef
addq $0x2, %r12
movq %rbx, %r9
jmp 0xff7e
xorl %r14d, %r14d
cmpl $0x7d, %eax
jne 0x10031
subl %r8d, %r12d
testq %r8, %r8
movl $0x0, %eax
cmovel %eax, %r12d
movq %r8, -0x60(%rbp)
movl %r12d, -0x58(%rbp)
movq %r8, %rdx
testb $0x1, -0x48(%rbp)
je 0xfecf
leaq 0xdf09(%rip), %rax      # 0x1dd60
movq %rax, -0x90(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0x88(%rbp)
movq $0x40, -0x80(%rbp)
movl %edi, -0x2c(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq %r15, %r12
movq %r8, %r15
movq %r9, %r13
callq 0xfd48
movq %r13, %r9
movq -0x68(%rbp), %r13
movq %r15, %r8
movq %r12, %r15
movl -0x2c(%rbp), %edi
leaq 0x9199(%rip), %rsi      # 0x19048
movl -0x7c(%rbp), %r12d
cmpl $0x40, %r12d
movl $0x40, %eax
cmovael %eax, %r12d
leaq -0xd0(%rbp), %rdx
movq %rdx, -0x60(%rbp)
movl %r12d, -0x58(%rbp)
testl %r15d, %r15d
jne 0x10031
movq %r8, -0x38(%rbp)
movl %edi, -0x2c(%rbp)
testl %r12d, %r12d
je 0x1000b
movslq %r12d, %rsi
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
jmp 0x1001e
cmpl $0x3a, %eax
je 0xfeff
movq %r12, %rbx
setne %al
movzbl %al, %r14d
addl $0x3, %r14d
jmp 0x10031
testq %r8, %r8
cmoveq %r12, %r8
movl $0x3, %r14d
cmpl $0x7d, %eax
je 0xff86
cmpl $0x7b, %eax
jne 0x10031
incl %edi
movb $0x1, %al
movq %rax, -0x48(%rbp)
jmp 0x10031
addb $-0x30, %al
cmpb $0x9, %al
ja 0xff62
leal (%r15,%r15), %ecx
testl %r15d, %r15d
movzbl %al, %eax
leal (%rcx,%rcx,4), %r15d
movl $0x0, %ecx
cmovsl %ecx, %r15d
addl %eax, %r15d
movl $0x1, %r14d
jmp 0x10031
testl %r15d, %r15d
jns 0xff71
movq -0x50(%rbp), %rax
movl (%rax), %r15d
incl %r15d
movq -0x50(%rbp), %rax
movl %r15d, (%rax)
movl $0x2, %r14d
movq %r12, %rbx
jmp 0x10031
testl %edi, %edi
jle 0xffe7
decl %edi
jmp 0x10031
movq %r8, -0x38(%rbp)
movl %edi, -0x2c(%rbp)
movl %ebx, %eax
subl %r9d, %eax
movslq %eax, %rsi
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
movq %r9, %rdx
callq *(%rax)
cmpq %r13, %rbx
jae 0x10020
cmpb $0x7d, 0x1(%r12)
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %r8
jne 0xffc7
addq $0x2, %r12
movq %r12, %rbx
leaq 0x907a(%rip), %rsi      # 0x19048
movq %rbx, %r9
jmp 0x10031
leaq 0x906e(%rip), %rsi      # 0x19048
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %r8
movq -0x70(%rbp), %r9
jmp 0x10031
movl $0x4, %r14d
jmp 0xff7e
movl $0x1, %r14d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq $0x0, -0x48(%rbp)
xorl %r8d, %r8d
movq %r12, %r9
jmp 0x10031
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
movl $0x7, %esi
leaq 0xb27f(%rip), %rdx      # 0x1b29d
callq *(%rax)
movq %rbx, %r9
leaq 0x901e(%rip), %rsi      # 0x19048
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %r8
movq %rbx, %r12
cmpq %r13, %rbx
jb 0xfda0
jmp 0x10042
movq %r12, %r9
cmpq %r13, %r9
je 0x10059
subl %r9d, %r13d
movslq %r13d, %rsi
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
movq %r9, %rdx
callq *(%rax)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq