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

/* Variables and functions */
scalar_t__ EEXIST;
int O_CREAT;
int O_EXCL;
int O_NONBLOCK;
int O_RDONLY;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ mkfifo(char *, int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv) {
  char buf[100];
  int fd;
  if (mkfifo("/tmp/myfifio", O_CREAT | O_EXCL) < 0) {
    printf("creat fifo error!\r\n");
    if (EEXIST != errno) {
      printf("EEXIST != errno!\r\n");
      exit(1);
    }
  }
  printf("preparing for reading bytes...\r\n");
  memset(buf, 0, sizeof(buf));
  fd = open("/tmp/myfifio", O_RDONLY | O_NONBLOCK, 0);
  if (-1 == fd) {
    printf("open fifo error!\r\n");
    exit(1);
  }
  while (1) {
    memset(buf, 0, sizeof(buf));
    if (-1 == read(fd, buf, 100)) {
      printf("read fifo error!\r\n");
    }
    printf("read %s from %s\r\n", buf, "/tmp/myfifio");
    sleep(2);
  }
  pause();
  unlink("/tmp/myfifio");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1060 <mkfifo@plt>
cmp    $0x0,%rax
jge    11f6 <main+0x86>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
je     11f1 <main+0x81>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11f6 <main+0x86>
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <memset@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
or     (%rax),%esi
lea    0xdd3(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x84(%rbp)
mov    $0xffffffff,%eax
cmp    -0x84(%rbp),%eax
jne    1263 <main+0xf3>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1268 <main+0xf8>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <memset@plt>
mov    -0x84(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12a9 <main+0x139>
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xdc7(%rip),%rdi
lea    0xd45(%rip),%rdx
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
jmp    1268 <main+0xf8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    0x2e65(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1070 <mkfifo@plt>
test   %rax,%rax
jns    11d5 <main+0x55>
lea    0xead(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
jne    1290 <main+0x110>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    0x2de1(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdf7(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1287 <main+0x107>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xe1e(%rip),%r14
lea    0xde5(%rip),%r15
lea    0xdd1(%rip),%r12
jmp    125a <main+0xda>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    1240 <main+0xc0>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    1240 <main+0xc0>
lea    0xdc3(%rip),%rdi
jmp    1297 <main+0x117>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/tmp/myfifio\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [20 x i8] c"creat fifo error!\0D\0A\00"
@EEXIST = global i64 0
@errno = global i64 0
@.str.2 = [19 x i8] c"EEXIST != errno!\0D\0A\00"
@.str.3 = [33 x i8] c"preparing for reading bytes...\0D\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [19 x i8] c"open fifo error!\0D\0A\00"
@.str.5 = [19 x i8] c"read fifo error!\0D\0A\00"
@.str.6 = [18 x i8] c"read %s from %s\0D\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [100 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = call i64 @mkfifo(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%15 = load i64, i64* @EEXIST
%16 = load i64, i64* @errno
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %13
br label %22
22: ; preds = %21, %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%25 = call i32 @memset(i8* %24, i32 0, i32 100)
%26 = load i32, i32* @O_RDONLY
%27 = load i32, i32* @O_NONBLOCK
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %28, i32 0)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp eq i32 -1, %30
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %22
br label %36
36: ; preds = %35, %45
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i32 0, i32 100)
%39 = load i32, i32* %7
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 100)
%42 = icmp eq i32 -1, %41
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %46, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%48 = call i32 @sleep(i32 2)
br label %36
}

LLVM IR (-O2):
@.str = [13 x i8] c"/tmp/myfifio\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@EEXIST = global i64 0
@errno = global i64 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.6 = [18 x i8] c"read %s from %s\0D\0A\00"
@str = [32 x i8] c"preparing for reading bytes...\0D\00"
@str.7 = [18 x i8] c"read fifo error!\0D\00"
@str.8 = [18 x i8] c"open fifo error!\0D\00"
@str.9 = [19 x i8] c"creat fifo error!\0D\00"
@str.10 = [18 x i8] c"EEXIST != errno!\0D\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_EXCL
%7 = or i32 %6, %5
%8 = tail call i64 @mkfifo(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
%12 = load i64, i64* @EEXIST
%13 = load i64, i64* @errno
%14 = icmp eq i64 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.10, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10, %2
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%20 = call i32 @memset(i8* %4, i32 0, i32 100)
%21 = load i32, i32* @O_RDONLY
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %22, %21
%24 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %23, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %18, %35
%30 = call i32 @memset(i8* %4, i32 0, i32 100)
%31 = call i32 @read(i32 %24, i8* %4, i32 100)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @sleep(i32 2)
br label %29
}