// File: /workspace/github_c_makefile_star2_O2/loversmile[P]sugarcrm/sugar_stdsoap2.c
// Function: fsend
static int
fsend(struct soap *soap, const char *s, size_t n)
{ register int nwritten, err;
#if defined(__cplusplus) && !defined(WITH_LEAN) && !defined(WITH_COMPAT)
  if (soap->os)
  { soap->os->write(s, (std::streamsize)n);
    if (soap->os->good())
      return SOAP_OK;
    soap->errnum = 0;
    return SOAP_EOF;
  }
#endif
  while (n)
  { if (soap_valid_socket(soap->socket))
    { 
#ifndef WITH_LEAN
      if (soap->send_timeout)
      { for (;;)
        { register int r;
#ifdef WITH_OPENSSL
          if (soap->ssl)
            r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_ALL, soap->send_timeout);
          else
#endif
            r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->send_timeout);
          if (r > 0)
            break;
          if (!r)
            return SOAP_EOF;
	  err = soap->errnum;
	  if (!err)
            return soap->error;
          if (err != SOAP_EINTR && err != SOAP_EAGAIN && err != SOAP_EWOULDBLOCK)
            return SOAP_EOF;
        }
      }
#endif
#ifdef WITH_OPENSSL
      if (soap->ssl)
        nwritten = SSL_write(soap->ssl, s, (int)n);
      else if (soap->bio)
        nwritten = BIO_write(soap->bio, s, (int)n);
      else
#endif
#ifndef WITH_LEAN
      if ((soap->omode & SOAP_IO_UDP))
      { if (soap->peerlen)
          nwritten = sendto(soap->socket, (char*)s, (SOAP_WINSOCKINT)n, soap->socket_flags, (struct sockaddr*)&soap->peer, (SOAP_WINSOCKINT)soap->peerlen);
        else
          nwritten = send(soap->socket, s, (SOAP_WINSOCKINT)n, soap->socket_flags);
        /* retry and back-off algorithm */
        /* TODO: this is not very clear from specs so verify and limit conditions under which we should loop (e.g. ENOBUFS) */
        if (nwritten < 0)
        { int udp_repeat;
          int udp_delay;
          if ((soap->connect_flags & SO_BROADCAST))
            udp_repeat = 3; /* SOAP-over-UDP MULTICAST_UDP_REPEAT - 1 */
          else
            udp_repeat = 1; /* SOAP-over-UDP UNICAST_UDP_REPEAT - 1 */
          udp_delay = (soap_random % 201) + 50; /* UDP_MIN_DELAY .. UDP_MAX_DELAY */
          do
          { tcp_select(soap, soap->socket, SOAP_TCP_SELECT_ERR, -1000 * udp_delay);
            if (soap->peerlen)
              nwritten = sendto(soap->socket, (char*)s, (SOAP_WINSOCKINT)n, soap->socket_flags, (struct sockaddr*)&soap->peer, (SOAP_WINSOCKINT)soap->peerlen);
            else
              nwritten = send(soap->socket, s, (SOAP_WINSOCKINT)n, soap->socket_flags);
            udp_delay <<= 1;
            if (udp_delay > 500) /* UDP_UPPER_DELAY */
              udp_delay = 500;
          }
          while (nwritten < 0 && --udp_repeat > 0);
        }
      }
      else
#endif
#if !defined(PALM) && !defined(AS400)
        nwritten = send(soap->socket, s, (int)n, soap->socket_flags);
#else
        nwritten = send(soap->socket, (void*)s, n, soap->socket_flags);
#endif
      if (nwritten <= 0)
      {
#if defined(WITH_OPENSSL) || !defined(WITH_LEAN)
        register int r = 0;
#endif
        err = soap_socket_errno(soap->socket);
#ifdef WITH_OPENSSL
        if (soap->ssl && (r = SSL_get_error(soap->ssl, nwritten)) != SSL_ERROR_NONE && r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
        { soap->errnum = err;
          return SOAP_EOF;
        }
#endif
        if (err == SOAP_EWOULDBLOCK || err == SOAP_EAGAIN)
        {
#ifndef WITH_LEAN
#ifdef WITH_OPENSSL
          if (soap->ssl && r == SSL_ERROR_WANT_READ)
            r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, soap->send_timeout ? soap->send_timeout : -10000);
          else
            r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->send_timeout ? soap->send_timeout : -10000);
#else
          r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->send_timeout ? soap->send_timeout : -10000);
#endif
	  if (!r && soap->send_timeout)
            return SOAP_EOF;
	  if (r < 0 && soap->errnum != SOAP_EINTR)
            return SOAP_EOF;
#endif
        }
        else if (err && err != SOAP_EINTR)
        { soap->errnum = err;
          return SOAP_EOF;
        }
        nwritten = 0; /* and call write() again */
      }
    }
    else
    {
#ifdef WITH_FASTCGI
      nwritten = fwrite((void*)s, 1, n, stdout);
      fflush(stdout);
#else
#ifdef UNDER_CE
      nwritten = fwrite(s, 1, n, soap->sendfd);
#else
#ifdef VXWORKS
#ifdef WMW_RPM_IO
      if (soap->rpmreqid)
        nwritten = (httpBlockPut(soap->rpmreqid, (char*)s, n) == 0) ? n : -1; 
      else
#endif
        nwritten = fwrite(s, sizeof(char), n, fdopen(soap->sendfd, "w"));
#else
#ifdef WIN32
      nwritten = _write(soap->sendfd, s, (unsigned int)n);
#else
      nwritten = write(soap->sendfd, s, (unsigned int)n);
#endif
#endif
#endif
#endif
      if (nwritten <= 0)
      { 
#ifndef WITH_FASTCGI
        err = soap_errno;
#else
        err = EOF;
#endif
        if (err && err != SOAP_EINTR && err != SOAP_EWOULDBLOCK && err != SOAP_EAGAIN)
        { soap->errnum = err;
          return SOAP_EOF;
        }
        nwritten = 0; /* and call write() again */
      }
    }
    n -= nwritten;
    s += nwritten;
  }
  return SOAP_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
leaq 0x1b324(%rdi), %rax
movq %rax, (%rsp)
pushq $0x6
popq %rbp
testq %r15, %r15
je 0x3a032
movl 0x60d4(%r14), %esi
cmpl $-0x1, %esi
je 0x39ea8
movl 0x54(%r14), %ecx
testl %ecx, %ecx
je 0x39ee5
movq %r14, %rdi
movl %ebp, %edx
callq 0x3a65e
testl %eax, %eax
jg 0x39ee5
je 0x3a03d
movl 0x1921c(%r14), %eax
cmpl $0x4, %eax
je 0x39e9b
cmpl $0xb, %eax
jne 0x3a022
movl 0x60d4(%r14), %esi
movl 0x54(%r14), %ecx
jmp 0x39e72
movl 0x60e8(%r14), %edi
movl %r15d, %edx
movq %r12, %rsi
callq 0x30e0
testl %eax, %eax
jg 0x3a015
callq 0x3060
movl (%rax), %eax
cmpl $0xb, %eax
ja 0x3a036
movl $0x811, %ecx            # imm = 0x811
btl %eax, %ecx
jb 0x3a013
jmp 0x3a036
testb $0x4, 0xc(%r14)
movl 0x60(%r14), %ecx
jne 0x39f04
movl 0x60d4(%r14), %edi
movslq %r15d, %rdx
movq %r12, %rsi
callq 0x3120
jmp 0x39f31
movq 0x1b338(%r14), %r9
movl 0x60d4(%r14), %edi
movq %r12, %rsi
movq %r15, %rdx
testq %r9, %r9
je 0x39f28
movq (%rsp), %r8
callq 0x3190
jmp 0x39f2d
callq 0x3120
testl %eax, %eax
js 0x39f3e
testl %eax, %eax
jg 0x3a015
jmp 0x39fc0
xorl %eax, %eax
testb $0x6, 0x64(%r14)
setne %al
leal 0x2(,%rax,2), %r13d
callq 0x3400
cltd
movl $0xc9, %ecx
idivl %ecx
movl %edx, %ebx
addl $0x32, %ebx
movl 0x60d4(%r14), %esi
imull $0xfffffc18, %ebx, %ecx # imm = 0xFFFFFC18
movq %r14, %rdi
pushq $0x4
popq %rdx
callq 0x3a65e
movq 0x1b338(%r14), %r9
movl 0x60d4(%r14), %edi
movl 0x60(%r14), %ecx
movq %r12, %rsi
movq %r15, %rdx
testq %r9, %r9
je 0x39fa2
movq (%rsp), %r8
callq 0x3190
jmp 0x39fa7
callq 0x3120
testl %eax, %eax
jns 0x39f31
addl %ebx, %ebx
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %ebx
cmovgel %eax, %ebx
decl %r13d
cmpl $0x1, %r13d
jg 0x39f62
callq 0x3060
movl (%rax), %eax
cmpl $0xb, %eax
jne 0x3a003
movl 0x60d4(%r14), %esi
movl 0x54(%r14), %ecx
testl %ecx, %ecx
movl $0xffffd8f0, %eax       # imm = 0xFFFFD8F0
cmovel %eax, %ecx
movq %r14, %rdi
movl %ebp, %edx
callq 0x3a65e
testl %eax, %eax
je 0x3a00c
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
jns 0x3a015
cmpl $0x4, 0x1921c(%r14)
je 0x3a015
jmp 0x3a03d
testl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
je 0x3a013
jmp 0x3a036
cmpl $0x0, 0x54(%r14)
jne 0x3a03d
xorl %eax, %eax
movl %eax, %eax
subq %rax, %r15
addq %rax, %r12
jmp 0x39e55
testl %eax, %eax
pushq $-0x1
popq %rax
jne 0x3a040
movl 0x19214(%r14), %eax
jmp 0x3a040
xorl %eax, %eax
jmp 0x3a040
movl %eax, 0x1921c(%r14)
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq