// File: mh0fm4[P]SCDC/src/components/transport/transport_stream.cc
// Function: scdc_transport_stream::receive(scdc_transport_connection_t*, void*, long long)
scdcint_t scdc_transport_stream::receive(scdc_transport_connection_t *conn, void *buf, scdcint_t max_buf_size)
{
  SCDC_TRACE("receive: conn: '" << conn << "', buf: '" << buf << "', max_buf_size: " << max_buf_size);

  scdc_transport_stream_connection_t *stream_conn = static_cast<scdc_transport_stream_connection_t *>(conn);

  if (max_buf_size <= 0)
  {
    SCDC_TRACE("connection: " << stream_conn << ": receive: nothing to receive");
    return 0;
  }

  scdcint_t n;

#if SCDC_TRANSPORT_STREAM_USE_SHUTDOWN_EXCEPTION
  try
  {
#endif
#if SCDC_TRANSPORT_STREAM_USE_IOSTREAM
    if (stream_conn->is)
    {
      if (stream_conn->is->eof())
      {
        SCDC_TRACE("connection: " << stream_conn << ": receive: end of input stream");
        n = -1;

      } else
      {
        stream_conn->is->getline(static_cast<char *>(buf), max_buf_size);
        n = stream_conn->is->gcount() - 1;
      }

    } else
#endif
    if (stream_conn->ifile)
    {
      if (feof(stream_conn->ifile))
      {
        SCDC_TRACE("connection: " << stream_conn << ": receive: end of input stream");
        n = -1;

      } else
      {
#if 0
        n = fread(buf, 1, max_buf_size, stream_conn->ifile);
        SCDC_TRACE("connection: " << stream_conn << ": receive: fread done: n = " << n);
#else
        char *s = fgets(static_cast<char *>(buf), max_buf_size, stream_conn->ifile);
        if (s) n = strlen(s);
        else n = -1;
        if (n > 0 && s[n - 1] == '\n')
        {
          s[n - 1] = '\0';
          --n;
        }
        SCDC_TRACE("connection: " << stream_conn << ": receive: fgets done: n = " << n);
#endif
      }

    } else
    {
      SCDC_FATAL("connection: " << stream_conn << ": receive: no output stream available");
      n = -1;
    }

#if SCDC_TRANSPORT_STREAM_USE_SHUTDOWN_EXCEPTION
  } catch (const scdc_transport_stream_shutdown_ex &ex)
  {
    SCDC_INFO("connection: " << stream_conn << ": receive: catch shutdown exception");
    n = -1;
  }
#endif

  SCDC_TRACE("connection: " << stream_conn << ": receive: received " << n << " byte(s)");

  return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
jmp 0x9af7a
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x28(%rbp)
jg 0x9af9a
jmp 0x9af8b
jmp 0x9af8d
movq $0x0, -0x8(%rbp)
jmp 0x9b103
movq -0x30(%rbp), %rax
cmpq $0x0, 0x2d8(%rax)
je 0x9b04e
movq -0x30(%rbp), %rax
movq 0x2d8(%rax), %rdi
callq 0xca80
cmpl $0x0, %eax
je 0x9afcf
jmp 0x9afc3
jmp 0x9afc5
movq $-0x1, -0x38(%rbp)
jmp 0x9b049
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %esi
movq -0x30(%rbp), %rax
movq 0x2d8(%rax), %rdx
callq 0xcab0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x9b003
movq -0x40(%rbp), %rdi
callq 0xc280
movq %rax, -0x38(%rbp)
jmp 0x9b00b
movq $-0x1, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jle 0x9b043
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x9b043
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x9b045
jmp 0x9b047
jmp 0x9b049
jmp 0x9b0f7
jmp 0x9b050
leaq 0x39651(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e8e0
cmpq $0x0, %rax
je 0x9b0ed
leaq 0x39638(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e900
leaq 0x39629(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e8e0
movq %rax, %rdi
leaq 0xea50(%rip), %rsi      # 0xa9ae1
callq 0xc670
movq %rax, %rdi
leaq 0xeac3(%rip), %rsi      # 0xa9b63
callq 0xc670
movq %rax, %rdi
leaq 0xeadc(%rip), %rsi      # 0xa9b8b
callq 0xc670
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0xc4f0
movq %rax, %rdi
leaq 0xeaf1(%rip), %rsi      # 0xa9bbb
callq 0xc670
movq %rax, %rdi
movq 0x38ee7(%rip), %rsi     # 0xd3fc0
callq 0xc6d0
leaq 0x395c3(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e920
jmp 0x9b0ef
movq $-0x1, -0x38(%rbp)
jmp 0x9b0f9
jmp 0x9b0fb
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)