#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 */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ err_sys(char *);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int main() {
  int n, fd;
  char buffer[1024];
  fd = open("myfifo", O_RDONLY);
  if (fd < 0)
    err_sys("open error");
  while (1) {
    n = read(fd, buffer, 1024);
    if (n < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        printf("no data");
        sleep(1);
      } else
        err_sys("read error");
    } else {
      buffer[n] = '\0';
      ;
      sleep(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1190 <main+0x40>
lea    0xe7c(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1195 <main+0x45>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1216 <main+0xc6>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
je     11ea <main+0x9a>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <main+0xb5>
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1211 <main+0xc1>
lea    0xe0e(%rip),%rdi
call   1030 <err_sys@plt>
jmp    122c <main+0xdc>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1195 <main+0x45>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1188 <main+0x38>
lea    0xe84(%rip),%rdi
call   1030 <err_sys@plt>
mov    %rsp,%r14
mov    0x2e46(%rip),%r13
mov    0x2e27(%rip),%rbp
mov    0x2e30(%rip),%r15
lea    0xe73(%rip),%r12
jmp    11c0 <main+0x70>
nopl   0x0(%rax)
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11b0 <main+0x60>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     11ec <main+0x9c>
cmp    (%r15),%rax
je     11ec <main+0x9c>
mov    %r12,%rdi
call   1030 <err_sys@plt>
jmp    11c0 <main+0x70>
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
jmp    11b6 <main+0x66>

LLVM IR (-O0):
@.str = [7 x i8] c"myfifo\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.2 = [8 x i8] c"no data\00"
@.str.3 = [11 x i8] c"read error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
br label %12
12: ; preds = %11, %37
%13 = load i32, i32* %3
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 1024)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @EAGAIN
%21 = icmp eq i64 %19, %20
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i64, i64* @errno
%24 = load i64, i64* @EWOULDBLOCK
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22, %18
%27 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @sleep(i32 1)
br label %31
29: ; preds = %22
%30 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
br label %37
32: ; preds = %12
%33 = load i32, i32* %2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %34
store i8 0, i8* %35
%36 = call i32 @sleep(i32 1)
br label %37
37: ; preds = %32, %31
br label %12
}

LLVM IR (-O2):
@.str = [7 x i8] c"myfifo\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.2 = [8 x i8] c"no data\00"
@.str.3 = [11 x i8] c"read error\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
br label %9
9: ; preds = %22, %8
%10 = call i32 @read(i32 %4, i8* %2, i32 1024)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %25
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EAGAIN
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EWOULDBLOCK
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @sleep(i32 1)
br label %22
22: ; preds = %19, %23, %25
br label %9
23: ; preds = %12
%24 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %22
25: ; preds = %9
%26 = zext i32 %10 to i64
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %26
store i8 0, i8* %27
%28 = tail call i32 @sleep(i32 1)
br label %22
}