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

/* Variables and functions */
scalar_t__ EINTR;
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 write(int, char *, int);
int xyz_sock_write_to(int sockfd, char *data, int len, int usec) {
  fd_set wtset;
  FD_ZERO(&wtset);
  FD_SET(sockfd, &wtset);
  struct timeval tv;
  tv.tv_sec = usec / 1000000;
  tv.tv_usec = usec % 1000000;
  if (sockfd >= 1024) {
    return write(sockfd, data, len);
  }
  int n = select(sockfd + 1, NULL, &wtset, NULL, &tv);
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return write(sockfd, data, len);
  }
  if (n == -1) {
    if (errno == EINTR) {
      return 0;
    } else {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<xyz_sock_write_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x18(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x24(%rbp)
cmpl   $0x400,-0x8(%rbp)
jl     11aa <xyz_sock_write_to+0x6a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x4(%rbp)
jmp    1241 <xyz_sock_write_to+0x101>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x1c(%rbp),%rdx
lea    -0x28(%rbp),%r8
mov    %rcx,%rsi
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11dd <xyz_sock_write_to+0x9d>
movl   $0x0,-0x4(%rbp)
jmp    1241 <xyz_sock_write_to+0x101>
cmpl   $0x1,-0x2c(%rbp)
jne    11fe <xyz_sock_write_to+0xbe>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x4(%rbp)
jmp    1241 <xyz_sock_write_to+0x101>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    123a <xyz_sock_write_to+0xfa>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
jne    122e <xyz_sock_write_to+0xee>
movl   $0x0,-0x4(%rbp)
jmp    1241 <xyz_sock_write_to+0x101>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <xyz_sock_write_to+0x101>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xyz_sock_write_to>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
movslq %ebx,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0xc(%rsp)
cmp    $0x400,%ebp
jge    11be <xyz_sock_write_to+0x7e>
lea    0x1(%rbp),%edi
xor    %ebx,%ebx
lea    0x4(%rsp),%rdx
lea    0x8(%rsp),%r8
xor    %esi,%esi
xor    %ecx,%ecx
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     11cf <xyz_sock_write_to+0x8f>
cmp    $0x1,%eax
jne    11ea <xyz_sock_write_to+0xaa>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
jmp    11ea <xyz_sock_write_to+0xaa>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    0x2df8(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
setne  %bl
neg    %ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @xyz_sock_write_to(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 i32
%11 = alloca %struct.timeval
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = call i32 @FD_ZERO(i32* %10)
%14 = load i32, i32* %6
%15 = call i32 @FD_SET(i32 %14, i32* %10)
%16 = load i32, i32* %9
%17 = sdiv i32 %16, 1000000
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %9
%20 = srem i32 %19, 1000000
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* %6
%23 = icmp sge i32 %22, 1024
br i1 %23, label %24, label %29
24: ; preds = %4
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = load i32, i32* %8
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
store i32 %28, i32* %5
br label %54
29: ; preds = %4
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = call i32 @select(i32 %31, i32* null, i32* %10, i32* null, %struct.timeval* %11)
store i32 %32, i32* %12
%33 = load i32, i32* %12
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 0, i32* %5
br label %54
36: ; preds = %29
%37 = load i32, i32* %12
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* %6
%41 = load i8*, i8** %7
%42 = load i32, i32* %8
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
store i32 %43, i32* %5
br label %54
44: ; preds = %36
%45 = load i32, i32* %12
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i64, i64* @errno
%49 = load i64, i64* @EINTR
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 0, i32* %5
br label %54
52: ; preds = %47
store i32 -1, i32* %5
br label %54
53: ; preds = %44
store i32 0, i32* %5
br label %54
54: ; preds = %53, %52, %51, %39, %35, %24
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @xyz_sock_write_to(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = call i32 @FD_ZERO(i32* %5)
%9 = call i32 @FD_SET(i32 %0, i32* %5)
%10 = bitcast %struct.timeval* %6 to i8*
%11 = sdiv i32 %3, 1000000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %11, i32* %12
%13 = srem i32 %3, 1000000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i32 %13, i32* %14
%15 = icmp sgt i32 %0, 1023
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @write(i32 %0, i8* %1, i32 %2)
br label %28
18: ; preds = %4
%19 = add nsw i32 %0, 1
%20 = call i32 @select(i32 %19, i32* null, i32* %5, i32* null, %struct.timeval* %6)
switch i32 %20, label %28 [
i32 -1, label %23
i32 1, label %21
]
21: ; preds = %18
%22 = call i32 @write(i32 %0, i8* %1, i32 %2)
br label %28
23: ; preds = %18
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp ne i64 %24, %25
%27 = sext i1 %26 to i32
br label %28
28: ; preds = %21, %23, %18, %16
%29 = phi i32 [ %17, %16 ], [ %22, %21 ], [ %27, %23 ], [ 0, %18 ]
ret i32 %29
}