#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 pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int POLLIN;
int POLLNVAL;
int POLLOUT;
int /*<<< orphan*/ errno;
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ zfatal(char *);
int ztimed_read_write_wait_millisecond(int fd, long read_write_wait_timeout,
                                       int *readable, int *writeable) {
  struct pollfd pollfd;
  long left_timeout = read_write_wait_timeout;
  int timeout;
  *readable = *writeable = 0;
  for (;;) {
    pollfd.fd = fd;
    pollfd.events = POLLIN | POLLOUT;
    if (read_write_wait_timeout < 0) {
      timeout = -1;
    } else if (read_write_wait_timeout == 0) {
      timeout = 0;
    } else {
      if (left_timeout < 1) {
        return 0;
      } else if (left_timeout > 1000 * 3600) {
        timeout = 1000 * 3600;
      } else {
        timeout = left_timeout;
      }
    }
    switch (poll(&pollfd, 1, timeout)) {
    case -1:
      if (errno != EINTR) {
        zfatal("FATAL poll error (%m)");
      }
      continue;
    case 0:
      if (read_write_wait_timeout <= 0) {
        return 0;
      }
      left_timeout -= 1000 * 3600;
      continue;
    default:
      if (pollfd.revents & POLLNVAL) {
        return -1;
      }
      if (pollfd.revents & POLLIN) {
        *readable = 1;
      }
      if (pollfd.revents & POLLOUT) {
        *writeable = 1;
      }
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<ztimed_read_write_wait_millisecond>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    0x2e8f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1185 <ztimed_read_write_wait_millisecond+0x65>
movl   $0xffffffff,-0x3c(%rbp)
jmp    11e3 <ztimed_read_write_wait_millisecond+0xc3>
cmpq   $0x0,-0x10(%rbp)
jne    119c <ztimed_read_write_wait_millisecond+0x7c>
movl   $0x0,-0x3c(%rbp)
jmp    11de <ztimed_read_write_wait_millisecond+0xbe>
cmpq   $0x1,-0x38(%rbp)
jge    11b3 <ztimed_read_write_wait_millisecond+0x93>
movl   $0x0,-0x4(%rbp)
jmp    12d0 <ztimed_read_write_wait_millisecond+0x1b0>
cmpq   $0x36ee80,-0x38(%rbp)
jle    11cd <ztimed_read_write_wait_millisecond+0xad>
movl   $0x36ee80,-0x3c(%rbp)
jmp    11d4 <ztimed_read_write_wait_millisecond+0xb4>
mov    -0x38(%rbp),%rax
mov    %eax,-0x3c(%rbp)
jmp    11d9 <ztimed_read_write_wait_millisecond+0xb9>
jmp    11de <ztimed_read_write_wait_millisecond+0xbe>
jmp    11e3 <ztimed_read_write_wait_millisecond+0xc3>
mov    -0x3c(%rbp),%edx
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,%ecx
mov    %ecx,-0x40(%rbp)
sub    $0xffffffff,%eax
je     1217 <ztimed_read_write_wait_millisecond+0xf7>
jmp    1207 <ztimed_read_write_wait_millisecond+0xe7>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     1240 <ztimed_read_write_wait_millisecond+0x120>
jmp    126a <ztimed_read_write_wait_millisecond+0x14a>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%eax
je     123b <ztimed_read_write_wait_millisecond+0x11b>
lea    0xdca(%rip),%rdi
call   1030 <zfatal@plt>
jmp    1153 <ztimed_read_write_wait_millisecond+0x33>
cmpq   $0x0,-0x10(%rbp)
jg     1257 <ztimed_read_write_wait_millisecond+0x137>
movl   $0x0,-0x4(%rbp)
jmp    12d0 <ztimed_read_write_wait_millisecond+0x1b0>
mov    -0x38(%rbp),%rax
sub    $0x36ee80,%rax
mov    %rax,-0x38(%rbp)
jmp    1153 <ztimed_read_write_wait_millisecond+0x33>
mov    -0x28(%rbp),%eax
mov    0x2d6c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     128b <ztimed_read_write_wait_millisecond+0x16b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d0 <ztimed_read_write_wait_millisecond+0x1b0>
mov    -0x28(%rbp),%eax
mov    0x2d43(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12aa <ztimed_read_write_wait_millisecond+0x18a>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x28(%rbp),%eax
mov    0x2d44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c9 <ztimed_read_write_wait_millisecond+0x1a9>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ztimed_read_write_wait_millisecond>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebp
mov    %rcx,0x18(%rsp)
movl   $0x0,(%rcx)
mov    %rdx,0x10(%rsp)
movl   $0x0,(%rdx)
mov    %rsi,%rdx
sar    $0x3f,%rdx
mov    0x2e84(%rip),%r13
mov    %rsp,%rbx
mov    0x2e6a(%rip),%r14
mov    0x2e5b(%rip),%r12
mov    %rsi,%rax
mov    %rsi,%rcx
mov    %rsi,0x28(%rsp)
mov    %rdx,0x20(%rsp)
sub    $0x36ee80,%rcx
mov    %rcx,0x30(%rsp)
mov    $0x36ee80,%r15d
cmovb  %eax,%r15d
mov    0x2e66(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%r13),%ecx
mov    %ebp,(%rsp)
mov    %ecx,0x4(%rsp)
test   %rsi,%rsi
cmovle %edx,%r15d
jle    11d4 <ztimed_read_write_wait_millisecond+0xb4>
test   %rax,%rax
jg     11d4 <ztimed_read_write_wait_millisecond+0xb4>
jmp    1268 <ztimed_read_write_wait_millisecond+0x148>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
or     0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
call   1040 <poll@plt>
cmp    $0xffffffff,%eax
jne    1200 <ztimed_read_write_wait_millisecond+0xe0>
mov    (%r14),%eax
cmp    (%r12),%eax
je     11c0 <ztimed_read_write_wait_millisecond+0xa0>
lea    0xe07(%rip),%rdi
call   1030 <zfatal@plt>
jmp    11c0 <ztimed_read_write_wait_millisecond+0xa0>
test   %eax,%eax
jne    1226 <ztimed_read_write_wait_millisecond+0x106>
mov    0x30(%rsp),%rcx
mov    %rcx,%rax
mov    0x28(%rsp),%rsi
test   %rsi,%rsi
mov    0x20(%rsp),%rdx
mov    $0x0,%edi
jg     1175 <ztimed_read_write_wait_millisecond+0x55>
jmp    126a <ztimed_read_write_wait_millisecond+0x14a>
mov    0x8(%rsp),%eax
mov    0x2daf(%rip),%rcx
mov    $0xffffffff,%edi
test   %eax,(%rcx)
jne    126a <ztimed_read_write_wait_millisecond+0x14a>
test   %eax,0x0(%r13)
je     124b <ztimed_read_write_wait_millisecond+0x12b>
mov    0x10(%rsp),%rcx
movl   $0x1,(%rcx)
mov    $0x1,%edi
mov    0x2da1(%rip),%rcx
test   %eax,(%rcx)
je     126a <ztimed_read_write_wait_millisecond+0x14a>
mov    0x18(%rsp),%rax
movl   $0x1,(%rax)
jmp    126a <ztimed_read_write_wait_millisecond+0x14a>
xor    %edi,%edi
mov    %edi,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@.str = [22 x i8] c"FATAL poll error (%m)\00"
@POLLNVAL = global i32 0
define i32 @ztimed_read_write_wait_millisecond(i32 %0, i64 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.pollfd
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %6
store i64 %1, i64* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%13 = load i64, i64* %7
store i64 %13, i64* %11
%14 = load i32*, i32** %9
store i32 0, i32* %14
%15 = load i32*, i32** %8
store i32 0, i32* %15
br label %16
16: ; preds = %58, %53, %4
%17 = load i32, i32* %6
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @POLLIN
%20 = load i32, i32* @POLLOUT
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i64, i64* %7
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %16
store i32 -1, i32* %12
br label %44
26: ; preds = %16
%27 = load i64, i64* %7
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 0, i32* %12
br label %43
30: ; preds = %26
%31 = load i64, i64* %11
%32 = icmp slt i64 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 0, i32* %5
br label %85
34: ; preds = %30
%35 = load i64, i64* %11
%36 = icmp sgt i64 %35, 3600000
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 3600000, i32* %12
br label %41
38: ; preds = %34
%39 = load i64, i64* %11
%40 = trunc i64 %39 to i32
store i32 %40, i32* %12
br label %41
41: ; preds = %38, %37
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42, %29
br label %44
44: ; preds = %43, %25
%45 = load i32, i32* %12
%46 = call i32 @poll(%struct.pollfd* %10, i32 1, i32 %45)
switch i32 %46, label %61 [
i32 -1, label %47
i32 0, label %54
]
47: ; preds = %44
%48 = load i32, i32* @errno
%49 = load i32, i32* @EINTR
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @zfatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
br label %16
54: ; preds = %44
%55 = load i64, i64* %7
%56 = icmp sle i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 0, i32* %5
br label %85
58: ; preds = %54
%59 = load i64, i64* %11
%60 = sub nsw i64 %59, 3600000
store i64 %60, i64* %11
br label %16
61: ; preds = %44
%62 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
%63 = load i32, i32* %62
%64 = load i32, i32* @POLLNVAL
%65 = and i32 %63, %64
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %61
store i32 -1, i32* %5
br label %85
68: ; preds = %61
%69 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
%70 = load i32, i32* %69
%71 = load i32, i32* @POLLIN
%72 = and i32 %70, %71
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %68
%75 = load i32*, i32** %8
store i32 1, i32* %75
br label %76
76: ; preds = %74, %68
%77 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
%78 = load i32, i32* %77
%79 = load i32, i32* @POLLOUT
%80 = and i32 %78, %79
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %76
%83 = load i32*, i32** %9
store i32 1, i32* %83
br label %84
84: ; preds = %82, %76
store i32 1, i32* %5
br label %85
85: ; preds = %84, %67, %57, %33
%86 = load i32, i32* %5
ret i32 %86
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@.str = [22 x i8] c"FATAL poll error (%m)\00"
@POLLNVAL = global i32 0
define i32 @ztimed_read_write_wait_millisecond(i32 %0, i64 %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
store i32 0, i32* %3
store i32 0, i32* %2
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
%9 = icmp slt i64 %1, 1
%10 = ashr i64 %1, 63
br label %11
11: ; preds = %34, %4
%12 = phi i64 [ %35, %34 ], [ %1, %4 ]
%13 = icmp sgt i64 %12, 0
%14 = icmp ult i64 %12, 3600000
%15 = select i1 %14, i64 %12, i64 3600000
%16 = select i1 %9, i1 true, i1 %13
%17 = select i1 %9, i64 %10, i64 %15
%18 = trunc i64 %17 to i32
store i32 %0, i32* %7
%19 = load i32, i32* @POLLIN
%20 = load i32, i32* @POLLOUT
%21 = or i32 %20, %19
store i32 %21, i32* %8
br i1 %16, label %26, label %52
22: ; preds = %28, %32
store i32 %0, i32* %7
%23 = load i32, i32* @POLLIN
%24 = load i32, i32* @POLLOUT
%25 = or i32 %24, %23
store i32 %25, i32* %8
br label %26
26: ; preds = %11, %22
%27 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %18)
switch i32 %27, label %36 [
i32 -1, label %28
i32 0, label %34
]
28: ; preds = %26
%29 = load i32, i32* @errno
%30 = load i32, i32* @EINTR
%31 = icmp eq i32 %29, %30
br i1 %31, label %22, label %32
32: ; preds = %28
%33 = call i32 @zfatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %22
34: ; preds = %26
%35 = add nsw i64 %12, -3600000
br i1 %9, label %52, label %11
36: ; preds = %26
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
%38 = load i32, i32* %37
%39 = load i32, i32* @POLLNVAL
%40 = and i32 %39, %38
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %52
42: ; preds = %36
%43 = load i32, i32* @POLLIN
%44 = and i32 %43, %38
%45 = icmp eq i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %42
store i32 1, i32* %2
br label %47
47: ; preds = %46, %42
%48 = load i32, i32* @POLLOUT
%49 = and i32 %48, %38
%50 = icmp eq i32 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %47
store i32 1, i32* %3
br label %52
52: ; preds = %34, %11, %47, %51, %36
%53 = phi i32 [ -1, %36 ], [ 1, %51 ], [ 1, %47 ], [ 0, %11 ], [ 0, %34 ]
ret i32 %53
}