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

/* Variables and functions */
int S_IFIFO;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mknod(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ strlen(char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int main() {
  umask(0);
  mknod("/tmp/MYFIFO", S_IFIFO | 0666, 0);
  pid_t forkResult;
  forkResult = fork();
  if (forkResult == 0) {
    FILE *fp;
    char readbuff[50];
    while (1) {
      if ((fp = fopen("/tmp/MYFIFO", "r")) != NULL) {
        memset(readbuff, 0, sizeof(readbuff) / sizeof(readbuff[0]));
        fgets(readbuff, sizeof(readbuff) / sizeof(readbuff[0]), fp);
        if (strlen(readbuff) > 0) {
          printf("PID %d: Received string : %s\n", getpid(), readbuff);
          fclose(fp);
          fp = fopen("/tmp/MYFIFO", "w");
        }
        fclose(fp);
      }
    }
    return 0;
  } else if (forkResult < 0) {
    return 69;
  } else {
    FILE *fp;
    for (;;) {
      if ((fp = fopen("/tmp/MYFIFO", "w")) != NULL) {
        fputs("Hello manyak\n", fp);
        printf("Message sent from PID:%d\n", getpid());
        fclose(fp);
      }
      usleep(5361 * 1000);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   10a0 <umask@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
lea    0xe14(%rip),%rdi
xor    %edx,%edx
call   10b0 <mknod@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12ad <main+0xed>
jmp    120e <main+0x4e>
lea    0xdeb(%rip),%rdi
lea    0xdf0(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12a8 <main+0xe8>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1080 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x32,%esi
call   1090 <fgets@plt>
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jle    129f <main+0xdf>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    -0x50(%rbp),%rdx
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd71(%rip),%rdi
lea    0xd96(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    120e <main+0x4e>
cmpq   $0x0,-0x10(%rbp)
jge    12c3 <main+0x103>
mov    $0x45,%eax
add    $0x60,%rsp
pop    %rbp
ret
jmp    12c8 <main+0x108>
lea    0xd31(%rip),%rdi
lea    0xd56(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
je     1319 <main+0x159>
mov    -0x58(%rbp),%rsi
lea    0xd3a(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x58(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x51cd68,%edi
call   10e0 <usleep@plt>
jmp    12c8 <main+0x108>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2e04(%rip),%rax
mov    $0x1b6,%esi
or     (%rax),%esi
lea    0xe16(%rip),%rdi
xor    %edx,%edx
call   10a0 <mknod@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     127f <main+0xbf>
js     1315 <main+0x155>
lea    0xdf2(%rip),%r12
lea    0xe17(%rip),%rbp
lea    0xe12(%rip),%r14
lea    0xe19(%rip),%r15
jmp    123a <main+0x7a>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x51cd68,%edi
call   10e0 <usleep@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1230 <main+0x70>
mov    %rax,%rbx
mov    $0xd,%esi
mov    $0x1,%edx
mov    %r14,%rdi
mov    %rax,%rcx
call   10c0 <fwrite@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1230 <main+0x70>
lea    0xd7a(%rip),%r15
lea    0xd7f(%rip),%r14
mov    %rsp,%rbx
lea    0xd77(%rip),%r12
lea    0xd8e(%rip),%r13
jmp    12a8 <main+0xe8>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a8 <main+0xe8>
mov    %rax,%rbp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    $0x32,%esi
mov    %rbp,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
jle    12a0 <main+0xe0>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbp
jmp    12a0 <main+0xe0>
mov    $0x45,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/tmp/MYFIFO\00"
@S_IFIFO = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"PID %d: Received string : %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [14 x i8] c"Hello manyak\0A\00"
@.str.5 = [26 x i8] c"Message sent from PID:%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32*
%4 = alloca [50 x i8]
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32 @umask(i32 0)
%7 = load i32, i32* @S_IFIFO
%8 = or i32 %7, 438
%9 = call i32 @mknod(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8, i32 0)
%10 = call i64 (...) @fork()
store i64 %10, i64* %2
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %0
br label %14
14: ; preds = %13, %36
%15 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %3
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %36
17: ; preds = %14
%18 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 0, i32 50)
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%21 = load i32*, i32** %3
%22 = call i32 @fgets(i8* %20, i32 50, i32* %21)
%23 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%24 = call i64 @strlen(i8* %23)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %17
%27 = call i32 (...) @getpid()
%28 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8* %28)
%30 = load i32*, i32** %3
%31 = call i32 @fclose(i32* %30)
%32 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %32, i32** %3
br label %33
33: ; preds = %26, %17
%34 = load i32*, i32** %3
%35 = call i32 @fclose(i32* %34)
br label %36
36: ; preds = %33, %14
br label %14
37: ; preds = %0
%38 = load i64, i64* %2
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %37
ret i32 69
41: ; preds = %37
br label %42
42: ; preds = %52, %41
%43 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %43, i32** %5
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %52
45: ; preds = %42
%46 = load i32*, i32** %5
%47 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32* %46)
%48 = call i32 (...) @getpid()
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %48)
%50 = load i32*, i32** %5
%51 = call i32 @fclose(i32* %50)
br label %52
52: ; preds = %45, %42
%53 = call i32 @usleep(i32 5361000)
br label %42
}

LLVM IR (-O2):
@.str = [12 x i8] c"/tmp/MYFIFO\00"
@S_IFIFO = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"PID %d: Received string : %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [14 x i8] c"Hello manyak\0A\00"
@.str.5 = [26 x i8] c"Message sent from PID:%d\0A\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = tail call i32 @umask(i32 0)
%3 = load i32, i32* @S_IFIFO
%4 = or i32 %3, 438
%5 = tail call i32 @mknod(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
%6 = tail call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %27
8: ; preds = %0
%9 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
br label %10
10: ; preds = %26, %8
%11 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %26, label %13
13: ; preds = %10
%14 = call i32 @memset(i8* %9, i32 0, i32 50)
%15 = call i32 @fgets(i8* %9, i32 50, i32* %11)
%16 = call i64 @strlen(i8* %9)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %19, i8* %9)
%21 = call i32 @fclose(i32* %11)
%22 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %18, %13
%24 = phi i32* [ %22, %18 ], [ %11, %13 ]
%25 = call i32 @fclose(i32* %24)
br label %26
26: ; preds = %23, %10
br label %10
27: ; preds = %0
%28 = icmp slt i64 %6, 0
br i1 %28, label %29, label %30
29: ; preds = %27
ret i32 69
30: ; preds = %27, %38
%31 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i32* %31, null
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = tail call i64 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i64 13, i64 1, i32* %31)
%35 = tail call i32 (...) @getpid()
%36 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = tail call i32 @fclose(i32* %31)
br label %38
38: ; preds = %33, %30
%39 = tail call i32 @usleep(i32 5361000)
br label %30
}