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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ mkfifo(char *, int);
int open(char *, int);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char buffer[80];
  memset(buffer, 0, sizeof(buffer));
  char FIFO[] = "FIFO";
  int fd;
  unlink(FIFO);
  if (mkfifo(FIFO, 0755) < 0) {
    perror("cannot create fifo");
    return 1;
  }
  fd = open(FIFO, O_RDONLY | O_NONBLOCK);
  if (fd == -1) {
    perror("cannot open fifo");
    return 2;
  }
  while (1) {
    memset(buffer, 0, sizeof(buffer));
    if (read(fd, buffer, sizeof(buffer)) == -1) {
      perror("no data yet!");
    } else {
      printf("read from fifo data is: %s\n", buffer);
    }
    sleep(1);
  }
  pause();
  unlink(FIFO);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
mov    0xe5b(%rip),%eax
mov    %eax,-0x65(%rbp)
mov    0xe56(%rip),%al
mov    %al,-0x61(%rbp)
lea    -0x65(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x65(%rbp),%rdi
mov    $0x1ed,%esi
call   1070 <mkfifo@plt>
cmp    $0x0,%rax
jge    11ea <main+0x6a>
lea    0xe2c(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1289 <main+0x109>
lea    -0x65(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0xffffffff,-0x6c(%rbp)
jne    122a <main+0xaa>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1289 <main+0x109>
jmp    122f <main+0xaf>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
mov    -0x6c(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x50,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    126a <main+0xea>
lea    0xdc9(%rip),%rdi
call   1090 <perror@plt>
jmp    127a <main+0xfa>
lea    -0x60(%rbp),%rsi
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    122f <main+0xaf>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
movb   $0x0,0xc(%rsp)
movl   $0x4f464946,0x8(%rsp)
4f
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
mov    $0x1ed,%esi
call   1070 <mkfifo@plt>
test   %rax,%rax
js     1242 <main+0xc2>
mov    0x2e12(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x8(%rsp),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1250 <main+0xd0>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
lea    0xe33(%rip),%r14
lea    0xe39(%rip),%r15
jmp    1215 <main+0x95>
nop
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    1200 <main+0x80>
mov    %r14,%rdi
call   1090 <perror@plt>
jmp    120b <main+0x8b>
mov    $0x1,%ebx
lea    0xdb7(%rip),%rdi
jmp    125c <main+0xdc>
mov    $0x2,%ebx
lea    0xdbc(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.FIFO = [5 x i8] c"FIFO\00"
@.str = [19 x i8] c"cannot create fifo\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [17 x i8] c"cannot open fifo\00"
@.str.2 = [13 x i8] c"no data yet!\00"
@.str.3 = [28 x i8] c"read from fifo data is: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [80 x i8]
%3 = alloca [5 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 80)
%7 = bitcast [5 x i8]* %3 to i8*
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%9 = call i32 @unlink(i8* %8)
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%11 = call i64 @mkfifo(i8* %10, i32 493)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %40
15: ; preds = %0
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %40
25: ; preds = %15
br label %26
26: ; preds = %25, %38
%27 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%28 = call i32 @memset(i8* %27, i32 0, i32 80)
%29 = load i32, i32* %4
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 80)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %38
35: ; preds = %26
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%37 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %33
%39 = call i32 @sleep(i32 1)
br label %26
40: ; preds = %23, %13
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@__const.main.FIFO = [5 x i8] c"FIFO\00"
@.str = [19 x i8] c"cannot create fifo\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [17 x i8] c"cannot open fifo\00"
@.str.2 = [13 x i8] c"no data yet!\00"
@.str.3 = [28 x i8] c"read from fifo data is: %s\0A\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 80)
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%6 = call i32 @unlink(i8* %5)
%7 = call i64 @mkfifo(i8* %5, i32 493)
%8 = icmp slt i64 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %0
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %10
%13 = call i32 @open(i8* %5, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %25, label %15
15: ; preds = %9, %23
%16 = call i32 @memset(i8* %3, i32 0, i32 80)
%17 = call i32 @read(i32 %13, i8* %3, i32 80)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %23
21: ; preds = %15
%22 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %23
23: ; preds = %21, %19
%24 = call i32 @sleep(i32 1)
br label %15
25: ; preds = %9, %0
%26 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %9 ]
%27 = phi i32 [ 1, %0 ], [ 2, %9 ]
%28 = call i32 @perror(i8* %26)
ret i32 %27
}