#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;

/* Variables and functions */
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
size_t write(int, char *, int);
int main() {
  int fd1[2], fd2[2];
  size_t size;
  char *string;
  pid_t pid;
  if (pipe(fd1) < 0) {
    printf("Can\'t create pipe\n");
    exit(-1);
  }
  if (pipe(fd2) < 0) {
    printf("Can\'t create pipe\n");
    exit(-1);
  }
  pid = fork();
  if (pid < 0)
    printf("Error in fork()\n");
  else if (pid == 0) {
    size = write(fd1[1], "Message from child", 19);
    if (size != 19) {
      printf("Can\'t write all string\n");
      exit(-1);
    }
    size = read(fd2[0], string, 20);
    if (size < 0) {
      printf("Can\'t read string\n");
      exit(-1);
    }
    printf("I\'m child, I've received \"%s\"\n", string);
  } else {
    size = read(fd1[0], string, 19);
    if (size < 0) {
      printf("Can\'t read string\n");
      exit(-1);
    }
    printf("I\'m parent, I've received \"%s\"\n", string);
    size = write(fd2[1], "Message from parent", 20);
    if (size != 20) {
      printf("Can\'t write all string\n");
      exit(-1);
    }
  }
  if (close(fd1[0]) < 0) {
    printf("Can\'t close input stream\n");
  }
  if (close(fd1[1]) < 0) {
    printf("Can\'t close output stream\n");
  }
  if (close(fd2[0]) < 0) {
    printf("Can\'t close input stream\n");
  }
  if (close(fd2[1]) < 0) {
    printf("Can\'t close output stream\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11aa <main+0x3a>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    -0x14(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11d5 <main+0x65>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11fe <main+0x8e>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1322 <main+0x1b2>
cmpq   $0x0,-0x30(%rbp)
jne    1293 <main+0x123>
mov    -0x8(%rbp),%edi
lea    0xe11(%rip),%rsi
mov    $0x13,%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x13,-0x20(%rbp)
je     1244 <main+0xd4>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x14,%edx
call   1070 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    127c <main+0x10c>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    131d <main+0x1ad>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x13,%edx
call   1070 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    12cb <main+0x15b>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
lea    0xdba(%rip),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x14,-0x20(%rbp)
je     1318 <main+0x1a8>
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    131d <main+0x1ad>
jmp    1322 <main+0x1b2>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1342 <main+0x1d2>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1362 <main+0x1f2>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1382 <main+0x212>
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    13a2 <main+0x232>
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     12ac <main+0x12c>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     12ac <main+0x12c>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11f4 <main+0x74>
je     1202 <main+0x82>
mov    0x10(%rsp),%edi
mov    $0x13,%edx
call   1080 <read@plt>
lea    0xe65(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
lea    0xe73(%rip),%rsi
mov    $0x14,%edx
call   1040 <write@plt>
cmp    $0x14,%rax
je     123d <main+0xbd>
jmp    12b5 <main+0x135>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
jmp    123d <main+0xbd>
mov    0x14(%rsp),%edi
lea    0xdf3(%rip),%rsi
mov    $0x13,%edx
call   1040 <write@plt>
cmp    $0x13,%rax
jne    12b5 <main+0x135>
mov    0x8(%rsp),%edi
mov    $0x14,%edx
call   1080 <read@plt>
lea    0xddd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
test   %rax,%rax
jns    1257 <main+0xd7>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
test   %rax,%rax
jns    1271 <main+0xf1>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
test   %rax,%rax
jns    128b <main+0x10b>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
test   %rax,%rax
jns    12a5 <main+0x125>
lea    0xdc6(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xe0d(%rip),%rdi
jmp    12bc <main+0x13c>
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Can't create pipe\0A\00"
@.str.1 = [17 x i8] c"Error in fork()\0A\00"
@.str.2 = [19 x i8] c"Message from child\00"
@.str.3 = [24 x i8] c"Can't write all string\0A\00"
@.str.4 = [19 x i8] c"Can't read string\0A\00"
@.str.5 = [31 x i8] c"I'm child, I've received \22%s\22\0A\00"
@.str.6 = [32 x i8] c"I'm parent, I've received \22%s\22\0A\00"
@.str.7 = [20 x i8] c"Message from parent\00"
@.str.8 = [26 x i8] c"Can't close input stream\0A\00"
@.str.9 = [27 x i8] c"Can't close output stream\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
%21 = call i64 (...) @fork()
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %74
26: ; preds = %20
%27 = load i64, i64* %6
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %51
29: ; preds = %26
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i64 @write(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 19)
store i64 %32, i64* %4
%33 = load i64, i64* %4
%34 = icmp ne i64 %33, 19
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %29
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%40 = load i32, i32* %39
%41 = load i8*, i8** %5
%42 = call i64 @read(i32 %40, i8* %41, i32 20)
store i64 %42, i64* %4
%43 = load i64, i64* %4
%44 = icmp ult i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %38
%49 = load i8*, i8** %5
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %49)
br label %73
51: ; preds = %26
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%53 = load i32, i32* %52
%54 = load i8*, i8** %5
%55 = call i64 @read(i32 %53, i8* %54, i32 19)
store i64 %55, i64* %4
%56 = load i64, i64* %4
%57 = icmp ult i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %51
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %51
%62 = load i8*, i8** %5
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%65 = load i32, i32* %64
%66 = call i64 @write(i32 %65, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 20)
store i64 %66, i64* %4
%67 = load i64, i64* %4
%68 = icmp ne i64 %67, 20
br i1 %68, label %69, label %72
69: ; preds = %61
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%71 = call i32 @exit(i32 -1)
unreachable
72: ; preds = %61
br label %73
73: ; preds = %72, %48
br label %74
74: ; preds = %73, %24
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%76 = load i32, i32* %75
%77 = call i64 @close(i32 %76)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %74
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
br label %81
81: ; preds = %79, %74
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%83 = load i32, i32* %82
%84 = call i64 @close(i32 %83)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %81
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
br label %88
88: ; preds = %86, %81
%89 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%90 = load i32, i32* %89
%91 = call i64 @close(i32 %90)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %95
93: ; preds = %88
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
br label %95
95: ; preds = %93, %88
%96 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%97 = load i32, i32* %96
%98 = call i64 @close(i32 %97)
%99 = icmp slt i64 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %95
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
br label %102
102: ; preds = %100, %95
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [19 x i8] c"Message from child\00"
@.str.5 = [31 x i8] c"I'm child, I've received \22%s\22\0A\00"
@.str.6 = [32 x i8] c"I'm parent, I've received \22%s\22\0A\00"
@.str.7 = [20 x i8] c"Message from parent\00"
@str.11 = [26 x i8] c"Can't close output stream\00"
@str.12 = [25 x i8] c"Can't close input stream\00"
@str.14 = [23 x i8] c"Can't write all string\00"
@str.15 = [16 x i8] c"Error in fork()\00"
@str.17 = [18 x i8] c"Can't create pipe\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = bitcast [2 x i32]* %2 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.17, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%13 = call i64 @pipe(i32* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.17, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %11
%19 = call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.15, i64 0, i64 0))
br label %48
23: ; preds = %18
%24 = icmp eq i64 %19, 0
br i1 %24, label %25, label %37
25: ; preds = %23
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i64 @write(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 19)
%29 = icmp eq i64 %28, 19
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.14, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %25
%34 = load i32, i32* %12
%35 = call i64 @read(i32 %34, i8* undef, i32 20)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* undef)
br label %48
37: ; preds = %23
%38 = load i32, i32* %5
%39 = call i64 @read(i32 %38, i8* undef, i32 19)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* undef)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i64 @write(i32 %42, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 20)
%44 = icmp eq i64 %43, 20
br i1 %44, label %48, label %45
45: ; preds = %37
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.14, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %33, %37, %21
%49 = load i32, i32* %5
%50 = call i64 @close(i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i64 @close(i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %54
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.11, i64 0, i64 0))
br label %61
61: ; preds = %59, %54
%62 = load i32, i32* %12
%63 = call i64 @close(i32 %62)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
br label %67
67: ; preds = %65, %61
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%69 = load i32, i32* %68
%70 = call i64 @close(i32 %69)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.11, i64 0, i64 0))
br label %74
74: ; preds = %72, %67
ret i32 0
}