#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 */
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(void) {
  char write_msg[25] = "Greetings";
  char read_msg[25];
  pid_t pid;
  int fd[2];
  if (pipe(fd) == -1) {
    fprintf(stderr, "Pipe failed");
    return 1;
  }
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork failed");
    return 1;
  }
  if (pid > 0) {
    close(fd[0]);
    write(fd[1], write_msg, strlen(write_msg) + 1);
    close(fd[1]);
  } else {
    close(fd[1]);
    read(fd[0], read_msg, 25);
    printf("child read %s\n", read_msg);
    close(fd[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe64(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe62(%rip),%al
mov    %al,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11ec <main+0x6c>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a5 <main+0x125>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jge    1223 <main+0xa3>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a5 <main+0x125>
cmpq   $0x0,-0x48(%rbp)
jle    126d <main+0xed>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x4c(%rbp),%edi
call   1060 <close@plt>
jmp    129e <main+0x11e>
mov    -0x4c(%rbp),%edi
call   1060 <close@plt>
mov    -0x50(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x19,%edx
call   1080 <read@plt>
lea    -0x40(%rbp),%rsi
lea    0xda0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
movups 0xe7b(%rip),%xmm0
movups %xmm0,0x19(%rsp)
movaps 0xe66(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     11ea <main+0x6a>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11fc <main+0x7c>
je     1218 <main+0x98>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    0xc(%rsp),%edi
jmp    124a <main+0xca>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
jmp    120c <main+0x8c>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%eax
jmp    1251 <main+0xd1>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x19,%edx
call   1080 <read@plt>
lea    0xdf3(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.write_msg = [25 x i8] c"Greetings\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@stderr = global i32 0
@.str = [12 x i8] c"Pipe failed\00"
@.str.1 = [12 x i8] c"Fork failed\00"
@.str.2 = [15 x i8] c"child read %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [25 x i8]
%3 = alloca [25 x i8]
%4 = alloca i64
%5 = alloca [2 x i32]
store i32 0, i32* %1
%6 = bitcast [25 x i8]* %2 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %51
13: ; preds = %0
%14 = call i64 (...) @fork()
store i64 %14, i64* %4
%15 = load i64, i64* %4
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %51
20: ; preds = %13
%21 = load i64, i64* %4
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %20
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%30 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 %31, 1
%33 = call i32 @write(i32 %28, i8* %29, i64 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
br label %50
37: ; preds = %20
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 25)
%45 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%46 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %37, %23
store i32 0, i32* %1
br label %51
51: ; preds = %50, %17, %10
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@__const.main.write_msg = [25 x i8] c"Greetings\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@stderr = global i32 0
@.str = [12 x i8] c"Pipe failed\00"
@.str.1 = [12 x i8] c"Fork failed\00"
@.str.2 = [15 x i8] c"child read %s\0A\00"
define i32 @main() {
%1 = alloca [25 x i8]
%2 = alloca [25 x i8]
%3 = alloca [2 x i32]
%4 = getelementptr inbounds [25 x i8], [25 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %40
13: ; preds = %0
%14 = call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %40
19: ; preds = %13
%20 = icmp eq i64 %14, 0
br i1 %20, label %31, label %21
21: ; preds = %19
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i64 @strlen(i8* %4)
%27 = add nsw i64 %26, 1
%28 = call i32 @write(i32 %25, i8* %4, i64 %27)
%29 = load i32, i32* %24
%30 = call i32 @close(i32 %29)
br label %40
31: ; preds = %19
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %7
%36 = call i32 @read(i32 %35, i8* %5, i32 25)
%37 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %21, %31, %16, %10
%41 = phi i32 [ 1, %10 ], [ 1, %16 ], [ 0, %31 ], [ 0, %21 ]
ret i32 %41
}