#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 */
scalar_t__ EEXIST;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int main(void) {
  int fifo_fd = -1;
  char chr;
  int retval = -1;
  if (mkfifo("/tmp/lab9cfifo",
             O_CREAT | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) != 0) {
    if (errno != EEXIST) {
      perror("Creating the fifo. Error");
      exit(EXIT_FAILURE);
    }
  }
  printf("FIFO has been created \n");
  if ((fifo_fd = open("/tmp/lab9cfifo", O_RDONLY)) < 0) {
    perror("Opening the fifo for reading. Error");
    exit(EXIT_FAILURE);
  }
  printf("FIFO has been opened for reading\n");
  printf("Read end fd: %d\n", fifo_fd);
  while (read(fifo_fd, &chr, 1) > 0) {
    printf("%c\n", chr);
    if (chr == '6')
      break;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%rip),%rax
or     (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1050 <mkfifo@plt>
cmp    $0x0,%rax
je     11f9 <main+0x99>
mov    0x2df1(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
je     11f4 <main+0x94>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11f9 <main+0x99>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xde9(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1242 <main+0xe2>
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jle    12a5 <main+0x145>
movsbl -0x9(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x9(%rbp),%eax
cmp    $0x36,%eax
jne    12a0 <main+0x140>
jmp    12a5 <main+0x145>
jmp    1261 <main+0x101>
mov    0x2cec(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7a(%rip),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   1060 <mkfifo@plt>
test   %rax,%rax
je     11cf <main+0x5f>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
jne    1267 <main+0xf7>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0xe15(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     125e <main+0xee>
mov    %eax,%ebx
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
lea    0xe43(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x7(%rsp),%r14
lea    0xe3f(%rip),%r15
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    124a <main+0xda>
movsbl 0x7(%rsp),%ebp
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x36,%ebp
jne    1220 <main+0xb0>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc3(%rip),%rdi
jmp    126e <main+0xfe>
lea    0xda1(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/tmp/lab9cfifo\00"
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [25 x i8] c"Creating the fifo. Error\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [24 x i8] c"FIFO has been created \0A\00"
@O_RDONLY = global i32 0
@.str.3 = [36 x i8] c"Opening the fifo for reading. Error\00"
@.str.4 = [34 x i8] c"FIFO has been opened for reading\0A\00"
@.str.5 = [17 x i8] c"Read end fd: %d\0A\00"
@.str.6 = [4 x i8] c"%c\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %2
store i32 -1, i32* %4
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @S_IRUSR
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IWGRP
%13 = or i32 %11, %12
%14 = call i64 @mkfifo(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %25
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 %24
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %16
br label %25
25: ; preds = %24, %0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %27)
store i32 %28, i32* %2
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %25
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* %2
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %50, %34
%39 = load i32, i32* %2
%40 = call i64 @read(i32 %39, i8* %3, i32 1)
%41 = icmp sgt i64 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = load i8, i8* %3
%44 = sext i8 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %44)
%46 = load i8, i8* %3
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 54
br i1 %48, label %49, label %50
49: ; preds = %42
br label %51
50: ; preds = %42
br label %38
51: ; preds = %49, %38
%52 = load i32, i32* @EXIT_SUCCESS
ret i32 %52
}

LLVM IR (-O2):
@.str = [15 x i8] c"/tmp/lab9cfifo\00"
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [25 x i8] c"Creating the fifo. Error\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [36 x i8] c"Opening the fifo for reading. Error\00"
@.str.5 = [17 x i8] c"Read end fd: %d\0A\00"
@.str.6 = [4 x i8] c"%c\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [23 x i8] c"FIFO has been created \00"
@str.7 = [33 x i8] c"FIFO has been opened for reading\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @S_IRUSR
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRGRP
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IWGRP
%10 = or i32 %8, %9
%11 = tail call i64 @mkfifo(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %0
%14 = load i64, i64* @errno
%15 = load i64, i64* @EEXIST
%16 = icmp eq i64 %14, %15
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %13, %0
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0))
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %24)
br label %33
33: ; preds = %36, %30
%34 = call i64 @read(i32 %24, i8* %1, i32 1)
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = load i8, i8* %1
%38 = sext i8 %37 to i32
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %38)
%40 = icmp eq i8 %37, 54
br i1 %40, label %41, label %33
41: ; preds = %36, %33
%42 = load i32, i32* @EXIT_SUCCESS
ret i32 %42
}