#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 {
  int tv_sec;
  int tv_usec;
};
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int O_APPEND;
int O_CREAT;
int O_NONBLOCK;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ POLLOUT;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int main() {
  const char *tmpdd = "tmp.d";
  int fd =
      open(tmpdd, O_RDWR | O_CREAT | O_TRUNC | O_APPEND | O_NONBLOCK, 0777);
  char buf[1024 + 1];
  int i, times1 = 0, times2 = 0;
  long time1 = 0, time2;
  struct timeval tv;
  for (i = 0; i < 1024 * 1000; i++) {
    lseek(fd, 0, SEEK_SET);
    struct pollfd pollfd;
    pollfd.fd = fd;
    pollfd.events = POLLOUT;
    gettimeofday(&tv, 0);
    long t1 = tv.tv_sec * 1000 * 1000 + tv.tv_usec;
    switch (poll(&pollfd, 1, 10000)) {
    case -1:
      if (errno != EINTR) {
        printf("poll error (%m)");
        exit(1);
      }
      continue;
    case 0:
      break;
    default:
      times1++;
      break;
    }
    gettimeofday(&tv, 0);
    long t2 = tv.tv_sec * 1000 * 1000 + tv.tv_usec;
    if (write(fd, buf, 1024) != 1024) {
      times2++;
    }
    gettimeofday(&tv, 0);
    long t3 = tv.tv_sec * 1000 * 1000 + tv.tv_usec;
    time1 += t2 - t1;
    time2 += t3 - t2;
  }
  printf("times: %d, %d, %f, %f\n", times1, times2, time1 * 1.0 / (1000 * 1000),
         time2 * 1.0 / (1000 * 1000));
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
mov    0x2de5(%rip),%rax
or     (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x428(%rbp)
movl   $0x0,-0x42c(%rbp)
movq   $0x0,-0x438(%rbp)
movl   $0x0,-0x424(%rbp)
cmpl   $0xfa000,-0x424(%rbp)
jge    13c0 <main+0x250>
mov    -0x14(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    %esi,-0x470(%rbp)
call   1050 <lseek@plt>
mov    -0x470(%rbp),%esi
mov    -0x14(%rbp),%eax
mov    %eax,-0x450(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44c(%rbp)
lea    -0x448(%rbp),%rdi
call   1060 <gettimeofday@plt>
mov    -0x448(%rbp),%eax
mov    -0x444(%rbp),%ecx
imul   $0xf4240,%eax,%eax
add    %ecx,%eax
cltq
mov    %rax,-0x458(%rbp)
lea    -0x450(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1070 <poll@plt>
mov    %eax,%ecx
mov    %ecx,-0x46c(%rbp)
sub    $0xffffffff,%eax
je     12a3 <main+0x133>
jmp    1290 <main+0x120>
mov    -0x46c(%rbp),%eax
test   %eax,%eax
je     12d8 <main+0x168>
jmp    12dd <main+0x16d>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    0x2cf5(%rip),%rcx
cmp    (%rcx),%eax
je     12d3 <main+0x163>
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    13ac <main+0x23c>
jmp    12ec <main+0x17c>
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
lea    -0x448(%rbp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
imul   $0x3e8,-0x448(%rbp),%eax
imul   $0x3e8,%eax,%eax
add    -0x444(%rbp),%eax
cltq
mov    %rax,-0x460(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
cmp    $0x400,%eax
je     1347 <main+0x1d7>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
lea    -0x448(%rbp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
imul   $0x3e8,-0x448(%rbp),%eax
imul   $0x3e8,%eax,%eax
add    -0x444(%rbp),%eax
cltq
mov    %rax,-0x468(%rbp)
mov    -0x460(%rbp),%rax
sub    -0x458(%rbp),%rax
add    -0x438(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x468(%rbp),%rax
sub    -0x460(%rbp),%rax
add    -0x440(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    11f4 <main+0x84>
mov    -0x428(%rbp),%esi
mov    -0x42c(%rbp),%edx
cvtsi2sdq -0x438(%rbp),%xmm0
ff ff
movsd  0xc2b(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xc17(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsi2sdq -0x440(%rbp),%xmm1
ff ff
movsd  0xc0a(%rip),%xmm2
mulsd  %xmm2,%xmm1
movsd  0xbf6(%rip),%xmm2
divsd  %xmm2,%xmm1
lea    0xc11(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    0x2e48(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
lea    0xe53(%rip),%rdi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,0xc(%rsp)
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    $0xfa000,%r13d
movl   $0x0,0x8(%rsp)
xor    %r14d,%r14d
jmp    1264 <main+0xf4>
mov    0x10(%rsp),%rax
add    $0x1,%eax
mov    %rax,0x10(%rsp)
movslq %ebp,%r15
add    %rbx,%r15
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
imul   $0xf4240,(%rsp),%eax
movslq 0x4(%rsp),%rcx
movslq %eax,%rbx
add    %rcx,%rbx
mov    0xc(%rsp),%edi
lea    0x20(%rsp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
xor    %ecx,%ecx
cmp    $0x400,%eax
setne  %cl
add    %ecx,0x8(%rsp)
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
imul   $0xf4240,(%rsp),%eax
movslq 0x4(%rsp),%rcx
cltq
add    %rcx,%rax
sub    %r15,%r14
add    %rbx,%r14
sub    %rbx,%r12
add    %rax,%r12
add    $0xffffffff,%r13d
je     12f2 <main+0x182>
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %ebx,0x18(%rsp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
imul   $0xf4240,(%rsp),%ebp
movslq 0x4(%rsp),%rbx
lea    0x18(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1070 <poll@plt>
test   %eax,%eax
je     11ed <main+0x7d>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x70>
mov    0x2ceb(%rip),%rax
mov    (%rax),%eax
mov    0x2cd2(%rip),%rcx
cmp    (%rcx),%eax
je     125a <main+0xea>
lea    0xd2d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cvtsi2sd %r14,%xmm0
movsd  0xd01(%rip),%xmm2
divsd  %xmm2,%xmm0
cvtsi2sd %r12,%xmm1
divsd  %xmm2,%xmm1
lea    0xd0b(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%edx
mov    $0x2,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.pollfd = type { i32, i32 }
@.str = [6 x i8] c"tmp.d\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
@POLLOUT = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@.str.1 = [16 x i8] c"poll error (%m)\00"
@.str.2 = [23 x i8] c"times: %d, %d, %f, %f\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [1025 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca %struct.timeval
%11 = alloca %struct.pollfd
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i32 0, i32* %1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %2
%15 = load i8*, i8** %2
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @O_APPEND
%22 = or i32 %20, %21
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %15, i32 %24, i32 511)
store i32 %25, i32* %3
store i32 0, i32* %6
store i32 0, i32* %7
store i64 0, i64* %8
store i32 0, i32* %5
br label %26
26: ; preds = %96, %0
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 1024000
br i1 %28, label %29, label %99
29: ; preds = %26
%30 = load i32, i32* %3
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @lseek(i32 %30, i32 0, i32 %31)
%33 = load i32, i32* %3
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* @POLLOUT
%36 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 1
store i32 %35, i32* %36
%37 = call i32 @gettimeofday(%struct.timeval* %10, i32 0)
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%39 = load i32, i32* %38
%40 = mul nsw i32 %39, 1000
%41 = mul nsw i32 %40, 1000
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%43 = load i32, i32* %42
%44 = add nsw i32 %41, %43
%45 = sext i32 %44 to i64
store i64 %45, i64* %12
%46 = call i32 @poll(%struct.pollfd* %11, i32 1, i32 10000)
switch i32 %46, label %56 [
i32 -1, label %47
i32 0, label %55
]
47: ; preds = %29
%48 = load i32, i32* @errno
%49 = load i32, i32* @EINTR
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %47
br label %96
55: ; preds = %29
br label %59
56: ; preds = %29
%57 = load i32, i32* %6
%58 = add nsw i32 %57, 1
store i32 %58, i32* %6
br label %59
59: ; preds = %56, %55
%60 = call i32 @gettimeofday(%struct.timeval* %10, i32 0)
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%62 = load i32, i32* %61
%63 = mul nsw i32 %62, 1000
%64 = mul nsw i32 %63, 1000
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%66 = load i32, i32* %65
%67 = add nsw i32 %64, %66
%68 = sext i32 %67 to i64
store i64 %68, i64* %13
%69 = load i32, i32* %3
%70 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%71 = call i32 @write(i32 %69, i8* %70, i32 1024)
%72 = icmp ne i32 %71, 1024
br i1 %72, label %73, label %76
73: ; preds = %59
%74 = load i32, i32* %7
%75 = add nsw i32 %74, 1
store i32 %75, i32* %7
br label %76
76: ; preds = %73, %59
%77 = call i32 @gettimeofday(%struct.timeval* %10, i32 0)
%78 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%79 = load i32, i32* %78
%80 = mul nsw i32 %79, 1000
%81 = mul nsw i32 %80, 1000
%82 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%83 = load i32, i32* %82
%84 = add nsw i32 %81, %83
%85 = sext i32 %84 to i64
store i64 %85, i64* %14
%86 = load i64, i64* %13
%87 = load i64, i64* %12
%88 = sub nsw i64 %86, %87
%89 = load i64, i64* %8
%90 = add nsw i64 %89, %88
store i64 %90, i64* %8
%91 = load i64, i64* %14
%92 = load i64, i64* %13
%93 = sub nsw i64 %91, %92
%94 = load i64, i64* %9
%95 = add nsw i64 %94, %93
store i64 %95, i64* %9
br label %96
96: ; preds = %76, %54
%97 = load i32, i32* %5
%98 = add nsw i32 %97, 1
store i32 %98, i32* %5
br label %26
99: ; preds = %26
%100 = load i32, i32* %6
%101 = load i32, i32* %7
%102 = load i64, i64* %8
%103 = sitofp i64 %102 to double
%104 = fmul double %103, 1.000000e+00
%105 = fdiv double %104, 1.000000e+06
%106 = load i64, i64* %9
%107 = sitofp i64 %106 to double
%108 = fmul double %107, 1.000000e+00
%109 = fdiv double %108, 1.000000e+06
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %100, i32 %101, double %105, double %109)
%111 = load i32, i32* %1
ret i32 %111
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.pollfd = type { i32, i32 }
@.str = [6 x i8] c"tmp.d\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
@POLLOUT = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@.str.1 = [16 x i8] c"poll error (%m)\00"
@.str.2 = [23 x i8] c"times: %d, %d, %f, %f\0A\00"
define i32 @main() {
%1 = alloca [1025 x i8]
%2 = alloca %struct.timeval
%3 = alloca %struct.pollfd
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @O_APPEND
%10 = or i32 %8, %9
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %12, i32 511)
%14 = getelementptr inbounds [1025 x i8], [1025 x i8]* %1, i64 0, i64 0
%15 = bitcast %struct.timeval* %2 to i8*
%16 = bitcast %struct.pollfd* %3 to i8*
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
br label %21
21: ; preds = %0, %68
%22 = phi i32 [ 0, %0 ], [ %73, %68 ]
%23 = phi i32 [ 0, %0 ], [ %72, %68 ]
%24 = phi i32 [ 0, %0 ], [ %71, %68 ]
%25 = phi i64 [ undef, %0 ], [ %70, %68 ]
%26 = phi i64 [ 0, %0 ], [ %69, %68 ]
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @lseek(i32 %13, i32 0, i32 %27)
store i32 %13, i32* %17
%29 = load i32, i32* @POLLOUT
store i32 %29, i32* %18
%30 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%31 = load i32, i32* %19
%32 = mul i32 %31, 1000000
%33 = load i32, i32* %20
%34 = add nsw i32 %32, %33
%35 = sext i32 %34 to i64
%36 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 10000)
switch i32 %36, label %44 [
i32 -1, label %37
i32 0, label %46
]
37: ; preds = %21
%38 = load i32, i32* @errno
%39 = load i32, i32* @EINTR
%40 = icmp eq i32 %38, %39
br i1 %40, label %68, label %41
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %21
%45 = add nsw i32 %23, 1
br label %46
46: ; preds = %44, %21
%47 = phi i32 [ %45, %44 ], [ %23, %21 ]
%48 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%49 = load i32, i32* %19
%50 = mul i32 %49, 1000000
%51 = load i32, i32* %20
%52 = add nsw i32 %50, %51
%53 = sext i32 %52 to i64
%54 = call i32 @write(i32 %13, i8* %14, i32 1024)
%55 = icmp ne i32 %54, 1024
%56 = zext i1 %55 to i32
%57 = add nsw i32 %24, %56
%58 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%59 = load i32, i32* %19
%60 = mul i32 %59, 1000000
%61 = load i32, i32* %20
%62 = add nsw i32 %60, %61
%63 = sext i32 %62 to i64
%64 = sub i64 %26, %35
%65 = add i64 %64, %53
%66 = sub i64 %25, %53
%67 = add i64 %66, %63
br label %68
68: ; preds = %37, %46
%69 = phi i64 [ %65, %46 ], [ %26, %37 ]
%70 = phi i64 [ %67, %46 ], [ %25, %37 ]
%71 = phi i32 [ %57, %46 ], [ %24, %37 ]
%72 = phi i32 [ %47, %46 ], [ %23, %37 ]
%73 = add nuw nsw i32 %22, 1
%74 = icmp eq i32 %73, 1024000
br i1 %74, label %75, label %21
75: ; preds = %68
%76 = sitofp i64 %69 to double
%77 = fdiv double %76, 1.000000e+06
%78 = sitofp i64 %70 to double
%79 = fdiv double %78, 1.000000e+06
%80 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %72, i32 %71, double %77, double %79)
ret i32 0
}