// File: nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: worker_thread
static void worker_thread(struct mg_context *ctx) {
  struct mg_connection *conn;
  int buf_size = atoi(ctx->config[MAX_REQUEST_SIZE]);

  conn = (struct mg_connection *) calloc(1, sizeof(*conn) + buf_size);
  conn->buf_size = buf_size;
  conn->buf = (char *) (conn + 1);
  assert(conn != NULL);

  while (ctx->stop_flag == 0 && consume_socket(ctx, &conn->client)) {
    conn->birth_time = time(NULL);
    conn->ctx = ctx;

    // Fill in IP, port info early so even if SSL setup below fails,
    // error handler would have the corresponding info.
    // Thanks to Johannes Winkelmann for the patch.
    conn->request_info.remote_port = ntohs(conn->client.rsa.u.sin.sin_port);
    memcpy(&conn->request_info.remote_ip,
           &conn->client.rsa.u.sin.sin_addr.s_addr, 4);
    conn->request_info.remote_ip = ntohl(conn->request_info.remote_ip);
    conn->request_info.is_ssl = conn->client.is_ssl;

    if (!conn->client.is_ssl ||
        (conn->client.is_ssl && sslize(conn, SSL_accept))) {
      process_new_connection(conn);
    }

    close_connection(conn);
  }
  free(conn);

  // Signal master that we're done with connection and exiting
  (void) pthread_mutex_lock(&ctx->mutex);
  ctx->num_threads--;
  (void) pthread_cond_signal(&ctx->cond);
  assert(ctx->num_threads >= 0);
  (void) pthread_mutex_unlock(&ctx->mutex);

  DEBUG_TRACE(("exiting"));
}

// Assembly:
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rdi
callq 0x5950
movl %eax, 0x4(%rsp)
movslq 0x4(%rsp), %rsi
addq $0x4e0, %rsi            # imm = 0x4E0
movl $0x1, %edi
callq 0x5350
movq %rax, 0x8(%rsp)
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x4d0(%rax)
movq 0x8(%rsp), %rcx
addq $0x4e0, %rcx            # imm = 0x4E0
movq 0x8(%rsp), %rax
movq %rcx, 0x4c8(%rax)
movq 0x10(%rsp), %rax
movl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x3(%rsp)
jne 0x193d2
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
addq $0x468, %rsi            # imm = 0x468
callq 0x1b3c0
cmpl $0x0, %eax
setne %al
movb %al, 0x3(%rsp)
movb 0x3(%rsp), %al
testb $0x1, %al
jne 0x193df
jmp 0x194b2
xorl %eax, %eax
movl %eax, %edi
callq 0x54f0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x4a8(%rax)
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x458(%rax)
movq 0x8(%rsp), %rax
movzwl 0x48e(%rax), %edi
callq 0x5ad0
movzwl %ax, %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x48(%rax)
movq 0x8(%rsp), %rax
movq 0x8(%rsp), %rcx
movl 0x490(%rcx), %ecx
movl %ecx, 0x40(%rax)
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
movl %eax, %edi
callq 0x5740
movl %eax, %eax
movl %eax, %ecx
movq 0x8(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x8(%rsp), %rax
movl 0x49c(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x50(%rax)
movq 0x8(%rsp), %rax
cmpl $0x0, 0x49c(%rax)
je 0x19499
movq 0x8(%rsp), %rax
cmpl $0x0, 0x49c(%rax)
je 0x194a3
movq 0x8(%rsp), %rdi
movq 0x12459(%rip), %rsi     # 0x2b8e8
callq 0x1b530
cmpl $0x0, %eax
je 0x194a3
movq 0x8(%rsp), %rdi
callq 0x1b5d0
movq 0x8(%rsp), %rdi
callq 0x1b8f0
jmp 0x193a0
movq 0x8(%rsp), %rdi
callq 0x56c0
movq 0x10(%rsp), %rdi
addq $0xe0, %rdi
callq 0x58a0
movq 0x10(%rsp), %rax
movl 0xd8(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xd8(%rax)
movq 0x10(%rsp), %rdi
addq $0x108, %rdi            # imm = 0x108
callq 0x5030
movq 0x10(%rsp), %rdi
addq $0xe0, %rdi
callq 0x53e0
addq $0x18, %rsp
retq
nopl (%rax,%rax)