// File: /workspace/github_c_makefile_star2_O2/mruby-zest[P]osc-bridge/src/parse-schema.c
// Function: parse_schema
void parse_schema(const char *json, schema_t *sch)
{
    sch->elements = 0;
    sch->handles  = 0;

    /* Lexer example */
    mm_json_size len = strlen(json);

    /* create iterator  */
    struct mm_json_iter iter;
    iter = mm_json_begin(json, len);

    //Read in parameters
    struct mm_json_pair pair;
    iter = mm_json_parse(&pair, &iter);
    assert(!mm_json_cmp(&pair.name, "parameters"));
    assert(pair.value.type == MM_JSON_ARRAY);

    //Read in parameter objects
    struct mm_json_iter array = mm_json_begin(pair.value.str, pair.value.len);
    struct mm_json_token tok;
    array = mm_json_read(&tok, &array);
    while (array.src) {
        /* read single token */
        printf("new schema handle...\n");
        sch->elements += 1;
        sch->handles   = realloc(sch->handles, sch->elements*sizeof(schema_handle_t));

        schema_handle_t *handle = sch->handles+(sch->elements-1);
        memset(handle, 0, sizeof(schema_handle_t));

        struct mm_json_iter array2 = mm_json_begin(tok.str, tok.len);
        struct mm_json_pair pair2;
        array2 = mm_json_parse(&pair2, &array2);
        while (!array2.err) {
            assert(pair2.name.type == MM_JSON_STRING);
            printf("  ");
            print_string(pair2.name.str, pair2.name.len);
            unsigned pad = pair2.name.len < 10 ? 10-pair2.name.len : 0;
            for(unsigned i=0; i<pad; ++i)
                putchar(' ');

            if(pair2.value.type == MM_JSON_STRING) {
                printf(" = \"");
                print_string(pair2.value.str, pair2.value.len);
                printf("\"\n");

                struct mm_json_token v = pair2.value;
                if(mm_json_cmp(&pair2.name, "path") == 0)
                    handle->pattern = strndup(v.str, v.len);
                else if(mm_json_cmp(&pair2.name, "name") == 0)
                    handle->name = strndup(v.str, v.len);
                else if(mm_json_cmp(&pair2.name, "shortname") == 0)
                    handle->short_name = strndup(v.str, v.len);
                else if(mm_json_cmp(&pair2.name, "units") == 0)
                    handle->units = strndup(v.str, v.len);
                else if(mm_json_cmp(&pair2.name, "scale") == 0)
                    handle->scale = strndup(v.str, v.len);
                else if(mm_json_cmp(&pair2.name, "tooltip") == 0)
                    handle->documentation = strndup(v.str, v.len);
                else if(mm_json_cmp(&pair2.name, "default") == 0)
                    handle->default_ = strndup(v.str, v.len);
                else if(mm_json_cmp(&pair2.name, "type") == 0) {
                    if(v.str[0] == 'i')
                        handle->type = 'i';
                    else if(v.str[0] == 'f')
                        handle->type = 'f';
                    else if(v.str[0] == 'T')
                        handle->type = 'T';
                    else
                        handle->type = 0;
                }
            } else if(pair2.value.type == MM_JSON_ARRAY &&
                    mm_json_cmp(&pair2.name, "options") == 0) {
                handle->opts = parse_options(pair2.value.str, pair2.value.len);
            } else if(pair2.value.type == MM_JSON_ARRAY &&
                    mm_json_cmp(&pair2.name, "range") == 0) {
                parse_range(handle, pair2.value.str, pair2.value.len);
            } else
                printf(" = ????\n");
            array2 = mm_json_parse(&pair2, &array2);
        }

        //MM_JSON_OBJECT
        array = mm_json_read(&tok, &array);
    }

    {
        printf("ACTIONS...\n\n");
        iter = mm_json_parse(&pair, &iter);
        assert(!mm_json_cmp(&pair.name, "actions"));
        assert(pair.value.type == MM_JSON_ARRAY);

        //Read in parameter objects
        struct mm_json_iter array = mm_json_begin(pair.value.str, pair.value.len);
        struct mm_json_token tok;
        iter = mm_json_read(&tok, &array);
        while (array.src) {
            /* read single token */
            printf("field.type = %d\n", tok.type);
            struct mm_json_iter array2 = mm_json_begin(tok.str, tok.len);
            struct mm_json_pair pair2;
            array2 = mm_json_parse(&pair2, &array2);
            while (!array2.err) {
                assert(pair2.name.type == MM_JSON_STRING);
                printf("  field.name[");
                print_string(pair2.name.str, pair2.name.len);
                if(pair2.value.type == MM_JSON_STRING) {
                    printf("] = \"");
                    print_string(pair2.value.str, pair2.value.len);
                    printf("\"\n");
                } else
                    printf("] = ????\n");
                array2 = mm_json_parse(&pair2, &array2);
            }

            //MM_JSON_OBJECT
            array = mm_json_read(&tok, &array);
        }
    }

    return;

    /* read subobject (array/objects) */
    iter = mm_json_parse(&pair, &iter);
    printf("pair.name = %s\n", pair.name.str);
    printf("pair.value.type = %d\n", pair.value.type);
    assert(pair.value.type == MM_JSON_ARRAY);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
andl $0x0, 0x10(%rsi)
movq %rdi, %rbx
movq %rsi, -0xb8(%rbp)
andq $0x0, 0x8(%rsi)
callq 0x20d0
movq %rax, %r14
xorps %xmm0, %xmm0
leaq -0x180(%rbp), %r15
movaps %xmm0, (%r15)
callq 0x40cc
movq %rbx, 0x10(%r15)
movq %r14, 0x18(%r15)
leaq -0x130(%rbp), %rbx
leaq -0x1d0(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4a68
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movaps %xmm1, 0x10(%r15)
movaps %xmm0, (%r15)
leaq 0x2fcc(%rip), %rsi      # 0x8709
movq %r14, %rdi
callq 0x45e9
testl %eax, %eax
jne 0x5ca2
cmpl $0x2, -0x188(%rbp)
jne 0x5cbb
movups -0x1a8(%rbp), %xmm0
movaps %xmm0, -0x40(%rbp)
xorps %xmm0, %xmm0
leaq -0x150(%rbp), %rbx
movaps %xmm0, (%rbx)
callq 0x40cc
movaps -0x40(%rbp), %xmm0
movaps %xmm0, 0x10(%rbx)
leaq -0x130(%rbp), %r15
leaq -0x1f8(%rbp), %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x465c
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movaps %xmm1, 0x10(%rbx)
movaps %xmm0, (%rbx)
leaq -0x60(%rbp), %r12
leaq -0xb0(%rbp), %r14
cmpq $0x0, -0x140(%rbp)
je 0x5b09
movq -0xb8(%rbp), %rbx
movslq 0x10(%rbx), %rax
incq %rax
movl %eax, 0x10(%rbx)
movq 0x8(%rbx), %rdi
imulq $0x58, %rax, %rsi
callq 0x2240
movq %rax, 0x8(%rbx)
movslq 0x10(%rbx), %rcx
imulq $0x58, %rcx, %rcx
leaq (%rax,%rcx), %rdx
movq %rdx, -0x40(%rbp)
addq %rcx, %rax
addq $-0x58, %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
andq $0x0, 0x50(%rax)
movups %xmm0, 0x30(%rax)
movq %rax, -0xc0(%rbp)
movups %xmm0, 0x40(%rax)
movups -0x1f8(%rbp), %xmm1
movaps %xmm1, -0x160(%rbp)
movaps %xmm0, -0xb0(%rbp)
callq 0x40cc
movaps -0x160(%rbp), %xmm0
movaps %xmm0, -0xa0(%rbp)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4a68
movups -0x60(%rbp), %xmm0
movups -0x50(%rbp), %xmm1
movaps %xmm1, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
cmpl $0x0, -0xb0(%rbp)
jne 0x5ad8
cmpl $0x4, -0x110(%rbp)
jne 0x5c64
movl -0xe8(%rbp), %eax
cmpl $0x2, %eax
je 0x598d
cmpl $0x4, %eax
jne 0x5844
movq %r14, %r13
movq -0x108(%rbp), %rbx
movq -0x100(%rbp), %r14
movq %r15, %rdi
leaq 0x2ef9(%rip), %rsi      # 0x87aa
callq 0x45e9
testl %eax, %eax
je 0x59d5
movq %r15, %rdi
leaq 0x2eec(%rip), %rsi      # 0x87b4
callq 0x45e9
testl %eax, %eax
je 0x5a0f
movq %r15, %rdi
leaq 0x2ed0(%rip), %rsi      # 0x87af
callq 0x45e9
testl %eax, %eax
je 0x5a2a
movq %r15, %rdi
leaq 0x2ec3(%rip), %rsi      # 0x87b9
callq 0x45e9
testl %eax, %eax
je 0x5a45
movq %r15, %rdi
leaq 0x2eb2(%rip), %rsi      # 0x87bf
callq 0x45e9
testl %eax, %eax
je 0x5a60
movq %r15, %rdi
leaq 0x2ea1(%rip), %rsi      # 0x87c5
callq 0x45e9
testl %eax, %eax
je 0x5a7b
movq %r15, %rdi
leaq 0x2e92(%rip), %rsi      # 0x87cd
callq 0x45e9
testl %eax, %eax
je 0x5a96
movq %r15, %rdi
leaq 0x352d(%rip), %rsi      # 0x8e7f
callq 0x45e9
testl %eax, %eax
movq %r13, %r14
jne 0x5844
movzbl (%rbx), %eax
cmpl $0x54, %eax
je 0x5abe
cmpl $0x66, %eax
je 0x5ab1
cmpl $0x69, %eax
jne 0x5acb
movq -0x40(%rbp), %rax
movb $0x69, -0x10(%rax)
jmp 0x5844
movq %r15, %rdi
leaq 0x2e3e(%rip), %rsi      # 0x87d5
callq 0x45e9
testl %eax, %eax
je 0x59f0
movq %r15, %rdi
leaq 0x2e33(%rip), %rsi      # 0x87dd
callq 0x45e9
testl %eax, %eax
jne 0x5844
movq -0x108(%rbp), %rsi
movl -0x100(%rbp), %edx
movq -0xc0(%rbp), %rdi
callq 0x5591
jmp 0x5844
movq %rbx, %rdi
movl %r14d, %esi
callq 0x555a
movq -0x40(%rbp), %rcx
movq %rax, -0x48(%rcx)
movq %r13, %r14
jmp 0x5844
movq -0x108(%rbp), %rdi
movl -0x100(%rbp), %esi
callq 0x52c5
movq -0x40(%rbp), %rcx
movq %rax, -0x50(%rcx)
jmp 0x5844
movq %rbx, %rdi
movl %r14d, %esi
callq 0x555a
movq -0x40(%rbp), %rcx
movq %rax, -0x40(%rcx)
movq %r13, %r14
jmp 0x5844
movq %rbx, %rdi
movl %r14d, %esi
callq 0x555a
movq -0x40(%rbp), %rcx
movq %rax, -0x38(%rcx)
movq %r13, %r14
jmp 0x5844
movq %rbx, %rdi
movl %r14d, %esi
callq 0x555a
movq -0x40(%rbp), %rcx
movq %rax, -0x30(%rcx)
movq %r13, %r14
jmp 0x5844
movq %rbx, %rdi
movl %r14d, %esi
callq 0x555a
movq -0x40(%rbp), %rcx
movq %rax, -0x20(%rcx)
movq %r13, %r14
jmp 0x5844
movq %rbx, %rdi
movl %r14d, %esi
callq 0x555a
movq -0x40(%rbp), %rcx
movq %rax, -0x28(%rcx)
movq %r13, %r14
jmp 0x5844
movq %rbx, %rdi
movl %r14d, %esi
callq 0x555a
movq -0x40(%rbp), %rcx
movq %rax, -0x18(%rcx)
movq %r13, %r14
jmp 0x5844
movq -0x40(%rbp), %rax
movb $0x66, -0x10(%rax)
jmp 0x5844
movq -0x40(%rbp), %rax
movb $0x54, -0x10(%rax)
jmp 0x5844
movq -0x40(%rbp), %rax
movb $0x0, -0x10(%rax)
jmp 0x5844
movq %r12, %rdi
leaq -0x1f8(%rbp), %rsi
leaq -0x150(%rbp), %rdx
callq 0x465c
movups -0x60(%rbp), %xmm0
movups -0x50(%rbp), %xmm1
movaps %xmm1, -0x140(%rbp)
movaps %xmm0, -0x150(%rbp)
jmp 0x57b3
leaq -0x130(%rbp), %rbx
leaq -0x1d0(%rbp), %r14
leaq -0x180(%rbp), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4a68
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movaps %xmm1, 0x10(%r15)
movaps %xmm0, (%r15)
leaq 0x2ca0(%rip), %rsi      # 0x87e3
movq %r14, %rdi
callq 0x45e9
testl %eax, %eax
jne 0x5cd8
cmpl $0x2, -0x188(%rbp)
jne 0x5cf7
movq -0x1a8(%rbp), %rbx
movq -0x1a0(%rbp), %r14
xorps %xmm0, %xmm0
leaq -0x60(%rbp), %r12
movaps %xmm0, (%r12)
callq 0x40cc
movq %rbx, 0x10(%r12)
movq %r14, 0x18(%r12)
leaq -0x130(%rbp), %r14
leaq -0xb0(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x465c
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movaps %xmm1, -0x170(%rbp)
movaps %xmm0, -0x180(%rbp)
leaq -0xe0(%rbp), %r12
leaq -0x80(%rbp), %r13
testq %rbx, %rbx
je 0x5c52
movups -0xb0(%rbp), %xmm0
movaps %xmm0, -0x40(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
callq 0x40cc
movaps -0x40(%rbp), %xmm0
movaps %xmm0, -0x70(%rbp)
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x4a68
movups -0xe0(%rbp), %xmm0
movups -0xd0(%rbp), %xmm1
movaps %xmm1, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
cmpl $0x0, -0x80(%rbp)
jne 0x5c24
cmpl $0x4, -0x110(%rbp)
je 0x5bef
jmp 0x5c83
movq %r12, %rdi
movq %r15, %rsi
leaq -0x60(%rbp), %rdx
callq 0x465c
movups -0xe0(%rbp), %xmm0
movups -0xd0(%rbp), %xmm1
movaps %xmm1, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movq -0x50(%rbp), %rbx
jmp 0x5bc7
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2b1d(%rip), %rdi      # 0x8788
leaq 0x296f(%rip), %rsi      # 0x85e1
leaq 0x2ac2(%rip), %rcx      # 0x873b
movl $0x91, %edx
callq 0x2130
leaq 0x2afe(%rip), %rdi      # 0x8788
leaq 0x2950(%rip), %rsi      # 0x85e1
leaq 0x2aa3(%rip), %rcx      # 0x873b
movl $0xd6, %edx
callq 0x2130
leaq 0x2a6b(%rip), %rdi      # 0x8714
leaq 0x2931(%rip), %rsi      # 0x85e1
leaq 0x2a84(%rip), %rcx      # 0x873b
pushq $0x7d
jmp 0x5cd2
leaq 0x2aa5(%rip), %rdi      # 0x8767
leaq 0x2918(%rip), %rsi      # 0x85e1
leaq 0x2a6b(%rip), %rcx      # 0x873b
pushq $0x7e
popq %rdx
callq 0x2130
leaq 0x2b0c(%rip), %rdi      # 0x87eb
leaq 0x28fb(%rip), %rsi      # 0x85e1
leaq 0x2a4e(%rip), %rcx      # 0x873b
movl $0xc8, %edx
callq 0x2130
leaq 0x2a69(%rip), %rdi      # 0x8767
leaq 0x28dc(%rip), %rsi      # 0x85e1
leaq 0x2a2f(%rip), %rcx      # 0x873b
movl $0xc9, %edx
callq 0x2130