#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*/ buf;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int close(int);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
int main() {
  int fds[2];
  char buf[255];
  if (pipe(fds) == -1) {
    perror("pipe");
    return EXIT_FAILURE;
  }
  switch (fork()) {
  case -1:
    perror("fork");
    return EXIT_FAILURE;
  case 0:
    if (close(fds[1]) == -1)
      perror("Child close");
    if (read(fds[0], buf, sizeof(buf)) == -1) {
      perror("Child read");
      return EXIT_FAILURE;
    }
    printf("Child readed: %s\n", buf);
    break;
  default:
    if (close(fds[0]) == -1)
      perror("Child close");
    strncpy(buf, "Hello from parent! Have a long life!", sizeof(buf));
    if (write(fds[1], buf, sizeof(buf)) == -1) {
      perror("Parent write");
      return EXIT_FAILURE;
    }
    printf("Parent wrote the message!\n");
    break;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c1 <main+0x41>
lea    0xe55(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1308 <main+0x188>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x114(%rbp)
sub    $0xffffffff,%eax
je     11f1 <main+0x71>
jmp    11de <main+0x5e>
mov    -0x114(%rbp),%eax
test   %eax,%eax
je     120e <main+0x8e>
jmp    127f <main+0xff>
lea    0xe0d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1308 <main+0x188>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    122b <main+0xab>
lea    0xde4(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
jne    1265 <main+0xe5>
lea    0xdc7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1308 <main+0x188>
lea    -0x110(%rbp),%rsi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12fc <main+0x17c>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    129c <main+0x11c>
lea    0xd73(%rip),%rdi
call   1090 <perror@plt>
lea    -0x110(%rbp),%rdi
lea    0xd89(%rip),%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12ee <main+0x16e>
lea    0xd80(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1308 <main+0x188>
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x110,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     1259 <main+0xc9>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     120e <main+0x7e>
cmp    $0xffffffff,%eax
je     1262 <main+0xd2>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     126b <main+0xdb>
lea    0xe5b(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     127c <main+0xec>
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
jmp    1247 <main+0xb7>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     1285 <main+0xf5>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0xff,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     1293 <main+0x103>
lea    0xde6(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
add    $0x110,%rsp
pop    %rbx
ret
lea    0xda0(%rip),%rdi
jmp    129a <main+0x10a>
lea    0xd9c(%rip),%rdi
jmp    129a <main+0x10a>
lea    0xd98(%rip),%rdi
call   10a0 <perror@plt>
jmp    11d1 <main+0x41>
lea    0xdd5(%rip),%rdi
jmp    129a <main+0x10a>
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
jmp    121c <main+0x8c>
lea    0xd7c(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d3a(%rip),%rax
jmp    124e <main+0xbe>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [12 x i8] c"Child close\00"
@.str.3 = [11 x i8] c"Child read\00"
@.str.4 = [18 x i8] c"Child readed: %s\0A\00"
@.str.5 = [37 x i8] c"Hello from parent! Have a long life!\00"
@.str.6 = [13 x i8] c"Parent write\00"
@.str.7 = [27 x i8] c"Parent wrote the message!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [255 x i8]
store i32 0, i32* %1
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
store i32 %9, i32* %1
br label %56
10: ; preds = %0
%11 = call i32 (...) @fork()
switch i32 %11, label %34 [
i32 -1, label %12
i32 0, label %15
]
12: ; preds = %10
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %1
br label %56
15: ; preds = %10
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 255)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
store i32 %30, i32* %1
br label %56
31: ; preds = %22
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %54
34: ; preds = %10
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%43 = call i32 @strncpy(i8* %42, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 255)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%45 = load i32, i32* %44
%46 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%47 = call i32 @write(i32 %45, i8* %46, i32 255)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %41
%50 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %1
br label %56
52: ; preds = %41
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
br label %54
54: ; preds = %52, %31
%55 = load i32, i32* @EXIT_SUCCESS
store i32 %55, i32* %1
br label %56
56: ; preds = %54, %49, %28, %12, %7
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [12 x i8] c"Child close\00"
@.str.3 = [11 x i8] c"Child read\00"
@.str.4 = [18 x i8] c"Child readed: %s\0A\00"
@.str.5 = [37 x i8] c"Hello from parent! Have a long life!\00"
@.str.6 = [13 x i8] c"Parent write\00"
@EXIT_SUCCESS = global i32 0
@str = [26 x i8] c"Parent wrote the message!\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [255 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %45
10: ; preds = %0
%11 = call i32 (...) @fork()
switch i32 %11, label %29 [
i32 -1, label %12
i32 0, label %14
]
12: ; preds = %10
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %45
14: ; preds = %10
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
%22 = load i32, i32* %5
%23 = call i32 @read(i32 %22, i8* %4, i32 255)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %45
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %4)
br label %45
29: ; preds = %10
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = call i32 @strncpy(i8* %4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 255)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @write(i32 %38, i8* %4, i32 255)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %45
43: ; preds = %35
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %27, %43, %41, %25, %12, %8
%46 = phi i32* [ @EXIT_FAILURE, %8 ], [ @EXIT_FAILURE, %41 ], [ @EXIT_FAILURE, %25 ], [ @EXIT_FAILURE, %12 ], [ @EXIT_SUCCESS, %43 ], [ @EXIT_SUCCESS, %27 ]
%47 = load i32, i32* %46
ret i32 %47
}