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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int send(int, char *, int, int /*<<< orphan*/);
int strlen(char *);
int clamd_send(int sock, char *buffer) {
  int ret;
  fd_set wfds;
  struct timeval tv;
  if (sock < 0)
    return -1;
  do {
    FD_ZERO(&wfds);
    FD_SET(sock, &wfds);
    tv.tv_sec = 60;
    tv.tv_usec = 0;
    ret = select(sock + 1, NULL, &wfds, NULL, &tv);
  } while (ret < 0 && errno == EINTR);
  if (ret <= 0)
    return -1;
  if (!FD_ISSET(sock, &wfds))
    return -1;
  do {
    ret = send(sock, buffer, strlen(buffer), 0);
  } while (ret < 0 && errno == EINTR);
  if (ret < strlen(buffer))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<clamd_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1185 <clamd_send+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <clamd_send+0x168>
jmp    118a <clamd_send+0x2a>
lea    -0x18(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <FD_SET@plt>
movl   $0x3c,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%r8
mov    %rcx,%rsi
call   1070 <select@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x29(%rbp)
jge    11f4 <clamd_send+0x94>
mov    0x2dff(%rip),%rax
mov    (%rax),%rax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    118a <clamd_send+0x2a>
cmpl   $0x0,-0x14(%rbp)
jg     1215 <clamd_send+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <clamd_send+0x168>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1236 <clamd_send+0xd6>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <clamd_send+0x168>
jmp    123b <clamd_send+0xdb>
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x3d(%rbp)
jge    128e <clamd_send+0x12e>
mov    0x2d65(%rip),%rax
mov    (%rax),%rax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    123b <clamd_send+0xdb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jge    12c1 <clamd_send+0x161>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <clamd_send+0x168>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clamd_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%ecx
test   %edi,%edi
js     1254 <clamd_send+0xf4>
mov    %rsi,%r14
mov    %edi,%r12d
lea    0x1(%r12),%ebp
lea    0x4(%rsp),%rbx
lea    0x8(%rsp),%r13
mov    0x2e41(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
movl   $0x3c,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %ebp,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %r13,%r8
call   1070 <select@plt>
test   %eax,%eax
jns    11e8 <clamd_send+0x88>
mov    (%r15),%rcx
mov    0x2dfd(%rip),%rdx
cmp    %rcx,(%rdx)
je     11a0 <clamd_send+0x40>
test   %eax,%eax
jle    124f <clamd_send+0xef>
lea    0x4(%rsp),%rsi
mov    %r12d,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
mov    $0xffffffff,%ecx
je     1254 <clamd_send+0xf4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    123c <clamd_send+0xdc>
mov    (%r15),%rax
mov    0x2da9(%rip),%rcx
cmp    %rax,(%rcx)
je     1210 <clamd_send+0xb0>
mov    %r14,%rdi
call   1040 <strlen@plt>
xor    %ecx,%ecx
cmp    %eax,%ebx
setl   %cl
neg    %ecx
jmp    1254 <clamd_send+0xf4>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @clamd_send(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %63
12: ; preds = %2
br label %13
13: ; preds = %29, %12
%14 = call i32 @FD_ZERO(i32* %7)
%15 = load i32, i32* %4
%16 = call i32 @FD_SET(i32 %15, i32* %7)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 60, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
%21 = call i32 @select(i32 %20, i32* null, i32* %7, i32* null, %struct.timeval* %8)
store i32 %21, i32* %6
br label %22
22: ; preds = %13
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br label %29
29: ; preds = %25, %22
%30 = phi i1 [ false, %22 ], [ %28, %25 ]
br i1 %30, label %13, label %31
31: ; preds = %29
%32 = load i32, i32* %6
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 -1, i32* %3
br label %63
35: ; preds = %31
%36 = load i32, i32* %4
%37 = call i32 @FD_ISSET(i32 %36, i32* %7)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %35
store i32 -1, i32* %3
br label %63
40: ; preds = %35
br label %41
41: ; preds = %54, %40
%42 = load i32, i32* %4
%43 = load i8*, i8** %5
%44 = load i8*, i8** %5
%45 = call i32 @strlen(i8* %44)
%46 = call i32 @send(i32 %42, i8* %43, i32 %45, i32 0)
store i32 %46, i32* %6
br label %47
47: ; preds = %41
%48 = load i32, i32* %6
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp eq i64 %51, %52
br label %54
54: ; preds = %50, %47
%55 = phi i1 [ false, %47 ], [ %53, %50 ]
br i1 %55, label %41, label %56
56: ; preds = %54
%57 = load i32, i32* %6
%58 = load i8*, i8** %5
%59 = call i32 @strlen(i8* %58)
%60 = icmp slt i32 %57, %59
br i1 %60, label %61, label %62
61: ; preds = %56
store i32 -1, i32* %3
br label %63
62: ; preds = %56
store i32 0, i32* %3
br label %63
63: ; preds = %62, %61, %39, %34, %11
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @clamd_send(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = icmp slt i32 %0, 0
br i1 %7, label %38, label %8
8: ; preds = %2
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%11 = add nuw nsw i32 %0, 1
br label %12
12: ; preds = %8, %12
%13 = call i32 @FD_ZERO(i32* %3)
%14 = call i32 @FD_SET(i32 %0, i32* %3)
store i32 60, i32* %9
store i64 0, i64* %10
%15 = call i32 @select(i32 %11, i32* null, i32* %3, i32* null, %struct.timeval* %4)
%16 = icmp slt i32 %15, 0
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
%20 = select i1 %16, i1 %19, i1 false
br i1 %20, label %12, label %21
21: ; preds = %12
%22 = icmp slt i32 %15, 1
br i1 %22, label %38, label %23
23: ; preds = %21
%24 = call i32 @FD_ISSET(i32 %0, i32* %3)
%25 = icmp eq i32 %24, 0
br i1 %25, label %38, label %26
26: ; preds = %23, %26
%27 = call i32 @strlen(i8* %1)
%28 = call i32 @send(i32 %0, i8* %1, i32 %27, i32 0)
%29 = icmp slt i32 %28, 0
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
%33 = select i1 %29, i1 %32, i1 false
br i1 %33, label %26, label %34
34: ; preds = %26
%35 = call i32 @strlen(i8* %1)
%36 = icmp slt i32 %28, %35
%37 = sext i1 %36 to i32
br label %38
38: ; preds = %34, %23, %21, %2
%39 = phi i32 [ -1, %2 ], [ -1, %21 ], [ -1, %23 ], [ %37, %34 ]
ret i32 %39
}