#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*/ wr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd;
  char to_companion[] = "lab072";
  char wr[64];
  (void)umask(0);
  printf("Waiting for connection...\n");
  while (access(to_companion, 0) == -1)
    ;
  printf("completed!\n");
  if ((fd = open(to_companion, O_WRONLY)) < 0) {
    printf("Can\'t open FIFO for writting\n");
    exit(-1);
  }
  FILE *file;
  char *fname = "lab07_01.c";
  file = fopen(fname, "r");
  while (fgets(wr, sizeof(wr), file)) {
    write(fd, wr, sizeof(wr));
  }
  fclose(file);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe4b(%rip),%eax
mov    %eax,-0xf(%rbp)
mov    0xe45(%rip),%ax
mov    %ax,-0xb(%rbp)
mov    0xe3d(%rip),%al
mov    %al,-0x9(%rbp)
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xe2d(%rip),%rdi
call   1050 <printf@plt>
lea    -0xf(%rbp),%rdi
xor    %esi,%esi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
jne    11f8 <main+0x58>
jmp    11df <main+0x3f>
lea    0xe23(%rip),%rdi
call   1050 <printf@plt>
lea    -0xf(%rbp),%rdi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1238 <main+0x98>
lea    0xe05(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xe0d(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    $0x40,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1289 <main+0xe9>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1030 <write@plt>
jmp    1257 <main+0xb7>
mov    -0x58(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
movl   $0x323730,0xb(%rsp)
movl   $0x3062616c,0x8(%rsp)
30
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
je     11d0 <main+0x30>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1280 <main+0xe0>
mov    %eax,%r14d
lea    0xdfb(%rip),%rdi
lea    0xdff(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1265 <main+0xc5>
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x40,%edx
call   1040 <write@plt>
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1240 <main+0xa0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@__const.main.to_companion = [7 x i8] c"lab072\00"
@.str = [27 x i8] c"Waiting for connection...\0A\00"
@.str.1 = [12 x i8] c"completed!\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [30 x i8] c"Can't open FIFO for writting\0A\00"
@.str.3 = [11 x i8] c"lab07_01.c\00"
@.str.4 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [7 x i8]
%4 = alloca [64 x i8]
%5 = alloca i32*
%6 = alloca i8*
store i32 0, i32* %1
%7 = bitcast [7 x i8]* %3 to i8*
%8 = call i32 @umask(i32 0)
%9 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %14, %0
%11 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%12 = call i32 @access(i8* %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %10
br label %10
15: ; preds = %10
%16 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %2
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %15
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8** %6
%25 = load i8*, i8** %6
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %26, i32** %5
br label %27
27: ; preds = %32, %24
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%29 = load i32*, i32** %5
%30 = call i64 @fgets(i8* %28, i32 64, i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* %2
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%35 = call i32 @write(i32 %33, i8* %34, i32 64)
br label %27
36: ; preds = %27
%37 = load i32*, i32** %5
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
ret i32 0
}

LLVM IR (-O2):
@__const.main.to_companion = [7 x i8] c"lab072\00"
@O_WRONLY = global i32 0
@.str.3 = [11 x i8] c"lab07_01.c\00"
@.str.4 = [2 x i8] c"r\00"
@str = [26 x i8] c"Waiting for connection...\00"
@str.5 = [11 x i8] c"completed!\00"
@str.6 = [29 x i8] c"Can't open FIFO for writting\00"
define i32 @main() {
%1 = alloca [7 x i8]
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @umask(i32 0)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %7
7: ; preds = %7, %0
%8 = call i32 @access(i8* %3, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %7, label %10
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.5, i64 0, i64 0))
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* %3, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.6, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %10
%19 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%20 = call i64 @fgets(i8* %4, i32 64, i32* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %18, %22
%23 = call i32 @write(i32 %13, i8* %4, i32 64)
%24 = call i64 @fgets(i8* %4, i32 64, i32* %19)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %22
26: ; preds = %22, %18
%27 = call i32 @fclose(i32* %19)
%28 = call i32 @close(i32 %13)
ret i32 0
}