// File: /workspace/github_c_makefile_star2_O1/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 0x6b0f
testl %eax, %eax
movq %rbx, -0x48(%rbp)
je 0x6c52
movl %eax, %r13d
xorl %r12d, %r12d
leaq 0x2d89(%rip), %r15      # 0x994c
movq -0x38(%rbp), %rdx
movzbl (%rbx), %eax
leal -0x62(%rax), %ecx
cmpl $0x12, %ecx
ja 0x6bed
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
incl %r12d
addl $0x4, %r14d
jmp 0x6c43
incl %r12d
addl $0x8, %r14d
jmp 0x6c43
cmpl $0x53, %eax
je 0x6bfb
testl %eax, %eax
jne 0x6c46
jmp 0x6f5b
movl %r12d, %eax
shlq $0x4, %rax
movq (%rdx,%rax), %rdi
testq %rdi, %rdi
je 0x6f3c
incl %r12d
callq 0x20d0
addl %eax, %r14d
andl $-0x4, %r14d
addl $0x4, %r14d
decl %r13d
movq -0x38(%rbp), %rdx
jmp 0x6c46
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 0x6bc7
movl %r14d, %r13d
movq -0x30(%rbp), %rbx
testq %rbx, %rbx
je 0x6f2a
movq -0x50(%rbp), %rdx
cmpq %rdx, %r13
jbe 0x6c7d
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
jmp 0x6f2a
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 0x6cb4
xorl %r14d, %r14d
movl %r14d, %ecx
movb %al, (%rbx,%rcx)
movb 0x1(%rdx,%r14), %al
incq %r14
testb %al, %al
jne 0x6c9e
andl $-0x4, %r14d
leal 0x4(%r14), %eax
addl $0x5, %r14d
movb $0x2c, (%rbx,%rax)
movb (%r12), %al
testb %al, %al
je 0x6cdf
leaq 0x1(%r12), %rcx
movl %r14d, %edx
incl %r14d
movb %al, (%rbx,%rdx)
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x6ccd
andl $-0x4, %r14d
addl $0x4, %r14d
movq %r12, %rdi
callq 0x6b0f
testl %eax, %eax
je 0x6f27
xorl %r15d, %r15d
leaq 0x2c97(%rip), %r9       # 0x9998
movq -0x38(%rbp), %r11
movzbl (%r12), %esi
leal -0x62(%rsi), %edi
cmpl $0x12, %edi
ja 0x6e16
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 0x6e00
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 0x6d05
jmp 0x6f27
cmpl $0x53, %esi
je 0x6e24
testl %esi, %esi
jne 0x6e06
jmp 0x6f7a
movl %r15d, %edx
shlq $0x4, %rdx
movq (%r11,%rdx), %rsi
movb (%rsi), %dil
testb %dil, %dil
je 0x6e4f
incq %rsi
movl %r14d, %edx
incl %r14d
movb %dil, (%rbx,%rdx)
movb (%rsi), %dil
incq %rsi
testb %dil, %dil
jne 0x6e3a
incl %r15d
andl $-0x4, %r14d
addl $0x4, %r14d
jmp 0x6e04
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 0x6f0b
testq %rbx, %rbx
je 0x6f11
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 0x6eae
addl %r8d, %r14d
jmp 0x6f11
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 0x6e04
addl %r14d, %ebx
movl %ebx, %r14d
incl %r15d
addl $0x3, %r14d
andl $-0x4, %r14d
decl %eax
movq -0x30(%rbp), %rbx
jmp 0x6e06
movl %r14d, %r13d
movq %r13, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2cee(%rip), %rdi      # 0x9c31
leaq 0x2b8a(%rip), %rsi      # 0x9ad4
leaq 0x2c9d(%rip), %rcx      # 0x9bee
movl $0xd7, %edx
callq 0x2130
leaq 0x2bfe(%rip), %rdi      # 0x9b60
leaq 0x2b6b(%rip), %rsi      # 0x9ad4
leaq 0x2c7e(%rip), %rcx      # 0x9bee
movl $0xc0, %edx
callq 0x2130
leaq 0x2bdf(%rip), %rdi      # 0x9b60
leaq 0x2b4c(%rip), %rsi      # 0x9ad4
leaq 0x2bd5(%rip), %rcx      # 0x9b64
movl $0x14a, %edx            # imm = 0x14A
callq 0x2130