#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 read(int, void *, int);
int ztimed_read_wait_millisecond(int, long);
int ztimed_read(int fd, void *buf, int size, int read_wait_timeout) {
  int ret, ec;
  if (read_wait_timeout == 0) {
    for (;;) {
      if ((ret = read(fd, buf, size)) < 0) {
        if (errno == EINTR) {
          continue;
        }
      }
      return ret;
    }
  }
  for (;;) {
    if ((ret = ztimed_read_wait_millisecond(fd, 1000L * read_wait_timeout)) ==
        0) {
      errno = ETIMEDOUT;
      return -1;
    }
    if ((ret = read(fd, buf, size)) < 0) {
      ec = errno;
      if (ec == EINTR) {
        continue;
      }
      if (ec == EAGAIN) {
        continue;
      }
    }
    return ret;
  }
  errno = ETIMEDOUT;
  return -1;
}


x86 assembly (clang, -O0):
<ztimed_read>:
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    118c <ztimed_read+0x6c>
jmp    1144 <ztimed_read+0x24>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1181 <ztimed_read+0x61>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%eax
jne    117c <ztimed_read+0x5c>
jmp    1144 <ztimed_read+0x24>
jmp    1181 <ztimed_read+0x61>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122e <ztimed_read+0x10e>
jmp    1191 <ztimed_read+0x71>
mov    -0x8(%rbp),%edi
movslq -0x18(%rbp),%rax
imul   $0x3e8,%rax,%rsi
call   1040 <ztimed_read_wait_millisecond@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11ce <ztimed_read+0xae>
mov    0x2e21(%rip),%rax
mov    (%rax),%ecx
mov    0x2e20(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <ztimed_read+0x10e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1228 <ztimed_read+0x108>
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_read+0xec>
jmp    1191 <ztimed_read+0x71>
mov    -0x20(%rbp),%eax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%eax
jne    1223 <ztimed_read+0x103>
jmp    1191 <ztimed_read+0x71>
jmp    1228 <ztimed_read+0x108>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ztimed_read>:
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     1186 <ztimed_read+0x66>
movslq %ecx,%rax
imul   $0x3e8,%rax,%r15
mov    0x2e98(%rip),%r12
mov    0x2e81(%rip),%r13
nop
mov    %ebp,%edi
mov    %r15,%rsi
call   1040 <ztimed_read_wait_millisecond@plt>
test   %eax,%eax
je     11bc <ztimed_read+0x9c>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
test   %eax,%eax
jns    11ce <ztimed_read+0xae>
mov    (%r12),%ecx
cmp    0x0(%r13),%ecx
je     1150 <ztimed_read+0x30>
mov    0x2e40(%rip),%rdx
cmp    (%rdx),%ecx
je     1150 <ztimed_read+0x30>
jmp    11ce <ztimed_read+0xae>
mov    0x2e53(%rip),%r15
mov    0x2e3c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
test   %eax,%eax
jns    11ce <ztimed_read+0xae>
mov    (%r15),%ecx
cmp    (%r12),%ecx
je     11a0 <ztimed_read+0x80>
jmp    11ce <ztimed_read+0xae>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r12)
mov    $0xffffffff,%eax
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_read(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 %29
14: ; preds = %4
br label %15
15: ; preds = %25, %14
%16 = load i32, i32* %6
%17 = load i8*, i8** %7
%18 = load i32, i32* %8
%19 = call i32 @read(i32 %16, i8* %17, i32 %18)
store i32 %19, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = load i32, i32* @errno
%23 = load i32, i32* @EINTR
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
br label %15
26: ; preds = %21
br label %27
27: ; preds = %26, %15
%28 = load i32, i32* %10
store i32 %28, i32* %5
br label %59
29: ; preds = %4
br label %30
30: ; preds = %55, %50, %29
%31 = load i32, i32* %6
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = mul nsw i64 1000, %33
%35 = call i32 @ztimed_read_wait_millisecond(i32 %31, i64 %34)
store i32 %35, i32* %10
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = load i32, i32* @ETIMEDOUT
store i32 %38, i32* @errno
store i32 -1, i32* %5
br label %59
39: ; preds = %30
%40 = load i32, i32* %6
%41 = load i8*, i8** %7
%42 = load i32, i32* %8
%43 = call i32 @read(i32 %40, i8* %41, i32 %42)
store i32 %43, i32* %10
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %57
45: ; preds = %39
%46 = load i32, i32* @errno
store i32 %46, i32* %11
%47 = load i32, i32* %11
%48 = load i32, i32* @EINTR
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %45
br label %30
51: ; preds = %45
%52 = load i32, i32* %11
%53 = load i32, i32* @EAGAIN
%54 = icmp eq i32 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %51
br label %30
56: ; preds = %51
br label %57
57: ; preds = %56, %39
%58 = load i32, i32* %10
store i32 %58, i32* %5
br label %59
59: ; preds = %57, %37, %27
%60 = load i32, i32* %5
ret i32 %60
}

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