#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 /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, char *, int);
void poll(int pinfd) {
  int i = 0;
  int count = 0;
  char c;
  for (int i = 0; i < 1000000 / 2; ++i) {
    read(pinfd, &c, 1);
    if (c == '1') {
      ++count;
    }
    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)
movl   $0x0,-0x14(%rbp)
cmpl   $0x7a120,-0x14(%rbp)
jge    11a5 <poll+0x75>
mov    -0x4(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl -0xd(%rbp),%eax
cmp    $0x31,%eax
jne    1184 <poll+0x54>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1150 <poll+0x20>
mov    -0xc(%rbp),%esi
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r15d
xor    %ebx,%ebx
mov    $0x7a120,%ebp
lea    0xf(%rsp),%r14
mov    0x2e8e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %eax,%eax
cmpb   $0x31,0xf(%rsp)
sete   %al
add    %eax,%ebx
mov    (%r12),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
add    $0xffffffff,%ebp
jne    1160 <poll+0x30>
lea    0xe6a(%rip),%rdi
mov    %ebx,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [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 i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %6
br label %7
7: ; preds = %23, %1
%8 = load i32, i32* %6
%9 = icmp slt i32 %8, 500000
br i1 %9, label %10, label %26
10: ; preds = %7
%11 = load i32, i32* %2
%12 = call i32 @read(i32 %11, i8* %5, i32 1)
%13 = load i8, i8* %5
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 49
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 1
store i32 %18, i32* %4
br label %19
19: ; preds = %16, %10
%20 = load i32, i32* %2
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @lseek(i32 %20, i32 0, i32 %21)
br label %23
23: ; preds = %19
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
br label %7
26: ; preds = %7
%27 = load i32, i32* %4
%28 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %27)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [13 x i8] c"count is %d\0A\00"
define void @poll(i32 %0) {
%2 = alloca i8
br label %5
3: ; preds = %5
%4 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %12)
ret void
5: ; preds = %1, %5
%6 = phi i32 [ 0, %1 ], [ %15, %5 ]
%7 = phi i32 [ 0, %1 ], [ %12, %5 ]
%8 = call i32 @read(i32 %0, i8* %2, i32 1)
%9 = load i8, i8* %2
%10 = icmp eq i8 %9, 49
%11 = zext i1 %10 to i32
%12 = add nuw nsw i32 %7, %11
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @lseek(i32 %0, i32 0, i32 %13)
%15 = add nuw nsw i32 %6, 1
%16 = icmp eq i32 %15, 500000
br i1 %16, label %3, label %5
}