#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;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int send(int, unsigned char const *, int, int /*<<< orphan*/);
int safeWrite(int socketFd, const unsigned char *writeData, int writeLen) {
  fd_set writeFds;
  struct timeval tv;
  int ret, len = 0;
  if ((0 >= writeLen) || (NULL == writeData)) {
    return -1;
  }
  FD_ZERO(&writeFds);
  FD_SET(socketFd, &writeFds);
  tv.tv_sec = 5;
  while (1) {
    ret = select(socketFd + 1, NULL, &writeFds, NULL, &tv);
    if (0 > ret) {
      perror("select...\n");
    } else if (0 == ret) {
      continue;
    } else {
      if (FD_ISSET(socketFd, &writeFds)) {
        len = send(socketFd, writeData, writeLen, 0);
        break;
      }
    }
  }
  return len;
}


x86 assembly (clang, -O0):
<safeWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jge    1190 <safeWrite+0x30>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    119c <safeWrite+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <safeWrite+0xe5>
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <FD_SET@plt>
movl   $0x5,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%r8
mov    %rcx,%rsi
call   1060 <select@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jle    11f1 <safeWrite+0x91>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
jmp    123a <safeWrite+0xda>
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jne    1201 <safeWrite+0xa1>
jmp    11b8 <safeWrite+0x58>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1230 <safeWrite+0xd0>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x28(%rbp)
jmp    123f <safeWrite+0xdf>
jmp    1235 <safeWrite+0xd5>
jmp    123a <safeWrite+0xda>
jmp    11b8 <safeWrite+0x58>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %rsi,%rsi
je     11f7 <safeWrite+0x97>
mov    %edx,%r14d
test   %edx,%edx
jle    11f7 <safeWrite+0x97>
mov    %rsi,%r15
mov    %edi,%r12d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
movl   $0x5,(%rsp)
lea    0x1(%r12),%ebp
mov    %rsp,%r13
jmp    11c0 <safeWrite+0x60>
lea    0xe4a(%rip),%rdi
call   1070 <perror@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %r13,%r8
call   1060 <select@plt>
test   %eax,%eax
js     11af <safeWrite+0x4f>
je     11c0 <safeWrite+0x60>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11c0 <safeWrite+0x60>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [11 x i8] c"select...\0A\00"
define i32 @safeWrite(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %11
%12 = load i32, i32* %7
%13 = icmp sge i32 0, %12
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = icmp eq i8* null, %15
br i1 %16, label %17, label %18
17: ; preds = %14, %3
store i32 -1, i32* %4
br label %49
18: ; preds = %14
%19 = call i32 @FD_ZERO(i32* %8)
%20 = load i32, i32* %5
%21 = call i32 @FD_SET(i32 %20, i32* %8)
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 5, i32* %22
br label %23
23: ; preds = %18, %34, %46
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* null, i32* %8, i32* null, %struct.timeval* %9)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp sgt i32 0, %27
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %46
31: ; preds = %23
%32 = load i32, i32* %10
%33 = icmp eq i32 0, %32
br i1 %33, label %34, label %35
34: ; preds = %31
br label %23
35: ; preds = %31
%36 = load i32, i32* %5
%37 = call i64 @FD_ISSET(i32 %36, i32* %8)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i32, i32* %5
%41 = load i8*, i8** %6
%42 = load i32, i32* %7
%43 = call i32 @send(i32 %40, i8* %41, i32 %42, i32 0)
store i32 %43, i32* %11
br label %47
44: ; preds = %35
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45, %29
br label %23
47: ; preds = %39
%48 = load i32, i32* %11
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %17
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [11 x i8] c"select...\0A\00"
define i32 @safeWrite(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = icmp slt i32 %2, 1
%9 = icmp eq i8* %1, null
%10 = or i1 %9, %8
br i1 %10, label %29, label %11
11: ; preds = %3
%12 = call i32 @FD_ZERO(i32* %4)
%13 = call i32 @FD_SET(i32 %0, i32* %4)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 5, i32* %14
%15 = add nsw i32 %0, 1
br label %16
16: ; preds = %21, %11
%17 = call i32 @select(i32 %15, i32* null, i32* %4, i32* null, %struct.timeval* %5)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %24, %22
br label %16
22: ; preds = %16
%23 = icmp eq i32 %17, 0
br i1 %23, label %21, label %24
24: ; preds = %22
%25 = call i64 @FD_ISSET(i32 %0, i32* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %21, label %27
27: ; preds = %24
%28 = call i32 @send(i32 %0, i8* %1, i32 %2, i32 0)
br label %29
29: ; preds = %3, %27
%30 = phi i32 [ %28, %27 ], [ -1, %3 ]
ret i32 %30
}