#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 /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int, int /*<<< orphan*/);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
scalar_t__ write(int, char *, int);
int main() {
  int sockets[2], child;
  char buffer[1024];
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
    perror("opening socket stream pair");
    exit(1);
  }
  if ((child = fork()) == -1) {
    perror(" at fork");
  }
  if (child != 0) {
    close(sockets[0]);
    if (read(sockets[1], buffer, 1024, 0) < 0)
      perror("reading message");
    printf("==> %s\n", buffer);
    if (write(sockets[1], "sam i am , i do not like green eggs and ham...",
              sizeof("sam i am , i do not like green eggs and ham...")) < 0)
      perror("writing message");
    close(sockets[1]);
  } else {
    close(sockets[1]);
    if (write(sockets[0], "how now brown cow...",
              sizeof("how now brown cow...")) < 0)
      perror("writing message");
    if (read(sockets[0], buffer, 1024, 0) < 0)
      perror("reading message");
    printf("==> %s\n", buffer);
    close(sockets[0]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0x0,%rax
jge    11cf <main+0x4f>
lea    0xe40(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11ee <main+0x6e>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
cmpl   $0x0,-0x10(%rbp)
je     1276 <main+0xf6>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <read@plt>
cmp    $0x0,%rax
jge    122c <main+0xac>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
lea    -0x410(%rbp),%rsi
lea    0xdfa(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xdf3(%rip),%rsi
mov    $0x2f,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1269 <main+0xe9>
lea    0xe07(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
jmp    12ef <main+0x16f>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    0xdf3(%rip),%rsi
mov    $0x15,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12a8 <main+0x128>
lea    0xdc8(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <read@plt>
cmp    $0x0,%rax
jge    12d4 <main+0x154>
lea    0xd55(%rip),%rdi
call   1080 <perror@plt>
lea    -0x410(%rbp),%rsi
lea    0xd52(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x418,%rsp
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
test   %rax,%rax
js     12ce <main+0x14e>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     121e <main+0x9e>
cmp    $0xffffffff,%eax
je     127f <main+0xff>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <read@plt>
test   %rax,%rax
js     1290 <main+0x110>
lea    0xe44(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
lea    0xe37(%rip),%rsi
mov    $0x2f,%edx
call   1030 <write@plt>
test   %rax,%rax
js     12a1 <main+0x121>
mov    0xc(%rsp),%edi
jmp    1270 <main+0xf0>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0xe49(%rip),%rsi
mov    $0x15,%edx
call   1030 <write@plt>
test   %rax,%rax
js     12b2 <main+0x132>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <read@plt>
test   %rax,%rax
js     12c0 <main+0x140>
lea    0xdd2(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
ret
lea    0xd95(%rip),%rdi
call   1080 <perror@plt>
jmp    11c2 <main+0x42>
lea    0xd8d(%rip),%rdi
call   1080 <perror@plt>
jmp    11e9 <main+0x69>
lea    0xdc3(%rip),%rdi
call   1080 <perror@plt>
jmp    1218 <main+0x98>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
jmp    1241 <main+0xc1>
lea    0xd5d(%rip),%rdi
call   1080 <perror@plt>
jmp    125b <main+0xdb>
lea    0xd2b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [27 x i8] c"opening socket stream pair\00"
@.str.1 = [9 x i8] c" at fork\00"
@.str.2 = [16 x i8] c"reading message\00"
@.str.3 = [8 x i8] c"==> %s\0A\00"
@.str.4 = [47 x i8] c"sam i am , i do not like green eggs and ham...\00"
@.str.5 = [16 x i8] c"writing message\00"
@.str.6 = [21 x i8] c"how now brown cow...\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 ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %45
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 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = call i64 @read(i32 %26, i8* %27, i32 1024, i32 0)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %21
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i64 @write(i32 %36, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 47)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %41
41: ; preds = %39, %32
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
br label %69
45: ; preds = %18
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%50 = load i32, i32* %49
%51 = call i64 @write(i32 %50, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 21)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %45
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%57 = load i32, i32* %56
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%59 = call i64 @read(i32 %57, i8* %58, i32 1024, i32 0)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %61, %55
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%65 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
br label %69
69: ; preds = %63, %41
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [27 x i8] c"opening socket stream pair\00"
@.str.1 = [9 x i8] c" at fork\00"
@.str.2 = [16 x i8] c"reading message\00"
@.str.3 = [8 x i8] c"==> %s\0A\00"
@.str.4 = [47 x i8] c"sam i am , i do not like green eggs and ham...\00"
@.str.5 = [16 x i8] c"writing message\00"
@.str.6 = [21 x i8] c"how now brown cow...\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 %35
]
15: ; preds = %13
%16 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
%18 = load i32, i32* %7
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i64 @read(i32 %21, i8* %4, i32 1024, i32 0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%28 = load i32, i32* %20
%29 = call i64 @write(i32 %28, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 47)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i32, i32* %20
br label %53
35: ; preds = %13
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %7
%40 = call i64 @write(i32 %39, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 21)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %35
%45 = load i32, i32* %7
%46 = call i64 @read(i32 %45, i8* %4, i32 1024, i32 0)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%52 = load i32, i32* %7
br label %53
53: ; preds = %50, %33
%54 = phi i32 [ %52, %50 ], [ %34, %33 ]
%55 = call i32 @close(i32 %54)
ret i32 0
}