#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 {
  unsigned int tv_sec;
  scalar_t__ tv_usec;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fd_clear_nonblocking(int, int);
int fd_get_flags(int);
scalar_t__ fd_set_nonblocking(int, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int write(int, void const *, size_t);
ssize_t timed_write(int fd, const void *buf, size_t size,
                    unsigned int timeout) {
  int ret;
  int flags;
  fd_set set;
  size_t have;
  ssize_t count;
  struct timeval wait_time;
  flags = fd_get_flags(fd);
  if (flags == -1)
    return -1;
  if (fd_set_nonblocking(fd, flags))
    return -1;
  if (!timeout)
    timeout = (unsigned int)-1;
  have = 0;
  for (;;) {
    FD_ZERO(&set);
    FD_SET(fd, &set);
    wait_time.tv_sec = timeout;
    wait_time.tv_usec = 0;
    ret = select(fd + 1, NULL, &set, NULL, &wait_time);
    if (ret == 0) {
      if (!have) {
        errno = ETIMEDOUT;
        have = -1;
      }
      break;
    } else if (ret == -1) {
      if (errno == EINTR) {
        continue;
      } else {
        have = -1;
        break;
      }
    } else {
      count = write(fd, buf + have, size - have);
      if (count == -1) {
        if (errno == EINTR || errno == EAGAIN) {
          continue;
        } else {
          have = -1;
          break;
        }
      } else if (count == 0) {
        break;
      } else {
        have += (size_t)count;
      }
    }
  }
  (void)fd_clear_nonblocking(fd, flags);
  return have;
}


x86 assembly (clang, -O0):
<timed_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <fd_get_flags@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11a7 <timed_write+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <timed_write+0x1c9>
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1040 <fd_set_nonblocking@plt>
cmp    $0x0,%rax
je     11c8 <timed_write+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <timed_write+0x1c9>
cmpl   $0x0,-0x1c(%rbp)
jne    11d9 <timed_write+0x69>
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%rdx
lea    -0x48(%rbp),%r8
mov    %rcx,%rsi
call   1070 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1257 <timed_write+0xe7>
cmpq   $0x0,-0x30(%rbp)
jne    1252 <timed_write+0xe2>
mov    0x2d9b(%rip),%rax
mov    (%rax),%rcx
mov    0x2d99(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
jmp    1327 <timed_write+0x1b7>
cmpl   $0xffffffff,-0x20(%rbp)
jne    128d <timed_write+0x11d>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
jne    1280 <timed_write+0x110>
jmp    11e1 <timed_write+0x71>
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
jmp    1327 <timed_write+0x1b7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12f8 <timed_write+0x188>
mov    0x2d27(%rip),%rax
mov    (%rax),%rax
mov    0x2d0d(%rip),%rcx
cmp    (%rcx),%rax
je     12e6 <timed_write+0x176>
mov    0x2d0d(%rip),%rax
mov    (%rax),%rax
mov    0x2ce3(%rip),%rcx
cmp    (%rcx),%rax
jne    12eb <timed_write+0x17b>
jmp    11e1 <timed_write+0x71>
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
jmp    1327 <timed_write+0x1b7>
cmpl   $0x0,-0x34(%rbp)
jne    1307 <timed_write+0x197>
jmp    1327 <timed_write+0x1b7>
movslq -0x34(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1318 <timed_write+0x1a8>
jmp    131d <timed_write+0x1ad>
jmp    1322 <timed_write+0x1b2>
jmp    11e1 <timed_write+0x71>
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1090 <fd_clear_nonblocking@plt>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timed_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r14d
mov    %rdx,%r12
mov    %rsi,%r13
mov    %edi,%ebx
call   1050 <fd_get_flags@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     12dc <timed_write+0x16c>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <fd_set_nonblocking@plt>
test   %rax,%rax
jne    12dc <timed_write+0x16c>
mov    %r13,0x18(%rsp)
mov    %ebp,0x4(%rsp)
cmp    $0x1,%r14d
sbb    %r13d,%r13d
or     %r14d,%r13d
lea    0x1(%rbx),%ebp
xor    %ecx,%ecx
lea    0x24(%rsp),%r15
mov    %r12,0x10(%rsp)
mov    0x18(%rsp),%rax
lea    (%rax,%rcx,1),%r14
mov    %rcx,0x8(%rsp)
sub    %rcx,%r12
jmp    120a <timed_write+0x9a>
cs nopw 0x0(%rax,%rax,1)
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
jne    1294 <timed_write+0x124>
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
mov    %r13d,0x28(%rsp)
movq   $0x0,0x30(%rsp)
mov    %ebp,%edi
xor    %esi,%esi
mov    %r15,%rdx
xor    %ecx,%ecx
lea    0x28(%rsp),%r8
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     11f0 <timed_write+0x80>
test   %eax,%eax
je     12a1 <timed_write+0x131>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    127c <timed_write+0x10c>
mov    0x2d81(%rip),%rax
mov    (%rax),%rax
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%rax
je     120a <timed_write+0x9a>
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
je     120a <timed_write+0x9a>
jmp    1294 <timed_write+0x124>
test   %eax,%eax
je     12cc <timed_write+0x15c>
cltq
mov    0x8(%rsp),%rcx
add    %rax,%rcx
mov    0x10(%rsp),%r12
jmp    11d3 <timed_write+0x63>
mov    $0xffffffffffffffff,%r15
mov    0x4(%rsp),%esi
jmp    12d5 <timed_write+0x165>
mov    0x8(%rsp),%r15
test   %r15,%r15
mov    0x4(%rsp),%esi
jne    12d5 <timed_write+0x165>
mov    0x2d22(%rip),%rax
mov    (%rax),%rax
mov    0x2d20(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffffffffffff,%r15
jmp    12d5 <timed_write+0x165>
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%r15
mov    %ebx,%edi
call   1090 <fd_clear_nonblocking@plt>
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@ETIMEDOUT = global i64 0
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @timed_write(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca i32
%15 = alloca %struct.timeval
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%16 = load i32, i32* %6
%17 = call i32 @fd_get_flags(i32 %16)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 -1, i32* %5
br label %99
21: ; preds = %4
%22 = load i32, i32* %6
%23 = load i32, i32* %11
%24 = call i64 @fd_set_nonblocking(i32 %22, i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -1, i32* %5
br label %99
27: ; preds = %21
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %27
store i32 -1, i32* %9
br label %31
31: ; preds = %30, %27
store i64 0, i64* %13
br label %32
32: ; preds = %92, %78, %57, %31
%33 = call i32 @FD_ZERO(i32* %12)
%34 = load i32, i32* %6
%35 = call i32 @FD_SET(i32 %34, i32* %12)
%36 = load i32, i32* %9
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i64 0, i64* %38
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
%41 = call i32 @select(i32 %40, i32* null, i32* %12, i32* null, %struct.timeval* %15)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %32
%45 = load i64, i64* %13
%46 = icmp ne i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = load i64, i64* @ETIMEDOUT
store i64 %48, i64* @errno
store i64 -1, i64* %13
br label %49
49: ; preds = %47, %44
br label %93
50: ; preds = %32
%51 = load i32, i32* %10
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %59
53: ; preds = %50
%54 = load i64, i64* @errno
%55 = load i64, i64* @EINTR
%56 = icmp eq i64 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %53
br label %32
58: ; preds = %53
store i64 -1, i64* %13
br label %93
59: ; preds = %50
%60 = load i32, i32* %6
%61 = load i8*, i8** %7
%62 = load i64, i64* %13
%63 = getelementptr i8, i8* %61, i64 %62
%64 = load i64, i64* %8
%65 = load i64, i64* %13
%66 = sub i64 %64, %65
%67 = call i32 @write(i32 %60, i8* %63, i64 %66)
store i32 %67, i32* %14
%68 = load i32, i32* %14
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %80
70: ; preds = %59
%71 = load i64, i64* @errno
%72 = load i64, i64* @EINTR
%73 = icmp eq i64 %71, %72
br i1 %73, label %78, label %74
74: ; preds = %70
%75 = load i64, i64* @errno
%76 = load i64, i64* @EAGAIN
%77 = icmp eq i64 %75, %76
br i1 %77, label %78, label %79
78: ; preds = %74, %70
br label %32
79: ; preds = %74
store i64 -1, i64* %13
br label %93
80: ; preds = %59
%81 = load i32, i32* %14
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %80
br label %93
84: ; preds = %80
%85 = load i32, i32* %14
%86 = sext i32 %85 to i64
%87 = load i64, i64* %13
%88 = add i64 %87, %86
store i64 %88, i64* %13
br label %89
89: ; preds = %84
br label %90
90: ; preds = %89
br label %91
91: ; preds = %90
br label %92
92: ; preds = %91
br label %32
93: ; preds = %83, %79, %58, %49
%94 = load i32, i32* %6
%95 = load i32, i32* %11
%96 = call i32 @fd_clear_nonblocking(i32 %94, i32 %95)
%97 = load i64, i64* %13
%98 = trunc i64 %97 to i32
store i32 %98, i32* %5
br label %99
99: ; preds = %93, %26, %20
%100 = load i32, i32* %5
ret i32 %100
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@ETIMEDOUT = global i64 0
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @timed_write(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = tail call i32 @fd_get_flags(i32 %0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %53, label %11
11: ; preds = %4
%12 = tail call i64 @fd_set_nonblocking(i32 %0, i32 %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %53
14: ; preds = %11
%15 = icmp eq i32 %3, 0
%16 = select i1 %15, i32 -1, i32 %3
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%19 = add nsw i32 %0, 1
br label %20
20: ; preds = %46, %14
%21 = phi i64 [ %48, %46 ], [ 0, %14 ]
%22 = getelementptr i8, i8* %1, i64 %21
%23 = sub i64 %2, %21
br label %24
24: ; preds = %36, %20
%25 = call i32 @FD_ZERO(i32* %5)
%26 = call i32 @FD_SET(i32 %0, i32* %5)
store i32 %16, i32* %17
store i64 0, i64* %18
%27 = call i32 @select(i32 %19, i32* null, i32* %5, i32* null, %struct.timeval* %6)
switch i32 %27, label %37 [
i32 0, label %28
i32 -1, label %32
]
28: ; preds = %24
%29 = icmp eq i64 %21, 0
br i1 %29, label %30, label %49
30: ; preds = %28
%31 = load i64, i64* @ETIMEDOUT
store i64 %31, i64* @errno
br label %49
32: ; preds = %24
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %49
36: ; preds = %32, %39
br label %24
37: ; preds = %24
%38 = call i32 @write(i32 %0, i8* %22, i64 %23)
switch i32 %38, label %46 [
i32 -1, label %39
i32 0, label %49
]
39: ; preds = %37
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
%43 = load i64, i64* @EAGAIN
%44 = icmp eq i64 %40, %43
%45 = select i1 %42, i1 true, i1 %44
br i1 %45, label %36, label %49
46: ; preds = %37
%47 = sext i32 %38 to i64
%48 = add i64 %21, %47
br label %20
49: ; preds = %37, %39, %32, %28, %30
%50 = phi i64 [ %21, %28 ], [ -1, %30 ], [ %21, %37 ], [ -1, %39 ], [ -1, %32 ]
%51 = call i32 @fd_clear_nonblocking(i32 %0, i32 %9)
%52 = trunc i64 %50 to i32
br label %53
53: ; preds = %11, %4, %49
%54 = phi i32 [ %52, %49 ], [ -1, %4 ], [ -1, %11 ]
ret i32 %54
}