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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
scalar_t__ write(int, char *, int);
int main() {
  int sockets[2], child;
  if (pipe(sockets) < 0) {
    perror("opening stream socket pair");
    exit(10);
  }
  if ((child = fork()) == -1) {
    perror("fork");
    exit(10);
  }
  if (child != 0) {
    char buffer[1024];
    close(sockets[1]);
    if (read(sockets[0], buffer, 1024) < 0)
      perror("reading messages");
    printf("--> %s\n", buffer);
    close(sockets[0]);
  } else {
    close(sockets[0]);
    if (write(sockets[1], "How now brown cow...",
              sizeof("How now brown cow...")) < 0)
      perror("writing message");
    close(sockets[1]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11bb <main+0x3b>
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11e4 <main+0x64>
lea    0xe46(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
je     1240 <main+0xc0>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    1220 <main+0xa0>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
lea    -0x410(%rbp),%rsi
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    127a <main+0xfa>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xde7(%rip),%rsi
mov    $0x15,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1272 <main+0xf2>
lea    0xde1(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x418,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     1238 <main+0xb8>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11e6 <main+0x66>
cmp    $0xffffffff,%eax
je     1241 <main+0xc1>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
js     121c <main+0x9c>
lea    0xe5b(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
mov    0x8(%rsp),%edi
jmp    120d <main+0x8d>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe3f(%rip),%rsi
mov    $0x15,%edx
call   1030 <write@plt>
test   %rax,%rax
js     122a <main+0xaa>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
ret
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
jmp    11cf <main+0x4f>
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
jmp    1209 <main+0x89>
lea    0xdc1(%rip),%rdi
jmp    1248 <main+0xc8>
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"opening stream socket pair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [17 x i8] c"reading messages\00"
@.str.3 = [8 x i8] c"--> %s\0A\00"
@.str.4 = [21 x i8] c"How now brown cow...\00"
@.str.5 = [16 x i8] c"writing message\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, 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 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 10)
unreachable
11: ; preds = %0
%12 = call i32 (...) @fork()
store i32 %12, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 10)
unreachable
17: ; preds = %11
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%27 = call i64 @read(i32 %25, i8* %26, i32 1024)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %20
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
br label %51
37: ; preds = %17
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%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 ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 21)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %47
47: ; preds = %45, %37
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %47, %31
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@.str = [27 x i8] c"opening stream socket pair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [17 x i8] c"reading messages\00"
@.str.3 = [8 x i8] c"--> %s\0A\00"
@.str.4 = [21 x i8] c"How now brown cow...\00"
@.str.5 = [16 x i8] c"writing message\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1024 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 10)
unreachable
10: ; preds = %0
%11 = call i32 (...) @fork()
switch i32 %11, label %15 [
i32 -1, label %12
i32 0, label %29
]
12: ; preds = %10
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 10)
unreachable
15: ; preds = %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %4
%21 = call i64 @read(i32 %20, i8* %16, i32 1024)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
br label %41
29: ; preds = %10
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i64 @write(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 21)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %36, %29
%39 = load i32, i32* %32
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %38, %25
ret i32 0
}