// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
testq %rcx, %rcx
jle 0x60c6a
movq %rsi, %r14
movq 0x2d8(%rsi), %rdi
testq %rdi, %rdi
je 0x60c71
movq %rcx, %r15
movq %rdx, %r12
callq 0xb880
movq $-0x1, %rbx
testl %eax, %eax
jne 0x60d31
movq 0x2d8(%r14), %rdx
movq %r12, %rdi
movl %r15d, %esi
callq 0xb8b0
testq %rax, %rax
je 0x60d31
movq %rax, %r14
movq %rax, %rdi
callq 0xb200
movq %rax, %rbx
testq %rax, %rax
jle 0x60d31
cmpb $0xa, -0x1(%r14,%rbx)
jne 0x60d31
movb $0x0, -0x1(%r14,%rbx)
decq %rbx
jmp 0x60d31
xorl %ebx, %ebx
jmp 0x60d31
leaq 0x2b8f8(%rip), %r12     # 0x8c570
movq (%r12), %rdi
movq $-0x1, %rbx
cmpq $0x0, 0x18(%rdi)
je 0x60d31
addq $0x20, %rdi
callq 0xb7d0
movq (%r12), %rax
movq 0x18(%rax), %r15
leaq 0xce63(%rip), %rsi      # 0x6db09
movl $0xc, %edx
movq %r15, %rdi
callq 0xb5f0
leaq 0xced1(%rip), %rsi      # 0x6db8b
movl $0x12, %edx
movq %r15, %rdi
callq 0xb5f0
leaq 0xcee5(%rip), %rsi      # 0x6dbb3
movl $0xc, %edx
movq %r15, %rdi
callq 0xb5f0
movq %r15, %rdi
movq %r14, %rsi
callq 0xb3c0
movq %rax, %r14
leaq 0xcef3(%rip), %rsi      # 0x6dbe3
movl $0x25, %edx
movq %rax, %rdi
callq 0xb5f0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0xb4d0
movsbl %al, %esi
movq %r14, %rdi
callq 0xb050
movq %rax, %rdi
callq 0xb340
movq (%r12), %rdi
addq $0x20, %rdi
callq 0xb3b0
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq