// File: /workspace/github_c_makefile_star2_O3/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:
testq %rdx, %rdx
je 0x3d7b0
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1b324(%rdi), %rax
movq %rax, (%rsp)
movl $0x1f4, %r13d           # imm = 0x1F4
movl 0x60d4(%rbx), %esi
cmpl $-0x1, %esi
je 0x3d5f8
movl 0x54(%rbx), %ecx
testl %ecx, %ecx
je 0x3d5d9
movq %rbx, %rdi
movl $0x6, %edx
callq 0x3df48
testl %eax, %eax
jle 0x3d657
testb $0x4, 0xc(%rbx)
movl 0x60(%rbx), %ecx
jne 0x3d634
movl 0x60d4(%rbx), %edi
movslq %r14d, %rdx
movq %r15, %rsi
callq 0x3120
jmp 0x3d6a4
movl 0x60e8(%rbx), %edi
movl %r14d, %edx
movq %r15, %rsi
callq 0x30e0
testl %eax, %eax
jg 0x3d79e
callq 0x3060
movl (%rax), %eax
cmpl $0xb, %eax
ja 0x3d7bf
movl $0x811, %ecx            # imm = 0x811
btl %eax, %ecx
jb 0x3d79c
jmp 0x3d7bf
movq 0x1b338(%rbx), %r9
movl 0x60d4(%rbx), %edi
movq %r15, %rsi
movq %r14, %rdx
testq %r9, %r9
je 0x3d69b
movq (%rsp), %r8
callq 0x3190
jmp 0x3d6a0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x3d7ca
movl 0x1921c(%rbx), %ecx
cmpl $0x4, %ecx
je 0x3d67a
cmpl $0xb, %ecx
jne 0x3d7b3
movl 0x60d4(%rbx), %esi
movl 0x54(%rbx), %ecx
movq %rbx, %rdi
movl $0x6, %edx
callq 0x3df48
movl %eax, %ecx
testl %eax, %eax
jle 0x3d659
jmp 0x3d5d9
callq 0x3120
testl %eax, %eax
js 0x3d6b1
testl %eax, %eax
jg 0x3d79e
jmp 0x3d74a
xorl %eax, %eax
testb $0x6, 0x64(%rbx)
setne %al
leal 0x2(,%rax,2), %ebp
callq 0x33f0
cltq
imulq $0x28c1979, %rax, %rcx  # imm = 0x28C1979
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rcx
addl %edx, %ecx
imull $0xc9, %ecx, %ecx
negl %ecx
leal (%rax,%rcx), %r12d
addl $0x32, %r12d
movl 0x60d4(%rbx), %esi
imull $0xfffffc18, %r12d, %ecx # imm = 0xFFFFFC18
movq %rbx, %rdi
movl $0x4, %edx
callq 0x3df48
movq 0x1b338(%rbx), %r9
movl 0x60d4(%rbx), %edi
movl 0x60(%rbx), %ecx
movq %r15, %rsi
movq %r14, %rdx
testq %r9, %r9
je 0x3d72c
movq (%rsp), %r8
callq 0x3190
jmp 0x3d731
callq 0x3120
testl %eax, %eax
jns 0x3d6a4
addl %r12d, %r12d
cmpl %r13d, %r12d
cmovgel %r13d, %r12d
decl %ebp
cmpl $0x1, %ebp
jg 0x3d6ec
callq 0x3060
movl (%rax), %eax
cmpl $0xb, %eax
jne 0x3d78d
movl 0x60d4(%rbx), %esi
movl 0x54(%rbx), %ecx
testl %ecx, %ecx
movl $0xffffd8f0, %eax       # imm = 0xFFFFD8F0
cmovel %eax, %ecx
movq %rbx, %rdi
movl $0x6, %edx
callq 0x3df48
testl %eax, %eax
je 0x3d796
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
jns 0x3d79e
cmpl $0x4, 0x1921c(%rbx)
je 0x3d79e
jmp 0x3d7c5
testl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
je 0x3d79c
jmp 0x3d7bf
cmpl $0x0, 0x54(%rbx)
jne 0x3d7c5
xorl %eax, %eax
movl %eax, %eax
addq %rax, %r15
subq %rax, %r14
jne 0x3d5b6
xorl %eax, %eax
jmp 0x3d7ca
xorl %eax, %eax
retq
testl %ecx, %ecx
jne 0x3d7ca
movl 0x19214(%rbx), %eax
jmp 0x3d7ca
movl %eax, 0x1921c(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq