#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*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
scalar_t__ read(int, char *, int);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
scalar_t__ write(int, char *, int);
int main() {
  int sockets[2], child;
  char buf[1024];
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
    perror("opening stream socket pair");
    exit(1);
  }
  if ((child = fork()) == -1)
    perror("fork");
  else if (child) {
    close(sockets[0]);
    printf("Parent (%d) --> sending: %s\n", getpid(),
           "In Xanadu, did Kublai Khan . . .");
    if (write(sockets[1], "In Xanadu, did Kublai Khan . . .",
              sizeof("In Xanadu, did Kublai Khan . . .")) < 0)
      perror("writing stream message");
    if (read(sockets[1], buf, 1024) < 0)
      perror("reading stream message");
    printf("Parent (%d) --> reading: %s\n", getpid(), buf);
    close(sockets[1]);
  } else {
    close(sockets[1]);
    printf("Child (%d) --> sending: %s\n", getpid(),
           "A stately pleasure dome decree . . .");
    if (write(sockets[0], "A stately pleasure dome decree . . .",
              sizeof("A stately pleasure dome decree . . .")) < 0)
      perror("writing stream message");
    if (read(sockets[0], buf, 1024) < 0)
      perror("reading stream message");
    printf("Child (%d) --> reading: %s\n", getpid(), buf);
    close(sockets[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
cmp    $0x0,%rax
jge    11df <main+0x4f>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1203 <main+0x73>
lea    0xe22(%rip),%rdi
call   1090 <perror@plt>
jmp    134f <main+0x1bf>
cmpl   $0x0,-0x10(%rbp)
je     12ae <main+0x11e>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdfb(%rip),%rdi
lea    0xe11(%rip),%rdx
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xe02(%rip),%rsi
mov    $0x21,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    125b <main+0xcb>
lea    0xe08(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    1285 <main+0xf5>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    -0x410(%rbp),%rdx
lea    0xdf0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    134a <main+0x1ba>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xde3(%rip),%rdi
lea    0xdf8(%rip),%rdx
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    0xde9(%rip),%rsi
mov    $0x25,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12fc <main+0x16c>
lea    0xd67(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    1326 <main+0x196>
lea    0xd54(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    -0x410(%rbp),%rdx
lea    0xdad(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
jmp    134f <main+0x1bf>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x410,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
test   %rax,%rax
js     1328 <main+0x198>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1255 <main+0xc5>
cmp    $0xffffffff,%eax
je     12d9 <main+0x149>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe32(%rip),%rdi
lea    0xe48(%rip),%rbx
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x21,%edx
call   1030 <write@plt>
test   %rax,%rax
js     12e7 <main+0x157>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
js     12f8 <main+0x168>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe49(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %eax,%esi
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
jmp    12c9 <main+0x139>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe3d(%rip),%rdi
lea    0xe52(%rip),%rbx
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x25,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1309 <main+0x179>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
js     131a <main+0x18a>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe31(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
ret
lea    0xd3b(%rip),%rdi
call   1090 <perror@plt>
jmp    12ce <main+0x13e>
lea    0xd70(%rip),%rdi
call   1090 <perror@plt>
jmp    1219 <main+0x89>
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
jmp    1235 <main+0xa5>
lea    0xd4e(%rip),%rdi
call   1090 <perror@plt>
jmp    1293 <main+0x103>
lea    0xd54(%rip),%rdi
call   1090 <perror@plt>
jmp    12ab <main+0x11b>
lea    0xcd1(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [27 x i8] c"opening stream socket pair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [29 x i8] c"Parent (%d) --> sending: %s\0A\00"
@.str.3 = [33 x i8] c"In Xanadu, did Kublai Khan . . .\00"
@.str.4 = [23 x i8] c"writing stream message\00"
@.str.5 = [23 x i8] c"reading stream message\00"
@.str.6 = [29 x i8] c"Parent (%d) --> reading: %s\0A\00"
@.str.7 = [28 x i8] c"Child (%d) --> sending: %s\0A\00"
@.str.8 = [37 x i8] c"A stately pleasure dome decree . . .\00"
@.str.9 = [28 x i8] c"Child (%d) --> reading: %s\0A\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 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @socketpair(i32 %5, i32 %6, i32 0, i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 (...) @fork()
store i32 %14, i32* %3
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %76
18: ; preds = %13
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %48
21: ; preds = %18
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = call i32 (...) @getpid()
%26 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %25, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i64 @write(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 33)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %21
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%37 = call i64 @read(i32 %35, i8* %36, i32 1024)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
%42 = call i32 (...) @getpid()
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%44 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %42, i8* %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
br label %75
48: ; preds = %18
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = call i32 (...) @getpid()
%53 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %52, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%55 = load i32, i32* %54
%56 = call i64 @write(i32 %55, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0), i32 37)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %48
%59 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %58, %48
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%64 = call i64 @read(i32 %62, i8* %63, i32 1024)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %68
68: ; preds = %66, %60
%69 = call i32 (...) @getpid()
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%71 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i32 %69, i8* %70)
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
br label %75
75: ; preds = %68, %41
br label %76
76: ; preds = %75, %16
ret i32 0
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [27 x i8] c"opening stream socket pair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [29 x i8] c"Parent (%d) --> sending: %s\0A\00"
@.str.3 = [33 x i8] c"In Xanadu, did Kublai Khan . . .\00"
@.str.4 = [23 x i8] c"writing stream message\00"
@.str.5 = [23 x i8] c"reading stream message\00"
@.str.6 = [29 x i8] c"Parent (%d) --> reading: %s\0A\00"
@.str.7 = [28 x i8] c"Child (%d) --> sending: %s\0A\00"
@.str.8 = [37 x i8] c"A stately pleasure dome decree . . .\00"
@.str.9 = [28 x i8] c"Child (%d) --> reading: %s\0A\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 [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i64 @socketpair(i32 %5, i32 %6, i32 0, i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 (...) @fork()
switch i32 %14, label %17 [
i32 -1, label %15
i32 0, label %39
]
15: ; preds = %13
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %61
17: ; preds = %13
%18 = load i32, i32* %7
%19 = call i32 @close(i32 %18)
%20 = call i32 (...) @getpid()
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i64 @write(i32 %23, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 33)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %17
%29 = load i32, i32* %22
%30 = call i64 @read(i32 %29, i8* %4, i32 1024)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = call i32 (...) @getpid()
%36 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %35, i8* %4)
%37 = load i32, i32* %22
%38 = call i32 @close(i32 %37)
br label %61
39: ; preds = %13
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = call i32 (...) @getpid()
%44 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %43, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
%45 = load i32, i32* %7
%46 = call i64 @write(i32 %45, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0), i32 37)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %39
%51 = load i32, i32* %7
%52 = call i64 @read(i32 %51, i8* %4, i32 1024)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %54, %50
%57 = call i32 (...) @getpid()
%58 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i32 %57, i8* %4)
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %34, %56, %15
ret i32 0
}