// File: rhoot[P]sp/tests/../include/sp.hpp
// Function: void sp::do_format<>(sp::IWriter&, sp::StringView const&, int*)
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
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x8(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x48(%rbp)
movb $0x0, -0x49(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x1543e
movq -0x28(%rbp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, -0x28(%rbp)
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movb (%rax), %al
movb %al, -0x59(%rbp)
movl -0x1c(%rbp), %eax
movq %rax, -0x100(%rbp)
subq $0x4, %rax
ja 0x15439
movq -0x100(%rbp), %rax
leaq 0xdf69(%rip), %rcx      # 0x230b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsbl -0x59(%rbp), %eax
cmpl $0x7b, %eax
jne 0x151cd
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movslq %eax, %rsi
movq -0x30(%rbp), %rdx
movq (%rdi), %rax
callq *(%rax)
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x151cb
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x7b, %eax
je 0x151b6
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movq -0x58(%rbp), %rax
movq %rax, -0x30(%rbp)
movb $0x0, -0x49(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x1, -0x1c(%rbp)
jmp 0x151c9
movq -0x28(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rbp)
movq %rax, -0x30(%rbp)
jmp 0x151cb
jmp 0x1521d
movsbl -0x59(%rbp), %eax
cmpl $0x7d, %eax
jne 0x1521b
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movslq %eax, %rsi
movq -0x30(%rbp), %rdx
movq (%rdi), %rax
callq *(%rax)
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x15213
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x7d, %eax
jne 0x15213
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x1521d
jmp 0x15439
movsbl -0x59(%rbp), %eax
cmpl $0x30, %eax
jl 0x15267
movsbl -0x59(%rbp), %eax
cmpl $0x39, %eax
jg 0x15267
cmpl $0x0, -0x44(%rbp)
jge 0x15249
movsbl -0x59(%rbp), %eax
subl $0x30, %eax
movl %eax, -0x104(%rbp)
jmp 0x1525c
imull $0xa, -0x44(%rbp), %eax
movsbl -0x59(%rbp), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, -0x104(%rbp)
movl -0x104(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x15295
cmpl $0x0, -0x44(%rbp)
jge 0x15279
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x2, -0x1c(%rbp)
movq -0x28(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x15439
movsbl -0x59(%rbp), %eax
cmpl $0x3a, %eax
jne 0x152ac
movl $0x3, -0x1c(%rbp)
jmp 0x152bf
movl $0x4, -0x1c(%rbp)
movq -0x28(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x15439
cmpq $0x0, -0x40(%rbp)
jne 0x152d3
movq -0x58(%rbp), %rax
movq %rax, -0x40(%rbp)
movsbl -0x59(%rbp), %eax
movl %eax, -0x108(%rbp)
subl $0x7b, %eax
je 0x152f1
jmp 0x152e4
movl -0x108(%rbp), %eax
subl $0x7d, %eax
je 0x15300
jmp 0x15326
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movb $0x1, -0x49(%rbp)
jmp 0x15326
cmpl $0x0, -0x48(%rbp)
jle 0x15311
movl -0x48(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x15324
movl $0x4, -0x1c(%rbp)
movq -0x28(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x15326
jmp 0x15439
movsbl -0x59(%rbp), %eax
cmpl $0x7d, %eax
jne 0x15432
cmpq $0x0, -0x40(%rbp)
je 0x1535b
movq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
subq %rcx, %rax
movl %eax, %edx
leaq -0x70(%rbp), %rdi
callq 0x14ec0
jmp 0x15364
leaq -0x70(%rbp), %rdi
callq 0x15470
testb $0x1, -0x49(%rbp)
je 0x15412
leaq -0xb0(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
movl $0x40, %edx
callq 0x13ec0
movq -0x18(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x150a0
leaq -0xc8(%rbp), %rdi
callq 0x13fc0
movl %eax, -0xcc(%rbp)
movslq -0xcc(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq $0x40, -0xe8(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x15030
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
leaq -0xb0(%rbp), %rsi
movq -0xd8(%rbp), %rax
movl %eax, %edx
leaq -0xf8(%rbp), %rdi
callq 0x14ec0
movq -0xf8(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0xf0(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq -0x70(%rbp), %rsi
callq 0x15490
testb $0x1, %al
jne 0x15428
jmp 0x15430
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x15432
movl $0x0, -0x1c(%rbp)
jmp 0x15104
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x15463
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movslq %eax, %rsi
movq -0x30(%rbp), %rdx
movq (%rdi), %rax
callq *(%rax)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)