// File: /workspace/github_c_makefile_star2_O1/canboat[P]canboat/n2kd/main.c
// Function: setFdUsed
static int setFdUsed(SOCKET fd, StreamType ct)
{
  int i;

  /* Find a free entry in socketFd(i) */
  for (i = 0; i <= socketIdxMax; i++)
  {
    if (stream[i].fd == INVALID_SOCKET || stream[i].fd == fd)
    {
      break;
    }
  }

  if (i == FD_SETSIZE)
  {
    logError("Already %d active streams, ignoring new one\n", FD_SETSIZE);
    close(fd);
    return -1;
  }

  stream[i].fd          = fd;
  stream[i].timeout     = getNow() + UPDATE_INTERVAL;
  stream[i].type        = ct;
  stream[i].readHandler = readHandlers[ct];
  stream[i].writeBuffer = sbNew;

  FD_SET(fd, &activeSet);
  if (stream[i].readHandler)
  {
    FD_SET(fd, &readSet);
  }
  else
  {
    FD_CLR(fd, &readSet);
    /* ignore */ shutdown(fd, SHUT_RD);
  }

  switch (stream[i].type)
  {
    case CLIENT_AIS:
    case CLIENT_JSON:
    case CLIENT_JSON_STREAM:
    case CLIENT_NMEA0183_STREAM:
    case CLIENT_STATUS_STREAM:
    case DATA_OUTPUT_STREAM:
    case DATA_OUTPUT_COPY:
    case DATA_OUTPUT_NMEA0183_STREAM:
    case SERVER_NMEA0183_DATAGRAM:
      FD_SET(fd, &writeSet);
      break;
    default:
      FD_CLR(fd, &writeSet);
      /* ignore */ shutdown(fd, SHUT_WR);
  }

#ifdef O_NONBLOCK
  {
    int flags = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  }
#else
  {
    int ioctlOptionValue = 1;

    ioctl(fd, FIONBIO, &ioctlOptionValue);
  }
#endif

  socketIdxMax = CB_MAX(socketIdxMax, i);
  socketFdMax  = CB_MAX(socketFdMax, fd);
  logDebug("New %s %u (%u..%u fd=%d fdMax=%d)\n", streamTypeName[stream[i].type], i, socketIdxMin, socketIdxMax, fd, socketFdMax);

  setHaveNMEA0183Client();
  return i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebp
movl %edi, %ebx
movl 0xb146(%rip), %r14d     # 0xe370
testl %r14d, %r14d
js 0x3257
incq %r14
leaq 0xb2e7(%rip), %rcx      # 0xe520
xorl %eax, %eax
movl (%rcx), %edx
cmpl $-0x1, %edx
je 0x325c
cmpl %ebx, %edx
je 0x325c
incq %rax
addq $0x8038, %rcx           # imm = 0x8038
cmpq %rax, %r14
jne 0x323b
jmp 0x325f
xorl %r14d, %r14d
jmp 0x325f
movl %eax, %r14d
cmpl $0x400, %r14d           # imm = 0x400
jne 0x328d
leaq 0x65f0(%rip), %rdi      # 0x985f
movl $0x400, %esi            # imm = 0x400
xorl %eax, %eax
callq 0x75dc
movl %ebx, %edi
callq 0x2190
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x33f0
movl %r14d, %eax
imulq $0x8038, %rax, %r13     # imm = 0x8038
leaq 0xb282(%rip), %r15      # 0xe520
leaq (%r15,%r13), %rax
movq %rax, 0x10(%rsp)
movl %ebx, (%r13,%r15)
callq 0x7328
addq $0x1f4, %rax            # imm = 0x1F4
movq %rax, 0x8(%r13,%r15)
movl %ebp, 0x4(%r13,%r15)
movl %ebp, %eax
leaq 0xafd6(%rip), %rcx      # 0xe2a0
movq (%rcx,%rax,8), %rax
movq %rax, 0x10(%r13,%r15)
leaq 0x20ac93e(%rip), %rcx   # 0x20afc18
movq 0x10(%rcx), %rdx
movq %rdx, 0x8028(%r13,%r15)
movups (%rcx), %xmm0
leal 0x3f(%rbx), %edx
testl %ebx, %ebx
cmovnsl %ebx, %edx
movl %edx, %esi
andl $-0x40, %esi
movl %ebx, %ecx
subl %esi, %ecx
movl $0x1, %r12d
shlq %cl, %r12
movups %xmm0, 0x8018(%r13,%r15)
sarl $0x6, %edx
movslq %edx, %r13
leaq 0xb077(%rip), %rcx      # 0xe390
orq %r12, (%rcx,%r13,8)
testq %rax, %rax
je 0x332f
leaq 0xb0e7(%rip), %rax      # 0xe410
orq %r12, (%rax,%r13,8)
jmp 0x3349
movq %r12, %rax
notq %rax
leaq 0xb0d4(%rip), %rcx      # 0xe410
andq %rax, (%rcx,%r13,8)
movl %ebx, %edi
xorl %esi, %esi
callq 0x20f0
movq 0x10(%rsp), %r15
movl 0x4(%r15), %eax
cmpl $0x12, %eax
ja 0x3402
movl $0x7107a, %ecx          # imm = 0x7107A
btl %eax, %ecx
jae 0x3402
leaq 0xb120(%rip), %rax      # 0xe490
orq %r12, (%rax,%r13,8)
movl %ebx, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20d0
orl $0x800, %eax            # imm = 0x800
movl %ebx, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x20d0
movl 0xafd0(%rip), %r8d      # 0xe370
cmpl %r14d, %r8d
cmovlel %r14d, %r8d
movl %r8d, 0xafc2(%rip)      # 0xe370
movl 0xafc0(%rip), %eax      # 0xe374
cmpl %ebx, %eax
cmovlel %ebx, %eax
movl %eax, 0xafb5(%rip)      # 0xe374
movl 0x4(%r15), %ecx
leaq 0xae36(%rip), %rdx      # 0xe200
movq (%rdx,%rcx,8), %rsi
movl 0xaf98(%rip), %ecx      # 0xe36c
movl %eax, (%rsp)
leaq 0x64ae(%rip), %rdi      # 0x988c
movl %r14d, %edx
movl %ebx, %r9d
xorl %eax, %eax
callq 0x7549
callq 0x4004
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
notq %r12
leaq 0xb084(%rip), %rax      # 0xe490
andq %r12, (%rax,%r13,8)
movl %ebx, %edi
movl $0x1, %esi
callq 0x20f0
jmp 0x3374