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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  FILE *fp = fopen("/dev/events", "r");
  volatile int j = 0;
  while (1) {
    j++;
    if (j == 1000000) {
      char buf[256];
      char *p = buf, ch;
      while ((ch = fgetc(fp)) != -1) {
        *p++ = ch;
        if (ch == '\n') {
          *p = '\0';
          break;
        }
      }
      printf("receive event: %s", buf);
      j = 0;
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
lea    0xebc(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    $0xf4240,%eax
jne    11fb <main+0xcb>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x129(%rbp)
movsbl %al,%eax
cmp    $0xffffffff,%eax
je     11e1 <main+0xb1>
mov    -0x129(%rbp),%cl
mov    -0x128(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x128(%rbp)
mov    %cl,(%rax)
movsbl -0x129(%rbp),%eax
cmp    $0xa,%eax
jne    11dc <main+0xac>
mov    -0x128(%rbp),%rax
movb   $0x0,(%rax)
jmp    11e1 <main+0xb1>
jmp    1185 <main+0x55>
lea    -0x120(%rbp),%rsi
lea    0xe1f(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
jmp    1160 <main+0x30>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xebb(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r12
movl   $0x0,0xc(%rsp)
lea    0x10(%rsp),%r14
lea    0xea6(%rip),%r15
jmp    1183 <main+0x53>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
movl   $0x0,0xc(%rsp)
addl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%eax
cmp    $0xf4240,%eax
jne    1183 <main+0x53>
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <fgetc@plt>
cmp    $0xff,%al
je     1170 <main+0x40>
mov    %al,(%rbx)
add    $0x1,%rbx
cmp    $0xa,%al
jne    11a0 <main+0x70>
movb   $0x0,(%rbx)
jmp    1170 <main+0x40>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/events\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"receive event: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i8*
%6 = alloca i8
store i32 0, i32* %1
%7 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
store volatile i32 0, i32* %3
br label %8
8: ; preds = %0, %33
%9 = load volatile i32, i32* %3
%10 = add nsw i32 %9, 1
store volatile i32 %10, i32* %3
%11 = load volatile i32, i32* %3
%12 = icmp eq i32 %11, 1000000
br i1 %12, label %13, label %33
13: ; preds = %8
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
store i8* %14, i8** %5
br label %15
15: ; preds = %29, %13
%16 = load i32*, i32** %2
%17 = call signext i8 @fgetc(i32* %16)
store i8 %17, i8* %6
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %30
20: ; preds = %15
%21 = load i8, i8* %6
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %5
store i8 %21, i8* %22
%24 = load i8, i8* %6
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = load i8*, i8** %5
store i8 0, i8* %28
br label %30
29: ; preds = %20
br label %15
30: ; preds = %27, %15
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store volatile i32 0, i32* %3
br label %33
33: ; preds = %30, %8
br label %8
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/events\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"receive event: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast i32* %1 to i8*
store volatile i32 0, i32* %1
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
br label %6
6: ; preds = %22, %0
%7 = load volatile i32, i32* %1
%8 = add nsw i32 %7, 1
store volatile i32 %8, i32* %1
%9 = load volatile i32, i32* %1
%10 = icmp eq i32 %9, 1000000
br i1 %10, label %11, label %22
11: ; preds = %6
br label %12
12: ; preds = %16, %11
%13 = phi i8* [ %5, %11 ], [ %17, %16 ]
%14 = call signext i8 @fgetc(i32* %3)
%15 = icmp eq i8 %14, -1
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %14, i8* %13
%18 = icmp eq i8 %14, 10
br i1 %18, label %19, label %12
19: ; preds = %16
store i8 0, i8* %17
br label %20
20: ; preds = %12, %19
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %5)
store volatile i32 0, i32* %1
br label %22
22: ; preds = %20, %6
br label %6
}