#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 */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int S_IRWXG;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ memset(char *, int, int);
scalar_t__ mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd;
  int pid;
  if (mkfifo("test.fifo", (S_IRWXU | S_IRWXG))) {
    perror("error make fifo file");
    return -1;
  }
  pid = fork();
  if (pid < 0) {
    perror("error folk");
    return -1;
  }
  if (pid) {
    fd = open("test.fifo", O_WRONLY);
    if (fd <= 0) {
      perror("error open file");
    }
    write(fd, "abcdefg", 1024);
  } else {
    char buf[32];
    memset(buf, 0x00, sizeof(buf));
    sleep(1);
    fd = open("test.fifo", O_RDONLY);
    if (fd <= 0) {
      perror("error open file");
    }
    read(fd, buf, sizeof(buf));
    printf("read from fifo:\n");
    printf("%s\n", buf);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe38(%rip),%rdi
call   1080 <mkfifo@plt>
cmp    $0x0,%rax
je     11ef <main+0x4f>
lea    0xe2c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f4 <main+0x154>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    121b <main+0x7b>
lea    0xe15(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f4 <main+0x154>
cmpl   $0x0,-0xc(%rbp)
je     126c <main+0xcc>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    0xdcb(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     1253 <main+0xb3>
lea    0xddc(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xddd(%rip),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
jmp    12e5 <main+0x145>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
lea    0xd6a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     12b4 <main+0x114>
lea    0xd7b(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1070 <read@plt>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rsi
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e3a(%rip),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe40(%rip),%rdi
call   1080 <mkfifo@plt>
test   %rax,%rax
jne    127e <main+0xde>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     1287 <main+0xe7>
je     1213 <main+0x73>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    0xe11(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    129a <main+0xfa>
lea    0xe35(%rip),%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1040 <write@plt>
jmp    126d <main+0xcd>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    0xdc4(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    12ab <main+0x10b>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x20,%edx
call   1070 <read@plt>
lea    0xde2(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd85(%rip),%rdi
jmp    128e <main+0xee>
lea    0xd91(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1276 <main+0xd6>
lea    0xd89(%rip),%rdi
call   10a0 <perror@plt>
jmp    11fe <main+0x5e>
lea    0xd78(%rip),%rdi
call   10a0 <perror@plt>
jmp    1247 <main+0xa7>

LLVM IR (-O0):
@.str = [10 x i8] c"test.fifo\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@.str.1 = [21 x i8] c"error make fifo file\00"
@.str.2 = [11 x i8] c"error folk\00"
@O_WRONLY = global i32 0
@.str.3 = [16 x i8] c"error open file\00"
@.str.4 = [8 x i8] c"abcdefg\00"
@O_RDONLY = global i32 0
@.str.5 = [17 x i8] c"read from fifo:\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [32 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @S_IRWXU
%6 = load i32, i32* @S_IRWXG
%7 = or i32 %5, %6
%8 = call i64 @mkfifo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %51
12: ; preds = %0
%13 = call i32 (...) @fork()
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %51
18: ; preds = %12
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* %2
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 1024)
br label %48
31: ; preds = %18
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 32)
%34 = call i32 @sleep(i32 1)
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %35)
store i32 %36, i32* %2
%37 = load i32, i32* %2
%38 = icmp sle i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %31
%42 = load i32, i32* %2
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 32)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %41, %28
%49 = load i32, i32* %2
%50 = call i32 @close(i32 %49)
store i32 0, i32* %1
br label %51
51: ; preds = %48, %16, %10
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@.str = [10 x i8] c"test.fifo\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@.str.1 = [21 x i8] c"error make fifo file\00"
@.str.2 = [11 x i8] c"error folk\00"
@O_WRONLY = global i32 0
@.str.3 = [16 x i8] c"error open file\00"
@.str.4 = [8 x i8] c"abcdefg\00"
@O_RDONLY = global i32 0
@str = [16 x i8] c"read from fifo:\00"
define i32 @main() {
%1 = alloca [32 x i8]
%2 = load i32, i32* @S_IRWXU
%3 = load i32, i32* @S_IRWXG
%4 = or i32 %3, %2
%5 = tail call i64 @mkfifo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %40
9: ; preds = %0
%10 = tail call i32 (...) @fork()
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %40
14: ; preds = %9
%15 = icmp eq i32 %10, 0
br i1 %15, label %24, label %16
16: ; preds = %14
%17 = load i32, i32* @O_WRONLY
%18 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = tail call i32 @write(i32 %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 1024)
br label %37
24: ; preds = %14
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 32)
%27 = call i32 @sleep(i32 1)
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = icmp slt i32 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %24
%34 = call i32 @read(i32 %29, i8* %25, i32 32)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%36 = call i32 @puts(i8* %25)
br label %37
37: ; preds = %33, %22
%38 = phi i32 [ %18, %22 ], [ %29, %33 ]
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %37, %12, %7
%41 = phi i32 [ -1, %7 ], [ -1, %12 ], [ 0, %37 ]
ret i32 %41
}