#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ write(int, char const *, int /*<<< orphan*/);
int main() {
  pid_t pid;
  int pipe_fd[2];
  char buf[128] = {0};
  const char data[] = "Pipe Test Program";
  int real_read, real_write;
  int fd = 0;
  if (access("myfifo", F_OK) == -1) {
    if ((mkfifo("myfifo", 0666) < 0)) {
      printf("Can not create fifo file\n");
      exit(1);
    }
  }
  if ((pid = fork()) == 0) {
    fd = open("myfifo", O_WRONLY);
    if (fd == -1) {
      printf("Open write file error\n");
      exit(1);
    }
    if (write(fd, data, strlen(data)) > 0) {
      printf("Write %s to FIFO\n", data);
    }
    close(fd);
    exit(0);
  } else if (pid > 0) {
    fd = open("myfifo", O_RDONLY);
    if (fd == -1) {
      printf("Open fifo error\n");
      exit(1);
    }
    if (read(fd, buf, 128) > 0) {
      printf("Read %s from FIFO\n", buf);
    }
    close(fd);
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    0xe24(%rip),%rax
mov    %rax,-0xc0(%rbp)
mov    0xe1e(%rip),%rax
mov    %rax,-0xb8(%rbp)
mov    0xe18(%rip),%ax
mov    %ax,-0xb0(%rbp)
movl   $0x0,-0xcc(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    0xdf9(%rip),%rdi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
jne    125f <main+0xaf>
lea    0xde4(%rip),%rdi
mov    $0x1b6,%esi
call   1090 <mkfifo@plt>
cmp    $0x0,%rax
jge    125a <main+0xaa>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    125f <main+0xaf>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    131f <main+0x16f>
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
lea    0xd8e(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0xcc(%rbp)
cmpl   $0xffffffff,-0xcc(%rbp)
jne    12b4 <main+0x104>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0xcc(%rbp),%eax
mov    %eax,-0xdc(%rbp)
lea    -0xc0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
lea    -0xc0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xdc(%rbp),%edi
mov    -0xd8(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jle    130d <main+0x15d>
lea    -0xc0(%rbp),%rsi
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xcc(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    13b2 <main+0x202>
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
lea    0xcd8(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0xcc(%rbp)
cmpl   $0xffffffff,-0xcc(%rbp)
jne    136a <main+0x1ba>
lea    0xd03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0xcc(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
jle    13a0 <main+0x1f0>
lea    -0xa0(%rbp),%rsi
lea    0xcd4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xcc(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    13b7 <main+0x207>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xa8,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps 0xe0e(%rip),%xmm0
movaps %xmm0,(%rsp)
movw   $0x6d,0x10(%rsp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    0xe05(%rip),%rdi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
jne    1231 <main+0x81>
lea    0xdf4(%rip),%rdi
mov    $0x1b6,%esi
call   1090 <mkfifo@plt>
test   %rax,%rax
js     12f8 <main+0x148>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     124c <main+0x9c>
jg     1272 <main+0xc2>
xor    %eax,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0xdb6(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    1295 <main+0xe5>
lea    0xde1(%rip),%rdi
jmp    12ff <main+0x14f>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
lea    0xd90(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    12bf <main+0x10f>
lea    0xdab(%rip),%rdi
jmp    12ff <main+0x14f>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
jle    12ea <main+0x13a>
lea    0xd5f(%rip),%rdi
mov    %rsp,%rsi
jmp    12e3 <main+0x133>
mov    %eax,%ebx
lea    0x20(%rsp),%rsi
mov    %eax,%edi
mov    $0x80,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    12ea <main+0x13a>
lea    0xd4d(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd65(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@__const.main.data = [18 x i8] c"Pipe Test Program\00"
@.str = [7 x i8] c"myfifo\00"
@F_OK = global i32 0
@.str.1 = [26 x i8] c"Can not create fifo file\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [23 x i8] c"Open write file error\0A\00"
@.str.3 = [18 x i8] c"Write %s to FIFO\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [17 x i8] c"Open fifo error\0A\00"
@.str.5 = [19 x i8] c"Read %s from FIFO\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i32]
%4 = alloca [128 x i8]
%5 = alloca [18 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = bitcast [128 x i8]* %4 to i8*
%10 = bitcast [18 x i8]* %5 to i8*
store i32 0, i32* %8
%11 = load i32, i32* @F_OK
%12 = call i32 @access(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %21
14: ; preds = %0
%15 = call i64 @mkfifo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 438)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %14
br label %21
21: ; preds = %20, %0
%22 = call i64 (...) @fork()
store i64 %22, i64* %2
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %46
24: ; preds = %21
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = load i32, i32* %8
%34 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%35 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i64 @write(i32 %33, i8* %34, i32 %36)
%38 = icmp sgt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %32
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %21
%47 = load i64, i64* %2
%48 = icmp sgt i64 %47, 0
br i1 %48, label %49, label %69
49: ; preds = %46
%50 = load i32, i32* @O_RDONLY
%51 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %50)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %49
%58 = load i32, i32* %8
%59 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%60 = call i64 @read(i32 %58, i8* %59, i32 128)
%61 = icmp sgt i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %63)
br label %65
65: ; preds = %62, %57
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
%68 = call i32 @exit(i32 0)
unreachable
69: ; preds = %46
br label %70
70: ; preds = %69
ret i32 0
}

LLVM IR (-O2):
@__const.main.data = [18 x i8] c"Pipe Test Program\00"
@.str = [7 x i8] c"myfifo\00"
@F_OK = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [18 x i8] c"Write %s to FIFO\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [19 x i8] c"Read %s from FIFO\0A\00"
@str = [16 x i8] c"Open fifo error\00"
@str.6 = [22 x i8] c"Open write file error\00"
@str.7 = [25 x i8] c"Can not create fifo file\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = alloca [18 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [18 x i8], [18 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @F_OK
%6 = tail call i32 @access(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = tail call i64 @mkfifo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 438)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8, %0
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = load i32, i32* @O_WRONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = call i32 @strlen(i8* %4)
%26 = call i64 @write(i32 %19, i8* %4, i32 %25)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %30
30: ; preds = %28, %24
%31 = call i32 @close(i32 %19)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %14
%34 = icmp sgt i64 %15, 0
br i1 %34, label %35, label %50
35: ; preds = %33
%36 = load i32, i32* @O_RDONLY
%37 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = call i64 @read(i32 %37, i8* %3, i32 128)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %3)
br label %47
47: ; preds = %45, %42
%48 = call i32 @close(i32 %37)
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %33
ret i32 0
}