#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*/ F_OK;
int O_RDONLY;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int mkfifo(char *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int fifo_fd = -1;
  int fd = -1;
  int flag = O_RDONLY;
  char buf[255];
  int buf_size = 255;
  int cnt = 0;
  if (access("/tmp/test_fifo", F_OK) < 0) {
    fifo_fd = mkfifo("/tmp/test_fifo", 0777);
    if (fifo_fd < 0) {
      perror("mkfifo error\n");
      exit(0);
    }
  }
  fd = open("/tmp/test_fifo", flag);
  if (fd < 0) {
    perror("fifo open error\n");
    exit(0);
  }
  do {
    cnt = read(fd, buf, buf_size);
    if (cnt > 0) {
      printf("read data from fifo: %s\n", buf);
    }
  } while (cnt > 0);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0xff,-0x124(%rbp)
movl   $0x0,-0x128(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
call   1080 <access@plt>
cmp    $0x0,%rax
jge    121c <main+0x9c>
lea    0xe13(%rip),%rdi
mov    $0x1ff,%esi
call   1060 <mkfifo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1217 <main+0x97>
lea    0xe04(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    121c <main+0x9c>
mov    -0x1c(%rbp),%esi
lea    0xdda(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    124b <main+0xcb>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1250 <main+0xd0>
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x124(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0x0,-0x128(%rbp)
jle    128b <main+0x10b>
lea    -0x120(%rbp),%rsi
lea    0xda8(%rip),%rdi
call   1030 <printf@plt>
jmp    1290 <main+0x110>
cmpl   $0x0,-0x128(%rbp)
jg     1250 <main+0xd0>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    0x2e56(%rip),%rax
mov    (%rax),%ebx
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1080 <access@plt>
test   %rax,%rax
jns    11c3 <main+0x43>
lea    0xe4b(%rip),%rdi
mov    $0x1ff,%esi
call   1060 <mkfifo@plt>
test   %eax,%eax
js     123c <main+0xbc>
lea    0xe36(%rip),%rdi
mov    %ebx,%esi
call   1070 <open@plt>
test   %eax,%eax
js     1233 <main+0xb3>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xff,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    121e <main+0x9e>
lea    0xe3d(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     1200 <main+0x80>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde3(%rip),%rdi
jmp    1243 <main+0xc3>
lea    0xdcc(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [15 x i8] c"/tmp/test_fifo\00"
@F_OK = global i32 0
@.str.1 = [14 x i8] c"mkfifo error\0A\00"
@.str.2 = [17 x i8] c"fifo open error\0A\00"
@.str.3 = [25 x i8] c"read data from fifo: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [255 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
%12 = load i32, i32* @O_RDONLY
store i32 %12, i32* %8
store i32 255, i32* %10
store i32 0, i32* %11
%13 = load i32, i32* @F_OK
%14 = call i64 @access(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %2
%17 = call i32 @mkfifo(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 511)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %16
br label %24
24: ; preds = %23, %2
%25 = load i32, i32* %8
%26 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %24
br label %33
33: ; preds = %44, %32
%34 = load i32, i32* %7
%35 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%36 = load i32, i32* %10
%37 = call i32 @read(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %11
%38 = load i32, i32* %11
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%42 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %40, %33
br label %44
44: ; preds = %43
%45 = load i32, i32* %11
%46 = icmp sgt i32 %45, 0
br i1 %46, label %33, label %47
47: ; preds = %44
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [15 x i8] c"/tmp/test_fifo\00"
@F_OK = global i32 0
@.str.1 = [14 x i8] c"mkfifo error\0A\00"
@.str.2 = [17 x i8] c"fifo open error\0A\00"
@.str.3 = [25 x i8] c"read data from fifo: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [255 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @F_OK
%7 = tail call i64 @access(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = tail call i32 @mkfifo(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 511)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %9, %2
%16 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%17 = icmp slt i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = call i32 @read(i32 %16, i8* %5, i32 255)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %24, label %28
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %18, %24
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%26 = call i32 @read(i32 %16, i8* %5, i32 255)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %24, label %28
28: ; preds = %24, %18
%29 = call i32 @close(i32 %16)
ret i32 0
}