// File: /workspace/github_c_makefile_star2_O2/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, %r15d
pushq $0x2
popq %r14
subl $0x1, %r15d
jb 0x3b8f
movq (%rbx), %rdi
movl %r14d, %esi
callq 0x2310
jmp 0x3b7c
andl $0x0, 0xfc(%rbx)
cmpq $0x0, 0x118(%rbx)
je 0x3c33
xorl %r12d, %r12d
pushq $-0x1
popq %r14
movl 0x12c(%rbx), %eax
cmpq $0x7f, %r12
ja 0x3be7
movslq %eax, %rcx
cmpq %rcx, %r12
jge 0x3be7
movq 0x118(%rbx), %rax
movq (%rax,%r12,8), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x6fcc
movq %rbx, %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0x2689
incq %r12
jmp 0x3bab
movslq 0xfc(%rbx), %rcx
cmpl %ecx, %eax
jne 0x3c0f
andl $0x0, 0x12c(%rbx)
movq 0x118(%rbx), %rdi
callq 0x2030
andq $0x0, 0x118(%rbx)
jmp 0x3c33
movq 0x118(%rbx), %rdi
leaq (%rdi,%rcx,8), %rsi
subl %ecx, %eax
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 0x43c0(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x43c8(%rip), %xmm0     # 0x8020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x43c4(%rip), %xmm1     # 0x8030
movapd %xmm1, -0x40(%rbp)
cmpq $0x0, 0x118(%rbx)
je 0x3d4d
movl 0x124(%rbx), %r13d
testq %r13, %r13
je 0x3d3e
movapd -0x40(%rbp), %xmm0
addsd 0x43b4(%rip), %xmm0     # 0x8050
movapd %xmm0, -0x40(%rbp)
movq %r13, %r14
shlq $0x4, %r14
leal -0x1(%r13), %r15d
testl %r13d, %r13d
jle 0x3d3e
movq %r13, %r12
decq %r12
movq 0x108(%rbx), %rax
movq %r12, %rcx
shlq $0x4, %rcx
movapd -0x40(%rbp), %xmm0
ucomisd 0x8(%rax,%rcx), %xmm0
jbe 0x3d2f
addq %rcx, %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x30ae
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3e0a
movl 0x124(%rbx), %ecx
movl %r15d, %eax
movq %r14, %rdx
cmpl %r13d, %ecx
jge 0x3d23
jmp 0x3deb
movq 0x108(%rbx), %rcx
movupd (%rcx,%rdx), %xmm0
movupd %xmm0, -0x10(%rcx,%rdx)
movl 0x124(%rbx), %ecx
addq $0x10, %rdx
incl %eax
decl %ecx
cmpl %eax, %ecx
jg 0x3d05
movl %ecx, 0x124(%rbx)
addq $-0x10, %r14
decl %r15d
movq %r12, %r13
jmp 0x3cac
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x42eb(%rip), %xmm1     # 0x8040
movapd -0x40(%rbp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, -0x50(%rbp)
movsd 0x42dd(%rip), %xmm1     # 0x8048
addsd %xmm0, %xmm1
movsd %xmm1, -0x48(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movslq 0x120(%rbx), %rax
cmpq %rax, %r15
jge 0x3c7f
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 0x3dc4
notb %al
testb $0xc, %al
jne 0x3de2
movsd -0x48(%rbp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3dd7
jmp 0x3de2
movsd -0x50(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3de2
cmpl $0xa, 0x18(%rsi,%r14)
jge 0x3de2
addq %r14, %rsi
movq %rbx, %rdi
callq 0x36a4
incq %r15
addq $0x30, %r14
jmp 0x3d7a
leaq 0x4630(%rip), %rdi      # 0x8422
leaq 0x4383(%rip), %rsi      # 0x817c
leaq 0x4639(%rip), %rcx      # 0x8439
movl $0x1ee, %edx            # imm = 0x1EE
callq 0x2130