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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int count = 1;
  int sockets[2];
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
    perror("socketpair");
    return 1;
  }
  const char *me;
  int fd;
  char buf[32];
  if (fork() > 0) {
    me = "parent";
    fd = sockets[0];
    while (1) {
      sprintf(buf, "%s(%d)", me, count++);
      if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
        break;
      }
      sleep(2);
    }
  } else {
    close(sockets[0]);
    me = "child";
    fd = sockets[1];
    while (1) {
      if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
        break;
      }
      printf("%s read: %s\n", me, buf);
    }
  }
  printf("%s done\n", me);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0x0,%rax
jge    11ec <main+0x5c>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d7 <main+0x147>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jle    1263 <main+0xd3>
lea    0xe07(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    0xdea(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x2c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x20,%edx
call   1030 <write@plt>
cltq
cmp    $0x20,%rax
je     124f <main+0xbf>
jmp    125e <main+0xce>
mov    $0x2,%edi
call   10a0 <sleep@plt>
jmp    120e <main+0x7e>
jmp    12be <main+0x12e>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
lea    0xda7(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
cltq
cmp    $0x20,%rax
je     129e <main+0x10e>
jmp    12b9 <main+0x129>
mov    -0x28(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127c <main+0xec>
jmp    12be <main+0x12e>
mov    -0x28(%rbp),%rsi
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
test   %rax,%rax
js     12ef <main+0x15f>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    0x8(%rsp),%r14d
test   %rax,%rax
jle    1264 <main+0xd4>
lea    0xe33(%rip),%rsi
lea    0xe25(%rip),%r12
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r12,%rdx
mov    $0x1,%ecx
call   1090 <sprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1030 <write@plt>
cmp    $0x20,%eax
jne    12cd <main+0x13d>
mov    $0x2,%ebp
lea    0xdf2(%rip),%r15
lea    0xde4(%rip),%r12
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %ebp,%ecx
call   1090 <sprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1030 <write@plt>
add    $0x1,%ebp
cmp    $0x20,%eax
je     1230 <main+0xa0>
jmp    12cd <main+0x13d>
mov    %r14d,%edi
call   1050 <close@plt>
mov    0xc(%rsp),%ebp
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x20,%edx
call   1060 <read@plt>
cmp    $0x20,%eax
jne    12c6 <main+0x136>
lea    0xd92(%rip),%r14
lea    0xd85(%rip),%r12
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1060 <read@plt>
cmp    $0x20,%eax
je     12a0 <main+0x110>
jmp    12cd <main+0x13d>
lea    0xd4c(%rip),%r12
lea    0xd58(%rip),%rdi
xor    %ebx,%ebx
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    12e0 <main+0x150>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@.str.1 = [7 x i8] c"parent\00"
@.str.2 = [7 x i8] c"%s(%d)\00"
@.str.3 = [6 x i8] c"child\00"
@.str.4 = [13 x i8] c"%s read: %s\0A\00"
@.str.5 = [9 x i8] c"%s done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%14 = call i64 @socketpair(i32 %11, i32 %12, i32 0, i32* %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %60
18: ; preds = %2
%19 = call i64 (...) @fork()
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %39
21: ; preds = %18
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %8
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%23 = load i32, i32* %22
store i32 %23, i32* %9
br label %24
24: ; preds = %21, %36
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%26 = load i8*, i8** %8
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
%29 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %26, i32 %27)
%30 = load i32, i32* %9
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%32 = call i32 @write(i32 %30, i8* %31, i32 32)
%33 = sext i32 %32 to i64
%34 = icmp ne i64 %33, 32
br i1 %34, label %35, label %36
35: ; preds = %24
br label %38
36: ; preds = %24
%37 = call i32 @sleep(i32 2)
br label %24
38: ; preds = %35
br label %57
39: ; preds = %18
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %8
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%44 = load i32, i32* %43
store i32 %44, i32* %9
br label %45
45: ; preds = %39, %52
%46 = load i32, i32* %9
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 32)
%49 = sext i32 %48 to i64
%50 = icmp ne i64 %49, 32
br i1 %50, label %51, label %52
51: ; preds = %45
br label %56
52: ; preds = %45
%53 = load i8*, i8** %8
%54 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%55 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %53, i8* %54)
br label %45
56: ; preds = %51
br label %57
57: ; preds = %56, %38
%58 = load i8*, i8** %8
%59 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %58)
store i32 0, i32* %3
br label %60
60: ; preds = %57, %16
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@.str.1 = [7 x i8] c"parent\00"
@.str.2 = [7 x i8] c"%s(%d)\00"
@.str.3 = [6 x i8] c"child\00"
@.str.4 = [13 x i8] c"%s read: %s\0A\00"
@.str.5 = [9 x i8] c"%s done\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [32 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i64 @socketpair(i32 %6, i32 %7, i32 0, i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %42
13: ; preds = %2
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%15 = call i64 (...) @fork()
%16 = icmp sgt i64 %15, 0
%17 = load i32, i32* %8
br i1 %16, label %18, label %29
18: ; preds = %13
%19 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1)
%20 = call i32 @write(i32 %17, i8* %14, i32 32)
%21 = icmp eq i32 %20, 32
br i1 %21, label %22, label %39
22: ; preds = %18, %22
%23 = phi i32 [ %24, %22 ], [ 1, %18 ]
%24 = add nuw nsw i32 %23, 1
%25 = call i32 @sleep(i32 2)
%26 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%27 = call i32 @write(i32 %17, i8* %14, i32 32)
%28 = icmp eq i32 %27, 32
br i1 %28, label %22, label %39
29: ; preds = %13
%30 = call i32 @close(i32 %17)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @read(i32 %32, i8* %14, i32 32)
%34 = icmp eq i32 %33, 32
br i1 %34, label %35, label %39
35: ; preds = %29, %35
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%37 = call i32 @read(i32 %32, i8* %14, i32 32)
%38 = icmp eq i32 %37, 32
br i1 %38, label %35, label %39
39: ; preds = %35, %22, %29, %18
%40 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %18 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %29 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %22 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %35 ]
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %11
%43 = phi i32 [ 1, %11 ], [ 0, %39 ]
ret i32 %43
}