#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 */

/* Variables and functions */
int EAGAIN;
int EINTR;
int ETIMEDOUT;
int errno;
int write(int, void const *, int);
scalar_t__ ztimed_write_wait_millisecond(int, long);
int ztimed_write(int fd, const void *buf, int size, int write_wait_timeout) {
  int ret, ec;
  if (write_wait_timeout == 0) {
    for (;;) {
      ret = write(fd, buf, size);
      if (ret < 0) {
        if (errno == EINTR) {
          continue;
        }
      }
      return ret;
    }
  }
  for (;;) {
    if (ztimed_write_wait_millisecond(fd, 1000L * write_wait_timeout) == 0) {
      errno = ETIMEDOUT;
      return -1;
    }
    ret = write(fd, buf, size);
    if (ret < 0) {
      ec = errno;
      if (ec == EINTR) {
        continue;
      }
      if (ec == EAGAIN) {
        continue;
      }
      return -1;
    } else if (ret == 0) {
      continue;
    }
    return ret;
  }
  errno = ETIMEDOUT;
  return -1;
}


x86 assembly (clang, -O0):
<ztimed_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    118d <ztimed_write+0x6d>
jmp    1144 <ztimed_write+0x24>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1182 <ztimed_write+0x62>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%eax
jne    117d <ztimed_write+0x5d>
jmp    1144 <ztimed_write+0x24>
jmp    1182 <ztimed_write+0x62>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <ztimed_write+0x129>
jmp    1192 <ztimed_write+0x72>
mov    -0x8(%rbp),%edi
movslq -0x18(%rbp),%rax
imul   $0x3e8,%rax,%rsi
call   1040 <ztimed_write_wait_millisecond@plt>
cmp    $0x0,%rax
jne    11cd <ztimed_write+0xad>
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
mov    0x2e21(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <ztimed_write+0x129>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122f <ztimed_write+0x10f>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%eax
jne    120c <ztimed_write+0xec>
jmp    1192 <ztimed_write+0x72>
mov    -0x20(%rbp),%eax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%eax
jne    1223 <ztimed_write+0x103>
jmp    1192 <ztimed_write+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <ztimed_write+0x129>
cmpl   $0x0,-0x1c(%rbp)
jne    123e <ztimed_write+0x11e>
jmp    1192 <ztimed_write+0x72>
jmp    1243 <ztimed_write+0x123>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ztimed_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
test   %ecx,%ecx
je     11cb <ztimed_write+0xab>
movslq %ecx,%rax
imul   $0x3e8,%rax,%r15
mov    %ebp,%edi
mov    %r15,%rsi
call   1040 <ztimed_write_wait_millisecond@plt>
test   %rax,%rax
je     11b2 <ztimed_write+0x92>
mov    0x2e85(%rip),%r12
mov    0x2e6e(%rip),%r13
jmp    1185 <ztimed_write+0x65>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
jne    11fa <ztimed_write+0xda>
mov    %ebp,%edi
mov    %r15,%rsi
call   1040 <ztimed_write_wait_millisecond@plt>
test   %rax,%rax
je     11b2 <ztimed_write+0x92>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1170 <ztimed_write+0x50>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     1176 <ztimed_write+0x56>
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%eax
je     1176 <ztimed_write+0x56>
mov    $0xffffffff,%eax
jmp    11fa <ztimed_write+0xda>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    0x2e1e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    11fa <ztimed_write+0xda>
mov    0x2e0e(%rip),%r15
mov    0x2df7(%rip),%r12
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11fa <ztimed_write+0xda>
mov    (%r15),%ecx
cmp    (%r12),%ecx
je     11e0 <ztimed_write+0xc0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
@ETIMEDOUT = global i32 0
@EAGAIN = global i32 0
define i32 @ztimed_write(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 i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %4
br label %15
15: ; preds = %26, %14
%16 = load i32, i32* %6
%17 = load i8*, i8** %7
%18 = load i32, i32* %8
%19 = call i32 @write(i32 %16, i8* %17, i32 %18)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %15
%23 = load i32, i32* @errno
%24 = load i32, i32* @EINTR
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %15
27: ; preds = %22
br label %28
28: ; preds = %27, %15
%29 = load i32, i32* %10
store i32 %29, i32* %5
br label %66
30: ; preds = %4
br label %31
31: ; preds = %62, %57, %52, %30
%32 = load i32, i32* %6
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = mul nsw i64 1000, %34
%36 = call i64 @ztimed_write_wait_millisecond(i32 %32, i64 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = load i32, i32* @ETIMEDOUT
store i32 %39, i32* @errno
store i32 -1, i32* %5
br label %66
40: ; preds = %31
%41 = load i32, i32* %6
%42 = load i8*, i8** %7
%43 = load i32, i32* %8
%44 = call i32 @write(i32 %41, i8* %42, i32 %43)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %59
47: ; preds = %40
%48 = load i32, i32* @errno
store i32 %48, i32* %11
%49 = load i32, i32* %11
%50 = load i32, i32* @EINTR
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %53
52: ; preds = %47
br label %31
53: ; preds = %47
%54 = load i32, i32* %11
%55 = load i32, i32* @EAGAIN
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %53
br label %31
58: ; preds = %53
store i32 -1, i32* %5
br label %66
59: ; preds = %40
%60 = load i32, i32* %10
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %59
br label %31
63: ; preds = %59
br label %64
64: ; preds = %63
%65 = load i32, i32* %10
store i32 %65, i32* %5
br label %66
66: ; preds = %64, %58, %38, %28
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@ETIMEDOUT = global i32 0
@EAGAIN = global i32 0
define i32 @ztimed_write(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %11, label %6
6: ; preds = %4
%7 = sext i32 %3 to i64
%8 = mul nsw i64 %7, 1000
%9 = tail call i64 @ztimed_write_wait_millisecond(i32 %0, i64 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %18, label %20
11: ; preds = %4, %14
%12 = tail call i32 @write(i32 %0, i8* %1, i32 %2)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %35
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = load i32, i32* @EINTR
%17 = icmp eq i32 %15, %16
br i1 %17, label %11, label %35
18: ; preds = %30, %6
%19 = load i32, i32* @ETIMEDOUT
store i32 %19, i32* @errno
br label %35
20: ; preds = %6, %30
%21 = tail call i32 @write(i32 %0, i8* %1, i32 %2)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = load i32, i32* @EINTR
%26 = icmp eq i32 %24, %25
%27 = load i32, i32* @EAGAIN
%28 = icmp eq i32 %24, %27
%29 = select i1 %26, i1 true, i1 %28
br i1 %29, label %30, label %35
30: ; preds = %23, %33
%31 = tail call i64 @ztimed_write_wait_millisecond(i32 %0, i64 %8)
%32 = icmp eq i64 %31, 0
br i1 %32, label %18, label %20
33: ; preds = %20
%34 = icmp eq i32 %21, 0
br i1 %34, label %30, label %35
35: ; preds = %33, %23, %11, %14, %18
%36 = phi i32 [ -1, %18 ], [ %12, %14 ], [ %12, %11 ], [ %21, %33 ], [ -1, %23 ]
ret i32 %36
}