#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ error(char *);
int execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int fork();
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int read_pipe[2];
  if (pipe(read_pipe) == -1) {
    error("Can't create the pipe");
  }
  int write_pipe[2];
  if (pipe(write_pipe) == -1) {
    error("Can't create the pipe");
  }
  pid_t pid = fork();
  if (pid == -1) {
    error("Can't fork process");
  }
  if (!pid) {
    close(read_pipe[0]);
    close(write_pipe[1]);
    dup2(write_pipe[0], 0);
    dup2(read_pipe[1], 1);
    if (execlp("node", "node", "./pipe.js", NULL) == -1) {
      error("Can't run script");
    }
  }
  close(write_pipe[0]);
  close(read_pipe[1]);
  dup2(read_pipe[0], 0);
  printf("starting...\n");
  int i;
  for (i = 0; i < 2; i++) {
    write(write_pipe[1], "abc", 3);
    char line[255];
    if (fgets(line, 255, stdin) != NULL) {
      printf("received fgets: %s", line);
    }
    if (fgets(line, 255, stdin) != NULL) {
      printf("received fgets: %s", line);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x30>
lea    0xe45(%rip),%rdi
call   1090 <error@plt>
lea    -0x14(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11de <main+0x4e>
lea    0xe27(%rip),%rdi
call   1090 <error@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11fe <main+0x6e>
lea    0xe1d(%rip),%rdi
call   1090 <error@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1263 <main+0xd3>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
lea    0xdf3(%rip),%rsi
lea    0xdf1(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10a0 <execlp@plt>
cmp    $0xffffffff,%eax
jne    125e <main+0xce>
lea    0xddf(%rip),%rdi
call   1090 <error@plt>
jmp    1263 <main+0xd3>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    1335 <main+0x1a5>
mov    -0x10(%rbp),%edi
lea    0xdb0(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d2a(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12e9 <main+0x159>
lea    -0x120(%rbp),%rsi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2cf1(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1322 <main+0x192>
lea    -0x120(%rbp),%rsi
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1327 <main+0x197>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1292 <main+0x102>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x110,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c3 <main+0x23>
lea    0xe42(%rip),%rdi
call   10a0 <error@plt>
mov    %rsp,%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11dc <main+0x3c>
lea    0xe29(%rip),%rdi
call   10a0 <error@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     123c <main+0x9c>
test   %eax,%eax
jne    1248 <main+0xa8>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
lea    0xe0c(%rip),%rdi
lea    0xe0a(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   10b0 <execlp@plt>
cmp    $0xffffffff,%eax
jne    1248 <main+0xa8>
lea    0xdfe(%rip),%rdi
jmp    1243 <main+0xa3>
lea    0xdd3(%rip),%rdi
call   10a0 <error@plt>
mov    (%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
lea    0xdf5(%rip),%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%edi
lea    0xdce(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
mov    0x2d5c(%rip),%rbx
mov    (%rbx),%edx
lea    0x10(%rsp),%rdi
mov    $0xff,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     12b5 <main+0x115>
lea    0xda4(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rbx),%edx
lea    0x10(%rsp),%rdi
mov    $0xff,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     12de <main+0x13e>
lea    0xd7b(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rsp),%edi
lea    0xd60(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
mov    (%rbx),%edx
lea    0x10(%rsp),%rdi
mov    $0xff,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     131c <main+0x17c>
lea    0xd3d(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rbx),%edx
lea    0x10(%rsp),%rdi
mov    $0xff,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     1345 <main+0x1a5>
lea    0xd14(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Can't create the pipe\00"
@.str.1 = [19 x i8] c"Can't fork process\00"
@.str.2 = [5 x i8] c"node\00"
@.str.3 = [10 x i8] c"./pipe.js\00"
@.str.4 = [17 x i8] c"Can't run script\00"
@.str.5 = [13 x i8] c"starting...\0A\00"
@.str.6 = [4 x i8] c"abc\00"
@stdin = global i32 0
@.str.7 = [19 x i8] c"received fgets: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca [255 x i8]
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%14 = call i32 @pipe(i32* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = call i32 (...) @fork()
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32, i32* %4
%26 = icmp ne i32 %25, 0
br i1 %26, label %45, label %27
27: ; preds = %24
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @dup2(i32 %35, i32 0)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @dup2(i32 %38, i32 1)
%40 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32* null)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %27
%43 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %27
br label %45
45: ; preds = %44, %24
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%53 = load i32, i32* %52
%54 = call i32 @dup2(i32 %53, i32 0)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %5
br label %56
56: ; preds = %79, %45
%57 = load i32, i32* %5
%58 = icmp slt i32 %57, 2
br i1 %58, label %59, label %82
59: ; preds = %56
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @write(i32 %61, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 3)
%63 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%64 = load i32, i32* @stdin
%65 = call i32* @fgets(i8* %63, i32 255, i32 %64)
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %70
67: ; preds = %59
%68 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %67, %59
%71 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%72 = load i32, i32* @stdin
%73 = call i32* @fgets(i8* %71, i32 255, i32 %72)
%74 = icmp ne i32* %73, null
br i1 %74, label %75, label %78
75: ; preds = %70
%76 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %76)
br label %78
78: ; preds = %75, %70
br label %79
79: ; preds = %78
%80 = load i32, i32* %5
%81 = add nsw i32 %80, 1
store i32 %81, i32* %5
br label %56
82: ; preds = %56
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"Can't create the pipe\00"
@.str.1 = [19 x i8] c"Can't fork process\00"
@.str.2 = [5 x i8] c"node\00"
@.str.3 = [10 x i8] c"./pipe.js\00"
@.str.4 = [17 x i8] c"Can't run script\00"
@.str.6 = [4 x i8] c"abc\00"
@stdin = global i32 0
@.str.7 = [19 x i8] c"received fgets: %s\00"
@str = [12 x i8] c"starting...\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca [255 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = bitcast [2 x i32]* %2 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = call i32 (...) @fork()
switch i32 %18, label %35 [
i32 -1, label %32
i32 0, label %19
]
19: ; preds = %17
%20 = load i32, i32* %5
%21 = call i32 @close(i32 %20)
%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 = load i32, i32* %12
%26 = call i32 @dup2(i32 %25, i32 0)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @dup2(i32 %28, i32 1)
%30 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32* null)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %19, %17
%33 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %17 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), %19 ]
%34 = call i32 @error(i8* %33)
br label %35
35: ; preds = %32, %17, %19
%36 = load i32, i32* %12
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %5
%42 = call i32 @dup2(i32 %41, i32 0)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%45 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%46 = load i32, i32* %44
%47 = call i32 @write(i32 %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 3)
%48 = load i32, i32* @stdin
%49 = call i32* @fgets(i8* %45, i32 255, i32 %48)
%50 = icmp eq i32* %49, null
br i1 %50, label %53, label %51
51: ; preds = %35
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %45)
br label %53
53: ; preds = %51, %35
%54 = load i32, i32* @stdin
%55 = call i32* @fgets(i8* %45, i32 255, i32 %54)
%56 = icmp eq i32* %55, null
br i1 %56, label %59, label %57
57: ; preds = %53
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %45)
br label %59
59: ; preds = %57, %53
%60 = load i32, i32* %44
%61 = call i32 @write(i32 %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 3)
%62 = load i32, i32* @stdin
%63 = call i32* @fgets(i8* %45, i32 255, i32 %62)
%64 = icmp eq i32* %63, null
br i1 %64, label %67, label %65
65: ; preds = %59
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %45)
br label %67
67: ; preds = %65, %59
%68 = load i32, i32* @stdin
%69 = call i32* @fgets(i8* %45, i32 255, i32 %68)
%70 = icmp eq i32* %69, null
br i1 %70, label %73, label %71
71: ; preds = %67
%72 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %45)
br label %73
73: ; preds = %71, %67
ret i32 0
}