#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*/ fname;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int, int /*<<< orphan*/);
int main() {
  char fname[50], buffer[1025];
  int request, response, file, n;
  mkfifo("request.fifo", 0777);
  mkfifo("response.fifo", 0777);
  printf("Waiting for request... \n");
  request = open("request.fifo", O_RDONLY);
  response = open("response.fifo", O_WRONLY);
  read(request, fname, sizeof(fname), 0);
  printf("Received request for %s \n", fname);
  file = open(fname, O_RDONLY);
  if (file < 0) {
    write(response, "File not found. \n", 18, 0);
  } else {
    while ((n = read(file, buffer, sizeof(buffer), 0)) > 0) {
      write(response, buffer, n, 0);
    }
  }
  printf("Response sent. \n");
  close(request);
  close(response);
  unlink("request.fifo");
  unlink("response.fifo");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
mov    $0x1ff,%esi
call   1080 <mkfifo@plt>
lea    0xe73(%rip),%rdi
mov    $0x1ff,%esi
call   1080 <mkfifo@plt>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x454(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe30(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x458(%rbp)
mov    -0x454(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x32,%edx
xor    %ecx,%ecx
call   1070 <read@plt>
lea    -0x40(%rbp),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x45c(%rbp)
cmpl   $0x0,-0x45c(%rbp)
jge    1253 <main+0xe3>
mov    -0x458(%rbp),%edi
lea    0xe0c(%rip),%rsi
mov    $0x12,%edx
xor    %ecx,%ecx
call   1040 <write@plt>
jmp    12a4 <main+0x134>
jmp    1258 <main+0xe8>
mov    -0x45c(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x401,%edx
xor    %ecx,%ecx
call   1070 <read@plt>
mov    %eax,-0x460(%rbp)
cmp    $0x0,%eax
jle    129f <main+0x12f>
mov    -0x458(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    -0x460(%rbp),%edx
xor    %ecx,%ecx
call   1040 <write@plt>
jmp    1258 <main+0xe8>
jmp    12a4 <main+0x134>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x454(%rbp),%edi
call   1060 <close@plt>
mov    -0x458(%rbp),%edi
call   1060 <close@plt>
lea    0xd31(%rip),%rdi
call   1030 <unlink@plt>
lea    0xd32(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x450,%rsp
lea    0xe6a(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1090 <mkfifo@plt>
lea    0xe63(%rip),%rbp
mov    %rbp,%rdi
mov    $0x1ff,%esi
call   1090 <mkfifo@plt>
lea    0xe89(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e1e(%rip),%r12
mov    (%r12),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
mov    %eax,%r14d
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x32,%edx
xor    %ecx,%ecx
call   1080 <read@plt>
lea    0xe12(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1276 <main+0xf6>
mov    %eax,%ebx
lea    0x40(%rsp),%rsi
mov    %eax,%edi
mov    $0x401,%edx
xor    %ecx,%ecx
call   1080 <read@plt>
test   %eax,%eax
jle    128c <main+0x10c>
lea    0x40(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x401,%edx
xor    %ecx,%ecx
call   1080 <read@plt>
test   %eax,%eax
jg     1250 <main+0xd0>
jmp    128c <main+0x10c>
lea    0xdb8(%rip),%rsi
mov    %r15d,%edi
mov    $0x12,%edx
xor    %ecx,%ecx
call   1050 <write@plt>
lea    0xdcc(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %r15d,%edi
call   1070 <close@plt>
lea    0xd51(%rip),%rdi
call   1030 <unlink@plt>
lea    0xd52(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"request.fifo\00"
@.str.1 = [14 x i8] c"response.fifo\00"
@.str.2 = [25 x i8] c"Waiting for request... \0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [26 x i8] c"Received request for %s \0A\00"
@.str.4 = [18 x i8] c"File not found. \0A\00"
@.str.5 = [17 x i8] c"Response sent. \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca [1025 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 @mkfifo(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 511)
%9 = call i32 @mkfifo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 511)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %4
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 50, i32 0)
%18 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %0
%26 = load i32, i32* %5
%27 = call i32 @write(i32 %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 18, i32 0)
br label %40
28: ; preds = %0
br label %29
29: ; preds = %34, %28
%30 = load i32, i32* %6
%31 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 1025, i32 0)
store i32 %32, i32* %7
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* %5
%36 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%37 = load i32, i32* %7
%38 = call i32 @write(i32 %35, i8* %36, i32 %37, i32 0)
br label %29
39: ; preds = %29
br label %40
40: ; preds = %39, %25
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %5
%45 = call i32 @close(i32 %44)
%46 = call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%47 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"request.fifo\00"
@.str.1 = [14 x i8] c"response.fifo\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [26 x i8] c"Received request for %s \0A\00"
@.str.4 = [18 x i8] c"File not found. \0A\00"
@str = [24 x i8] c"Waiting for request... \00"
@str.6 = [16 x i8] c"Response sent. \00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = alloca [1025 x i8]
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @mkfifo(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 511)
%6 = tail call i32 @mkfifo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 511)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @read(i32 %9, i8* %3, i32 50, i32 0)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %3, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %0
%18 = call i32 @read(i32 %15, i8* %4, i32 1025, i32 0)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %22, label %27
20: ; preds = %0
%21 = call i32 @write(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 18, i32 0)
br label %27
22: ; preds = %17, %22
%23 = phi i32 [ %25, %22 ], [ %18, %17 ]
%24 = call i32 @write(i32 %11, i8* %4, i32 %23, i32 0)
%25 = call i32 @read(i32 %15, i8* %4, i32 1025, i32 0)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %22, label %27
27: ; preds = %22, %17, %20
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
%29 = call i32 @close(i32 %9)
%30 = call i32 @close(i32 %11)
%31 = call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}