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

/* Variables and functions */
char EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void test2() {
  FILE *fp0, *fp1;
  char ch;
  pid_t childid;
  printf("\nCase: \tWriting longer data / full fifos behaviour test\n\n");
  printf("Opening read and write ends of fifo 0\n");
  fp0 = fopen("/dev/fifo0", "w");
  fp1 = fopen("/dev/fifo1", "r");
  if (fp0 == NULL || fp1 == NULL) {
    perror("Error while opening fifo.\n");
    exit(EXIT_FAILURE);
  }
  printf("Forking new process for writer and parent as reader\n");
  childid = fork();
  if (childid > 0) {
    fclose(fp0);
    printf("Reading from fifo. Read: \n");
    while ((ch = fgetc(fp1)) != EOF)
      printf("%c", ch);
    fclose(fp1);
  } else if (childid == 0) {
    fclose(fp1);
    printf("Writing long data to fifo\n");
    fwrite("Yolo. Let's test this with a long message :D\n", 1,
           sizeof("Yolo. Let's test this with a long message :D\n"), fp0);
    fclose(fp0);
  } else {
    printf("Failed to fork.\n");
    exit(0);
  }
}


x86 assembly (clang, -O0):
<test2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb6(%rip),%rdi
lea    0xeba(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
lea    0xeac(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11e8 <test2+0x68>
cmpq   $0x0,-0x10(%rbp)
jne    1202 <test2+0x82>
lea    0xe8c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1283 <test2+0x103>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
mov    0x2d95(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1275 <test2+0xf5>
movsbl -0x11(%rbp),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123d <test2+0xbd>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12e7 <test2+0x167>
cmpq   $0x0,-0x20(%rbp)
jne    12cd <test2+0x14d>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rcx
lea    0xe54(%rip),%rdi
mov    $0x1,%esi
mov    $0x2e,%edx
call   1090 <fwrite@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12e2 <test2+0x162>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    12e7 <test2+0x167>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test2>:
push   %r14
push   %rbx
push   %rax
lea    0xec6(%rip),%rdi
call   1040 <puts@plt>
lea    0xef3(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4d(%rip),%rdi
lea    0xe51(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
lea    0xe44(%rip),%rdi
lea    0xf22(%rip),%rsi
call   1070 <fopen@plt>
test   %r14,%r14
je     1288 <test2+0xf8>
mov    %rax,%rbx
test   %rax,%rax
je     1288 <test2+0xf8>
lea    0xecf(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jle    124a <test2+0xba>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xf0d(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    0x2dc3(%rip),%r14
cmp    (%r14),%al
je     1245 <test2+0xb5>
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r14),%al
jne    1230 <test2+0xa0>
mov    %rbx,%rdi
jmp    127c <test2+0xec>
jne    12a2 <test2+0x112>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xea9(%rip),%rdi
call   1040 <puts@plt>
lea    0xdcc(%rip),%rdi
mov    $0x1,%esi
mov    $0x2e,%edx
mov    %r14,%rcx
call   10a0 <fwrite@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <fclose@plt>
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [58 x i8] c"\0ACase: \09Writing longer data / full fifos behaviour test\0A\0A\00"
@.str.1 = [39 x i8] c"Opening read and write ends of fifo 0\0A\00"
@.str.2 = [11 x i8] c"/dev/fifo0\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [11 x i8] c"/dev/fifo1\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [27 x i8] c"Error while opening fifo.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.7 = [53 x i8] c"Forking new process for writer and parent as reader\0A\00"
@.str.8 = [27 x i8] c"Reading from fifo. Read: \0A\00"
@EOF = global i8 0
@.str.9 = [3 x i8] c"%c\00"
@.str.10 = [27 x i8] c"Writing long data to fifo\0A\00"
@.str.11 = [46 x i8] c"Yolo. Let's test this with a long message :D\0A\00"
@.str.12 = [17 x i8] c"Failed to fork.\0A\00"
define void @test2() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i8
%4 = alloca i64
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %7, i32** %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %1
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = load i32*, i32** %2
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11, %0
%15 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %11
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0))
%20 = call i64 (...) @fork()
store i64 %20, i64* %4
%21 = load i64, i64* %4
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %41
23: ; preds = %18
%24 = load i32*, i32** %1
%25 = call i32 @fclose(i32* %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0))
br label %27
27: ; preds = %34, %23
%28 = load i32*, i32** %2
%29 = call signext i8 @fgetc(i32* %28)
store i8 %29, i8* %3
%30 = sext i8 %29 to i32
%31 = load i8, i8* @EOF
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %30, %32
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = load i8, i8* %3
%36 = sext i8 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 %36)
br label %27
38: ; preds = %27
%39 = load i32*, i32** %2
%40 = call i32 @fclose(i32* %39)
br label %56
41: ; preds = %18
%42 = load i64, i64* %4
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %41
%45 = load i32*, i32** %2
%46 = call i32 @fclose(i32* %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%48 = load i32*, i32** %1
%49 = call i32 @fwrite(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.11, i64 0, i64 0), i32 1, i32 46, i32* %48)
%50 = load i32*, i32** %1
%51 = call i32 @fclose(i32* %50)
br label %55
52: ; preds = %41
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %44
br label %56
56: ; preds = %55, %38
ret void
}

LLVM IR (-O2):
@.str.2 = [11 x i8] c"/dev/fifo0\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [11 x i8] c"/dev/fifo1\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [27 x i8] c"Error while opening fifo.\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
@.str.11 = [46 x i8] c"Yolo. Let's test this with a long message :D\0A\00"
@str = [57 x i8] c"\0ACase: \09Writing longer data / full fifos behaviour test\0A\00"
@str.13 = [38 x i8] c"Opening read and write ends of fifo 0\00"
@str.14 = [52 x i8] c"Forking new process for writer and parent as reader\00"
@str.15 = [16 x i8] c"Failed to fork.\00"
@str.16 = [26 x i8] c"Writing long data to fifo\00"
@str.17 = [26 x i8] c"Reading from fifo. Read: \00"
define void @test2() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.13, i64 0, i64 0))
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%5 = icmp eq i32* %3, null
%6 = icmp eq i32* %4, null
%7 = or i1 %5, %6
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.14, i64 0, i64 0))
%14 = tail call i64 (...) @fork()
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %12
%17 = tail call i32 @fclose(i32* %3)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.17, i64 0, i64 0))
%19 = tail call signext i8 @fgetc(i32* %4)
%20 = load i8, i8* @EOF
%21 = icmp eq i8 %19, %20
br i1 %21, label %29, label %22
22: ; preds = %16, %22
%23 = phi i8 [ %26, %22 ], [ %19, %16 ]
%24 = sext i8 %23 to i32
%25 = tail call i32 @putchar(i32 %24)
%26 = tail call signext i8 @fgetc(i32* %4)
%27 = load i8, i8* @EOF
%28 = icmp eq i8 %26, %27
br i1 %28, label %29, label %22
29: ; preds = %22, %16
%30 = tail call i32 @fclose(i32* %4)
br label %41
31: ; preds = %12
%32 = icmp eq i64 %14, 0
br i1 %32, label %33, label %38
33: ; preds = %31
%34 = tail call i32 @fclose(i32* %4)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.16, i64 0, i64 0))
%36 = tail call i32 @fwrite(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.11, i64 0, i64 0), i32 1, i32 46, i32* %3)
%37 = tail call i32 @fclose(i32* %3)
br label %41
38: ; preds = %31
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.15, i64 0, i64 0))
%40 = tail call i32 @exit(i32 0)
unreachable
41: ; preds = %33, %29
ret void
}