#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 *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int sockets[2], child;
  if (pipe(sockets) < 0) {
    perror("opening stram socket pair");
    exit(1);
  }
  child = fork();
  if (child == -1) {
    perror("fork");
    exit(1);
  }
  if (child) {
    char buf[1024];
    close(sockets[1]);
    read(sockets[0], buf, 1024);
    printf("%s\n", buf);
    close(sockets[0]);
  } else {
    close(sockets[0]);
    write(sockets[1], "hello wicked world", sizeof("hello wicked world"));
    close(sockets[1]);
  }
  return 0;
}


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    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11e5 <main+0x65>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
je     122b <main+0xab>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    -0x410(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    124f <main+0xcf>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xde6(%rip),%rsi
mov    $0x13,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x410,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     120d <main+0x8d>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11db <main+0x5b>
cmp    $0xffffffff,%eax
je     1216 <main+0x96>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    0xe52(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x8(%rsp),%edi
jmp    11fd <main+0x7d>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe34(%rip),%rsi
mov    $0x13,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
ret
lea    0xdec(%rip),%rdi
jmp    121d <main+0x9d>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"opening stram socket pair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [19 x i8] c"hello wicked world\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 ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 (...) @fork()
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 1024)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
br label %44
34: ; preds = %18
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 19)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %34, %21
ret i32 0
}

LLVM IR (-O2):
@.str = [26 x i8] c"opening stram socket pair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [19 x i8] c"hello wicked world\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 ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i32 (...) @fork()
switch i32 %11, label %15 [
i32 -1, label %12
i32 0, label %25
]
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 1)
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 i32 @read(i32 %20, i8* %16, i32 1024)
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
br label %33
25: ; preds = %10
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 19)
%31 = load i32, i32* %28
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %25, %15
ret i32 0
}