// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
testq %rdx, %rdx
je 0x3d832
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x1b324(%rdi), %rax
movq %rax, 0x10(%rsp)
cmpl $-0x1, 0x60d4(%r14)
je 0x3d678
cmpl $0x0, 0x54(%r14)
je 0x3d64f
movl 0x60d4(%r14), %esi
movl 0x54(%r14), %ecx
movq %r14, %rdi
movl $0x6, %edx
callq 0x3dfc3
movl $0x4, %ecx
testl %eax, %eax
jg 0x3d642
movl $0x1, %ecx
je 0x3d639
movl 0x1921c(%r14), %eax
cmpl $0xb, %eax
je 0x3d640
cmpl $0x4, %eax
je 0x3d640
testl %eax, %eax
jne 0x3d639
movl 0x19214(%r14), %ebp
jmp 0x3d642
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3d642
xorl %ecx, %ecx
testl %ecx, %ecx
je 0x3d5f3
cmpl $0x4, %ecx
jne 0x3d834
testb $0x4, 0xc(%r14)
movl %ebp, 0xc(%rsp)
jne 0x3d6bb
movl 0x60d4(%r14), %edi
movslq %ebx, %rdx
movl 0x60(%r14), %ecx
movq %r15, %rsi
callq 0x3120
movq %rax, %r13
jmp 0x3d7a9
movl 0x60e8(%r14), %edi
movl %ebx, %edx
movq %r15, %rsi
callq 0x30e0
movq %rax, %r13
testl %r13d, %r13d
jg 0x3d823
callq 0x3060
movl (%rax), %eax
cmpl $0xb, %eax
ja 0x3d845
xorl %r13d, %r13d
movl $0x811, %ecx            # imm = 0x811
btl %eax, %ecx
jb 0x3d823
jmp 0x3d845
movq 0x1b338(%r14), %r9
movl 0x60d4(%r14), %edi
testq %r9, %r9
je 0x3d6e4
movl 0x60(%r14), %ecx
movq %r15, %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %r8
callq 0x3190
jmp 0x3d6f3
movl 0x60(%r14), %ecx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3120
movq %rax, %r13
testl %r13d, %r13d
jns 0x3d7a9
xorl %eax, %eax
testb $0x6, 0x64(%r14)
setne %al
leal 0x2(,%rax,2), %r12d
callq 0x33e0
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), %ebp
addl $0x32, %ebp
movl 0x60d4(%r14), %esi
imull $0xfffffc18, %ebp, %ecx # imm = 0xFFFFFC18
movq %r14, %rdi
movl $0x4, %edx
callq 0x3dfc3
movq 0x1b338(%r14), %r9
movl 0x60d4(%r14), %edi
testq %r9, %r9
je 0x3d77d
movl 0x60(%r14), %ecx
movq %r15, %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %r8
callq 0x3190
jmp 0x3d78c
movl 0x60(%r14), %ecx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3120
movq %rax, %r13
testl %r13d, %r13d
jns 0x3d7a9
addl %ebp, %ebp
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %ebp
cmovgel %eax, %ebp
decl %r12d
cmpl $0x1, %r12d
jg 0x3d73a
testl %r13d, %r13d
jle 0x3d7b4
movl 0xc(%rsp), %ebp
jmp 0x3d823
callq 0x3060
movl (%rax), %eax
cmpl $0xb, %eax
movl 0xc(%rsp), %ebp
jne 0x3d7fa
movl 0x60d4(%r14), %esi
movl 0x54(%r14), %ecx
testl %ecx, %ecx
movl $0xffffd8f0, %eax       # imm = 0xFFFFD8F0
cmovel %eax, %ecx
movq %r14, %rdi
movl $0x6, %edx
callq 0x3dfc3
testl %eax, %eax
je 0x3d811
testl %eax, %eax
jns 0x3d80a
cmpl $0x4, 0x1921c(%r14)
je 0x3d80a
jmp 0x3d818
testl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
je 0x3d80a
movl %eax, 0x1921c(%r14)
jmp 0x3d818
movb $0x1, %al
xorl %r13d, %r13d
jmp 0x3d81f
cmpl $0x0, 0x54(%r14)
je 0x3d7ea
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %eax, %eax
testb %al, %al
je 0x3d834
movslq %r13d, %rax
addq %rax, %r15
subq %rax, %rbx
jne 0x3d5de
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, 0x1921c(%r14)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3d834