// File: /workspace/github_c_makefile_star2_O3/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_tun_client_handler
static void mg_tun_client_handler(struct mg_connection *nc, int ev,
                                  void *ev_data MG_UD_ARG(void *user_data)) {
#if !MG_ENABLE_CALLBACK_USERDATA
  void *user_data = nc->user_data;
#else
  (void) nc;
#endif
  struct mg_tun_client *client = (struct mg_tun_client *) user_data;

  switch (ev) {
    case MG_EV_CONNECT: {
      int err = *(int *) ev_data;

      if (err) {
        LOG(LL_ERROR, ("Cannot connect to the tunnel dispatcher: %d", err));
      } else {
        LOG(LL_INFO, ("Connected to the tunnel dispatcher"));
      }
      break;
    }
    case MG_EV_HTTP_REPLY: {
      struct http_message *hm = (struct http_message *) ev_data;

      if (hm->resp_code != 200) {
        LOG(LL_ERROR,
            ("Tunnel dispatcher reply non-OK status code %d", hm->resp_code));
      }
      break;
    }
    case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
      LOG(LL_INFO, ("Tunnel dispatcher handshake done"));
      break;
    }
    case MG_EV_WEBSOCKET_FRAME: {
      struct websocket_message *wm = (struct websocket_message *) ev_data;
      struct mg_connection *tc;
      struct mg_tun_frame frame;

      if (mg_tun_parse_frame(wm->data, wm->size, &frame) == -1) {
        LOG(LL_ERROR, ("Got invalid tun frame dropping"));
        break;
      }

      mg_tun_log_frame(&frame);

      tc = mg_tun_if_find_conn(client, frame.stream_id);
      if (tc == NULL) {
        if (frame.body.len > 0) {
          LOG(LL_DEBUG, ("Got frame after receiving end has been closed"));
        }
        break;
      }
      if (frame.body.len > 0) {
        mg_if_recv_tcp_cb(tc, (void *) frame.body.p, frame.body.len,
                          0 /* own */);
      }
      if (frame.flags & MG_TUN_F_END_STREAM) {
        LOG(LL_DEBUG, ("Closing tunneled connection because got end of stream "
                       "from other end"));
        tc->flags |= MG_F_CLOSE_IMMEDIATELY;
        mg_close_conn(tc);
      }
      break;
    }
    case MG_EV_CLOSE: {
      LOG(LL_DEBUG, ("Closing all tunneled connections"));
      /*
       * The client might have been already freed when the listening socket is
       * closed.
       */
      if (client != NULL) {
        mg_tun_close_all(client);
        client->disp = NULL;
        LOG(LL_INFO, ("Dispatcher connection is no more, reconnecting"));
        /* TODO(mkm): implement exp back off */
        mg_tun_reconnect(client, MG_TUN_RECONNECT_INTERVAL);
      }
      break;
    }
    default:
      break;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq 0xa0(%rdi), %rbx
cmpl $0x64, %esi
jle 0x14540
cmpl $0x65, %esi
je 0x1463a
cmpl $0x70, %esi
je 0x14672
cmpl $0x71, %esi
jne 0x147dc
movq 0x8(%r14), %r15
cmpq $0x5, %r15
ja 0x146e4
leaq 0x32a0(%rip), %rsi      # 0x177b7
leaq 0x10b0(%rip), %rdx      # 0x155ce
xorl %edi, %edi
callq 0x4447
testl %eax, %eax
je 0x147dc
leaq 0x3337(%rip), %rdi      # 0x1786b
xorl %eax, %eax
callq 0x4539
jmp 0x147dc
cmpl $0x2, %esi
je 0x1469e
cmpl $0x5, %esi
jne 0x147dc
leaq 0x325e(%rip), %rsi      # 0x177b7
leaq 0x106e(%rip), %rdx      # 0x155ce
movl $0x3, %edi
callq 0x4447
testl %eax, %eax
je 0x1457c
leaq 0x3388(%rip), %rdi      # 0x178fd
xorl %eax, %eax
callq 0x4539
testq %rbx, %rbx
je 0x147dc
movq (%rbx), %rax
movq (%rax), %r14
testq %r14, %r14
je 0x145ed
leaq 0x33b6(%rip), %r15      # 0x1794d
leaq 0x1030(%rip), %r12      # 0x155ce
leaq 0x33b9(%rip), %r13      # 0x1795e
movq 0xc0(%r14), %rax
cmpq 0x8(%rbx), %rax
jne 0x145e5
testb $0x1, 0xc8(%r14)
jne 0x145e5
movl $0x3, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4447
testl %eax, %eax
je 0x145dd
movq %r13, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x4539
orb $0x8, 0xc9(%r14)
movq (%r14), %r14
testq %r14, %r14
jne 0x145a5
movq $0x0, 0x20(%rbx)
leaq 0x31bb(%rip), %rsi      # 0x177b7
leaq 0xfcb(%rip), %rdx       # 0x155ce
movl $0x2, %edi
callq 0x4447
testl %eax, %eax
je 0x1461f
leaq 0x3306(%rip), %rdi      # 0x1791e
xorl %eax, %eax
callq 0x4539
movq %rbx, %rdi
movl $0x1, %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12f5c
cmpl $0xc8, 0x50(%r14)
je 0x147dc
leaq 0x3168(%rip), %rsi      # 0x177b7
leaq 0xf78(%rip), %rdx       # 0x155ce
xorl %edi, %edi
callq 0x4447
testl %eax, %eax
je 0x147dc
movl 0x50(%r14), %esi
leaq 0x31ac(%rip), %rdi      # 0x1781c
jmp 0x146cf
leaq 0x313e(%rip), %rsi      # 0x177b7
leaq 0xf4e(%rip), %rdx       # 0x155ce
movl $0x2, %edi
callq 0x4447
testl %eax, %eax
je 0x147dc
leaq 0x31b1(%rip), %rdi      # 0x1784a
jmp 0x1479a
movl (%r14), %ebx
leaq 0x310f(%rip), %rsi      # 0x177b7
leaq 0xf1f(%rip), %rdx       # 0x155ce
testl %ebx, %ebx
je 0x14785
xorl %edi, %edi
callq 0x4447
testl %eax, %eax
je 0x147dc
leaq 0x3100(%rip), %rdi      # 0x177cd
movl %ebx, %esi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4539
movq (%r14), %r14
movb (%r14), %al
movq %rsp, %rdi
movb %al, (%rdi)
movb 0x1(%r14), %r12b
movb %r12b, 0x1(%rdi)
movl 0x2(%r14), %ebp
bswapl %ebp
movl %ebp, 0x4(%rdi)
addq $0x6, %r14
movq %r14, 0x8(%rdi)
addq $-0x6, %r15
movq %r15, 0x10(%rdi)
callq 0x12e01
movq %rbx, %rdi
movl %ebp, %esi
callq 0xac85
testq %rax, %rax
je 0x147af
movq %rax, %rbx
testq %r15, %r15
je 0x14740
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x8488
testb $0x1, %r12b
je 0x147dc
leaq 0x3066(%rip), %rsi      # 0x177b7
leaq 0xe76(%rip), %rdx       # 0x155ce
movl $0x3, %edi
callq 0x4447
testl %eax, %eax
je 0x14774
leaq 0x314b(%rip), %rdi      # 0x178b8
xorl %eax, %eax
callq 0x4539
orb $0x8, 0xc9(%rbx)
movq %rbx, %rdi
callq 0x7e85
jmp 0x147dc
movl $0x2, %edi
callq 0x4447
testl %eax, %eax
je 0x147dc
leaq 0x305f(%rip), %rdi      # 0x177f9
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4539
testq %r15, %r15
je 0x147dc
leaq 0x2ffc(%rip), %rsi      # 0x177b7
leaq 0xe0c(%rip), %rdx       # 0x155ce
movl $0x3, %edi
callq 0x4447
testl %eax, %eax
je 0x147dc
leaq 0x30b3(%rip), %rdi      # 0x1788a
jmp 0x14534
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop