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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char **, size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
scalar_t__ write(int, char **, scalar_t__);
int main(int argc, char **argv) {
  int pipefds[2];
  char buffer[1024];
  size_t bufsize = sizeof(buffer);
  if (pipe(pipefds) < 0) {
    perror("pipe error");
    return 1;
  }
  int pid = fork();
  if (pid < 0) {
    perror("fork error");
    return 1;
  } else if (pid == 0) {
    close(pipefds[1]);
    if (read(pipefds[0], &buffer, bufsize) < 0) {
      perror("read error");
      return 1;
    }
    buffer[bufsize - 1] = '\0';
    printf("read on pipe: %s\n", buffer);
  } else {
    close(pipefds[0]);
    snprintf(buffer, bufsize, "Hello from write end of pipe!");
    if (write(pipefds[1], &buffer, strlen(buffer) + 1) < 0) {
      perror("write error");
      return 1;
    }
    wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x400,-0x428(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jge    11ef <main+0x4f>
lea    0xe22(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    132e <main+0x18e>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jge    1221 <main+0x81>
lea    0xdfb(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    132e <main+0x18e>
cmpl   $0x0,-0x42c(%rbp)
jne    1299 <main+0xf9>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1090 <read@plt>
cmp    $0x0,%rax
jge    126e <main+0xce>
lea    0xdb9(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    132e <main+0x18e>
mov    -0x428(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xd92(%rip),%rdi
call   1050 <printf@plt>
jmp    1322 <main+0x182>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rsi
lea    0xd7d(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1319 <main+0x179>
lea    0xd49(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    132e <main+0x18e>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
jmp    1327 <main+0x187>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x418,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     124c <main+0xbc>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1255 <main+0xc5>
je     120e <main+0x7e>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
movups 0xe65(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movups 0xe67(%rip),%xmm0
movups %xmm0,0x1e(%rsp)
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>
test   %rax,%rax
js     125e <main+0xce>
xor    %ebx,%ebx
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    1278 <main+0xe8>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %rax,%rax
js     1267 <main+0xd7>
movb   $0x0,0x40f(%rsp)
lea    0xde3(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1050 <printf@plt>
xor    %ebx,%ebx
jmp    1278 <main+0xe8>
lea    0xdad(%rip),%rdi
jmp    126e <main+0xde>
lea    0xdaf(%rip),%rdi
jmp    126e <main+0xde>
lea    0xdec(%rip),%rdi
jmp    126e <main+0xde>
lea    0xda8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [11 x i8] c"read error\00"
@.str.3 = [18 x i8] c"read on pipe: %s\0A\00"
@.str.4 = [30 x i8] c"Hello from write end of pipe!\00"
@.str.5 = [12 x i8] c"write error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [1024 x i8]
%8 = alloca i64
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 1024, i64* %8
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %63
15: ; preds = %2
%16 = call i32 (...) @fork()
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %63
21: ; preds = %15
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%29 = load i32, i32* %28
%30 = bitcast [1024 x i8]* %7 to i8**
%31 = load i64, i64* %8
%32 = call i64 @read(i32 %29, i8** %30, i64 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %63
36: ; preds = %24
%37 = load i64, i64* %8
%38 = sub i64 %37, 1
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %61
42: ; preds = %21
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%47 = load i64, i64* %8
%48 = call i32 @snprintf(i8* %46, i64 %47, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%50 = load i32, i32* %49
%51 = bitcast [1024 x i8]* %7 to i8**
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%53 = call i64 @strlen(i8* %52)
%54 = add nsw i64 %53, 1
%55 = call i64 @write(i32 %50, i8** %51, i64 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %42
%58 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %63
59: ; preds = %42
%60 = call i32 @wait(i32* null)
br label %61
61: ; preds = %59, %36
br label %62
62: ; preds = %61
store i32 0, i32* %3
br label %63
63: ; preds = %62, %57, %34, %19, %13
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [11 x i8] c"read error\00"
@.str.3 = [18 x i8] c"read on pipe: %s\0A\00"
@.str.4 = [30 x i8] c"Hello from write end of pipe!\00"
@.str.5 = [12 x i8] c"write error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [1024 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %46
12: ; preds = %2
%13 = call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %46
17: ; preds = %12
%18 = icmp eq i32 %13, 0
br i1 %18, label %19, label %32
19: ; preds = %17
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %7
%24 = bitcast [1024 x i8]* %4 to i8**
%25 = call i64 @read(i32 %23, i8** %24, i64 1024)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %46
29: ; preds = %19
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 1023
store i8 0, i8* %30
%31 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %46
32: ; preds = %17
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%36 = load i32, i32* %35
%37 = bitcast [1024 x i8]* %4 to i8**
%38 = call i64 @strlen(i8* %6)
%39 = add nsw i64 %38, 1
%40 = call i64 @write(i32 %36, i8** %37, i64 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %32
%43 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %46
44: ; preds = %32
%45 = call i32 @wait(i32* null)
br label %46
46: ; preds = %15, %27, %42, %29, %44, %10
%47 = phi i32 [ 1, %10 ], [ 1, %15 ], [ 1, %27 ], [ 1, %42 ], [ 0, %29 ], [ 0, %44 ]
ret i32 %47
}