#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDBUF;
int /*<<< orphan*/ SO_SNDTIMEO;
scalar_t__ errno;
int /*<<< orphan*/ logcat(char *, ...);
int send(int, unsigned char *, int, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int socket_send(int sockfd, unsigned char *buf, int len, int timeout) {
  unsigned char *p_buf = buf;
  int sendBytes = 0;
  int total_len = len;
  struct timeval tv;
  int ret;
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
                   sizeof(struct timeval));
  if (ret < 0) {
    logcat("setsockopt: %s\n", strerror(errno));
    return -1;
  }
  int s_buf_size = 64 * 1024;
  if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&s_buf_size,
                 sizeof(int)) < 0) {
    logcat("setsockopt fail to change SNDbuf.\n");
    return -1;
  }
  while (1) {
    sendBytes = send(sockfd, p_buf, total_len, MSG_NOSIGNAL);
    if (sendBytes < 0 && errno != EINTR) {
      if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
        logcat("Send timeout.\n");
      else
        logcat("Send errors!\n");
      return -1;
    } else if (sendBytes == 0) {
      logcat("Send: disconnected.\n");
      return 0;
    } else if (sendBytes > 0) {
      if (sendBytes >= total_len) {
        break;
      }
      p_buf += sendBytes;
      total_len -= sendBytes;
      continue;
    } else {
      continue;
    }
  }
  return len;
}


x86 assembly (clang, -O0):
<socket_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11d4 <socket_send+0x94>
mov    0x2e18(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <socket_send+0x1f2>
movl   $0x10000,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%eax
jge    1222 <socket_send+0xe2>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <socket_send+0x1f2>
jmp    1227 <socket_send+0xe7>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
call   1040 <send@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12c7 <socket_send+0x187>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
je     12c7 <socket_send+0x187>
mov    0x2d5b(%rip),%rax
mov    (%rax),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
je     129a <socket_send+0x15a>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    0x2d0f(%rip),%rcx
cmp    (%rcx),%rax
jne    12ad <socket_send+0x16d>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
jmp    12bb <socket_send+0x17b>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <socket_send+0x1f2>
cmpl   $0x0,-0x24(%rbp)
jne    12eb <socket_send+0x1ab>
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0x0,-0x4(%rbp)
jmp    1332 <socket_send+0x1f2>
cmpl   $0x0,-0x24(%rbp)
jle    1327 <socket_send+0x1e7>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jl     1306 <socket_send+0x1c6>
jmp    132c <socket_send+0x1ec>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
jmp    1227 <socket_send+0xe7>
jmp    1227 <socket_send+0xe7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%ebp
mov    %ecx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e76(%rip),%rbx
mov    (%rbx),%esi
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1213 <socket_send+0xd3>
movl   $0x10000,0x4(%rsp)
mov    (%rbx),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     126a <socket_send+0x12a>
mov    0x2dea(%rip),%r13
mov    %r14d,%ebx
mov    0x2df8(%rip),%r14
mov    0x2de1(%rip),%r15
mov    %ebx,(%rsp)
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%ecx
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <send@plt>
test   %eax,%eax
jns    11fe <socket_send+0xbe>
mov    (%r14),%rax
cmp    (%r15),%rax
je     11e0 <socket_send+0xa0>
jmp    1239 <socket_send+0xf9>
je     1289 <socket_send+0x149>
sub    %eax,%ebx
jle    1294 <socket_send+0x154>
mov    %eax,%eax
add    %rax,%r12
jmp    11e0 <socket_send+0xa0>
mov    0x2dae(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
lea    0xdd7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <logcat@plt>
mov    $0xffffffff,%eax
jmp    127a <socket_send+0x13a>
mov    0x2d80(%rip),%rcx
mov    0x2d59(%rip),%rdx
cmp    (%rdx),%rax
lea    0xde2(%rip),%rdx
lea    0xdea(%rip),%rdi
cmove  %rdx,%rdi
cmp    (%rcx),%rax
cmove  %rdx,%rdi
mov    $0xffffffff,%ebx
jmp    1271 <socket_send+0x131>
lea    0xd9f(%rip),%rdi
xor    %eax,%eax
call   1050 <logcat@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
lea    0xdbe(%rip),%rdi
jmp    1271 <socket_send+0x131>
mov    (%rsp),%eax
jmp    127a <socket_send+0x13a>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@.str = [16 x i8] c"setsockopt: %s\0A\00"
@errno = global i64 0
@SO_SNDBUF = global i32 0
@.str.1 = [35 x i8] c"setsockopt fail to change SNDbuf.\0A\00"
@MSG_NOSIGNAL = global i32 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.2 = [15 x i8] c"Send timeout.\0A\00"
@.str.3 = [14 x i8] c"Send errors!\0A\00"
@.str.4 = [21 x i8] c"Send: disconnected.\0A\00"
define i32 @socket_send(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%16 = load i8*, i8** %7
store i8* %16, i8** %10
store i32 0, i32* %11
%17 = load i32, i32* %8
store i32 %17, i32* %12
%18 = load i32, i32* %9
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* %6
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_SNDTIMEO
%24 = bitcast %struct.timeval* %13 to i8*
%25 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32 16)
store i32 %25, i32* %14
%26 = load i32, i32* %14
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %4
%29 = load i64, i64* @errno
%30 = call i32 @strerror(i64 %29)
%31 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %30)
store i32 -1, i32* %5
br label %91
32: ; preds = %4
store i32 65536, i32* %15
%33 = load i32, i32* %6
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_SNDBUF
%36 = bitcast i32* %15 to i8*
%37 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i8* %36, i32 4)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %91
41: ; preds = %32
br label %42
42: ; preds = %41, %80, %88
%43 = load i32, i32* %6
%44 = load i8*, i8** %10
%45 = load i32, i32* %12
%46 = load i32, i32* @MSG_NOSIGNAL
%47 = call i32 @send(i32 %43, i8* %44, i32 %45, i32 %46)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %67
50: ; preds = %42
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %67
54: ; preds = %50
%55 = load i64, i64* @errno
%56 = load i64, i64* @EWOULDBLOCK
%57 = icmp eq i64 %55, %56
br i1 %57, label %62, label %58
58: ; preds = %54
%59 = load i64, i64* @errno
%60 = load i64, i64* @EAGAIN
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %58, %54
%63 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %66
64: ; preds = %58
%65 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %66
66: ; preds = %64, %62
store i32 -1, i32* %5
br label %91
67: ; preds = %50, %42
%68 = load i32, i32* %11
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %91
72: ; preds = %67
%73 = load i32, i32* %11
%74 = icmp sgt i32 %73, 0
br i1 %74, label %75, label %88
75: ; preds = %72
%76 = load i32, i32* %11
%77 = load i32, i32* %12
%78 = icmp sge i32 %76, %77
br i1 %78, label %79, label %80
79: ; preds = %75
br label %89
80: ; preds = %75
%81 = load i32, i32* %11
%82 = load i8*, i8** %10
%83 = sext i32 %81 to i64
%84 = getelementptr inbounds i8, i8* %82, i64 %83
store i8* %84, i8** %10
%85 = load i32, i32* %11
%86 = load i32, i32* %12
%87 = sub nsw i32 %86, %85
store i32 %87, i32* %12
br label %42
88: ; preds = %72
br label %42
89: ; preds = %79
%90 = load i32, i32* %8
store i32 %90, i32* %5
br label %91
91: ; preds = %89, %70, %66, %39, %28
%92 = load i32, i32* %5
ret i32 %92
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@.str = [16 x i8] c"setsockopt: %s\0A\00"
@errno = global i64 0
@SO_SNDBUF = global i32 0
@.str.1 = [35 x i8] c"setsockopt fail to change SNDbuf.\0A\00"
@MSG_NOSIGNAL = global i32 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.2 = [15 x i8] c"Send timeout.\0A\00"
@.str.3 = [14 x i8] c"Send errors!\0A\00"
@.str.4 = [21 x i8] c"Send: disconnected.\0A\00"
define i32 @socket_send(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = bitcast %struct.timeval* %5 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %3, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %9
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_SNDTIMEO
%12 = call i32 @setsockopt(i32 %0, i32 %10, i32 %11, i8* %7, i32 16)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %4
%15 = load i64, i64* @errno
%16 = call i32 @strerror(i64 %15)
%17 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %56
18: ; preds = %4
%19 = bitcast i32* %6 to i8*
store i32 65536, i32* %6
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_SNDBUF
%22 = call i32 @setsockopt(i32 %0, i32 %20, i32 %21, i8* %19, i32 4)
%23 = icmp slt i32 %22, 0
br i1 %23, label %50, label %47
24: ; preds = %47, %28
%25 = load i32, i32* @MSG_NOSIGNAL
%26 = call i32 @send(i32 %0, i8* %48, i32 %49, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %24, label %32
32: ; preds = %28
%33 = load i64, i64* @EWOULDBLOCK
%34 = icmp eq i64 %29, %33
%35 = load i64, i64* @EAGAIN
%36 = icmp eq i64 %29, %35
%37 = select i1 %34, i1 true, i1 %36
%38 = select i1 %37, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0)
br label %50
39: ; preds = %24
%40 = icmp eq i32 %26, 0
br i1 %40, label %50, label %41
41: ; preds = %39
%42 = icmp sgt i32 %49, %26
br i1 %42, label %43, label %54
43: ; preds = %41
%44 = zext i32 %26 to i64
%45 = getelementptr inbounds i8, i8* %48, i64 %44
%46 = sub nsw i32 %49, %26
br label %47
47: ; preds = %18, %43
%48 = phi i8* [ %45, %43 ], [ %1, %18 ]
%49 = phi i32 [ %46, %43 ], [ %2, %18 ]
br label %24
50: ; preds = %39, %32, %18
%51 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), %18 ], [ %38, %32 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), %39 ]
%52 = phi i32 [ -1, %18 ], [ -1, %32 ], [ 0, %39 ]
%53 = call i32 (i8*, ...) @logcat(i8* %51)
br label %54
54: ; preds = %41, %50
%55 = phi i32 [ %52, %50 ], [ %2, %41 ]
br label %56
56: ; preds = %54, %14
%57 = phi i32 [ -1, %14 ], [ %55, %54 ]
ret i32 %57
}