// File: mh0fm4[P]SCDC/src/components/transport/transport.cc
// Function: send_outgoing_buf(scdc_transport_connection_t*, _scdc_buf_t*)
static bool send_outgoing_buf(scdc_transport_connection_t *conn, scdc_buf_t *buf)
{
  bool ret = true;

  scdcint_t mx = buf->current;
  buf->current = 0;

  const char *src = static_cast<char *>(buf->ptr);
  scdcint_t src_size = buf->size;

  if (mx > 0)
  {
    /* 1st: copy from inout to outgoing */
    char *dst = conn->outgoing.get_write_pos_buf();
    scdcint_t dst_size = conn->outgoing.get_write_pos_buf_size();

    scdcint_t n = min(src_size, mx);

    /* only short messages that fit into outgoing */
    if (n <= DEFAULT_TRANSPORT_SEND_OUTGOING_DATASET_INOUT_THRESHOLD && n <= dst_size)
    {
      memcpy(dst, src, n);

      conn->outgoing.inc_write_pos(n);

      src += n;
      src_size -= n;

      mx -= n;
      buf->current += n;
    }
  }

  if (mx > 0)
  {
    /* send outgoing before sending inout directly */
    ret = ret && conn->transport->send_outgoing(conn);

    /* 2nd: send from inout */

    scdcint_t n;

    while (1)
    {
      n = min(src_size, mx);

      if (n <= 0) break;

      n = conn->transport->send(conn, src, n);

/*      SCDC_INFO("conn->transport->send: " << n);*/

      if (n < 0) break;

      conn->total_send += n;

      src += n;
      src_size -= n;

      mx -= n;
      buf->current += n;
    }

    ret = ret && (n >= 0);
  }

  return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x1, -0x11(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x20(%rbp)
jle 0x999f6
movq -0x8(%rbp), %rdi
addq $0x70, %rdi
callq 0x2ec80
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
addq $0x70, %rdi
callq 0x2eca0
movq %rax, -0x40(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x2b080
movq (%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x400, -0x48(%rbp)     # imm = 0x400
jg 0x999f4
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jg 0x999f4
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0xc530
movq -0x8(%rbp), %rdi
addq $0x70, %rdi
movq -0x48(%rbp), %rsi
callq 0x2ecc0
movq -0x48(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
jmp 0x999f6
cmpq $0x0, -0x20(%rbp)
jle 0x99adb
xorl %eax, %eax
testb $0x1, -0x11(%rbp)
movb %al, -0x51(%rbp)
je 0x99a23
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rsi
movq (%rdi), %rax
callq *0xd0(%rax)
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
andb $0x1, %al
movb %al, -0x11(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x2b080
movq (%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jg 0x99a48
jmp 0x99abd
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq (%rdi), %rax
callq *0x78(%rax)
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jge 0x99a6e
jmp 0x99abd
movq -0x50(%rbp), %rcx
movq -0x8(%rbp), %rax
addq 0x20(%rax), %rcx
movq %rcx, 0x20(%rax)
movq -0x50(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
jmp 0x99a2b
xorl %eax, %eax
testb $0x1, -0x11(%rbp)
movb %al, -0x52(%rbp)
je 0x99ad3
cmpq $0x0, -0x50(%rbp)
setge %al
movb %al, -0x52(%rbp)
movb -0x52(%rbp), %al
andb $0x1, %al
movb %al, -0x11(%rbp)
movb -0x11(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)