#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 */
typedef int /*<<< orphan*/ uint8_t;
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
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*/ *);
int /*<<< orphan*/ MSG_DONTWAIT;
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int sendto(int, int /*<<< orphan*/ *, size_t, int /*<<< orphan*/,
           struct sockaddr *, int);
int sendto_u(int sock, uint8_t *buf, size_t buflen, struct timeval wait,
             struct sockaddr_in *to) {
  int bytes = 0;
  int ret = -1;
  fd_set wset;
  struct timeval tv;
  static socklen_t svrlen = sizeof(*to);
  if (wait.tv_sec && wait.tv_usec) {
    tv.tv_sec = wait.tv_sec;
    tv.tv_usec = wait.tv_usec;
  RE_SEND:
    FD_ZERO(&wset);
    FD_SET(sock, &wset);
    ret = select(sock + 1, (fd_set *)0, &wset, (fd_set *)0, &tv);
    if (ret == -1) {
      if (errno == EINTR) {
        goto RE_SEND;
      }
      return -1;
    } else if (ret == 0) {
      return 0;
    }
    if (!FD_ISSET(sock, &wset)) {
      return -1;
    }
    bytes =
        sendto(sock, buf, buflen, MSG_DONTWAIT, (struct sockaddr *)to, svrlen);
    if (bytes <= 0) {
      return -1;
    }
    return bytes;
  }
  bytes = sendto(sock, buf, buflen, 0, (struct sockaddr *)to, svrlen);
  if (bytes <= 0) {
    return -1;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<sendto_u>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x10(%rbp)
mov    %edi,-0x1c(%rbp)
mov    %rsi,-0x28(%rbp)
mov    %rdx,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0xffffffff,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     128e <sendto_u+0x13e>
cmpq   $0x0,-0x18(%rbp)
je     128e <sendto_u+0x13e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x44(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x44(%rbp),%rdx
lea    -0x58(%rbp),%r8
mov    %rcx,%rsi
call   1060 <select@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    120a <sendto_u+0xba>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <sendto_u+0xae>
jmp    11a3 <sendto_u+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <sendto_u+0x17a>
cmpl   $0x0,-0x40(%rbp)
jne    1220 <sendto_u+0xd0>
movl   $0x0,-0x4(%rbp)
jmp    12ca <sendto_u+0x17a>
jmp    1225 <sendto_u+0xd5>
mov    -0x1c(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1246 <sendto_u+0xf6>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <sendto_u+0x17a>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    0x2d80(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%r8
mov    0x2de3(%rip),%r9d
call   1040 <sendto@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     1283 <sendto_u+0x133>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <sendto_u+0x17a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ca <sendto_u+0x17a>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%r8
mov    0x2da4(%rip),%r9d
xor    %ecx,%ecx
call   1040 <sendto@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     12c4 <sendto_u+0x174>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <sendto_u+0x17a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendto_u>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%r13d
test   %r8,%r8
je     11d4 <sendto_u+0x84>
test   %rcx,%rcx
je     11d4 <sendto_u+0x84>
mov    %rsi,0x10(%rsp)
mov    %rdx,0x18(%rsp)
mov    %r9,0x20(%rsp)
mov    %r8,0x30(%rsp)
mov    %rcx,0x28(%rsp)
lea    0x1(%r13),%ebp
lea    0xc(%rsp),%rbx
lea    0x28(%rsp),%r14
mov    0x2e47(%rip),%r15
mov    0x2e30(%rip),%r12
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
mov    %ebp,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %r14,%r8
call   1060 <select@plt>
cmp    $0xffffffff,%eax
jne    11f5 <sendto_u+0xa5>
mov    (%r15),%rax
cmp    (%r12),%rax
je     11a0 <sendto_u+0x50>
jmp    1227 <sendto_u+0xd7>
mov    %r13d,%edi
xor    %ecx,%ecx
mov    %r9,%r8
mov    $0x4,%r9d
call   1040 <sendto@plt>
mov    %eax,%ecx
test   %eax,%eax
mov    $0xffffffff,%eax
cmovg  %ecx,%eax
jmp    122c <sendto_u+0xdc>
test   %eax,%eax
je     122c <sendto_u+0xdc>
lea    0xc(%rsp),%rsi
mov    %r13d,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
je     1227 <sendto_u+0xd7>
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
mov    %r13d,%edi
mov    0x10(%rsp),%rsi
mov    0x18(%rsp),%rdx
mov    0x20(%rsp),%r8
jmp    11dc <sendto_u+0x8c>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@sendto_u.svrlen = internal global i32 4
@errno = global i64 0
@EINTR = global i64 0
@MSG_DONTWAIT = global i32 0
define i32 @sendto_u(i32 %0, i32* %1, i64 %2, i64 %3, i64 %4, %struct.sockaddr_in* %5) {
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i64
%12 = alloca %struct.sockaddr_in*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
%17 = bitcast %struct.timeval* %8 to { i64, i64 }*
%18 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %17, i32 0, i32 0
store i64 %3, i64* %18
%19 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %17, i32 0, i32 1
store i64 %4, i64* %19
store i32 %0, i32* %9
store i32* %1, i32** %10
store i64 %2, i64* %11
store %struct.sockaddr_in* %5, %struct.sockaddr_in** %12
store i32 0, i32* %13
store i32 -1, i32* %14
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%21 = load i64, i64* %20
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %73
23: ; preds = %6
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%25 = load i64, i64* %24
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %73
27: ; preds = %23
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%29 = load i64, i64* %28
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i64 %29, i64* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%32 = load i64, i64* %31
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i64 %32, i64* %33
br label %34
34: ; preds = %47, %27
%35 = call i32 @FD_ZERO(i32* %15)
%36 = load i32, i32* %9
%37 = call i32 @FD_SET(i32 %36, i32* %15)
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
%40 = call i32 @select(i32 %39, i32* null, i32* %15, i32* null, %struct.timeval* %16)
store i32 %40, i32* %14
%41 = load i32, i32* %14
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %49
43: ; preds = %34
%44 = load i64, i64* @errno
%45 = load i64, i64* @EINTR
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
br label %34
48: ; preds = %43
store i32 -1, i32* %7
br label %86
49: ; preds = %34
%50 = load i32, i32* %14
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %49
store i32 0, i32* %7
br label %86
53: ; preds = %49
br label %54
54: ; preds = %53
%55 = load i32, i32* %9
%56 = call i32 @FD_ISSET(i32 %55, i32* %15)
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %54
store i32 -1, i32* %7
br label %86
59: ; preds = %54
%60 = load i32, i32* %9
%61 = load i32*, i32** %10
%62 = load i64, i64* %11
%63 = load i32, i32* @MSG_DONTWAIT
%64 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%65 = bitcast %struct.sockaddr_in* %64 to %struct.sockaddr*
%66 = load i32, i32* @sendto_u.svrlen
%67 = call i32 @sendto(i32 %60, i32* %61, i64 %62, i32 %63, %struct.sockaddr* %65, i32 %66)
store i32 %67, i32* %13
%68 = load i32, i32* %13
%69 = icmp sle i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %59
store i32 -1, i32* %7
br label %86
71: ; preds = %59
%72 = load i32, i32* %13
store i32 %72, i32* %7
br label %86
73: ; preds = %23, %6
%74 = load i32, i32* %9
%75 = load i32*, i32** %10
%76 = load i64, i64* %11
%77 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%78 = bitcast %struct.sockaddr_in* %77 to %struct.sockaddr*
%79 = load i32, i32* @sendto_u.svrlen
%80 = call i32 @sendto(i32 %74, i32* %75, i64 %76, i32 0, %struct.sockaddr* %78, i32 %79)
store i32 %80, i32* %13
%81 = load i32, i32* %13
%82 = icmp sle i32 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %73
store i32 -1, i32* %7
br label %86
84: ; preds = %73
%85 = load i32, i32* %13
store i32 %85, i32* %7
br label %86
86: ; preds = %84, %83, %71, %70, %58, %52, %48
%87 = load i32, i32* %7
ret i32 %87
}

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