// File: /workspace/github_c_makefile_star2_O2/mruby-zest[P]osc-bridge/rtosc/rtosc.c
// Function: rtosc_amessage
size_t rtosc_amessage(char              *buffer,
                      size_t             len,
                      const char        *address,
                      const char        *arguments,
                      const rtosc_arg_t *args)
{
    const size_t total_len = vsosc_null(address, arguments, args);

    if(!buffer)
        return total_len;

    //Abort if the message cannot fit
    if(total_len>len) {
        memset(buffer, 0, len);
        return 0;
    }

    memset(buffer, 0, total_len);

    unsigned pos = 0;
    while(*address)
        buffer[pos++] = *address++;

    //get 32 bit alignment
    pos += 4-pos%4;

    buffer[pos++] = ',';

    const char *arg_str = arguments;
    while(*arg_str)
        buffer[pos++] = *arg_str++;

    pos += 4-pos%4;

    unsigned toparse = nreserved(arguments);
    unsigned arg_pos = 0;
    while(toparse)
    {
        char arg = *arguments++;
        assert(arg);
        int32_t i;
        int64_t d;
        const uint8_t *m;
        const char *s;
        const unsigned char *u;
        rtosc_blob_t b;
        switch(arg) {
            case 'h':
            case 't':
            case 'd':
                d = args[arg_pos++].t;
                buffer[pos++] = ((d>>56) & 0xff);
                buffer[pos++] = ((d>>48) & 0xff);
                buffer[pos++] = ((d>>40) & 0xff);
                buffer[pos++] = ((d>>32) & 0xff);
                buffer[pos++] = ((d>>24) & 0xff);
                buffer[pos++] = ((d>>16) & 0xff);
                buffer[pos++] = ((d>>8) & 0xff);
                buffer[pos++] = (d & 0xff);
                --toparse;
                break;
            case 'r':
            case 'f':
            case 'c':
            case 'i':
                i = args[arg_pos++].i;
                buffer[pos++] = ((i>>24) & 0xff);
                buffer[pos++] = ((i>>16) & 0xff);
                buffer[pos++] = ((i>>8) & 0xff);
                buffer[pos++] = (i & 0xff);
                --toparse;
                break;
            case 'm':
                //TODO verify ordering of spec
                m = args[arg_pos++].m;
                buffer[pos++] = m[0];
                buffer[pos++] = m[1];
                buffer[pos++] = m[2];
                buffer[pos++] = m[3];
                --toparse;
                break;
            case 'S':
            case 's':
                s = args[arg_pos++].s;
                while(*s)
                    buffer[pos++] = *s++;
                pos += 4-pos%4;
                --toparse;
                break;
            case 'b':
                b = args[arg_pos++].b;
                i = b.len;
                buffer[pos++] = ((i>>24) & 0xff);
                buffer[pos++] = ((i>>16) & 0xff);
                buffer[pos++] = ((i>>8) & 0xff);
                buffer[pos++] = (i & 0xff);
                u = b.data;
                if(u) {
                    while(i--)
                        buffer[pos++] = *u++;
                }
                else
                    pos += i;
                if(pos%4)
                    pos += 4-pos%4;
                --toparse;
                break;
            default:
                ;
        }
    }

    return pos;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, -0x38(%rbp)
movq %rcx, %rbx
movq %rsi, -0x50(%rbp)
movq %rdi, -0x30(%rbp)
movq %rdx, -0x40(%rbp)
movq %rdx, %rdi
callq 0x20d0
movq %rax, %r13
addl $0x4, %r13d
movq %rbx, %rdi
callq 0x20d0
andl $-0x4, %r13d
leal (%rax,%r13), %r14d
incl %r14d
andl $-0x4, %r14d
addl $0x4, %r14d
movq %rbx, %rdi
callq 0x6488
movl %eax, %r13d
xorl %r12d, %r12d
leaq 0x23d0(%rip), %r15      # 0x8904
movq %rbx, -0x48(%rbp)
testl %r13d, %r13d
je 0x65b0
movzbl (%rbx), %ecx
incq %rbx
leal -0x62(%rcx), %edx
movl %r12d, %eax
cmpl $0x12, %edx
ja 0x655d
movslq (%r15,%rdx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
addl $0x8, %r14d
jmp 0x6590
cmpl $0x53, %ecx
je 0x656b
testl %ecx, %ecx
jne 0x6538
jmp 0x6844
shlq $0x4, %rax
movq -0x38(%rbp), %rcx
movq (%rcx,%rax), %rdi
testq %rdi, %rdi
je 0x6882
callq 0x20d0
addl %eax, %r14d
andl $-0x4, %r14d
addl $0x4, %r14d
decl %r13d
incl %r12d
jmp 0x6538
shlq $0x4, %rax
movq -0x38(%rbp), %rcx
movl (%rcx,%rax), %eax
addl %eax, %r14d
addl $0x7, %r14d
andl $-0x4, %r14d
jmp 0x6590
movl %r14d, %r13d
movq -0x30(%rbp), %rbx
testq %rbx, %rbx
je 0x65d2
movq -0x50(%rbp), %rdx
cmpq %rdx, %r13
jbe 0x65e4
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
movq %r13, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2140
movq -0x38(%rbp), %r15
movq -0x48(%rbp), %r12
movq -0x40(%rbp), %rdx
movb (%rdx,%r14), %al
testb %al, %al
je 0x6613
movl %r14d, %ecx
movb %al, (%rbx,%rcx)
incq %r14
jmp 0x6600
andl $-0x4, %r14d
addl $0x4, %r14d
movb $0x2c, %al
movq %r12, %rcx
movl %r14d, %edx
movb %al, (%rbx,%rdx)
movb (%rcx), %al
testb %al, %al
je 0x6634
incq %rcx
incl %r14d
jmp 0x6620
incl %r14d
andl $-0x4, %r14d
addl $0x4, %r14d
movq %r12, %rdi
callq 0x6488
xorl %ecx, %ecx
leaq 0x2300(%rip), %r9       # 0x8950
movq -0x30(%rbp), %r11
movl %r14d, %r13d
testl %eax, %eax
je 0x65d2
movzbl (%r12), %edi
incq %r12
leal -0x62(%rdi), %r8d
movl %ecx, %esi
cmpl $0x12, %r8d
ja 0x66c1
movslq (%r9,%r8,4), %rdi
addq %r9, %rdi
jmpq *%rdi
shlq $0x4, %rsi
movl (%r15,%rsi), %ebx
movl %ebx, %edx
shrl $0x18, %edx
leal 0x1(%r14), %esi
movq -0x30(%rbp), %rdi
movb %dl, (%rdi,%r13)
movl %ebx, %edx
shrl $0x10, %edx
leal 0x2(%r14), %edi
movq -0x30(%rbp), %r8
movb %dl, (%r8,%rsi)
leal 0x3(%r14), %edx
movq -0x30(%rbp), %rsi
movb %bh, (%rsi,%rdi)
addl $0x4, %r14d
movq -0x30(%rbp), %rsi
movb %bl, (%rsi,%rdx)
jmp 0x6837
cmpl $0x53, %edi
je 0x6764
testl %edi, %edi
jne 0x6654
jmp 0x6863
shlq $0x4, %rsi
movq (%r15,%rsi), %rbx
movq %rbx, %rsi
shrq $0x38, %rsi
leal 0x1(%r14), %edi
movq -0x30(%rbp), %rdx
movb %sil, (%rdx,%r13)
movq %rbx, %rsi
shrq $0x30, %rsi
leal 0x2(%r14), %r8d
movq -0x30(%rbp), %rdx
movb %sil, (%rdx,%rdi)
movq %rbx, %rsi
shrq $0x28, %rsi
leal 0x3(%r14), %edi
movq -0x30(%rbp), %rdx
movb %sil, (%rdx,%r8)
movq %rbx, %rsi
shrq $0x20, %rsi
leal 0x4(%r14), %r8d
movq -0x30(%rbp), %rdx
movb %sil, (%rdx,%rdi)
movl %ebx, %esi
shrl $0x18, %esi
leal 0x5(%r14), %edi
movq -0x30(%rbp), %rdx
movb %sil, (%rdx,%r8)
movl %ebx, %esi
shrl $0x10, %esi
leal 0x6(%r14), %edx
movq -0x30(%rbp), %r8
movb %sil, (%r8,%rdi)
leal 0x7(%r14), %esi
movq -0x30(%rbp), %rdi
movb %bh, (%rdi,%rdx)
addl $0x8, %r14d
movq -0x30(%rbp), %rdx
movb %bl, (%rdx,%rsi)
jmp 0x6837
shlq $0x4, %rsi
movq (%r15,%rsi), %rsi
movb (%rsi), %dil
testb %dil, %dil
je 0x6783
incq %rsi
movl %r14d, %edx
incl %r14d
movb %dil, (%rbx,%rdx)
jmp 0x676c
andl $-0x4, %r14d
addl $0x4, %r14d
jmp 0x683b
shlq $0x4, %rsi
movb (%r15,%rsi), %dl
leal 0x1(%r14), %edi
movb %dl, (%rbx,%r13)
movb 0x1(%r15,%rsi), %dl
leal 0x2(%r14), %r8d
movb %dl, (%rbx,%rdi)
movb 0x2(%r15,%rsi), %dl
leal 0x3(%r14), %edi
movb %dl, (%rbx,%r8)
movb 0x3(%r15,%rsi), %dl
addl $0x4, %r14d
movb %dl, (%rbx,%rdi)
jmp 0x683b
shlq $0x4, %rsi
movq %rbx, %rdi
movl (%r15,%rsi), %ebx
movq 0x8(%r15,%rsi), %rsi
movl %ebx, %edx
shrl $0x18, %edx
leal 0x1(%r14), %r8d
movb %dl, (%rdi,%r13)
movl %ebx, %edx
shrl $0x10, %edx
leal 0x2(%r14), %r10d
movb %dl, (%rdi,%r8)
leal 0x3(%r14), %r8d
movq %r10, %rdx
movb %bh, (%rdi,%rdx)
addl $0x4, %r14d
movb %bl, (%rdi,%r8)
testq %rsi, %rsi
je 0x6829
movl %r14d, %r8d
xorl %edi, %edi
cmpl %edi, %ebx
je 0x6824
leaq (%r8,%rdi), %rdx
movb (%rsi,%rdi), %r10b
movl %edx, %edx
movb %r10b, (%r11,%rdx)
incq %rdi
jmp 0x680d
addl %edi, %r14d
jmp 0x682f
addl %r14d, %ebx
movl %ebx, %r14d
addl $0x3, %r14d
andl $-0x4, %r14d
movq -0x30(%rbp), %rbx
incl %ecx
decl %eax
jmp 0x6654
leaq 0x22cd(%rip), %rdi      # 0x8b18
leaq 0x223a(%rip), %rsi      # 0x8a8c
leaq 0x234d(%rip), %rcx      # 0x8ba6
movl $0xc0, %edx
callq 0x2130
leaq 0x22ae(%rip), %rdi      # 0x8b18
leaq 0x221b(%rip), %rsi      # 0x8a8c
leaq 0x22a4(%rip), %rcx      # 0x8b1c
movl $0x14a, %edx            # imm = 0x14A
callq 0x2130
leaq 0x2360(%rip), %rdi      # 0x8be9
leaq 0x21fc(%rip), %rsi      # 0x8a8c
leaq 0x230f(%rip), %rcx      # 0x8ba6
movl $0xd7, %edx
callq 0x2130