#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 size_t ssize_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int socketpair(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
               int *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int main() {
  int sv[2];
  int ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, sv);
  if (ret < 0) {
    perror("scoketpair");
    return 1;
  }
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    return 2;
  } else if (pid == 0) {
    close(sv[0]);
    const char *msg = "Hello Father,I am child";
    char buf[128];
    while (1) {
      write(sv[1], msg, strlen(msg));
      memset(buf, '\0', sizeof(buf));
      ssize_t size = read(sv[1], buf, sizeof(buf) - 1);
      if (size > 0) {
        buf[size] = '\0';
        printf("[child] father -> child : %s \n", buf);
        fflush(stdout);
      }
      sleep(1);
    }
  } else {
    close(sv[1]);
    const char *msg = "Hello child, I am father";
    char buf[128];
    while (1) {
      memset(buf, '\0', sizeof(buf));
      ssize_t size = read(sv[0], buf, sizeof(buf) - 1);
      if (size > 0) {
        buf[size] = '\0';
        printf("[father] child -> father : %s \n", buf);
        fflush(stdout);
      }
      write(sv[0], msg, strlen(msg));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1090 <socketpair@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1204 <main+0x54>
lea    0xe0d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ba <main+0x20a>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1232 <main+0x82>
lea    0xdea(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13ba <main+0x20a>
cmpq   $0x0,-0x18(%rbp)
jne    12fc <main+0x14c>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
lea    0xdc4(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x144(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x144(%rbp),%edi
mov    -0x140(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x7f,%edx
call   1080 <read@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jbe    12ed <main+0x13d>
mov    -0xa8(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rsi
lea    0xd4e(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    1250 <main+0xa0>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
lea    0xd3c(%rip),%rax
mov    %rax,-0xb0(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x7f,%edx
call   1080 <read@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jbe    137e <main+0x1ce>
mov    -0x138(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
lea    -0x130(%rbp),%rsi
lea    0xcf5(%rip),%rdi
call   1050 <printf@plt>
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x154(%rbp)
mov    -0xb0(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    -0xb0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x154(%rbp),%edi
mov    -0x150(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1312 <main+0x162>
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1090 <socketpair@plt>
test   %eax,%eax
js     1302 <main+0x152>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1310 <main+0x160>
je     1278 <main+0xc8>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rbx
lea    0xe37(%rip),%r14
lea    0xe49(%rip),%r15
mov    0x2db2(%rip),%r12
jmp    1238 <main+0x88>
mov    0x8(%rsp),%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x7f,%edx
call   1080 <read@plt>
test   %rax,%rax
je     1220 <main+0x70>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    (%r12),%edi
call   10a0 <fflush@plt>
jmp    1220 <main+0x70>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xd88(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xd94(%rip),%r15
mov    0x2d35(%rip),%r12
jmp    12aa <main+0xfa>
nopl   (%rax)
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    0xc(%rsp),%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x7f,%edx
call   1080 <read@plt>
test   %rax,%rax
je     12a0 <main+0xf0>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    (%r12),%edi
call   10a0 <fflush@plt>
jmp    12a0 <main+0xf0>
mov    $0x1,%ebx
lea    0xcf2(%rip),%rdi
jmp    131c <main+0x16c>
mov    $0x2,%ebx
lea    0xcef(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"scoketpair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [24 x i8] c"Hello Father,I am child\00"
@.str.3 = [31 x i8] c"[child] father -> child : %s \0A\00"
@stdout = global i32 0
@.str.4 = [25 x i8] c"Hello child, I am father\00"
@.str.5 = [32 x i8] c"[father] child -> father : %s \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca [128 x i8]
%7 = alloca i64
%8 = alloca i8*
%9 = alloca [128 x i8]
%10 = alloca i64
store i32 0, i32* %1
%11 = load i32, i32* @AF_LOCAL
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%14 = call i32 @socketpair(i32 %11, i32 %12, i32 0, i32* %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %83
19: ; preds = %0
%20 = call i64 (...) @fork()
store i64 %20, i64* %4
%21 = load i64, i64* %4
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %83
25: ; preds = %19
%26 = load i64, i64* %4
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %56
28: ; preds = %25
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8** %5
br label %32
32: ; preds = %28, %54
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i8*, i8** %5
%36 = load i8*, i8** %5
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %34, i8* %35, i32 %37)
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%40 = call i32 @memset(i8* %39, i8 signext 0, i32 128)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%44 = call i64 @read(i32 %42, i8* %43, i32 127)
store i64 %44, i64* %7
%45 = load i64, i64* %7
%46 = icmp ugt i64 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %32
%48 = load i64, i64* %7
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %48
store i8 0, i8* %49
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%51 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %50)
%52 = load i32, i32* @stdout
%53 = call i32 @fflush(i32 %52)
br label %54
54: ; preds = %47, %32
%55 = call i32 @sleep(i32 1)
br label %32
56: ; preds = %25
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8** %8
br label %60
60: ; preds = %56, %76
%61 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%62 = call i32 @memset(i8* %61, i8 signext 0, i32 128)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%64 = load i32, i32* %63
%65 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%66 = call i64 @read(i32 %64, i8* %65, i32 127)
store i64 %66, i64* %10
%67 = load i64, i64* %10
%68 = icmp ugt i64 %67, 0
br i1 %68, label %69, label %76
69: ; preds = %60
%70 = load i64, i64* %10
%71 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 %70
store i8 0, i8* %71
%72 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%73 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %72)
%74 = load i32, i32* @stdout
%75 = call i32 @fflush(i32 %74)
br label %76
76: ; preds = %69, %60
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%78 = load i32, i32* %77
%79 = load i8*, i8** %8
%80 = load i8*, i8** %8
%81 = call i32 @strlen(i8* %80)
%82 = call i32 @write(i32 %78, i8* %79, i32 %81)
br label %60
83: ; preds = %23, %17
%84 = load i32, i32* %1
ret i32 %84
}

LLVM IR (-O2):
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"scoketpair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [24 x i8] c"Hello Father,I am child\00"
@.str.3 = [31 x i8] c"[child] father -> child : %s \0A\00"
@stdout = global i32 0
@.str.4 = [25 x i8] c"Hello child, I am father\00"
@.str.5 = [32 x i8] c"[father] child -> father : %s \0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [128 x i8]
%3 = alloca [128 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = load i32, i32* @AF_LOCAL
%6 = load i32, i32* @SOCK_STREAM
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i32 @socketpair(i32 %5, i32 %6, i32 0, i32* %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %54, label %10
10: ; preds = %0
%11 = call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %54, label %13
13: ; preds = %10
%14 = icmp eq i64 %11, 0
br i1 %14, label %15, label %35
15: ; preds = %13
%16 = load i32, i32* %7
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %20
20: ; preds = %33, %15
%21 = load i32, i32* %19
%22 = call i32 @strlen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @write(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = call i32 @memset(i8* %18, i8 signext 0, i32 128)
%25 = load i32, i32* %19
%26 = call i64 @read(i32 %25, i8* %18, i32 127)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %20
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %26
store i8 0, i8* %29
%30 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
br label %33
33: ; preds = %28, %20
%34 = call i32 @sleep(i32 1)
br label %20
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 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
br label %40
40: ; preds = %50, %35
%41 = call i32 @memset(i8* %39, i8 signext 0, i32 128)
%42 = load i32, i32* %7
%43 = call i64 @read(i32 %42, i8* %39, i32 127)
%44 = icmp eq i64 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %40
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %43
store i8 0, i8* %46
%47 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %39)
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
br label %50
50: ; preds = %45, %40
%51 = load i32, i32* %7
%52 = call i32 @strlen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @write(i32 %51, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %52)
br label %40
54: ; preds = %10, %0
%55 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %10 ]
%56 = phi i32 [ 1, %0 ], [ 2, %10 ]
%57 = call i32 @perror(i8* %55)
ret i32 %56
}