// File: /workspace/github_c_makefile_star2_O0/mruby-zest[P]osc-bridge/src/bridge.c
// Function: br_create
bridge_t *br_create(uri_t uri)
{
    bridge_t *br = (bridge_t*)calloc(1,sizeof(bridge_t));

    br->loop = (uv_loop_t*)calloc(1, sizeof(uv_loop_t));
    uv_loop_init(br->loop);

    uv_udp_init(br->loop, &br->socket);
    int no_collide = rand()%1000;
    for(int offset=0; offset < 1000; ++offset) {
        struct sockaddr_in recv_addr;
        recv_addr.sin_family = AF_INET;
        recv_addr.sin_port = htons(1338+(offset+no_collide)%1000);
        recv_addr.sin_addr.s_addr = INADDR_ANY;
        if(!uv_udp_bind(&br->socket, (const struct sockaddr *)&recv_addr, 0))
            break;
    }
    br->socket.data = br;

    uv_udp_recv_start(&br->socket, alloc_buffer, on_read);

    //parse the url to read
    if(strstr(uri, "osc.udp://") != uri) {
        fprintf(stderr, "[ERROR] Unknown protocol in '%s'\n", uri);
        fprintf(stderr, "[ERROR] Try something like osc.udp://localhost:1234\n");
        exit(1);
    }
    uri = uri+10;
    char *tmp = br->address = strdup(uri);
    while(*tmp && *tmp != ':') ++tmp;
    if(*tmp == ':')
        *tmp++ = 0;

    br->port = atoi(tmp);

    return br;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, %edi
movl $0x138, %esi            # imm = 0x138
callq 0x2180
movq %rax, -0x10(%rbp)
movl $0x1, %edi
movl $0x350, %esi            # imm = 0x350
callq 0x2180
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2150
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x21d0
callq 0x22c0
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl %edx, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x3e8, -0x18(%rbp)     # imm = 0x3E8
jge 0x292f
movw $0x2, -0x28(%rbp)
movl -0x18(%rbp), %eax
addl -0x14(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
addl $0x53a, %edx            # imm = 0x53A
movw %dx, %ax
movzwl %ax, %edi
callq 0x20d0
movw %ax, -0x26(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
leaq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x2190
cmpl $0x0, %eax
jne 0x2922
jmp 0x292f
jmp 0x2924
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x28cf
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
leaq 0x106(%rip), %rsi       # 0x2a50
leaq -0x551(%rip), %rdx      # 0x2400
callq 0x2210
movq -0x8(%rbp), %rdi
leaq 0x96f7(%rip), %rsi      # 0xc058
callq 0x22b0
cmpq -0x8(%rbp), %rax
je 0x29aa
movq 0xd685(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x96e2(%rip), %rsi      # 0xc063
movb $0x0, %al
callq 0x21b0
movq 0xd669(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x96ec(%rip), %rsi      # 0xc085
movb $0x0, %al
callq 0x21b0
movl $0x1, %edi
callq 0x2290
movq -0x8(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x22a0
movq -0x10(%rbp), %rcx
movq %rax, 0xf0(%rcx)
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x31(%rbp)
je 0x29ef
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
setne %al
movb %al, -0x31(%rbp)
movb -0x31(%rbp), %al
testb $0x1, %al
jne 0x29f8
jmp 0x2a06
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x29ce
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
jne 0x2a24
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movb $0x0, (%rax)
movq -0x30(%rbp), %rdi
callq 0x2270
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xf8(%rax)
movq -0x10(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)