// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x64(%rbp)
subl $0x2, %eax
je 0x1d3b2
jmp 0x1d377
movl -0x64(%rbp), %eax
subl $0x5, %eax
je 0x1d5dc
jmp 0x1d385
movl -0x64(%rbp), %eax
subl $0x65, %eax
je 0x1d428
jmp 0x1d393
movl -0x64(%rbp), %eax
subl $0x70, %eax
je 0x1d477
jmp 0x1d3a1
movl -0x64(%rbp), %eax
subl $0x71, %eax
je 0x1d4ab
jmp 0x1d664
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1d3f2
jmp 0x1d3c3
xorl %edi, %edi
leaq 0x43b5(%rip), %rsi      # 0x21781
leaq 0x2177(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d3ee
movl -0x2c(%rbp), %esi
leaq 0x43b0(%rip), %rdi      # 0x21797
movb $0x0, %al
callq 0x47a0
jmp 0x1d3f0
jmp 0x1d423
jmp 0x1d3f4
movl $0x2, %edi
leaq 0x4381(%rip), %rsi      # 0x21781
leaq 0x2143(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d41f
leaq 0x43ab(%rip), %rdi      # 0x217c3
movb $0x0, %al
callq 0x47a0
jmp 0x1d421
jmp 0x1d423
jmp 0x1d666
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpl $0xc8, 0x50(%rax)
je 0x1d472
jmp 0x1d43f
xorl %edi, %edi
leaq 0x4339(%rip), %rsi      # 0x21781
leaq 0x20fb(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d46e
movq -0x38(%rbp), %rax
movl 0x50(%rax), %esi
leaq 0x437f(%rip), %rdi      # 0x217e6
movb $0x0, %al
callq 0x47a0
jmp 0x1d470
jmp 0x1d472
jmp 0x1d666
jmp 0x1d479
movl $0x2, %edi
leaq 0x42fc(%rip), %rsi      # 0x21781
leaq 0x20be(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d4a4
leaq 0x4377(%rip), %rdi      # 0x21814
movb $0x0, %al
callq 0x47a0
jmp 0x1d4a6
jmp 0x1d666
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x60(%rbp), %rdx
callq 0x196c0
cmpl $-0x1, %eax
jne 0x1d501
jmp 0x1d4d2
xorl %edi, %edi
leaq 0x42a6(%rip), %rsi      # 0x21781
leaq 0x2068(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d4fa
leaq 0x4342(%rip), %rdi      # 0x21835
movb $0x0, %al
callq 0x47a0
jmp 0x1d4fc
jmp 0x1d666
leaq -0x60(%rbp), %rdi
callq 0x19300
movq -0x28(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0xf490
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x1d55e
cmpq $0x0, -0x50(%rbp)
jbe 0x1d559
jmp 0x1d52a
movl $0x3, %edi
leaq 0x424b(%rip), %rsi      # 0x21781
leaq 0x200d(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d555
leaq 0x4306(%rip), %rdi      # 0x21854
movb $0x0, %al
callq 0x47a0
jmp 0x1d557
jmp 0x1d559
jmp 0x1d666
cmpq $0x0, -0x50(%rbp)
jbe 0x1d57a
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rax
movl %eax, %edx
xorl %ecx, %ecx
callq 0xba10
movzbl -0x5f(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x1d5d7
jmp 0x1d588
movl $0x3, %edi
leaq 0x41ed(%rip), %rsi      # 0x21781
leaq 0x1faf(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d5b3
leaq 0x42d6(%rip), %rdi      # 0x21882
movb $0x0, %al
callq 0x47a0
jmp 0x1d5b5
movq -0x48(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x800, %rcx            # imm = 0x800
movq %rcx, 0xc8(%rax)
movq -0x48(%rbp), %rdi
callq 0xb110
jmp 0x1d666
jmp 0x1d5de
movl $0x3, %edi
leaq 0x4197(%rip), %rsi      # 0x21781
leaq 0x1f59(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d609
leaq 0x42c5(%rip), %rdi      # 0x218c7
movb $0x0, %al
callq 0x47a0
jmp 0x1d60b
cmpq $0x0, -0x28(%rbp)
je 0x1d662
movq -0x28(%rbp), %rdi
callq 0x1d670
movq -0x28(%rbp), %rax
movq $0x0, 0x20(%rax)
movl $0x2, %edi
leaq 0x414e(%rip), %rsi      # 0x21781
leaq 0x1f10(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1d652
leaq 0x429d(%rip), %rdi      # 0x218e8
movb $0x0, %al
callq 0x47a0
jmp 0x1d654
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x19500
jmp 0x1d666
jmp 0x1d666
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)