// File: nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: mg_start
struct mg_context *mg_start(mg_callback_t user_callback, void *user_data,
                            const char **options) {
  struct mg_context *ctx;
  const char *name, *value, *default_value;
  int i;

#if defined(_WIN32) && !defined(__SYMBIAN32__)
  WSADATA data;
  WSAStartup(MAKEWORD(2,2), &data);
#endif // _WIN32

  // Allocate context and initialize reasonable general case defaults.
  // TODO(lsm): do proper error handling here.
  ctx = (struct mg_context *) calloc(1, sizeof(*ctx));
  ctx->user_callback = user_callback;
  ctx->user_data = user_data;

  while (options && (name = *options++) != NULL) {
    if ((i = get_option_index(name)) == -1) {
      cry(fc(ctx), "Invalid option: %s", name);
      free_context(ctx);
      return NULL;
    } else if ((value = *options++) == NULL) {
      cry(fc(ctx), "%s: option value cannot be NULL", name);
      free_context(ctx);
      return NULL;
    }
    ctx->config[i] = mg_strdup(value);
    DEBUG_TRACE(("[%s] -> [%s]", name, value));
  }

  // Set default value if needed
  for (i = 0; config_options[i * ENTRIES_PER_CONFIG_OPTION] != NULL; i++) {
    default_value = config_options[i * ENTRIES_PER_CONFIG_OPTION + 2];
    if (ctx->config[i] == NULL && default_value != NULL) {
      ctx->config[i] = mg_strdup(default_value);
      DEBUG_TRACE(("Setting default: [%s] -> [%s]",
                   config_options[i * ENTRIES_PER_CONFIG_OPTION + 1],
                   default_value));
    }
  }

  // NOTE(lsm): order is important here. SSL certificates must
  // be initialized before listening ports. UID must be set last.
  if (!set_gpass_option(ctx) ||
#if !defined(NO_SSL)
      !set_ssl_option(ctx) ||
#endif
      !set_ports_option(ctx) ||
#if !defined(_WIN32)
      !set_uid_option(ctx) ||
#endif
      !set_acl_option(ctx)) {
    free_context(ctx);
    return NULL;
  }

#if !defined(_WIN32) && !defined(__SYMBIAN32__)
  // Ignore SIGPIPE signal, so if browser cancels the request, it
  // won't kill the whole process.
  (void) signal(SIGPIPE, SIG_IGN);
#endif // !_WIN32

  (void) pthread_mutex_init(&ctx->mutex, NULL);
  (void) pthread_cond_init(&ctx->cond, NULL);
  (void) pthread_cond_init(&ctx->sq_empty, NULL);
  (void) pthread_cond_init(&ctx->sq_full, NULL);

  // Start master (listening) thread
  start_thread(ctx, (mg_thread_func_t) master_thread, ctx);

  // Start worker threads
  for (i = 0; i < atoi(ctx->config[NUM_THREADS]); i++) {
    if (start_thread(ctx, (mg_thread_func_t) worker_thread, ctx) != 0) {
      cry(fc(ctx), "Cannot start worker thread: %d", ERRNO);
    } else {
      ctx->num_threads++;
    }
  }

  return ctx;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movl $0x1, %edi
movl $0x6a0, %esi            # imm = 0x6A0
callq 0x5350
movq %rax, 0x30(%rsp)
movq 0x48(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0xc0(%rax)
movq 0x40(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0xc8(%rax)
xorl %eax, %eax
cmpq $0x0, 0x38(%rsp)
movb %al, 0x13(%rsp)
je 0x1820b
movq 0x38(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0x38(%rsp)
movq (%rax), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
setne %al
movb %al, 0x13(%rsp)
movb 0x13(%rsp), %al
testb $0x1, %al
jne 0x18218
jmp 0x182dd
movq 0x28(%rsp), %rdi
callq 0x16ac0
movl %eax, 0x14(%rsp)
cmpl $-0x1, %eax
jne 0x18263
movq 0x30(%rsp), %rdi
callq 0x18750
movq %rax, %rdi
movq 0x28(%rsp), %rdx
leaq 0xa55e(%rip), %rsi      # 0x227a2
movb $0x0, %al
callq 0x184e0
movq 0x30(%rsp), %rdi
callq 0x18100
movq $0x0, 0x50(%rsp)
jmp 0x184d6
movq 0x38(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0x38(%rsp)
movq (%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
jne 0x182ba
movq 0x30(%rsp), %rdi
callq 0x18750
movq %rax, %rdi
movq 0x28(%rsp), %rdx
leaq 0xa51a(%rip), %rsi      # 0x227b5
movb $0x0, %al
callq 0x184e0
movq 0x30(%rsp), %rdi
callq 0x18100
movq $0x0, 0x50(%rsp)
jmp 0x184d6
jmp 0x182bc
movq 0x20(%rsp), %rdi
callq 0x18770
movq %rax, %rdx
movq 0x30(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq %rdx, 0x10(%rax,%rcx,8)
jmp 0x181d9
movl $0x0, 0x14(%rsp)
imull $0x3, 0x14(%rsp), %eax
movslq %eax, %rcx
leaq 0x133bc(%rip), %rax     # 0x2b6b0
cmpq $0x0, (%rax,%rcx,8)
je 0x1835b
imull $0x3, 0x14(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rcx
leaq 0x133a3(%rip), %rax     # 0x2b6b0
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
movslq 0x14(%rsp), %rcx
cmpq $0x0, 0x10(%rax,%rcx,8)
jne 0x1834c
cmpq $0x0, 0x18(%rsp)
je 0x1834c
movq 0x18(%rsp), %rdi
callq 0x18770
movq %rax, %rdx
movq 0x30(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq %rdx, 0x10(%rax,%rcx,8)
jmp 0x1834e
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x182e5
movq 0x30(%rsp), %rdi
callq 0x187a0
cmpl $0x0, %eax
je 0x183a6
movq 0x30(%rsp), %rdi
callq 0x187f0
cmpl $0x0, %eax
je 0x183a6
movq 0x30(%rsp), %rdi
callq 0x18bb0
cmpl $0x0, %eax
je 0x183a6
movq 0x30(%rsp), %rdi
callq 0x18ed0
cmpl $0x0, %eax
je 0x183a6
movq 0x30(%rsp), %rdi
callq 0x19020
cmpl $0x0, %eax
jne 0x183be
movq 0x30(%rsp), %rdi
callq 0x18100
movq $0x0, 0x50(%rsp)
jmp 0x184d6
movl $0xd, %edi
movl $0x1, %esi
callq 0x5ab0
movq 0x30(%rsp), %rdi
addq $0xe0, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x58d0
movq 0x30(%rsp), %rdi
addq $0x108, %rdi            # imm = 0x108
xorl %eax, %eax
movl %eax, %esi
callq 0x56d0
movq 0x30(%rsp), %rdi
addq $0x670, %rdi            # imm = 0x670
xorl %eax, %eax
movl %eax, %esi
callq 0x56d0
movq 0x30(%rsp), %rdi
addq $0x640, %rdi            # imm = 0x640
xorl %eax, %eax
movl %eax, %esi
callq 0x56d0
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rdx
leaq 0xcae(%rip), %rsi       # 0x190e0
callq 0x19050
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x30(%rsp), %rax
movq 0xb0(%rax), %rdi
callq 0x5950
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x184cc
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rdx
leaq 0xecd(%rip), %rsi       # 0x19340
callq 0x19050
cmpl $0x0, %eax
je 0x184a6
movq 0x30(%rsp), %rdi
callq 0x18750
movq %rax, (%rsp)
callq 0x5060
movq (%rsp), %rdi
movl (%rax), %edx
leaq 0xa338(%rip), %rsi      # 0x227d5
movb $0x0, %al
callq 0x184e0
jmp 0x184ba
movq 0x30(%rsp), %rax
movl 0xd8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xd8(%rax)
jmp 0x184bc
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x1843f
movq 0x30(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
addq $0x58, %rsp
retq