#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 time_t;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ ETIMEDOUT;
int aeWait(int, int, int);
int /*<<< orphan*/ errno;
int time(int /*<<< orphan*/ *);
int write(int, void *, int);
int syncWrite(int fd, void *ptr, ssize_t size, int timeout) {
  ssize_t nwritten, ret = size;
  time_t start = time(NULL);
  timeout++;
  while (size) {
    if (aeWait(fd, 2, 1000) & 2) {
      nwritten = write(fd, ptr, size);
      if (nwritten == -1)
        return -1;
      ptr += nwritten;
      size -= nwritten;
    }
    if ((time(NULL) - start) > timeout) {
      errno = ETIMEDOUT;
      return -1;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<syncWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1204 <syncWrite+0xd4>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
mov    $0x3e8,%edx
call   1050 <aeWait@plt>
and    $0x2,%eax
cmp    $0x0,%eax
je     11cc <syncWrite+0x9c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b0 <syncWrite+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <syncWrite+0xda>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
sub    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11ff <syncWrite+0xcf>
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    0x2def(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <syncWrite+0xda>
jmp    1160 <syncWrite+0x30>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<syncWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%r13
mov    %edi,%ebp
xor    %edi,%edi
call   1040 <time@plt>
mov    %eax,%r12d
add    $0x1,%r15d
mov    %r14d,%ebx
jmp    116f <syncWrite+0x3f>
nopl   0x0(%rax)
xor    %edi,%edi
call   1040 <time@plt>
sub    %r12d,%eax
cmp    %r15d,%eax
jg     11a3 <syncWrite+0x73>
test   %ebx,%ebx
je     11bb <syncWrite+0x8b>
mov    %ebp,%edi
mov    $0x2,%esi
mov    $0x3e8,%edx
call   1050 <aeWait@plt>
test   $0x2,%al
je     1160 <syncWrite+0x30>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11b5 <syncWrite+0x85>
movslq %eax,%rcx
add    %rcx,%r13
sub    %eax,%ebx
jmp    1160 <syncWrite+0x30>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    0x2e2d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @syncWrite(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
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* %8
store i32 %13, i32* %11
%14 = call i32 @time(i32* null)
store i32 %14, i32* %12
%15 = load i32, i32* %9
%16 = add nsw i32 %15, 1
store i32 %16, i32* %9
br label %17
17: ; preds = %49, %4
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %50
20: ; preds = %17
%21 = load i32, i32* %6
%22 = call i32 @aeWait(i32 %21, i32 2, i32 1000)
%23 = and i32 %22, 2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %41
25: ; preds = %20
%26 = load i32, i32* %6
%27 = load i8*, i8** %7
%28 = load i32, i32* %8
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %5
br label %52
33: ; preds = %25
%34 = load i32, i32* %10
%35 = load i8*, i8** %7
%36 = sext i32 %34 to i64
%37 = getelementptr i8, i8* %35, i64 %36
store i8* %37, i8** %7
%38 = load i32, i32* %10
%39 = load i32, i32* %8
%40 = sub nsw i32 %39, %38
store i32 %40, i32* %8
br label %41
41: ; preds = %33, %20
%42 = call i32 @time(i32* null)
%43 = load i32, i32* %12
%44 = sub nsw i32 %42, %43
%45 = load i32, i32* %9
%46 = icmp sgt i32 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = load i32, i32* @ETIMEDOUT
store i32 %48, i32* @errno
store i32 -1, i32* %5
br label %52
49: ; preds = %41
br label %17
50: ; preds = %17
%51 = load i32, i32* %11
store i32 %51, i32* %5
br label %52
52: ; preds = %50, %47, %32
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @syncWrite(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32 @time(i32* null)
%6 = add nsw i32 %3, 1
br label %7
7: ; preds = %22, %4
%8 = phi i32 [ %2, %4 ], [ %23, %22 ]
%9 = phi i8* [ %1, %4 ], [ %24, %22 ]
%10 = icmp eq i32 %8, 0
br i1 %10, label %30, label %11
11: ; preds = %7
%12 = tail call i32 @aeWait(i32 %0, i32 2, i32 1000)
%13 = and i32 %12, 2
%14 = icmp eq i32 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %11
%16 = tail call i32 @write(i32 %0, i8* %9, i32 %8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %30, label %18
18: ; preds = %15
%19 = sext i32 %16 to i64
%20 = getelementptr i8, i8* %9, i64 %19
%21 = sub nsw i32 %8, %16
br label %22
22: ; preds = %18, %11
%23 = phi i32 [ %21, %18 ], [ %8, %11 ]
%24 = phi i8* [ %20, %18 ], [ %9, %11 ]
%25 = tail call i32 @time(i32* null)
%26 = sub nsw i32 %25, %5
%27 = icmp sgt i32 %26, %6
br i1 %27, label %28, label %7
28: ; preds = %22
%29 = load i32, i32* @ETIMEDOUT
store i32 %29, i32* @errno
br label %30
30: ; preds = %7, %15, %28
%31 = phi i32 [ -1, %28 ], [ %2, %7 ], [ -1, %15 ]
ret i32 %31
}