// File: /workspace/github_c_makefile_star2_O0/canboat[P]canboat/n2kd/main.c
// Function: writeAllClients
static void writeAllClients(void)
{
  fd_set         ws;
  fd_set         es;
  struct timeval timeout = {0, 0};
  int            r;
  int            i;
  SOCKET         fd;
  int64_t        now      = 0;
  char          *aisState = 0;
  char          *state    = 0;

  logDebug("writeAllClients tcp=%d out=%d nmea=%d\n", tcpMessage.len, outMessage.len, nmeaMessage.len);
  FD_ZERO(&ws);
  FD_ZERO(&es);

  if (socketIdxMax >= 0)
  {
    ws  = writeSet;
    es  = writeSet;
    r   = select(socketFdMax + 1, 0, &ws, &es, &timeout);
    now = getNow();
    logDebug("write to %d streams (%u..%u fdMax=%d)\n", r, socketIdxMin, socketIdxMax, socketFdMax);

    for (i = socketIdxMin; r > 0 && i <= socketIdxMax; i++)
    {
      fd = stream[i].fd;
      if (fd < 0)
      {
        continue;
      }
      if (fd > 8192)
      {
        logAbort("Stream %d contains invalid fd %d\n", i, fd);
      }
      if (fd > socketFdMax)
      {
        logAbort("Inconsistent: fd[%u]=%d, fdMax=%d\n", i, fd, socketFdMax);
      }
      if (FD_ISSET(fd, &es) && fd != stdoutfd)
      {
        logDebug("%s i=%u fd=%d write error, closing\n", streamTypeName[stream[i].type], i, fd);
        closeStream(i);
      }
      else if (FD_ISSET(fd, &ws))
      {
        logDebug("%s i=%u fd=%d writable=%d timeout=%" PRId64 "\n",
                 streamTypeName[stream[i].type],
                 i,
                 fd,
                 FD_ISSET(fd, &ws),
                 stream[i].timeout);
        r--;

        if (stream[i].writeBuffer.len > 0)
        {
          safeWriteBuffer(i, &stream[i].writeBuffer);
          continue;
        }

        switch (stream[i].type)
        {
          case CLIENT_AIS:
            if (stream[i].timeout && stream[i].timeout < now)
            {
              if (!aisState)
              {
                aisState = getFullStateJSON(CLIENT_AIS, now);
              }
              writeAndClose(i, aisState, strlen(aisState));
            }
            break;
          case CLIENT_JSON:
            if (stream[i].timeout && stream[i].timeout < now)
            {
              if (!state)
              {
                state = getFullStateJSON(CLIENT_JSON, now);
                logDebug("json=%s", state);
              }
              writeAndClose(i, state, strlen(state));
            }
            break;
          case CLIENT_STATUS_STREAM: {
            char *statusState = getFullStateJSON(CLIENT_STATUS_STREAM, now);
            writeAndClose(i, statusState, strlen(statusState));
            free(statusState);
          }
          break;
          case CLIENT_NMEA0183_STREAM:
          case DATA_OUTPUT_NMEA0183_STREAM:
            logDebug("NMEA-> %d\n", nmeaMessage.len);
            if (nmeaMessage.len)
            {
              safeWriteBuffer(i, &nmeaMessage);
            }
            break;
          case SERVER_NMEA0183_DATAGRAM:
            logDebug("udp NMEA-> %d\n", nmeaMessage.len);
            if (nmeaMessage.len)
            {
              sendto(fd, nmeaMessage.data, nmeaMessage.len, 0, (struct sockaddr *) &udpWildcardAddress, sizeof(udpWildcardAddress));
            }
            break;
          case CLIENT_JSON_STREAM:
            if (tcpMessage.len)
            {
              safeWriteBuffer(i, &tcpMessage);
            }
            break;
          case DATA_OUTPUT_STREAM:
          case DATA_OUTPUT_COPY:
            if (outMessage.len)
            {
              safeWriteBuffer(i, &outMessage);
            }
            break;
          default:
            break;
        }
      }
    }
  }

  if (aisState)
  {
    free(aisState);
  }

  if (state)
  {
    free(state);
  }

  sbEmpty(&tcpMessage);
  sbEmpty(&nmeaMessage);
  sbEmpty(&outMessage);

#ifdef NEVER
  {
    static int count = 0;
    if (count++ > 100)
    {
      exit(1);
    }
  }
#endif
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2190
movq $0x0, -0x128(%rbp)
movq $0x0, -0x130(%rbp)
movq $0x0, -0x138(%rbp)
movq 0x201a55a(%rip), %rsi   # 0x201f5b0
movq 0x201a56b(%rip), %rdx   # 0x201f5c8
movq 0x201a57c(%rip), %rcx   # 0x201f5e0
leaq 0x7e52(%rip), %rdi      # 0xcebd
movb $0x0, %al
callq 0x9ae0
leaq -0x80(%rbp), %rax
movq %rax, -0x148(%rbp)
movl $0x0, -0x13c(%rbp)
movl -0x13c(%rbp), %eax
cmpq $0x10, %rax
jae 0x50b9
movq -0x148(%rbp), %rax
movl -0x13c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x13c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x13c(%rbp)
jmp 0x5087
jmp 0x50bb
jmp 0x50bd
leaq -0x100(%rbp), %rax
movq %rax, -0x158(%rbp)
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
cmpq $0x10, %rax
jae 0x5107
movq -0x158(%rbp), %rax
movl -0x14c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x50d5
jmp 0x5109
cmpl $0x0, 0xc2e0(%rip)      # 0x113f0
jl 0x5760
leaq -0x80(%rbp), %rdi
leaq 0xc3ef(%rip), %rsi      # 0x11510
movl $0x80, %edx
callq 0x2220
leaq -0x100(%rbp), %rdi
leaq 0xc3d7(%rip), %rsi      # 0x11510
movl $0x80, %edx
callq 0x2220
movl 0xc2ab(%rip), %edi      # 0x113f4
addl $0x1, %edi
xorl %eax, %eax
movl %eax, %esi
leaq -0x80(%rbp), %rdx
leaq -0x100(%rbp), %rcx
leaq -0x110(%rbp), %r8
callq 0x2230
movl %eax, -0x114(%rbp)
callq 0x9820
movq %rax, -0x128(%rbp)
movl -0x114(%rbp), %esi
movl 0xc267(%rip), %edx      # 0x113ec
movl 0xc265(%rip), %ecx      # 0x113f0
movl 0xc262(%rip), %r8d      # 0x113f4
leaq 0x7d4b(%rip), %rdi      # 0xcee4
movb $0x0, %al
callq 0x9ae0
movl 0xc246(%rip), %eax      # 0x113ec
movl %eax, -0x118(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x114(%rbp)
movb %al, -0x161(%rbp)
jle 0x51d2
movl -0x118(%rbp), %eax
cmpl 0xc227(%rip), %eax      # 0x113f0
setle %al
movb %al, -0x161(%rbp)
movb -0x161(%rbp), %al
testb $0x1, %al
jne 0x51e1
jmp 0x575e
movslq -0x118(%rbp), %rcx
leaq 0xc3b1(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x11c(%rbp)
cmpl $0x0, -0x11c(%rbp)
jge 0x520f
jmp 0x574a
cmpl $0x2000, -0x11c(%rbp)   # imm = 0x2000
jle 0x5235
movl -0x118(%rbp), %esi
movl -0x11c(%rbp), %edx
leaq 0x7cdd(%rip), %rdi      # 0xcf0b
movb $0x0, %al
callq 0x9c40
movl -0x11c(%rbp), %eax
cmpl 0xc1b3(%rip), %eax      # 0x113f4
jle 0x5263
movl -0x118(%rbp), %esi
movl -0x11c(%rbp), %edx
movl 0xc19f(%rip), %ecx      # 0x113f4
leaq 0x7cd1(%rip), %rdi      # 0xcf2d
movb $0x0, %al
callq 0x9c40
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x100(%rbp,%rax,8), %rax
movq %rax, -0x170(%rbp)
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x170(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x5308
cmpl $0x1, -0x11c(%rbp)
je 0x5308
movslq -0x118(%rbp), %rcx
leaq 0xc2dc(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
leaq 0xbf36(%rip), %rax      # 0x11210
movq (%rax,%rcx,8), %rsi
movl -0x118(%rbp), %edx
movl -0x11c(%rbp), %ecx
leaq 0x7c5f(%rip), %rdi      # 0xcf50
movb $0x0, %al
callq 0x9ae0
movl -0x118(%rbp), %edi
callq 0x3a30
jmp 0x5748
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x80(%rbp,%rax,8), %rax
movq %rax, -0x178(%rbp)
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x178(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x5746
movslq -0x118(%rbp), %rcx
leaq 0xc23f(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
leaq 0xbe99(%rip), %rax      # 0x11210
movq (%rax,%rcx,8), %rsi
movl -0x118(%rbp), %eax
movl %eax, -0x180(%rbp)
movl -0x11c(%rbp), %eax
movl %eax, -0x17c(%rbp)
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x80(%rbp,%rax,8), %rax
movq %rax, -0x188(%rbp)
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x188(%rbp), %rax
movl %edx, %ecx
movl -0x180(%rbp), %edx
movl %ecx, %ecx
movl $0x1, %edi
shlq %cl, %rdi
movl -0x17c(%rbp), %ecx
andq %rdi, %rax
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movzbl %al, %r8d
movslq -0x118(%rbp), %rdi
leaq 0xc1a6(%rip), %rax      # 0x115a0
imulq $0x8038, %rdi, %rdi     # imm = 0x8038
addq %rdi, %rax
movq 0x8(%rax), %r9
leaq 0x7b65(%rip), %rdi      # 0xcf74
movb $0x0, %al
callq 0x9ae0
movl -0x114(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x114(%rbp)
movslq -0x118(%rbp), %rcx
leaq 0xc16d(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
cmpq $0x0, 0x8020(%rax)
jbe 0x5476
movl -0x118(%rbp), %edi
movslq -0x118(%rbp), %rax
leaq 0xc145(%rip), %rsi      # 0x115a0
imulq $0x8038, %rax, %rax     # imm = 0x8038
addq %rax, %rsi
addq $0x8018, %rsi           # imm = 0x8018
callq 0x49e0
jmp 0x574a
movslq -0x118(%rbp), %rax
imulq $0x8038, %rax, %rax     # imm = 0x8038
leaq 0xc115(%rip), %rcx      # 0x115a0
movl 0x4(%rax,%rcx), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x190(%rbp)
subl $0x11, %eax
ja 0x5742
movq -0x190(%rbp), %rax
leaq 0x6b53(%rip), %rcx      # 0xc004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movslq -0x118(%rbp), %rcx
leaq 0xc0d8(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x555f
movslq -0x118(%rbp), %rcx
leaq 0xc0b5(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movq 0x8(%rax), %rax
cmpq -0x128(%rbp), %rax
jae 0x555f
cmpq $0x0, -0x130(%rbp)
jne 0x5524
movq -0x128(%rbp), %rsi
movl $0x1, %edi
callq 0x5880
movq %rax, -0x130(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x19c(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x130(%rbp), %rdi
callq 0x20f0
movl -0x19c(%rbp), %edi
movq -0x198(%rbp), %rsi
movq %rax, %rdx
callq 0x5c60
jmp 0x5744
movslq -0x118(%rbp), %rcx
leaq 0xc02e(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x561e
movslq -0x118(%rbp), %rcx
leaq 0xc00b(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movq 0x8(%rax), %rax
cmpq -0x128(%rbp), %rax
jae 0x561e
cmpq $0x0, -0x138(%rbp)
jne 0x55e3
movq -0x128(%rbp), %rsi
movl $0x3, %edi
callq 0x5880
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rsi
leaq 0x79bf(%rip), %rdi      # 0xcf9b
movb $0x0, %al
callq 0x9ae0
movl -0x118(%rbp), %eax
movl %eax, -0x1ac(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x138(%rbp), %rdi
callq 0x20f0
movl -0x1ac(%rbp), %edi
movq -0x1a8(%rbp), %rsi
movq %rax, %rdx
callq 0x5c60
jmp 0x5744
movq -0x128(%rbp), %rsi
movl $0x6, %edi
callq 0x5880
movq %rax, -0x160(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x1bc(%rbp)
movq -0x160(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x160(%rbp), %rdi
callq 0x20f0
movl -0x1bc(%rbp), %edi
movq -0x1b8(%rbp), %rsi
movq %rax, %rdx
callq 0x5c60
movq -0x160(%rbp), %rdi
callq 0x2030
jmp 0x5744
movq 0x2019f52(%rip), %rsi   # 0x201f5e0
leaq 0x7912(%rip), %rdi      # 0xcfa7
movb $0x0, %al
callq 0x9ae0
cmpq $0x0, 0x2019f3c(%rip)   # 0x201f5e0
je 0x56b8
movl -0x118(%rbp), %edi
leaq 0x2019f25(%rip), %rsi   # 0x201f5d8
callq 0x49e0
jmp 0x5744
movq 0x2019f1c(%rip), %rsi   # 0x201f5e0
leaq 0x78d8(%rip), %rdi      # 0xcfa3
movb $0x0, %al
callq 0x9ae0
cmpq $0x0, 0x2019f06(%rip)   # 0x201f5e0
je 0x5704
movl -0x11c(%rbp), %edi
movq 0x2019eef(%rip), %rsi   # 0x201f5d8
movq 0x2019ef0(%rip), %rdx   # 0x201f5e0
xorl %ecx, %ecx
leaq 0xbd03(%rip), %r8       # 0x113fc
movl $0x10, %r9d
callq 0x21a0
jmp 0x5744
cmpq $0x0, 0x2019ea2(%rip)   # 0x201f5b0
je 0x5722
movl -0x118(%rbp), %edi
leaq 0x2019e8b(%rip), %rsi   # 0x201f5a8
callq 0x49e0
jmp 0x5744
cmpq $0x0, 0x2019e9c(%rip)   # 0x201f5c8
je 0x5740
movl -0x118(%rbp), %edi
leaq 0x2019e85(%rip), %rsi   # 0x201f5c0
callq 0x49e0
jmp 0x5744
jmp 0x5744
jmp 0x5746
jmp 0x5748
jmp 0x574a
movl -0x118(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x118(%rbp)
jmp 0x51ac
jmp 0x5760
cmpq $0x0, -0x130(%rbp)
je 0x5776
movq -0x130(%rbp), %rdi
callq 0x2030
cmpq $0x0, -0x138(%rbp)
je 0x578c
movq -0x138(%rbp), %rdi
callq 0x2030
xorl %eax, %eax
cmpq 0x2019e1b(%rip), %rax   # 0x201f5b0
jae 0x57a2
xorl %eax, %eax
movq %rax, -0x1c8(%rbp)
jmp 0x57b0
movq 0x2019e07(%rip), %rax   # 0x201f5b0
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rax
movq %rax, 0x2019df2(%rip)   # 0x201f5b0
cmpq $0x0, 0x2019de2(%rip)   # 0x201f5a8
je 0x57da
movq 0x2019dd9(%rip), %rax   # 0x201f5a8
movq 0x2019dda(%rip), %rcx   # 0x201f5b0
movb $0x0, (%rax,%rcx)
xorl %eax, %eax
cmpq 0x2019dfd(%rip), %rax   # 0x201f5e0
jae 0x57f0
xorl %eax, %eax
movq %rax, -0x1d0(%rbp)
jmp 0x57fe
movq 0x2019de9(%rip), %rax   # 0x201f5e0
movq %rax, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rax
movq %rax, 0x2019dd4(%rip)   # 0x201f5e0
cmpq $0x0, 0x2019dc4(%rip)   # 0x201f5d8
je 0x5828
movq 0x2019dbb(%rip), %rax   # 0x201f5d8
movq 0x2019dbc(%rip), %rcx   # 0x201f5e0
movb $0x0, (%rax,%rcx)
xorl %eax, %eax
cmpq 0x2019d97(%rip), %rax   # 0x201f5c8
jae 0x583e
xorl %eax, %eax
movq %rax, -0x1d8(%rbp)
jmp 0x584c
movq 0x2019d83(%rip), %rax   # 0x201f5c8
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rax
movq %rax, 0x2019d6e(%rip)   # 0x201f5c8
cmpq $0x0, 0x2019d5e(%rip)   # 0x201f5c0
je 0x5876
movq 0x2019d55(%rip), %rax   # 0x201f5c0
movq 0x2019d56(%rip), %rcx   # 0x201f5c8
movb $0x0, (%rax,%rcx)
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nop