#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;
typedef int /*<<< orphan*/ dev_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ O_RDWR;
int S_IFIFO;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int mknod(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int /*<<< orphan*/ write(int, char *, size_t);
int main() {
  char *fifo_name = "fifo_channel";
  int err = mknod(fifo_name, S_IFIFO | 0644, (dev_t)0);
  if (err != 0 && errno != EEXIST) {
    perror("mknod fails");
  }
  int fd = open(fifo_name, O_RDWR);
  pid_t pid = fork();
  char buf[200];
  memset(buf, 0, sizeof(buf));
  if (pid != 0) {
    char *test_string = "Test";
    size_t len = strlen(test_string);
    strncpy(buf, test_string, len);
    write(fd, buf, len + 1);
  } else {
    int sread = read(fd, buf, sizeof(buf));
    printf("Message: %d, %s\n", sread, buf);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe37(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
or     $0x1a4,%esi
xor    %edx,%edx
call   10a0 <mknod@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     121a <main+0x6a>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     121a <main+0x6a>
lea    0xdf8(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
lea    -0xf0(%rbp),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1070 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
je     12b2 <main+0x102>
lea    0xdba(%rip),%rax
mov    %rax,-0xf8(%rbp)
mov    -0xf8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x100(%rbp)
lea    -0xf0(%rbp),%rdi
mov    -0xf8(%rbp),%rsi
mov    -0x100(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
lea    -0xf0(%rbp),%rsi
mov    -0x100(%rbp),%rdx
add    $0x1,%rdx
call   1040 <write@plt>
jmp    12e5 <main+0x135>
mov    -0x18(%rbp),%edi
lea    -0xf0(%rbp),%rsi
mov    $0xc8,%edx
call   1090 <read@plt>
mov    %eax,-0x104(%rbp)
mov    -0x104(%rbp),%esi
lea    -0xf0(%rbp),%rdx
lea    0xd3e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0xc8,%rsp
mov    0x2e18(%rip),%rax
mov    $0x1a4,%esi
or     (%rax),%esi
lea    0xe42(%rip),%rdi
xor    %edx,%edx
call   1090 <mknod@plt>
test   %eax,%eax
je     11e3 <main+0x43>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    1276 <main+0xd6>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0xe0d(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1060 <memset@plt>
test   %rbx,%rbx
je     1240 <main+0xa0>
lea    0xdfa(%rip),%rsi
mov    %rsp,%rbx
mov    $0x4,%edx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
mov    $0x5,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
jmp    1263 <main+0xc3>
mov    %rsp,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc8,%edx
call   1080 <read@plt>
lea    0xdc5(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd90(%rip),%rdi
call   10b0 <perror@plt>
jmp    11e3 <main+0x43>

LLVM IR (-O0):
@.str = [13 x i8] c"fifo_channel\00"
@S_IFIFO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [12 x i8] c"mknod fails\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"Test\00"
@.str.3 = [17 x i8] c"Message: %d, %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca [200 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %2
%10 = load i8*, i8** %2
%11 = load i32, i32* @S_IFIFO
%12 = or i32 %11, 420
%13 = call i32 @mknod(i8* %10, i32 %12, i32 0)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %0
%17 = load i64, i64* @errno
%18 = load i64, i64* @EEXIST
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16, %0
%23 = load i8*, i8** %2
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %4
%26 = call i64 (...) @fork()
store i64 %26, i64* %5
%27 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%28 = call i32 @memset(i8* %27, i32 0, i32 200)
%29 = load i64, i64* %5
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %43
31: ; preds = %22
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %7
%32 = load i8*, i8** %7
%33 = call i64 @strlen(i8* %32)
store i64 %33, i64* %8
%34 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%35 = load i8*, i8** %7
%36 = load i64, i64* %8
%37 = call i32 @strncpy(i8* %34, i8* %35, i64 %36)
%38 = load i32, i32* %4
%39 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%40 = load i64, i64* %8
%41 = add i64 %40, 1
%42 = call i32 @write(i32 %38, i8* %39, i64 %41)
br label %50
43: ; preds = %22
%44 = load i32, i32* %4
%45 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 200)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%49 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %47, i8* %48)
br label %50
50: ; preds = %43, %31
%51 = load i32, i32* %4
%52 = call i32 @close(i32 %51)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"fifo_channel\00"
@S_IFIFO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [12 x i8] c"mknod fails\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"Test\00"
@.str.3 = [17 x i8] c"Message: %d, %s\0A\00"
define i32 @main() {
%1 = alloca [200 x i8]
%2 = load i32, i32* @S_IFIFO
%3 = or i32 %2, 420
%4 = tail call i32 @mknod(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3, i32 0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %0
%7 = load i64, i64* @errno
%8 = load i64, i64* @EEXIST
%9 = icmp eq i64 %7, %8
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %6, %0
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = tail call i64 (...) @fork()
%16 = getelementptr inbounds [200 x i8], [200 x i8]* %1, i64 0, i64 0
%17 = call i32 @memset(i8* %16, i32 0, i32 200)
%18 = icmp eq i64 %15, 0
br i1 %18, label %22, label %19
19: ; preds = %12
%20 = call i32 @strncpy(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 4)
%21 = call i32 @write(i32 %14, i8* %16, i64 5)
br label %25
22: ; preds = %12
%23 = call i32 @read(i32 %14, i8* %16, i32 200)
%24 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %23, i8* %16)
br label %25
25: ; preds = %22, %19
%26 = call i32 @close(i32 %14)
ret i32 0
}