// File: /workspace/github_c_makefile_star2_O0/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_handle_tcp_read
static void mg_handle_tcp_read(struct mg_connection *conn) {
  int n = 0;
  char *buf = (char *) MG_MALLOC(MG_TCP_RECV_BUFFER_SIZE);

  if (buf == NULL) {
    DBG(("OOM"));
    return;
  }

#if MG_ENABLE_SSL
  if (conn->flags & MG_F_SSL) {
    if (conn->flags & MG_F_SSL_HANDSHAKE_DONE) {
      /* SSL library may have more bytes ready to read than we ask to read.
       * Therefore, read in a loop until we read everything. Without the loop,
       * we skip to the next select() cycle which can just timeout. */
      while ((n = mg_ssl_if_read(conn, buf, MG_TCP_RECV_BUFFER_SIZE)) > 0) {
        DBG(("%p %d bytes <- %d (SSL)", conn, n, conn->sock));
        mg_if_recv_tcp_cb(conn, buf, n, 1 /* own */);
        buf = NULL;
        if (conn->flags & MG_F_CLOSE_IMMEDIATELY) break;
        /* buf has been freed, we need a new one. */
        buf = (char *) MG_MALLOC(MG_TCP_RECV_BUFFER_SIZE);
        if (buf == NULL) break;
      }
      MG_FREE(buf);
      if (n < 0 && n != MG_SSL_WANT_READ) conn->flags |= MG_F_CLOSE_IMMEDIATELY;
    } else {
      MG_FREE(buf);
      mg_ssl_begin(conn);
      return;
    }
  } else
#endif
  {
    n = (int) MG_RECV_FUNC(conn->sock, buf,
                           recv_avail_size(conn, MG_TCP_RECV_BUFFER_SIZE), 0);
    DBG(("%p %d bytes (PLAIN) <- %d", conn, n, conn->sock));
    if (n > 0) {
      mg_if_recv_tcp_cb(conn, buf, n, 1 /* own */);
    } else {
      MG_FREE(buf);
    }
    if (n == 0) {
      /* Orderly shutdown of the socket, try flushing output. */
      conn->flags |= MG_F_SEND_AND_CLOSE;
    } else if (n < 0 && mg_is_error()) {
      conn->flags |= MG_F_CLOSE_IMMEDIATELY;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x400, %edi            # imm = 0x400
callq 0x3420
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xdf4c
jmp 0xdf1a
movl $0x4, %edi
leaq 0x1242b(%rip), %rsi     # 0x20351
leaq 0x1161d(%rip), %rdx     # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0xdf45
leaq 0x117b9(%rip), %rdi     # 0x1f6f7
movb $0x0, %al
callq 0x47a0
jmp 0xdf47
jmp 0xe02d
movq -0x8(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x19a00
movl -0x24(%rbp), %edi
movq -0x20(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x3060
movl %eax, -0xc(%rbp)
movl $0x4, %edi
leaq 0x123c5(%rip), %rsi     # 0x20351
leaq 0x115b7(%rip), %rdx     # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0xdfb9
movq -0x8(%rbp), %rsi
movl -0xc(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0x20(%rax), %ecx
leaq 0x123b2(%rip), %rdi     # 0x20364
movb $0x0, %al
callq 0x47a0
jmp 0xdfbb
cmpl $0x0, -0xc(%rbp)
jle 0xdfd8
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0xc(%rbp), %edx
movl $0x1, %ecx
callq 0xba10
jmp 0xdfe1
movq -0x18(%rbp), %rdi
callq 0x3050
cmpl $0x0, -0xc(%rbp)
jne 0xe002
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x400, %rcx            # imm = 0x400
movq %rcx, 0xc8(%rax)
jmp 0xe02d
cmpl $0x0, -0xc(%rbp)
jge 0xe02b
callq 0xd6b0
cmpl $0x0, %eax
je 0xe02b
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x800, %rcx            # imm = 0x800
movq %rcx, 0xc8(%rax)
jmp 0xe02d
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)