// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl 0xe782(%rip), %eax      # 0x113f0
jg 0x2cb5
movslq -0x10(%rbp), %rcx
leaq 0xe925(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
cmpl $-0x1, (%rax)
je 0x2ca6
movslq -0x10(%rbp), %rcx
leaq 0xe90b(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jne 0x2ca8
jmp 0x2cb5
jmp 0x2caa
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x2c65
cmpl $0x400, -0x10(%rbp)     # imm = 0x400
jne 0x2ce5
leaq 0x9b5a(%rip), %rdi      # 0xc81f
movl $0x400, %esi            # imm = 0x400
movb $0x0, %al
callq 0x9b90
movl -0x8(%rbp), %edi
callq 0x21b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x304a
movl -0x8(%rbp), %ecx
movslq -0x10(%rbp), %rdx
leaq 0xe8ad(%rip), %rax      # 0x115a0
imulq $0x8038, %rdx, %rdx     # imm = 0x8038
addq %rdx, %rax
movl %ecx, (%rax)
callq 0x9820
movq %rax, %rcx
addq $0x1f4, %rcx            # imm = 0x1F4
movslq -0x10(%rbp), %rdx
leaq 0xe887(%rip), %rax      # 0x115a0
imulq $0x8038, %rdx, %rdx     # imm = 0x8038
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0xc(%rbp), %ecx
movslq -0x10(%rbp), %rdx
leaq 0xe86b(%rip), %rax      # 0x115a0
imulq $0x8038, %rdx, %rdx     # imm = 0x8038
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xe562(%rip), %rax      # 0x112b0
movq (%rax,%rcx,8), %rcx
movslq -0x10(%rbp), %rdx
leaq 0xe843(%rip), %rax      # 0x115a0
imulq $0x8038, %rdx, %rdx     # imm = 0x8038
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movslq -0x10(%rbp), %rcx
leaq 0xe82a(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
leaq 0x20aff11(%rip), %rcx   # 0x20b2c98
movq (%rcx), %rdx
movq %rdx, 0x8018(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8020(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x8028(%rax)
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x20(%rbp)
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x20(%rbp), %rdx
movslq %eax, %rcx
leaq 0xe635(%rip), %rax      # 0x11410
orq (%rax,%rcx,8), %rdx
leaq 0xe62a(%rip), %rax      # 0x11410
movq %rdx, (%rax,%rcx,8)
movslq -0x10(%rbp), %rcx
leaq 0xe7ab(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
je 0x2e4b
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x28(%rbp)
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x28(%rbp), %rdx
movslq %eax, %rcx
leaq 0xe656(%rip), %rax      # 0x11490
orq (%rax,%rcx,8), %rdx
leaq 0xe64b(%rip), %rax      # 0x11490
movq %rdx, (%rax,%rcx,8)
jmp 0x2e9c
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
xorq $-0x1, %rax
movq %rax, -0x30(%rbp)
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x30(%rbp), %rdx
movslq %eax, %rcx
leaq 0xe60d(%rip), %rax      # 0x11490
andq (%rax,%rcx,8), %rdx
leaq 0xe602(%rip), %rax      # 0x11490
movq %rdx, (%rax,%rcx,8)
movl -0x8(%rbp), %edi
xorl %esi, %esi
callq 0x20e0
movslq -0x10(%rbp), %rax
imulq $0x8038, %rax, %rax     # imm = 0x8038
leaq 0xe6f2(%rip), %rcx      # 0x115a0
movl 0x4(%rax,%rcx), %eax
movl %eax, -0x34(%rbp)
subl $0x1, %eax
je 0x2ee0
jmp 0x2ebc
movl -0x34(%rbp), %eax
addl $-0x3, %eax
subl $0x4, %eax
jb 0x2ee0
jmp 0x2ec9
movl -0x34(%rbp), %eax
subl $0xc, %eax
je 0x2ee0
jmp 0x2ed3
movl -0x34(%rbp), %eax
addl $-0x10, %eax
subl $0x2, %eax
ja 0x2f25
jmp 0x2ee0
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x40(%rbp)
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x40(%rbp), %rdx
movslq %eax, %rcx
leaq 0xe5fc(%rip), %rax      # 0x11510
orq (%rax,%rcx,8), %rdx
leaq 0xe5f1(%rip), %rax      # 0x11510
movq %rdx, (%rax,%rcx,8)
jmp 0x2f79
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
xorq $-0x1, %rax
movq %rax, -0x48(%rbp)
movl -0x8(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x48(%rbp), %rdx
movslq %eax, %rcx
leaq 0xe5b3(%rip), %rax      # 0x11510
andq (%rax,%rcx,8), %rdx
leaq 0xe5a8(%rip), %rax      # 0x11510
movq %rdx, (%rax,%rcx,8)
movl -0x8(%rbp), %edi
movl $0x1, %esi
callq 0x20e0
movl -0x8(%rbp), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x20c0
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %edi
movl -0x14(%rbp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x20c0
movl 0xe445(%rip), %eax      # 0x113f0
cmpl -0x10(%rbp), %eax
jle 0x2fbb
movl 0xe43a(%rip), %eax      # 0x113f0
movl %eax, -0x4c(%rbp)
jmp 0x2fc1
movl -0x10(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, 0xe426(%rip)      # 0x113f0
movl 0xe424(%rip), %eax      # 0x113f4
cmpl -0x8(%rbp), %eax
jle 0x2fe0
movl 0xe419(%rip), %eax      # 0x113f4
movl %eax, -0x50(%rbp)
jmp 0x2fe6
movl -0x8(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl %eax, 0xe405(%rip)      # 0x113f4
movslq -0x10(%rbp), %rcx
leaq 0xe5a6(%rip), %rax      # 0x115a0
imulq $0x8038, %rcx, %rcx     # imm = 0x8038
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
leaq 0xe200(%rip), %rax      # 0x11210
movq (%rax,%rcx,8), %rsi
movl -0x10(%rbp), %edx
movl 0xe3cf(%rip), %ecx      # 0x113ec
movl 0xe3cc(%rip), %r8d      # 0x113f0
movl -0x8(%rbp), %r9d
movl 0xe3c6(%rip), %eax      # 0x113f4
leaq 0x9817(%rip), %rdi      # 0xc84c
movl %eax, (%rsp)
movb $0x0, %al
callq 0x9ae0
callq 0x3970
movl -0x10(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)