#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 */
scalar_t__ EOF;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ pipe(int *);
scalar_t__ scanf(char *, int *);
int main(int argc, char **argv) {
  int fds1[2];
  pipe(fds1);
  if (!fork()) {
    close(fds1[0]);
    close(1);
    dup(fds1[1]);
    execlp("ps", "ps", "ax", NULL);
  }
  close(fds1[1]);
  int fds2[2];
  pipe(fds2);
  if (!fork()) {
    close(fds2[0]);
    close(0);
    dup(fds1[0]);
    close(1);
    dup(fds2[1]);
    execlp("grep", "grep", argv[1], NULL);
  }
  close(fds1[0]);
  close(fds2[1]);
  int fds3[2];
  pipe(fds3);
  if (!fork()) {
    close(fds3[0]);
    close(0);
    dup(fds2[0]);
    close(1);
    dup(fds3[1]);
    execlp("awk", "awk", "{ print $1 }", NULL);
  }
  close(fds2[0]);
  close(fds3[1]);
  close(0);
  dup(fds3[0]);
  int pid;
  while (scanf("%d", &pid) != EOF)
    kill(pid, SIGTERM);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%eax
jne    11d3 <main+0x63>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1030 <dup@plt>
lea    0xe40(%rip),%rsi
lea    0xe3c(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1080 <execlp@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%eax
jne    1238 <main+0xc8>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1030 <dup@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdda(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1080 <execlp@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%eax
jne    12a4 <main+0x134>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1030 <dup@plt>
lea    0xd7a(%rip),%rsi
lea    0xd77(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1080 <execlp@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1030 <dup@plt>
lea    0xd52(%rip),%rdi
lea    -0x2c(%rbp),%rsi
call   1070 <scanf@plt>
mov    0x2d06(%rip),%rcx
cmp    (%rcx),%rax
je     12f9 <main+0x189>
mov    -0x2c(%rbp),%edi
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
jmp    12c3 <main+0x153>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
lea    0x18(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    11c7 <main+0x57>
mov    0x18(%rsp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x1c(%rsp),%edi
call   1030 <dup@plt>
lea    0xe4a(%rip),%rdi
lea    0xe46(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1080 <execlp@plt>
mov    0x1c(%rsp),%edi
call   1040 <close@plt>
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    1226 <main+0xb6>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x18(%rsp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1030 <dup@plt>
mov    0x8(%rbx),%rdx
lea    0xdea(%rip),%rdi
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1080 <execlp@plt>
mov    0x18(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
lea    0x20(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    1291 <main+0x121>
mov    0x20(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x24(%rsp),%edi
call   1030 <dup@plt>
lea    0xd8b(%rip),%rdi
lea    0xd88(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1080 <execlp@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x24(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x20(%rsp),%edi
call   1030 <dup@plt>
lea    0xd62(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x2d15(%rip),%r15
cmp    (%r15),%rax
je     130d <main+0x19d>
mov    0x2d11(%rip),%r12
lea    0xd3e(%rip),%r14
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%edi
mov    (%r12),%esi
call   1060 <kill@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
cmp    (%r15),%rax
jne    12f0 <main+0x180>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ps\00"
@.str.1 = [3 x i8] c"ax\00"
@.str.2 = [5 x i8] c"grep\00"
@.str.3 = [4 x i8] c"awk\00"
@.str.4 = [13 x i8] c"{ print $1 }\00"
@.str.5 = [3 x i8] c"%d\00"
@EOF = global i64 0
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [2 x i32]
%8 = alloca [2 x i32]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = call i32 (...) @fork()
%13 = icmp ne i32 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %2
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
%18 = call i32 @close(i32 1)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @dup(i32 %20)
%22 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %23
23: ; preds = %14, %2
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%28 = call i32 @pipe(i32* %27)
%29 = call i32 (...) @fork()
%30 = icmp ne i32 %29, 0
br i1 %30, label %47, label %31
31: ; preds = %23
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = call i32 @close(i32 0)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @dup(i32 %37)
%39 = call i32 @close(i32 1)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @dup(i32 %41)
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = 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* %45, i32* null)
br label %47
47: ; preds = %31, %23
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%55 = call i32 @pipe(i32* %54)
%56 = call i32 (...) @fork()
%57 = icmp ne i32 %56, 0
br i1 %57, label %71, label %58
58: ; preds = %47
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = call i32 @close(i32 0)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%64 = load i32, i32* %63
%65 = call i32 @dup(i32 %64)
%66 = call i32 @close(i32 1)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @dup(i32 %68)
%70 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32* null)
br label %71
71: ; preds = %58, %47
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%76 = load i32, i32* %75
%77 = call i32 @close(i32 %76)
%78 = call i32 @close(i32 0)
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%80 = load i32, i32* %79
%81 = call i32 @dup(i32 %80)
br label %82
82: ; preds = %86, %71
%83 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %9)
%84 = load i64, i64* @EOF
%85 = icmp ne i64 %83, %84
br i1 %85, label %86, label %90
86: ; preds = %82
%87 = load i32, i32* %9
%88 = load i32, i32* @SIGTERM
%89 = call i32 @kill(i32 %87, i32 %88)
br label %82
90: ; preds = %82
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"ps\00"
@.str.1 = [3 x i8] c"ax\00"
@.str.2 = [5 x i8] c"grep\00"
@.str.3 = [4 x i8] c"awk\00"
@.str.4 = [13 x i8] c"{ print $1 }\00"
@.str.5 = [3 x i8] c"%d\00"
@EOF = global i64 0
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = bitcast [2 x i32]* %3 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* %8
%14 = call i32 @close(i32 %13)
%15 = call i32 @close(i32 1)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @dup(i32 %17)
%19 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %20
20: ; preds = %12, %2
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = bitcast [2 x i32]* %4 to i8*
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%26 = call i32 @pipe(i32* %25)
%27 = call i32 (...) @fork()
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %20
%30 = load i32, i32* %25
%31 = call i32 @close(i32 %30)
%32 = call i32 @close(i32 0)
%33 = load i32, i32* %8
%34 = call i32 @dup(i32 %33)
%35 = call i32 @close(i32 1)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @dup(i32 %37)
%39 = getelementptr inbounds i8*, i8** %1, i64 1
%40 = load i8*, i8** %39
%41 = 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* %40, i32* null)
br label %42
42: ; preds = %29, %20
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = bitcast [2 x i32]* %5 to i8*
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%50 = call i32 @pipe(i32* %49)
%51 = call i32 (...) @fork()
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %64
53: ; preds = %42
%54 = load i32, i32* %49
%55 = call i32 @close(i32 %54)
%56 = call i32 @close(i32 0)
%57 = load i32, i32* %25
%58 = call i32 @dup(i32 %57)
%59 = call i32 @close(i32 1)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @dup(i32 %61)
%63 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32* null)
br label %64
64: ; preds = %53, %42
%65 = load i32, i32* %25
%66 = call i32 @close(i32 %65)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = call i32 @close(i32 0)
%71 = load i32, i32* %49
%72 = call i32 @dup(i32 %71)
%73 = bitcast i32* %6 to i8*
%74 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %6)
%75 = load i64, i64* @EOF
%76 = icmp eq i64 %74, %75
br i1 %76, label %84, label %77
77: ; preds = %64, %77
%78 = load i32, i32* %6
%79 = load i32, i32* @SIGTERM
%80 = call i32 @kill(i32 %78, i32 %79)
%81 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %6)
%82 = load i64, i64* @EOF
%83 = icmp eq i64 %81, %82
br i1 %83, label %84, label %77
84: ; preds = %77, %64
ret i32 0
}