// File: /workspace/github_c_makefile_star2_O2/mruby-zest[P]osc-bridge/src/bridge.c
// Function: br_recv
void br_recv(bridge_t *br, const char *msg)
{
    //char buffer[128];
    //rtosc_message(buffer, 128, "/part0/Pvolume", "i", 74);
    if(!msg)
        return;

    //if(rtosc_narguments(msg) < 3) {
    //    //printf("BR RECEIVE %s:%s\n", msg, rtosc_argument_string(msg));
    //    //printf("MESSAGE IS %d bytes\n", rtosc_message_length(msg, -1));
    //}
    br->last_update = 1e-3*uv_now(br->loop);

    if(!strcmp("/damage", msg) && !strcmp("s", rtosc_argument_string(msg))) {
        const char *dmg = rtosc_argument(msg, 0).s;
        br_damage(br, dmg);
        return;
    }

    const int nargs = rtosc_narguments(msg);
    if(nargs == 1)
        cache_set(br, msg, rtosc_type(msg, 0), rtosc_argument(msg, 0), 0);
    else {
        //Try to handle the vector message cases
        //printf("BRIDGE RECEIVE A VECTOR MESSAGE\n");
        //TODO verify that we've got some sort of uniformity?
        rtosc_arg_itr_t  itr   = rtosc_itr_begin(msg);
        rtosc_arg_t     *args  = (rtosc_arg_t*)calloc(nargs, sizeof(rtosc_arg_t));
        char            *types = strdup(rtosc_argument_string(msg));

        int offset = 0;
        while(!rtosc_itr_end(itr))
            args[offset++] = rtosc_itr_next(&itr).val;

        cache_set_vector(br, msg, types, args);
        free(args);
        free(types);
    }
    //for(int i=0; i<br->callback_len; ++i) {
    //    printf("cb name = %s\n", br->callback[i].path);
    //    bridge_callback_t cb = br->callback[i];
    //    if(!strcmp(cb.path, msg))
    //        cb.cb(msg, cb.data);
    //}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
testq %rsi, %rsi
je 0x260c
movq %rsi, %r15
movq %rdi, %r14
movq (%rdi), %rdi
callq 0x2170
movq %rax, %xmm0
punpckldq 0x5b6f(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x5b77(%rip), %xmm0     # 0x8020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x5b73(%rip), %xmm1     # 0x8030
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x5b6b(%rip), %xmm1     # 0x8038
cvttsd2si %xmm1, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, 0x130(%r14)
leaq 0x5d9c(%rip), %rdi      # 0x8286
movq %r15, %rsi
callq 0x21a0
testl %eax, %eax
jne 0x2514
movq %r15, %rdi
callq 0x60cc
movzbl (%rax), %ecx
subl $0x73, %ecx
jne 0x250a
movzbl 0x1(%rax), %ecx
negl %ecx
testl %ecx, %ecx
je 0x261b
movq %r15, %rdi
callq 0x6111
cmpl $0x1, %eax
jne 0x255c
movq %r15, %rdi
xorl %esi, %esi
callq 0x613e
movl %eax, %ebx
movq %r15, %rdi
xorl %esi, %esi
callq 0x6b6e
movq %rdx, %r8
movsbl %bl, %edx
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rcx
xorl %r9d, %r9d
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31b4
movl %eax, %ebx
movq %r14, -0x48(%rbp)
movq %r15, %rdi
callq 0x68a1
movq %rax, %r13
movq %rdx, %r12
leaq -0x58(%rbp), %rax
movq %r13, (%rax)
movq %rdx, 0x8(%rax)
movslq %ebx, %rdi
pushq $0x10
popq %rsi
callq 0x2180
movq %rax, -0x30(%rbp)
movq %r15, -0x40(%rbp)
movq %r15, %rdi
callq 0x60cc
movq %rax, %rdi
callq 0x22c0
movq %rax, -0x38(%rbp)
leaq -0x68(%rbp), %r14
leaq -0x70(%rbp), %rbx
movq -0x30(%rbp), %r15
movq %r13, %rdi
movq %r12, %rsi
callq 0x6b54
testl %eax, %eax
jne 0x25e1
movq %rbx, %rdi
leaq -0x58(%rbp), %rsi
callq 0x6913
movupd (%r14), %xmm0
movupd %xmm0, (%r15)
movq -0x58(%rbp), %r13
movq -0x50(%rbp), %r12
addq $0x10, %r15
jmp 0x25ae
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %r14
movq %r14, %rdx
movq -0x30(%rbp), %rbx
movq %rbx, %rcx
callq 0x2e5c
movq %rbx, %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
xorl %esi, %esi
callq 0x6b6e
movq %r14, %rdi
movq %rax, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x389a