// File: nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: process_new_connection
static void process_new_connection(struct mg_connection *conn) {
  struct mg_request_info *ri = &conn->request_info;
  int keep_alive_enabled;
  const char *cl;

  keep_alive_enabled = !strcmp(conn->ctx->config[ENABLE_KEEP_ALIVE], "yes");

  do {
    reset_per_request_attributes(conn);

    // If next request is not pipelined, read it in
    if ((conn->request_len = get_request_len(conn->buf, conn->data_len)) == 0) {
      conn->request_len = read_request(NULL, conn->client.sock, conn->ssl,
          conn->buf, conn->buf_size, &conn->data_len);
    }
    assert(conn->data_len >= conn->request_len);
    if (conn->request_len == 0 && conn->data_len == conn->buf_size) {
      send_http_error(conn, 413, "Request Too Large", "");
      return;
    } if (conn->request_len <= 0) {
      return;  // Remote end closed the connection
    }

    // Nul-terminate the request cause parse_http_request() uses sscanf
    conn->buf[conn->request_len - 1] = '\0';
    if (!parse_http_request(conn->buf, ri) ||
        (!conn->client.is_proxy && !is_valid_uri(ri->uri))) {
      // Do not put garbage in the access log, just send it back to the client
      send_http_error(conn, 400, "Bad Request",
          "Cannot parse HTTP request: [%.*s]", conn->data_len, conn->buf);
    } else if (strcmp(ri->http_version, "1.0") &&
               strcmp(ri->http_version, "1.1")) {
      // Request seems valid, but HTTP version is strange
      send_http_error(conn, 505, "HTTP version not supported", "");
      log_access(conn);
    } else {
      // Request is valid, handle it
      cl = get_header(ri, "Content-Length");
      conn->content_len = cl == NULL ? -1 : strtoll(cl, NULL, 10);
      conn->birth_time = time(NULL);
      if (conn->client.is_proxy) {
        handle_proxy_request(conn);
      } else {
        handle_request(conn);
      }
      log_access(conn);
      discard_current_request_from_buffer(conn);
    }
    // conn->peer is not NULL only for SSL-ed proxy connections
  } while (conn->peer || (keep_alive_enabled && should_keep_alive(conn)));
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rax
movq 0x458(%rax), %rax
movq 0x78(%rax), %rdi
leaq 0x72da(%rip), %rsi      # 0x228d8
callq 0x5810
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x24(%rsp)
movq 0x30(%rsp), %rdi
callq 0x1b950
movq 0x30(%rsp), %rax
movq 0x4c8(%rax), %rdi
movq 0x30(%rsp), %rax
movl 0x4d8(%rax), %esi
callq 0x1ba20
movq 0x30(%rsp), %rcx
movl %eax, 0x4d4(%rcx)
cmpl $0x0, %eax
jne 0x1b69b
movq 0x30(%rsp), %rax
movl 0x470(%rax), %esi
movq 0x30(%rsp), %rax
movq 0x460(%rax), %rdx
movq 0x30(%rsp), %rax
movq 0x4c8(%rax), %rcx
movq 0x30(%rsp), %rax
movl 0x4d0(%rax), %r8d
movq 0x30(%rsp), %r9
addq $0x4d8, %r9             # imm = 0x4D8
xorl %eax, %eax
movl %eax, %edi
callq 0x1bb80
movl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x4d4(%rax)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x4d4(%rax)
jne 0x1b6e5
movq 0x30(%rsp), %rax
movl 0x4d8(%rax), %eax
movq 0x30(%rsp), %rcx
cmpl 0x4d0(%rcx), %eax
jne 0x1b6e5
movq 0x30(%rsp), %rdi
movl $0x19d, %esi            # imm = 0x19D
leaq 0x7762(%rip), %rdx      # 0x22e34
leaq 0x6c17(%rip), %rcx      # 0x222f0
movb $0x0, %al
callq 0x1bc40
jmp 0x1b8e2
movq 0x30(%rsp), %rax
cmpl $0x0, 0x4d4(%rax)
jg 0x1b6f8
jmp 0x1b8e2
movq 0x30(%rsp), %rax
movq 0x4c8(%rax), %rax
movq 0x30(%rsp), %rcx
movl 0x4d4(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq 0x30(%rsp), %rax
movq 0x4c8(%rax), %rdi
movq 0x28(%rsp), %rsi
callq 0x1bec0
cmpl $0x0, %eax
je 0x1b755
movq 0x30(%rsp), %rax
cmpl $0x0, 0x4a0(%rax)
jne 0x1b791
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1bff0
cmpl $0x0, %eax
jne 0x1b791
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rax
movl 0x4d8(%rax), %r8d
movq 0x30(%rsp), %rax
movq 0x4c8(%rax), %r9
movl $0x190, %esi            # imm = 0x190
leaq 0x76c8(%rip), %rdx      # 0x22e46
leaq 0x76cd(%rip), %rcx      # 0x22e52
movb $0x0, %al
callq 0x1bc40
jmp 0x1b89a
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x76d3(%rip), %rsi      # 0x22e74
callq 0x5810
cmpl $0x0, %eax
je 0x1b7f3
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x7e50(%rip), %rsi      # 0x2360b
callq 0x5810
cmpl $0x0, %eax
je 0x1b7f3
movq 0x30(%rsp), %rdi
movl $0x1f9, %esi            # imm = 0x1F9
leaq 0x76a2(%rip), %rdx      # 0x22e78
leaq 0x6b13(%rip), %rcx      # 0x222f0
movb $0x0, %al
callq 0x1bc40
movq 0x30(%rsp), %rdi
callq 0x1c040
jmp 0x1b898
movq 0x28(%rsp), %rdi
leaq 0x7694(%rip), %rsi      # 0x22e93
callq 0x16ba0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x1b81f
movq $-0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x1b837
movq 0x18(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x50c0
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x4b8(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x54f0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x4a8(%rax)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x4a0(%rax)
je 0x1b87a
movq 0x30(%rsp), %rdi
callq 0x1c2c0
jmp 0x1b884
movq 0x30(%rsp), %rdi
callq 0x1c5a0
movq 0x30(%rsp), %rdi
callq 0x1c040
movq 0x30(%rsp), %rdi
callq 0x1cb00
jmp 0x1b89a
jmp 0x1b89c
movq 0x30(%rsp), %rcx
movb $0x1, %al
cmpq $0x0, (%rcx)
movb %al, 0xf(%rsp)
jne 0x1b8d6
xorl %eax, %eax
cmpl $0x0, 0x24(%rsp)
movb %al, 0xe(%rsp)
je 0x1b8ce
movq 0x30(%rsp), %rdi
callq 0x1cbf0
cmpl $0x0, %eax
setne %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x1b614
addq $0x38, %rsp
retq
nopw (%rax,%rax)