// File: /workspace/github_cpp_makefile_star2_O2/nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: master_thread
static void master_thread(struct mg_context *ctx) {
  fd_set read_set;
  struct timeval tv;
  struct socket *sp;
  int max_fd;

  while (ctx->stop_flag == 0) {
    FD_ZERO(&read_set);
    max_fd = -1;

    // Add listening sockets to the read set
    for (sp = ctx->listening_sockets; sp != NULL; sp = sp->next) {
      add_to_set(sp->sock, &read_set, &max_fd);
    }

    tv.tv_sec = 0;
    tv.tv_usec = 200 * 1000;

    if (select(max_fd + 1, &read_set, NULL, NULL, &tv) < 0) {
#ifdef _WIN32
      // On windows, if read_set and write_set are empty,
      // select() returns "Invalid parameter" error
      // (at least on my Windows XP Pro). So in this case, we sleep here.
      sleep(1);
#endif // _WIN32
    } else {
      for (sp = ctx->listening_sockets; sp != NULL; sp = sp->next) {
        if (FD_ISSET(sp->sock, &read_set)) {
          accept_new_connection(sp, ctx);
        }
      }
    }
  }
  DEBUG_TRACE(("stopping workers"));

  // Stop signal received: somebody called mg_stop. Quit.
  close_all_listening_sockets(ctx);

  // Wakeup workers that are waiting for connections to handle.
  pthread_cond_broadcast(&ctx->sq_full);

  // Wait until all threads finish
  (void) pthread_mutex_lock(&ctx->mutex);
  while (ctx->num_threads > 0) {
    (void) pthread_cond_wait(&ctx->cond, &ctx->mutex);
  }
  (void) pthread_mutex_unlock(&ctx->mutex);

  // All threads exited, no sync is needed. Destroy mutex and condvars
  (void) pthread_mutex_destroy(&ctx->mutex);
  (void) pthread_cond_destroy(&ctx->cond);
  (void) pthread_cond_destroy(&ctx->sq_empty);
  (void) pthread_cond_destroy(&ctx->sq_full);

  // Signal mg_stop() that we're done
  ctx->stop_flag = 2;

  DEBUG_TRACE(("exiting"));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdi, %rbx
leaq 0xd0(%rdi), %rax
movq %rax, 0x8(%rsp)
leaq 0x48(%rsp), %r13
leaq 0xe0(%rdi), %r12
leaq 0x670(%rdi), %r15
leaq 0x138(%rdi), %rax
movq %rax, 0x10(%rsp)
leaq 0x640(%rdi), %rax
movq %rax, (%rsp)
pushq $0x40
popq %rbp
cmpl $0x0, (%rbx)
jne 0xd196
xorl %eax, %eax
pushq $0x1
popq %r9
cmpq $0x10, %rax
je 0xd007
andq $0x0, 0x68(%rsp,%rax,8)
incq %rax
jmp 0xcff6
pushq $-0x1
popq %rdi
movq 0x8(%rsp), %r8
movq (%r8), %r8
testq %r8, %r8
je 0xd03c
movl 0x8(%r8), %esi
movl %esi, %eax
cltd
idivl %ebp
movslq %eax, %rdx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
movq %r9, %rax
shlq %cl, %rax
orq %rax, 0x68(%rsp,%rdx,8)
cmpl %esi, %edi
cmovlel %esi, %edi
jmp 0xd00f
andq $0x0, 0x18(%rsp)
movq $0x30d40, 0x20(%rsp)    # imm = 0x30D40
incl %edi
leaq 0x68(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x18(%rsp), %r8
callq 0x5460
testl %eax, %eax
js 0xcfe7
movq 0x8(%rsp), %r14
movq (%r14), %r14
testq %r14, %r14
je 0xcfe7
movl 0x8(%r14), %eax
cltd
idivl %ebp
cltq
movq 0x68(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0xd069
movl $0x10, 0x48(%rsp)
movl 0x1c(%r14), %eax
movl %eax, -0x4(%r13)
movups 0xc(%r14), %xmm0
movups %xmm0, -0x14(%r13)
movl 0x8(%r14), %edi
leaq 0x4c(%rsp), %rsi
movq %r13, %rdx
callq 0x5150
movl %eax, 0x30(%rsp)
cmpl $-0x1, %eax
je 0xd069
movq %rbx, %rdi
movq %r13, %rsi
callq 0xddba
testl %eax, %eax
je 0xd159
movq 0x34(%r14), %rax
movq %rax, 0x5c(%rsp)
movq %r12, %rdi
callq 0x5750
movl 0x638(%rbx), %eax
subl 0x63c(%rbx), %eax
cmpl $0x14, %eax
jl 0xd0ff
movq %r15, %rdi
movq %r12, %rsi
callq 0x57c0
jmp 0xd0e1
movslq 0x638(%rbx), %rax
xorl %edx, %edx
pushq $0x14
popq %rcx
divq %rcx
shll $0x6, %edx
movups 0x28(%rsp), %xmm0
movups 0x38(%rsp), %xmm1
movups 0x48(%rsp), %xmm2
movups 0x58(%rsp), %xmm3
movq 0x10(%rsp), %rax
movups %xmm3, 0x30(%rax,%rdx)
movups %xmm2, 0x20(%rax,%rdx)
movups %xmm1, 0x10(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
incl 0x638(%rbx)
movq (%rsp), %rdi
callq 0x5030
movq %r12, %rdi
callq 0x5350
jmp 0xd069
movq %rbx, 0xdcf0(%rip)      # 0x1ae50
movl 0x50(%rsp), %edi
callq 0x51f0
leaq 0xd888(%rip), %rdi      # 0x1a9f8
leaq 0x5c7e(%rip), %rsi      # 0x12df5
leaq 0x5c98(%rip), %rdx      # 0x12e16
movq %rax, %rcx
xorl %eax, %eax
callq 0xcd67
movl 0x30(%rsp), %edi
callq 0x59e0
jmp 0xd069
movq %rbx, %rdi
callq 0xdd8a
movq (%rsp), %rdi
callq 0x5190
movq %r12, %rdi
callq 0x5750
leaq 0x108(%rbx), %r13
cmpl $0x0, 0xd8(%rbx)
jle 0xd1cc
movq %r13, %rdi
movq %r12, %rsi
callq 0x57c0
jmp 0xd1b6
movq %r12, %rdi
callq 0x5350
movq %r12, %rdi
callq 0x5580
movq %r13, %rdi
callq 0x59b0
movq %r15, %rdi
callq 0x59b0
movq (%rsp), %rdi
callq 0x59b0
movl $0x2, (%rbx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq