// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movl %edi, %ebx
movslq 0xa1b0(%rip), %rax      # 0xd380
leaq 0xa359(%rip), %r15      # 0xd530
xorl %r14d, %r14d
cmpq %rax, %r14
jg 0x31f7
movl (%r15), %ecx
cmpl $-0x1, %ecx
je 0x31f7
cmpl %ebx, %ecx
je 0x31f7
incq %r14
addq $0x8038, %r15           # imm = 0x8038
jmp 0x31da
cmpq $0x400, %r14            # imm = 0x400
jne 0x3223
leaq 0x5668(%rip), %rdi      # 0x886f
movl $0x400, %esi            # imm = 0x400
xorl %eax, %eax
callq 0x6e09
movl %ebx, %edi
callq 0x2190
pushq $-0x1
popq %r14
jmp 0x3352
movl %ebx, (%r15)
movl %esi, %ebp
callq 0x6b84
movl %ebp, %edi
addq $0x1f4, %rax            # imm = 0x1F4
movq %rax, 0x8(%r15)
movl %ebp, 0x4(%r15)
movl %ebp, %eax
leaq 0xa06a(%rip), %rcx      # 0xd2b0
movq (%rcx,%rax,8), %rsi
movq %rsi, 0x10(%r15)
leaq 0x20ab9d3(%rip), %rax   # 0x20aec28
movq 0x10(%rax), %rcx
movq %rcx, 0x8028(%r15)
movups (%rax), %xmm0
movups %xmm0, 0x8018(%r15)
pushq $0x40
popq %rcx
movl %ebx, %eax
cltd
idivl %ecx
movslq %eax, %r12
shll $0x6, %eax
movl %ebx, %ecx
subl %eax, %ecx
pushq $0x1
popq %r13
shlq %cl, %r13
leaq 0xa115(%rip), %rax      # 0xd3a0
orq %r13, (%rax,%r12,8)
movq %r13, %rbp
notq %rbp
leaq 0xa184(%rip), %rax      # 0xd420
testq %rsi, %rsi
je 0x32a7
orq %r13, (%rax,%r12,8)
jmp 0x32b8
andq %rbp, (%rax,%r12,8)
movl %ebx, %edi
xorl %esi, %esi
callq 0x20f0
movl 0x4(%r15), %edi
cmpl $0x12, %edi
ja 0x3364
movl $0x7107a, %eax          # imm = 0x7107A
btl %edi, %eax
jae 0x3364
leaq 0xa1ca(%rip), %rax      # 0xd4a0
orq %r13, (%rax,%r12,8)
pushq $0x3
popq %rsi
movl %ebx, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20d0
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %ebx, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x20d0
movl 0xa07e(%rip), %r8d      # 0xd380
cmpl %r14d, %r8d
cmovlel %r14d, %r8d
movl %r8d, 0xa070(%rip)      # 0xd380
movl 0xa06e(%rip), %eax      # 0xd384
cmpl %ebx, %eax
cmovlel %ebx, %eax
movl %eax, 0xa063(%rip)      # 0xd384
movl 0x4(%r15), %ecx
leaq 0x9ee4(%rip), %rdx      # 0xd210
movq (%rdx,%rcx,8), %rsi
movl 0xa046(%rip), %ecx      # 0xd37c
movl %eax, (%rsp)
leaq 0x555c(%rip), %rdi      # 0x889c
movl %r14d, %edx
movl %ebx, %r9d
xorl %eax, %eax
callq 0x6d78
callq 0x3e0c
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa135(%rip), %rax      # 0xd4a0
andq %rbp, (%rax,%r12,8)
pushq $0x1
popq %rsi
movl %ebx, %edi
callq 0x20f0
jmp 0x32da