// File: /workspace/github_c_makefile_star2_O3/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 0x6a8a
testl %eax, %eax
movq %rbx, -0x48(%rbp)
je 0x6bcd
movl %eax, %r13d
xorl %r12d, %r12d
leaq 0x2e0e(%rip), %r15      # 0x994c
movq -0x38(%rbp), %rdx
movzbl (%rbx), %eax
leal -0x62(%rax), %ecx
cmpl $0x12, %ecx
ja 0x6b68
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
incl %r12d
addl $0x4, %r14d
jmp 0x6bbe
incl %r12d
addl $0x8, %r14d
jmp 0x6bbe
cmpl $0x53, %eax
je 0x6b76
testl %eax, %eax
jne 0x6bc1
jmp 0x6ed6
movl %r12d, %eax
shlq $0x4, %rax
movq (%rdx,%rax), %rdi
testq %rdi, %rdi
je 0x6eb7
incl %r12d
callq 0x20d0
addl %eax, %r14d
andl $-0x4, %r14d
addl $0x4, %r14d
decl %r13d
movq -0x38(%rbp), %rdx
jmp 0x6bc1
movl %r12d, %eax
incl %r12d
shlq $0x4, %rax
movl (%rdx,%rax), %eax
addl %eax, %r14d
addl $0x7, %r14d
andl $-0x4, %r14d
decl %r13d
incq %rbx
testl %r13d, %r13d
jne 0x6b42
movl %r14d, %r13d
movq -0x30(%rbp), %rbx
testq %rbx, %rbx
je 0x6ea5
movq -0x50(%rbp), %rdx
cmpq %rdx, %r13
jbe 0x6bf8
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
jmp 0x6ea5
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2140
movq -0x40(%rbp), %rdx
movb (%rdx), %al
testb %al, %al
movq -0x48(%rbp), %r12
je 0x6c2f
xorl %r14d, %r14d
movl %r14d, %ecx
movb %al, (%rbx,%rcx)
movb 0x1(%rdx,%r14), %al
incq %r14
testb %al, %al
jne 0x6c19
andl $-0x4, %r14d
leal 0x4(%r14), %eax
addl $0x5, %r14d
movb $0x2c, (%rbx,%rax)
movb (%r12), %al
testb %al, %al
je 0x6c5a
leaq 0x1(%r12), %rcx
movl %r14d, %edx
incl %r14d
movb %al, (%rbx,%rdx)
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x6c48
andl $-0x4, %r14d
addl $0x4, %r14d
movq %r12, %rdi
callq 0x6a8a
testl %eax, %eax
je 0x6ea2
xorl %r15d, %r15d
leaq 0x2d1c(%rip), %r9       # 0x9998
movq -0x38(%rbp), %r11
movzbl (%r12), %esi
leal -0x62(%rsi), %edi
cmpl $0x12, %edi
ja 0x6d91
movslq (%r9,%rdi,4), %rsi
addq %r9, %rsi
jmpq *%rsi
movl %r15d, %edx
incl %r15d
shlq $0x4, %rdx
movl (%r11,%rdx), %ebx
movl %ebx, %edx
shrl $0x18, %edx
leal 0x1(%r14), %esi
movl %r14d, %edi
movq -0x30(%rbp), %rcx
movb %dl, (%rcx,%rdi)
movl %ebx, %edx
shrl $0x10, %edx
leal 0x2(%r14), %edi
movq -0x30(%rbp), %rcx
movb %dl, (%rcx,%rsi)
leal 0x3(%r14), %edx
movq -0x30(%rbp), %rcx
movb %bh, (%rcx,%rdi)
addl $0x4, %r14d
movq -0x30(%rbp), %rcx
movb %bl, (%rcx,%rdx)
jmp 0x6d7b
movl %r15d, %esi
incl %r15d
shlq $0x4, %rsi
movq (%r11,%rsi), %rbx
movq %rbx, %rsi
shrq $0x38, %rsi
leal 0x1(%r14), %edi
movl %r14d, %r8d
movq -0x30(%rbp), %rcx
movb %sil, (%rcx,%r8)
movq %rbx, %rsi
shrq $0x30, %rsi
leal 0x2(%r14), %r8d
movq -0x30(%rbp), %rcx
movb %sil, (%rcx,%rdi)
movq %rbx, %rsi
shrq $0x28, %rsi
leal 0x3(%r14), %edi
movq -0x30(%rbp), %rcx
movb %sil, (%rcx,%r8)
movq %rbx, %rsi
shrq $0x20, %rsi
leal 0x4(%r14), %r8d
movq -0x30(%rbp), %rcx
movb %sil, (%rcx,%rdi)
movl %ebx, %esi
shrl $0x18, %esi
leal 0x5(%r14), %edi
movq -0x30(%rbp), %rcx
movb %sil, (%rcx,%r8)
movl %ebx, %esi
shrl $0x10, %esi
leal 0x6(%r14), %edx
movq -0x30(%rbp), %rcx
movb %sil, (%rcx,%rdi)
leal 0x7(%r14), %esi
movq -0x30(%rbp), %rcx
movb %bh, (%rcx,%rdx)
addl $0x8, %r14d
movq -0x30(%rbp), %rcx
movb %bl, (%rcx,%rsi)
movq -0x30(%rbp), %rbx
decl %eax
incq %r12
testl %eax, %eax
jne 0x6c80
jmp 0x6ea2
cmpl $0x53, %esi
je 0x6d9f
testl %esi, %esi
jne 0x6d81
jmp 0x6ef5
movl %r15d, %edx
shlq $0x4, %rdx
movq (%r11,%rdx), %rsi
movb (%rsi), %dil
testb %dil, %dil
je 0x6dca
incq %rsi
movl %r14d, %edx
incl %r14d
movb %dil, (%rbx,%rdx)
movb (%rsi), %dil
incq %rsi
testb %dil, %dil
jne 0x6db5
incl %r15d
andl $-0x4, %r14d
addl $0x4, %r14d
jmp 0x6d7f
movl %r15d, %edx
shlq $0x4, %rdx
movq %rbx, %rcx
movl (%r11,%rdx), %ebx
movq 0x8(%r11,%rdx), %rsi
movl %ebx, %edx
shrl $0x18, %edx
leal 0x1(%r14), %r10d
movl %r14d, %r8d
movb %dl, (%rcx,%r8)
movl %ebx, %edx
shrl $0x10, %edx
leal 0x2(%r14), %edi
movb %dl, (%rcx,%r10)
leal 0x3(%r14), %edx
movb %bh, (%rcx,%rdi)
addl $0x4, %r14d
movb %bl, (%rcx,%rdx)
testq %rsi, %rsi
je 0x6e86
testq %rbx, %rbx
je 0x6e8c
movl %r14d, %edi
xorl %r8d, %r8d
movq -0x30(%rbp), %rcx
leal (%rdi,%r8), %edx
movb (%rsi,%r8), %r10b
movb %r10b, (%rcx,%rdx)
incq %r8
cmpl %r8d, %ebx
jne 0x6e29
addl %r8d, %r14d
jmp 0x6e8c
movl %r15d, %edx
incl %r15d
shlq $0x4, %rdx
movb (%r11,%rdx), %sil
leal 0x1(%r14), %edi
movl %r14d, %r8d
movb %sil, (%rbx,%r8)
movb 0x1(%r11,%rdx), %sil
leal 0x2(%r14), %r8d
movb %sil, (%rbx,%rdi)
movb 0x2(%r11,%rdx), %sil
leal 0x3(%r14), %edi
movb %sil, (%rbx,%r8)
movb 0x3(%r11,%rdx), %dl
addl $0x4, %r14d
movb %dl, (%rbx,%rdi)
jmp 0x6d7f
addl %r14d, %ebx
movl %ebx, %r14d
incl %r15d
addl $0x3, %r14d
andl $-0x4, %r14d
decl %eax
movq -0x30(%rbp), %rbx
jmp 0x6d81
movl %r14d, %r13d
movq %r13, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2d73(%rip), %rdi      # 0x9c31
leaq 0x2c0f(%rip), %rsi      # 0x9ad4
leaq 0x2d22(%rip), %rcx      # 0x9bee
movl $0xd7, %edx
callq 0x2130
leaq 0x2c83(%rip), %rdi      # 0x9b60
leaq 0x2bf0(%rip), %rsi      # 0x9ad4
leaq 0x2d03(%rip), %rcx      # 0x9bee
movl $0xc0, %edx
callq 0x2130
leaq 0x2c64(%rip), %rdi      # 0x9b60
leaq 0x2bd1(%rip), %rsi      # 0x9ad4
leaq 0x2c5a(%rip), %rcx      # 0x9b64
movl $0x14a, %edx            # imm = 0x14A
callq 0x2130