#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 member_0;
  int /*<<< orphan*/ member_1;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDTIMEO;
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int send_nonb(int sock, char *buff, int size) {
  struct timeval timeout = {5, 0};
  setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
             sizeof(struct timeval));
  while (size > 0) {
    int send_size = send(sock, buff, size, 0);
    if (send_size <= 0)
      return -1;
    buff += send_size;
    size -= send_size;
  }
  return size;
}


x86 assembly (clang, -O0):
<send_nonb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0xec7(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    0x2ea0(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmpl   $0x0,-0x14(%rbp)
jle    11b6 <send_nonb+0x96>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1195 <send_nonb+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <send_nonb+0x9c>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1161 <send_nonb+0x41>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_nonb>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
movq   $0x5,0x8(%rsp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
mov    0x2ea8(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
nopw   0x0(%rax,%rax,1)
test   %ebx,%ebx
jle    1182 <send_nonb+0x62>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cltq
add    %rax,%r14
sub    %eax,%ebx
test   %eax,%eax
jg     1160 <send_nonb+0x40>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.send_nonb.timeout = %struct.timeval { i32 5, i32 0 }
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
define i32 @send_nonb(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = bitcast %struct.timeval* %8 to i8*
%11 = load i32, i32* %5
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_SNDTIMEO
%14 = bitcast %struct.timeval* %8 to i8*
%15 = call i32 @setsockopt(i32 %11, i32 %12, i32 %13, i8* %14, i32 8)
br label %16
16: ; preds = %27, %3
%17 = load i32, i32* %7
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %16
%20 = load i32, i32* %5
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = call i32 @send(i32 %20, i8* %21, i32 %22, i32 0)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -1, i32* %4
br label %37
27: ; preds = %19
%28 = load i32, i32* %9
%29 = load i8*, i8** %6
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %6
%32 = load i32, i32* %9
%33 = load i32, i32* %7
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %7
br label %16
35: ; preds = %16
%36 = load i32, i32* %7
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %26
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
define i32 @send_nonb(i32 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 5, i64* %4
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_SNDTIMEO
%8 = call i32 @setsockopt(i32 %0, i32 %6, i32 %7, i8* %5, i32 8)
br label %9
9: ; preds = %13, %3
%10 = phi i8* [ %1, %3 ], [ %17, %13 ]
%11 = phi i32 [ %2, %3 ], [ %18, %13 ]
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = call i32 @send(i32 %0, i8* %10, i32 %11, i32 0)
%15 = icmp slt i32 %14, 1
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds i8, i8* %10, i64 %16
%18 = sub nsw i32 %11, %14
br i1 %15, label %19, label %9
19: ; preds = %9, %13
%20 = phi i32 [ -1, %13 ], [ %11, %9 ]
ret i32 %20
}