// File: /workspace/github_c_makefile_star2_O2/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 $0x71, %esi
je 0x12a70
cmpl $0x5, %esi
je 0x12964
cmpl $0x65, %esi
je 0x12a25
cmpl $0x70, %esi
je 0x129fb
cmpl $0x2, %esi
jne 0x12be2
movl (%r14), %ebx
leaq 0x3e62(%rip), %rsi      # 0x1679a
leaq 0x1c6f(%rip), %rdx      # 0x145ae
testl %ebx, %ebx
je 0x12b8f
xorl %edi, %edi
callq 0x4227
testl %eax, %eax
je 0x12be2
leaq 0x3e53(%rip), %rdi      # 0x167b0
movl %ebx, %esi
jmp 0x12a5b
leaq 0x3e2f(%rip), %rsi      # 0x1679a
leaq 0x1c3c(%rip), %rdx      # 0x145ae
pushq $0x3
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x1298c
leaq 0x3f5b(%rip), %rdi      # 0x168e0
xorl %eax, %eax
callq 0x430a
testq %rbx, %rbx
je 0x12be2
movq (%rbx), %r14
leaq 0x3f91(%rip), %r15      # 0x16930
leaq 0x1c08(%rip), %r12      # 0x145ae
pushq $0x3
popq %rbp
leaq 0x3f91(%rip), %r13      # 0x16941
movq (%r14), %r14
testq %r14, %r14
je 0x12aaa
movq 0xc0(%r14), %rax
cmpq 0x8(%rbx), %rax
jne 0x129b0
testb $0x1, 0xc8(%r14)
jne 0x129b0
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4227
testl %eax, %eax
je 0x129f1
movq %r13, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x430a
orb $0x8, 0xc9(%r14)
jmp 0x129b0
leaq 0x3d98(%rip), %rsi      # 0x1679a
leaq 0x1ba5(%rip), %rdx      # 0x145ae
pushq $0x2
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x12be2
leaq 0x3e0d(%rip), %rdi      # 0x1682d
jmp 0x12ba2
cmpl $0xc8, 0x50(%r14)
je 0x12be2
leaq 0x3d60(%rip), %rsi      # 0x1679a
leaq 0x1b6d(%rip), %rdx      # 0x145ae
xorl %edi, %edi
callq 0x4227
testl %eax, %eax
je 0x12be2
movl 0x50(%r14), %esi
leaq 0x3da4(%rip), %rdi      # 0x167ff
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x430a
movq 0x8(%r14), %r15
cmpq $0x5, %r15
ja 0x12af0
leaq 0x3d19(%rip), %rsi      # 0x1679a
leaq 0x1b26(%rip), %rdx      # 0x145ae
xorl %edi, %edi
callq 0x4227
testl %eax, %eax
je 0x12be2
leaq 0x3db0(%rip), %rdi      # 0x1684e
xorl %eax, %eax
callq 0x430a
jmp 0x12be2
andq $0x0, 0x20(%rbx)
leaq 0x3ce4(%rip), %rsi      # 0x1679a
leaq 0x1af1(%rip), %rdx      # 0x145ae
pushq $0x2
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x12ad7
leaq 0x3e31(%rip), %rdi      # 0x16901
xorl %eax, %eax
callq 0x430a
pushq $0x1
popq %rsi
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11cb9
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 0x11b68
movq %rbx, %rdi
movl %ebp, %esi
callq 0xa3fe
testq %rax, %rax
je 0x12bb7
movq %rax, %rbx
testq %r15, %r15
je 0x12b4c
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x8010
testb $0x1, %r12b
je 0x12be2
leaq 0x3c3d(%rip), %rsi      # 0x1679a
leaq 0x1a4a(%rip), %rdx      # 0x145ae
pushq $0x3
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x12b7e
leaq 0x3d24(%rip), %rdi      # 0x1689b
xorl %eax, %eax
callq 0x430a
orb $0x8, 0xc9(%rbx)
movq %rbx, %rdi
callq 0x7a87
jmp 0x12be2
pushq $0x2
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x12be2
leaq 0x3c3a(%rip), %rdi      # 0x167dc
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x430a
testq %r15, %r15
je 0x12be2
leaq 0x3bd7(%rip), %rsi      # 0x1679a
leaq 0x19e4(%rip), %rdx      # 0x145ae
pushq $0x3
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x12be2
leaq 0x3c90(%rip), %rdi      # 0x1686d
jmp 0x12a9e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)