#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 /*<<< orphan*/ time_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
double difftime(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void poll(int pinfd) {
  int i = 0;
  int count = 0;
  char c, oldc;
  c = '0';
  oldc = '0';
  time_t oldtime = time(NULL);
  time_t start_time = oldtime;
  while (difftime(time(NULL), start_time) <= 10.0) {
    time_t curtime = time(NULL);
    if (difftime(curtime, oldtime) >= 1.0) {
      oldtime = curtime;
      printf("%d count -> %d\n", pinfd, count);
      count = 0;
    }
    read(pinfd, &c, 1);
    if (c == '1' && oldc == '0') {
      ++count;
    }
    oldc = c;
    lseek(pinfd, 0, SEEK_SET);
  }
  printf("count is %d\n", count);
}


x86 assembly (clang, -O0):
<poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movb   $0x30,-0xd(%rbp)
movb   $0x30,-0xe(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%esi
call   1040 <difftime@plt>
movaps %xmm0,%xmm1
movsd  0xe5f(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     1247 <poll+0xf7>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <difftime@plt>
movsd  0xe3e(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11f5 <poll+0xa5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl -0xd(%rbp),%eax
cmp    $0x31,%eax
jne    1229 <poll+0xd9>
movsbl -0xe(%rbp),%eax
cmp    $0x30,%eax
jne    1229 <poll+0xd9>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xd(%rbp),%al
mov    %al,-0xe(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
jmp    1183 <poll+0x33>
mov    -0xc(%rbp),%esi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebp
movb   $0x30,0x7(%rsp)
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%r12d
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
mov    %r12d,%esi
call   1040 <difftime@plt>
movsd  0xe79(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    11ab <poll+0x5b>
lea    0xe8c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>
mov    $0x30,%r15b
xor    %ebx,%ebx
mov    %r12d,%r14d
jmp    1222 <poll+0xd2>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movzbl 0x7(%rsp),%r13d
mov    %r13d,%eax
xor    $0x31,%al
xor    $0x30,%r15b
xor    %ecx,%ecx
or     %al,%r15b
sete   %cl
add    %ecx,%ebx
mov    0x2def(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
mov    %r12d,%esi
call   1040 <difftime@plt>
movsd  0xdeb(%rip),%xmm1
ucomisd %xmm0,%xmm1
mov    %r13d,%r15d
jb     118d <poll+0x3d>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%r13d
mov    %eax,%edi
mov    %r14d,%esi
call   1040 <difftime@plt>
ucomisd 0xdca(%rip),%xmm0
jb     11c0 <poll+0x70>
lea    0xdc9(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r13d,%r14d
xor    %ebx,%ebx
jmp    11c0 <poll+0x70>

LLVM IR (-O0):
@.str = [16 x i8] c"%d count -> %d\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [13 x i8] c"count is %d\0A\00"
define void @poll(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i8 48, i8* %5
store i8 48, i8* %6
%10 = call i32 @time(i32* null)
store i32 %10, i32* %7
%11 = load i32, i32* %7
store i32 %11, i32* %8
br label %12
12: ; preds = %41, %1
%13 = call i32 @time(i32* null)
%14 = load i32, i32* %8
%15 = call double @difftime(i32 %13, i32 %14)
%16 = fcmp ole double %15, 1.000000e+01
br i1 %16, label %17, label %46
17: ; preds = %12
%18 = call i32 @time(i32* null)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = load i32, i32* %7
%21 = call double @difftime(i32 %19, i32 %20)
%22 = fcmp oge double %21, 1.000000e+00
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i32, i32* %9
store i32 %24, i32* %7
%25 = load i32, i32* %2
%26 = load i32, i32* %4
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26)
store i32 0, i32* %4
br label %28
28: ; preds = %23, %17
%29 = load i32, i32* %2
%30 = call i32 @read(i32 %29, i8* %5, i32 1)
%31 = load i8, i8* %5
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 49
br i1 %33, label %34, label %41
34: ; preds = %28
%35 = load i8, i8* %6
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 48
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %41
41: ; preds = %38, %34, %28
%42 = load i8, i8* %5
store i8 %42, i8* %6
%43 = load i32, i32* %2
%44 = load i32, i32* @SEEK_SET
%45 = call i32 @lseek(i32 %43, i32 0, i32 %44)
br label %12
46: ; preds = %12
%47 = load i32, i32* %4
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %47)
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"%d count -> %d\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [13 x i8] c"count is %d\0A\00"
define void @poll(i32 %0) {
%2 = alloca i8
store i8 48, i8* %2
%3 = tail call i32 @time(i32* null)
%4 = tail call i32 @time(i32* null)
%5 = tail call double @difftime(i32 %4, i32 %3)
%6 = fcmp ugt double %5, 1.000000e+01
br i1 %6, label %31, label %7
7: ; preds = %1, %16
%8 = phi i32 [ %25, %16 ], [ 0, %1 ]
%9 = phi i8 [ %20, %16 ], [ 48, %1 ]
%10 = phi i32 [ %17, %16 ], [ %3, %1 ]
%11 = tail call i32 @time(i32* null)
%12 = tail call double @difftime(i32 %11, i32 %10)
%13 = fcmp ult double %12, 1.000000e+00
br i1 %13, label %16, label %14
14: ; preds = %7
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %8)
br label %16
16: ; preds = %14, %7
%17 = phi i32 [ %11, %14 ], [ %10, %7 ]
%18 = phi i32 [ 0, %14 ], [ %8, %7 ]
%19 = call i32 @read(i32 %0, i8* %2, i32 1)
%20 = load i8, i8* %2
%21 = icmp eq i8 %20, 49
%22 = icmp eq i8 %9, 48
%23 = and i1 %22, %21
%24 = zext i1 %23 to i32
%25 = add nsw i32 %18, %24
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @lseek(i32 %0, i32 0, i32 %26)
%28 = tail call i32 @time(i32* null)
%29 = tail call double @difftime(i32 %28, i32 %3)
%30 = fcmp ugt double %29, 1.000000e+01
br i1 %30, label %31, label %7
31: ; preds = %16, %1
%32 = phi i32 [ 0, %1 ], [ %25, %16 ]
%33 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %32)
ret void
}