// File: /workspace/github_c_makefile_star2_O3/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 0xb135(%rip), %r14d     # 0xe370
testl %r14d, %r14d
js 0x3268
incq %r14
leaq 0xb2d6(%rip), %rcx      # 0xe520
xorl %eax, %eax
movl (%rcx), %edx
cmpl $-0x1, %edx
je 0x326d
cmpl %ebx, %edx
je 0x326d
incq %rax
addq $0x8038, %rcx           # imm = 0x8038
cmpq %rax, %r14
jne 0x324c
jmp 0x3270
xorl %r14d, %r14d
jmp 0x329e
movl %eax, %r14d
cmpl $0x400, %r14d           # imm = 0x400
jne 0x329e
leaq 0x65ef(%rip), %rdi      # 0x986f
movl $0x400, %esi            # imm = 0x400
xorl %eax, %eax
callq 0x759c
movl %ebx, %edi
callq 0x2190
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x3406
movl %r14d, %eax
imulq $0x8038, %rax, %r13     # imm = 0x8038
leaq 0xb271(%rip), %r15      # 0xe520
leaq (%r15,%r13), %rax
movq %rax, 0x10(%rsp)
movl %ebx, (%r13,%r15)
callq 0x72e8
addq $0x1f4, %rax            # imm = 0x1F4
movq %rax, 0x8(%r13,%r15)
movl %ebp, 0x4(%r13,%r15)
movl %ebp, %eax
leaq 0xafc5(%rip), %rcx      # 0xe2a0
movq (%rcx,%rax,8), %rax
movq %rax, 0x10(%r13,%r15)
leaq 0x20ac92d(%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 0xb066(%rip), %rcx      # 0xe390
orq %r12, (%rcx,%r13,8)
testq %rax, %rax
je 0x3345
leaq 0xb0d6(%rip), %rax      # 0xe410
orq %r12, (%rax,%r13,8)
movq 0x10(%rsp), %r15
jmp 0x3368
movq %r12, %rax
notq %rax
leaq 0xb0be(%rip), %rcx      # 0xe410
andq %rax, (%rcx,%r13,8)
movl %ebx, %edi
xorl %esi, %esi
callq 0x20f0
movq 0x10(%rsp), %r15
movl 0x4(%r15), %ebp
cmpl $0x12, %ebp
ja 0x3418
movl $0x7107a, %eax          # imm = 0x7107A
btl %ebp, %eax
jae 0x3418
leaq 0xb10a(%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 0xafba(%rip), %r8d      # 0xe370
cmpl %r14d, %r8d
cmovlel %r14d, %r8d
movl %r8d, 0xafac(%rip)      # 0xe370
movl 0xafaa(%rip), %eax      # 0xe374
cmpl %ebx, %eax
cmovlel %ebx, %eax
movl %eax, 0xaf9f(%rip)      # 0xe374
movl 0x4(%r15), %ecx
leaq 0xae20(%rip), %rdx      # 0xe200
movq (%rdx,%rcx,8), %rsi
movl 0xaf82(%rip), %ecx      # 0xe36c
movl %eax, (%rsp)
leaq 0x64a8(%rip), %rdi      # 0x989c
movl %r14d, %edx
movl %ebx, %r9d
xorl %eax, %eax
callq 0x7509
callq 0x3f87
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
notq %r12
leaq 0xb06e(%rip), %rax      # 0xe490
andq %r12, (%rax,%r13,8)
movl %ebx, %edi
movl $0x1, %esi
callq 0x20f0
jmp 0x338a