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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
scalar_t__ wait(int *);
int main() {
  pid_t first_pid, second_pid;
  int pipefd[2] = {0};
  if (pipe(pipefd) < 0) {
    perror("pipe");
    exit(1);
  }
  first_pid = fork();
  if (first_pid == 0) {
    dup2(pipefd[1], 1);
    close(pipefd[0]);
    close(pipefd[1]);
    execl("numbers", "numbers", NULL);
  } else {
    second_pid = fork();
    if (second_pid == 0) {
      dup2(pipefd[0], 0);
      close(pipefd[0]);
      close(pipefd[1]);
      execlp("cat", "cat", NULL);
    } else {
      close(pipefd[0]);
      close(pipefd[1]);
      pid_t ret_pid;
      int status;
      while ((ret_pid = wait(&status)) > 0) {
        if (ret_pid == first_pid)
          printf("p1 finish\n");
        if (ret_pid == second_pid)
          printf("p2 finish\n");
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11f8 <main+0x48>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1243 <main+0x93>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    0xdd3(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10b0 <execl@plt>
jmp    12f5 <main+0x145>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128b <main+0xdb>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    0xd93(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10a0 <execlp@plt>
jmp    12f0 <main+0x140>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    -0x2c(%rbp),%rdi
call   10c0 <wait@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jle    12eb <main+0x13b>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    12cc <main+0x11c>
lea    0xd4a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    12e6 <main+0x136>
lea    0xd3b(%rip),%rdi
call   1040 <printf@plt>
jmp    129b <main+0xeb>
jmp    12f0 <main+0x140>
jmp    12f5 <main+0x145>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     12ca <main+0x12a>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     125c <main+0xbc>
mov    %rax,%r14
xor    %eax,%eax
call   10c0 <fork@plt>
mov    0x8(%rsp),%edi
test   %rax,%rax
je     128f <main+0xef>
mov    %rax,%rbx
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0x14(%rsp),%rdi
call   10b0 <wait@plt>
test   %rax,%rax
jle    12b9 <main+0x119>
mov    %rax,%rbp
lea    0xdfc(%rip),%r15
lea    0xdeb(%rip),%r12
lea    0x14(%rsp),%r13
jmp    1240 <main+0xa0>
nopl   (%rax)
mov    %r13,%rdi
call   10b0 <wait@plt>
mov    %rax,%rbp
test   %rax,%rax
jle    12b9 <main+0x119>
cmp    %r14,%rbp
jne    124d <main+0xad>
mov    %r15,%rdi
call   1030 <puts@plt>
cmp    %rbx,%rbp
jne    1230 <main+0x90>
mov    %r12,%rdi
call   1030 <puts@plt>
jmp    1230 <main+0x90>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xd82(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10a0 <execl@plt>
jmp    12b9 <main+0x119>
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xd5e(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [8 x i8] c"numbers\00"
@.str.2 = [4 x i8] c"cat\00"
@.str.3 = [11 x i8] c"p1 finish\0A\00"
@.str.4 = [11 x i8] c"p2 finish\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca [2 x i32]
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [2 x i32]* %4 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i64 (...) @fork()
store i64 %15, i64* %2
%16 = load i64, i64* %2
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @dup2(i32 %20, i32 1)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %69
29: ; preds = %14
%30 = call i64 (...) @fork()
store i64 %30, i64* %3
%31 = load i64, i64* %3
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %29
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %4, 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]* %4, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %68
44: ; preds = %29
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %66, %44
%52 = call i64 @wait(i32* %6)
store i64 %52, i64* %5
%53 = icmp sgt i64 %52, 0
br i1 %53, label %54, label %67
54: ; preds = %51
%55 = load i64, i64* %5
%56 = load i64, i64* %2
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %58, %54
%61 = load i64, i64* %5
%62 = load i64, i64* %3
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %64, %60
br label %51
67: ; preds = %51
br label %68
68: ; preds = %67, %33
br label %69
69: ; preds = %68, %18
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [8 x i8] c"numbers\00"
@.str.2 = [4 x i8] c"cat\00"
@str = [10 x i8] c"p2 finish\00"
@str.5 = [10 x i8] c"p1 finish\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to [2 x i32]*
%3 = alloca i32
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%5 = bitcast i64* %1 to i32*
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @dup2(i32 %16, i32 1)
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %15
%21 = call i32 @close(i32 %20)
%22 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %56
23: ; preds = %11
%24 = call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
%26 = load i32, i32* %5
br i1 %25, label %27, label %35
27: ; preds = %23
%28 = call i32 @dup2(i32 %26, i32 0)
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %56
35: ; preds = %23
%36 = call i32 @close(i32 %26)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = bitcast i32* %3 to i8*
%41 = call i64 @wait(i32* %3)
%42 = icmp sgt i64 %41, 0
br i1 %42, label %43, label %55
43: ; preds = %35, %52
%44 = phi i64 [ %53, %52 ], [ %41, %35 ]
%45 = icmp eq i64 %44, %12
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
%49 = icmp eq i64 %44, %24
br i1 %49, label %50, label %52
50: ; preds = %48
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %50, %48
%53 = call i64 @wait(i32* %3)
%54 = icmp sgt i64 %53, 0
br i1 %54, label %43, label %55
55: ; preds = %52, %35
br label %56
56: ; preds = %27, %55, %14
ret i32 0
}