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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ mkfifo(char *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, scalar_t__);
int main() {
  char *fn = (char *)"temp.fifo";
  char out[20] = "FIFO's are fun!", in[20];
  int rfd, wfd;
  if (mkfifo(fn, 0666) != 0)
    perror("mkfifo() error");
  else {
    if ((rfd = open(fn, O_RDONLY | O_NONBLOCK)) < 0)
      perror("open() error for read end");
    else {
      if ((wfd = open(fn, O_WRONLY)) < 0)
        perror("open() error for write end");
      else {
        if (write(wfd, out, strlen(out) + 1) == -1)
          perror("write() error");
        else if (read(rfd, in, sizeof(in)) == -1)
          perror("read() error");
        else
          printf("read '%s' from the FIFO\n", in);
        close(wfd);
      }
      close(rfd);
    }
    unlink(fn);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xedf(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xedc(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xeda(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   1090 <mkfifo@plt>
cmp    $0x0,%rax
je     11f2 <main+0x62>
lea    0xe22(%rip),%rdi
call   10b0 <perror@plt>
jmp    12f6 <main+0x166>
mov    -0x10(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jge    122a <main+0x9a>
lea    0xdf9(%rip),%rdi
call   10b0 <perror@plt>
jmp    12ed <main+0x15d>
mov    -0x10(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
jge    1259 <main+0xc9>
lea    0xde4(%rip),%rdi
call   10b0 <perror@plt>
jmp    12e5 <main+0x155>
mov    -0x58(%rbp),%eax
mov    %eax,-0x64(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    129d <main+0x10d>
lea    0xdbb(%rip),%rdi
call   10b0 <perror@plt>
jmp    12dd <main+0x14d>
mov    -0x54(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
jne    12c8 <main+0x138>
lea    0xd9e(%rip),%rdi
call   10b0 <perror@plt>
jmp    12d8 <main+0x148>
lea    -0x50(%rbp),%rsi
lea    0xd96(%rip),%rdi
call   1060 <printf@plt>
jmp    12dd <main+0x14d>
mov    -0x58(%rbp),%edi
call   1070 <close@plt>
mov    -0x54(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
movaps 0xef1(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
lea    0xe4e(%rip),%rdi
mov    $0x1b6,%esi
call   1090 <mkfifo@plt>
test   %rax,%rax
jne    1279 <main+0xe9>
mov    0x2e14(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe22(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1287 <main+0xf7>
mov    %eax,%r14d
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    0xe02(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1295 <main+0x105>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
lea    0x20(%rsp),%rsi
mov    %r14d,%edi
mov    $0x14,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     12b1 <main+0x121>
lea    0xe20(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%edi
call   1070 <close@plt>
lea    0xd97(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd8a(%rip),%rdi
call   10b0 <perror@plt>
jmp    126e <main+0xde>
lea    0xd8b(%rip),%rdi
call   10b0 <perror@plt>
jmp    1262 <main+0xd2>
lea    0xd97(%rip),%rdi
call   10b0 <perror@plt>
jmp    125a <main+0xca>
lea    0xda4(%rip),%rdi
call   10b0 <perror@plt>
jmp    1253 <main+0xc3>
lea    0xda4(%rip),%rdi
call   10b0 <perror@plt>
jmp    1253 <main+0xc3>

LLVM IR (-O0):
@.str = [10 x i8] c"temp.fifo\00"
@__const.main.out = [20 x i8] c"FIFO's are fun!\00\00\00\00\00"
@.str.1 = [15 x i8] c"mkfifo() error\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [26 x i8] c"open() error for read end\00"
@O_WRONLY = global i32 0
@.str.3 = [27 x i8] c"open() error for write end\00"
@.str.4 = [14 x i8] c"write() error\00"
@.str.5 = [13 x i8] c"read() error\00"
@.str.6 = [25 x i8] c"read '%s' from the FIFO\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [20 x i8]
%4 = alloca [20 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %2
%7 = bitcast [20 x i8]* %3 to i8*
%8 = load i8*, i8** %2
%9 = call i64 @mkfifo(i8* %8, i32 438)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %59
13: ; preds = %0
%14 = load i8*, i8** %2
%15 = load i32, i32* @O_RDONLY
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
store i32 %18, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %56
22: ; preds = %13
%23 = load i8*, i8** %2
%24 = load i32, i32* @O_WRONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %53
29: ; preds = %22
%30 = load i32, i32* %6
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%33 = call i64 @strlen(i8* %32)
%34 = add nsw i64 %33, 1
%35 = call i32 @write(i32 %30, i8* %31, i64 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %50
39: ; preds = %29
%40 = load i32, i32* %5
%41 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 20)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %49
46: ; preds = %39
%47 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%48 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %46, %44
br label %50
50: ; preds = %49, %37
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50, %27
%54 = load i32, i32* %5
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53, %20
%57 = load i8*, i8** %2
%58 = call i32 @unlink(i8* %57)
br label %59
59: ; preds = %56, %11
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@.str = [10 x i8] c"temp.fifo\00"
@__const.main.out = [20 x i8] c"FIFO's are fun!\00\00\00\00\00"
@.str.1 = [15 x i8] c"mkfifo() error\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [26 x i8] c"open() error for read end\00"
@O_WRONLY = global i32 0
@.str.3 = [27 x i8] c"open() error for write end\00"
@.str.4 = [14 x i8] c"write() error\00"
@.str.5 = [13 x i8] c"read() error\00"
@.str.6 = [25 x i8] c"read '%s' from the FIFO\0A\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = tail call i64 @mkfifo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 438)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %43
9: ; preds = %0
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %41
17: ; preds = %9
%18 = load i32, i32* @O_WRONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %39
23: ; preds = %17
%24 = call i64 @strlen(i8* %3)
%25 = add nsw i64 %24, 1
%26 = call i32 @write(i32 %19, i8* %3, i64 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %37
30: ; preds = %23
%31 = call i32 @read(i32 %13, i8* %4, i32 20)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %37
35: ; preds = %30
%36 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %4)
br label %37
37: ; preds = %33, %35, %28
%38 = call i32 @close(i32 %19)
br label %39
39: ; preds = %37, %21
%40 = call i32 @close(i32 %13)
br label %41
41: ; preds = %39, %15
%42 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %7
ret i32 0
}