// File: /workspace/github_cpp_makefile_star2_O3/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
cmpl $0x0, (%rdi)
jne 0x1005a
leaq 0x40(%rsp), %r14
leaq 0xe0(%rbx), %r12
leaq 0x670(%rbx), %r13
leaq 0x138(%rbx), %rbp
leaq 0x640(%rbx), %rax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq 0xd0(%rbx), %rax
movl $0x0, %edi
testq %rax, %rax
je 0xfec2
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, %edx
movl 0x8(%rax), %edi
leal 0x3f(%rdi), %esi
testl %edi, %edi
cmovnsl %edi, %esi
movl %esi, %r8d
andl $-0x40, %r8d
movl %edi, %ecx
subl %r8d, %ecx
movl $0x1, %r8d
shlq %cl, %r8
sarl $0x6, %esi
movslq %esi, %rcx
orq %r8, 0x60(%rsp,%rcx,8)
cmpl %edi, %edx
cmovgl %edx, %edi
movq (%rax), %rax
testq %rax, %rax
jne 0xfe86
incl %edi
movq $0x0, 0x10(%rsp)
movq $0x30d40, 0x18(%rsp)    # imm = 0x30D40
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x10(%rsp), %r8
callq 0x5470
testl %eax, %eax
js 0x10051
movq 0xd0(%rbx), %r15
testq %r15, %r15
je 0x10051
movl 0x8(%r15), %eax
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0x60(%rsp,%rcx,8), %rcx
btq %rax, %rcx
jae 0x10049
movl $0x10, 0x40(%rsp)
movl 0x1c(%r15), %eax
movl %eax, -0x4(%r14)
movups 0xc(%r15), %xmm0
movups %xmm0, -0x14(%r14)
movl 0x8(%r15), %edi
leaq 0x44(%rsp), %rsi
movq %r14, %rdx
callq 0x5170
movl %eax, 0x28(%rsp)
cmpl $-0x1, %eax
je 0x10049
movl 0x48(%rsp), %esi
movq %rbx, %rdi
callq 0x11de7
testl %eax, %eax
je 0x10011
movq 0x34(%r15), %rax
movq %rax, 0x54(%rsp)
movq %r12, %rdi
callq 0x5740
movl 0x638(%rbx), %eax
subl 0x63c(%rbx), %eax
cmpl $0x14, %eax
jl 0xffaa
movq %r13, %rdi
movq %r12, %rsi
callq 0x57b0
movl 0x638(%rbx), %eax
subl 0x63c(%rbx), %eax
cmpl $0x13, %eax
jg 0xff8e
movslq 0x638(%rbx), %rcx
movq %rcx, %rax
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rdx
shrl $0x2, %edx
andl $-0x4, %edx
leal (%rdx,%rdx,4), %eax
subl %eax, %ecx
shll $0x6, %ecx
movups 0x20(%rsp), %xmm0
movups 0x30(%rsp), %xmm1
movups 0x40(%rsp), %xmm2
movups 0x50(%rsp), %xmm3
movups %xmm3, 0x30(%rbp,%rcx)
movups %xmm2, 0x20(%rbp,%rcx)
movups %xmm1, 0x10(%rbp,%rcx)
movups %xmm0, (%rbp,%rcx)
incl 0x638(%rbx)
movq 0x8(%rsp), %rdi
callq 0x5030
movq %r12, %rdi
callq 0x5360
jmp 0x10049
movq %rbx, 0xce28(%rip)      # 0x1ce40
movl 0x48(%rsp), %edi
callq 0x5220
leaq 0xc9c0(%rip), %rdi      # 0x1c9e8
leaq 0x5dc6(%rip), %rsi      # 0x15df5
leaq 0x5de0(%rip), %rdx      # 0x15e16
movq %rax, %rcx
xorl %eax, %eax
callq 0xfb6a
movl 0x28(%rsp), %edi
callq 0x59c0
movq (%r15), %r15
jmp 0xfef6
cmpl $0x0, (%rbx)
je 0xfe33
movq 0xd0(%rbx), %rdi
callq 0x11db8
leaq 0x640(%rbx), %r14
movq %r14, %rdi
callq 0x51c0
leaq 0xe0(%rbx), %r15
movq %r15, %rdi
callq 0x5740
cmpl $0x0, 0xd8(%rbx)
jle 0x100a8
leaq 0x108(%rbx), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x57b0
cmpl $0x0, 0xd8(%rbx)
jg 0x10094
movq %r15, %rdi
callq 0x5360
movq %r15, %rdi
callq 0x55a0
leaq 0x108(%rbx), %rdi
callq 0x59a0
leaq 0x670(%rbx), %rdi
callq 0x59a0
movq %r14, %rdi
callq 0x59a0
movl $0x2, (%rbx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq