// File: /workspace/github_c_makefile_star2_O3/mruby-zest[P]osc-bridge/src/bridge.c
// Function: br_tick
void br_tick(bridge_t *br)
{
    //Run all network events
    for(int i=0; i<200; ++i)
        uv_run(br->loop, UV_RUN_NOWAIT);

    if(br->frame_messages >= BR_RATE_LIMIT) {
        //printf("[INFO] Hit rate limit\n");
    }

    br->frame_messages = 0;
    if(br->rlimit) {
        //printf("[INFO] Reading through rate limited fields\n");
        for(int i=0; i<br->rlimit_len && i<BR_RATE_LIMIT; ++i) {
            char *msg = br->rlimit[i];
            //printf("[DEBUG] message = \"%s\"\n", msg);
            do_send(br, msg, rtosc_message_length(msg, -1));
        }
        if(br->frame_messages == br->rlimit_len) {
            //printf("[INFO] Clearing rate limit queue\n");
            br->rlimit_len = 0;
            free(br->rlimit);
            br->rlimit = 0;
        } else {
            char **base = br->rlimit;
            int N = br->frame_messages;
            int M = br->rlimit_len;
            //printf("[INFO] Shrinking rate limit queue %d=>%d\n", M, M-N);
            memmove(base, base+N, sizeof(void*)*(M-N));
            br->rlimit_len = M-N;
        }
        //wait 10ms
        //usleep(10000);
    }
    uv_update_time(br->loop);
    double now  = 1e-3*uv_now(br->loop);

    if(!br->rlimit) {
        for(int i=0; i<br->cache_len; ++i) {
            char *path   = br->cache[i].path;
            int   pend   = br->cache[i].pending;
            int   valid  = br->cache[i].valid;
            int   usable = br->cache[i].usable;
            int   fref   = br->cache[i].force_refresh;
            double uptim = br->cache[i].request_time;
            int   rq     = br->cache[i].requests;
            (void) path;
            if(usable && (pend || !valid)) {
                //printf("cache status = <%s, %d, %d, %f, %d>\n", path, pend, valid, uptim, rq);
                if(uptim < now - 300e-3) {
                    if(rq < 10)
                        cache_update(br, &br->cache[i]);
                    //else if(br->cache[i].requests++ == 10)
                    //    printf("[ERROR] Invalid parameter cannot be accessed at <%s>\n", path);
                }
            } else if(usable && fref)
                if(uptim < now - 50e-3)
                    cache_update(br, &br->cache[i]);

        }
    }

    //Attempt to disable debouncing
    if(br->debounce_len == 0)
        return;
    double delta  = 200e-3;
    double thresh = now - delta;
    for(int i=br->debounce_len-1; i >= 0; --i) {
        if(br->bounce[i].last_set < thresh) {
            run_callbacks(br, cache_get(br, br->bounce[i].path));
            debounce_pop(br, i);
        }
    }


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl $0xc8, %r14d
movq (%rbx), %rdi
movl $0x2, %esi
callq 0x2310
decl %r14d
jne 0x3ca7
movl $0x0, 0xfc(%rbx)
cmpq $0x0, 0x118(%rbx)
je 0x3d72
movl 0x12c(%rbx), %eax
testl %eax, %eax
jle 0x3d24
xorl %r15d, %r15d
movq 0x118(%rbx), %rax
movq (%rax,%r15,8), %r14
movq %r14, %rdi
movq $-0x1, %rsi
callq 0x787b
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x2725
movl 0x12c(%rbx), %eax
cmpq $0x7e, %r15
ja 0x3d1c
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x3cde
movl 0xfc(%rbx), %ecx
jmp 0x3d26
xorl %ecx, %ecx
subl %ecx, %eax
jne 0x3d4d
movl $0x0, 0x12c(%rbx)
movq 0x118(%rbx), %rdi
callq 0x2030
movq $0x0, 0x118(%rbx)
jmp 0x3d72
movq 0x118(%rbx), %rdi
movslq %ecx, %rcx
leaq (%rdi,%rcx,8), %rsi
movslq %eax, %r14
leaq (,%r14,8), %rdx
callq 0x2250
movl %r14d, 0x12c(%rbx)
movq (%rbx), %rdi
callq 0x2290
movq (%rbx), %rdi
callq 0x2170
movq %rax, %xmm0
punpckldq 0x5281(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x5289(%rip), %xmm0     # 0x9020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x5285(%rip), %xmm1     # 0x9030
movapd %xmm1, -0x40(%rbp)
cmpq $0x0, 0x118(%rbx)
jne 0x3e63
cmpl $0x0, 0x120(%rbx)
jle 0x3e63
movsd 0x526d(%rip), %xmm1     # 0x9040
movapd -0x40(%rbp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, -0x50(%rbp)
movsd 0x525f(%rip), %xmm1     # 0x9048
addsd %xmm0, %xmm1
movsd %xmm1, -0x48(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x100(%rbx), %rsi
movb 0x8(%rsi,%r14), %al
movsd 0x10(%rsi,%r14), %xmm0
testb $0x4, %al
sete %cl
movl %eax, %edx
andb $0x3, %dl
cmpb $0x1, %dl
sete %dl
orb %cl, %dl
je 0x3e32
notb %al
testb $0xc, %al
jne 0x3e50
movsd -0x48(%rbp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3e45
jmp 0x3e50
movsd -0x50(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3e50
cmpl $0xa, 0x18(%rsi,%r14)
jge 0x3e50
addq %r14, %rsi
movq %rbx, %rdi
callq 0x379c
incq %r15
movslq 0x120(%rbx), %rax
addq $0x30, %r14
cmpq %rax, %r15
jl 0x3df8
movl 0x124(%rbx), %r14d
testq %r14, %r14
je 0x3f28
testl %r14d, %r14d
jle 0x3f28
movapd -0x40(%rbp), %xmm0
addsd 0x51c7(%rip), %xmm0     # 0x9050
movapd %xmm0, -0x40(%rbp)
leal -0x1(%r14), %r15d
movq %r14, %r12
shlq $0x4, %r12
leaq -0x1(%r14), %r13
movq 0x108(%rbx), %rax
movq %r13, %rcx
shlq $0x4, %rcx
movapd -0x40(%rbp), %xmm0
ucomisd 0x8(%rax,%rcx), %xmm0
jbe 0x3f14
addq %rcx, %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x3171
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3f56
movslq 0x124(%rbx), %rax
cmpq %rax, %r14
jg 0x3f37
decl %eax
movq %r12, %rcx
movl %r15d, %edx
cmpq %rax, %r13
jae 0x3f0e
movq 0x108(%rbx), %rax
movupd (%rax,%rcx), %xmm0
movupd %xmm0, -0x10(%rax,%rcx)
movl 0x124(%rbx), %eax
decl %eax
incl %edx
addq $0x10, %rcx
cmpl %edx, %eax
jg 0x3eea
movl %eax, 0x124(%rbx)
decl %r15d
addq $-0x10, %r12
cmpq $0x1, %r14
movq %r13, %r14
jg 0x3e99
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x54e4(%rip), %rdi      # 0x9422
leaq 0x5237(%rip), %rsi      # 0x917c
leaq 0x54ed(%rip), %rcx      # 0x9439
movl $0x1ee, %edx            # imm = 0x1EE
callq 0x2130