#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ exit(int);
int mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char msg[64];
  int fd, nread, cnt;
  if (mkfifo("./fifo", 0666) == -1) {
    printf("fail to create fifo file\n");
    exit(1);
  }
  if ((fd = open("./fifo", O_RDWR)) < 0) {
    printf("fail to open fifo file\n");
    exit(1);
  }
  for (cnt = 0; cnt < 3; cnt++) {
    if ((nread = read(fd, msg, 64)) < 0) {
      printf("fail to read from fifo file\n");
      exit(1);
    }
    printf("recv: %s\n", msg);
  }
  unlink("./fifo");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x1b6,%esi
call   1060 <mkfifo@plt>
cmp    $0xffffffff,%eax
jne    11a1 <main+0x41>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jge    11da <main+0x7a>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x5c(%rbp)
cmpl   $0x3,-0x5c(%rbp)
jge    1240 <main+0xe0>
mov    -0x54(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
jge    1220 <main+0xc0>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x50(%rbp),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    11e1 <main+0x81>
lea    0xdb9(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
lea    0xe82(%rip),%rdi
mov    $0x1b6,%esi
call   1070 <mkfifo@plt>
cmp    $0xffffffff,%eax
je     1241 <main+0xd1>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     124a <main+0xda>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1238 <main+0xc8>
lea    0xe3d(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1238 <main+0xc8>
lea    0xe16(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1238 <main+0xc8>
lea    0xdef(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd7(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd2(%rip),%rdi
jmp    1251 <main+0xe1>
lea    0xdfc(%rip),%rdi
jmp    1251 <main+0xe1>
lea    0xddc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"./fifo\00"
@.str.1 = [26 x i8] c"fail to create fifo file\0A\00"
@O_RDWR = global i32 0
@.str.2 = [24 x i8] c"fail to open fifo file\0A\00"
@.str.3 = [29 x i8] c"fail to read from fifo file\0A\00"
@.str.4 = [10 x i8] c"recv: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @mkfifo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 438)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
store i32 0, i32* %5
br label %19
19: ; preds = %33, %18
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = load i32, i32* %3
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 64)
store i32 %25, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %30
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %19
36: ; preds = %19
%37 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [7 x i8] c"./fifo\00"
@O_RDWR = global i32 0
@.str.4 = [10 x i8] c"recv: %s\0A\00"
@str = [28 x i8] c"fail to read from fifo file\00"
@str.5 = [23 x i8] c"fail to open fifo file\00"
@str.6 = [25 x i8] c"fail to create fifo file\00"
define i32 @main() {
%1 = alloca [64 x i8]
%2 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @mkfifo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 438)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = call i32 @read(i32 %10, i8* %2, i32 64)
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %21
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %25, %21, %12
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%23 = call i32 @read(i32 %10, i8* %2, i32 64)
%24 = icmp slt i32 %23, 0
br i1 %24, label %18, label %25
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%27 = call i32 @read(i32 %10, i8* %2, i32 64)
%28 = icmp slt i32 %27, 0
br i1 %28, label %18, label %29
29: ; preds = %25
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%31 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
ret i32 0
}