#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 */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int read(int, char *, char);
int main(void) {
  puts("Server - listening");
  int code = mkfifo("/tmp/demo6_fifo", 0666);
  if (code == -1) {
    perror("mkfifo returned an error - file may already exist");
  }
  int fd = open("/tmp/demo6_fifo", O_RDONLY);
  if (fd == -1) {
    perror("Cannot open FIFO for read");
    return EXIT_FAILURE;
  }
  puts("FIFO OPEN");
  char stringBuffer[256];
  memset(stringBuffer, 0, 256);
  int res;
  char Len;
  while (1) {
    res = read(fd, &Len, 1);
    if (res == 0) {
      break;
    }
    read(fd, stringBuffer, Len);
    stringBuffer[(int)Len] = 0;
    printf("Server received: %s\n", stringBuffer);
  }
  puts("EOF found");
  close(fd);
  puts("FIFO Closed");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6e(%rip),%rdi
mov    $0x1b6,%esi
call   1080 <mkfifo@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c8 <main+0x48>
lea    0xe60(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1207 <main+0x87>
lea    0xe64(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12bd <main+0x13d>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x115(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jne    1252 <main+0xd2>
jmp    1291 <main+0x111>
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
movsbl -0x115(%rbp),%edx
call   1070 <read@plt>
movsbl -0x115(%rbp),%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xdf2(%rip),%rdi
call   1040 <printf@plt>
jmp    1226 <main+0xa6>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xdec(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe71(%rip),%rdi
mov    $0x1b6,%esi
call   1080 <mkfifo@plt>
cmp    $0xffffffff,%eax
je     128d <main+0x10d>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     129e <main+0x11e>
mov    %eax,%ebp
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1255 <main+0xd5>
lea    0x10(%rsp),%r14
lea    0xe66(%rip),%r15
lea    0xf(%rsp),%r12
nopl   0x0(%rax,%rax,1)
movsbq 0xf(%rsp),%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1070 <read@plt>
movb   $0x0,0x10(%rsp,%rbx,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    1220 <main+0xa0>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8f(%rip),%rdi
call   10a0 <perror@plt>
jmp    11b5 <main+0x35>
lea    0xdb0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d27(%rip),%rax
jmp    127b <main+0xfb>

LLVM IR (-O0):
@.str = [19 x i8] c"Server - listening\00"
@.str.1 = [16 x i8] c"/tmp/demo6_fifo\00"
@.str.2 = [50 x i8] c"mkfifo returned an error - file may already exist\00"
@O_RDONLY = global i32 0
@.str.3 = [26 x i8] c"Cannot open FIFO for read\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [10 x i8] c"FIFO OPEN\00"
@.str.5 = [21 x i8] c"Server received: %s\0A\00"
@.str.6 = [10 x i8] c"EOF found\00"
@.str.7 = [12 x i8] c"FIFO Closed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca i8
store i32 0, i32* %1
%7 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @mkfifo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 438)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
store i32 %20, i32* %1
br label %48
21: ; preds = %13
%22 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 256)
br label %25
25: ; preds = %21, %31
%26 = load i32, i32* %3
%27 = call i32 @read(i32 %26, i8* %6, i8 signext 1)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
br label %42
31: ; preds = %25
%32 = load i32, i32* %3
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%34 = load i8, i8* %6
%35 = call i32 @read(i32 %32, i8* %33, i8 signext %34)
%36 = load i8, i8* %6
%37 = sext i8 %36 to i32
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %40)
br label %25
42: ; preds = %30
%43 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
%46 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%47 = load i32, i32* @EXIT_SUCCESS
store i32 %47, i32* %1
br label %48
48: ; preds = %42, %18
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@.str = [19 x i8] c"Server - listening\00"
@.str.1 = [16 x i8] c"/tmp/demo6_fifo\00"
@.str.2 = [50 x i8] c"mkfifo returned an error - file may already exist\00"
@O_RDONLY = global i32 0
@.str.3 = [26 x i8] c"Cannot open FIFO for read\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [10 x i8] c"FIFO OPEN\00"
@.str.5 = [21 x i8] c"Server received: %s\0A\00"
@.str.6 = [10 x i8] c"EOF found\00"
@.str.7 = [12 x i8] c"FIFO Closed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca i8
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @mkfifo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 438)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
br label %34
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 256)
%19 = call i32 @read(i32 %10, i8* %2, i8 signext 1)
%20 = icmp eq i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %15, %21
%22 = load i8, i8* %2
%23 = call i32 @read(i32 %10, i8* %17, i8 signext %22)
%24 = sext i8 %22 to i64
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %17)
%27 = call i32 @read(i32 %10, i8* %2, i8 signext 1)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %21
29: ; preds = %21, %15
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%31 = call i32 @close(i32 %10)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%33 = load i32, i32* @EXIT_SUCCESS
br label %34
34: ; preds = %29, %12
%35 = phi i32 [ %14, %12 ], [ %33, %29 ]
ret i32 %35
}