#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 */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
char *malloc(int);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("Error! I must get one argument!\n");
    exit(-1);
  }
  int pfd1[2], pfd2[2];
  if (pipe(pfd1) == -1) {
    printf("Error! Could not open pipe!\n");
    exit(-3);
  };
  if (pipe(pfd2) == -1) {
    printf("Error! Could not open pipe!\n");
    exit(-3);
  };
  if (fork() == 0) {
    close(pfd1[1]);
    close(pfd1[0]);
    close(pfd2[0]);
    dup2(pfd2[1], STDOUT_FILENO);
    execlp("cat", "cat", argv[1], NULL);
    printf("Error calling exec!\n");
    exit(-10);
  }
  close(pfd2[1]);
  if (fork() == 0) {
    close(pfd2[1]);
    close(pfd1[0]);
    dup2(pfd2[0], STDIN_FILENO);
    execlp("sort", "sort", NULL);
    printf("Error calling exec!\n");
    exit(-10);
  }
  close(pfd1[1]);
  wait(NULL);
  wait(NULL);
  return 0;
  char *buf = malloc(100);
  int rr;
  while ((rr = read(pfd1[0], buf, 100)) > 0) {
    write(STDOUT_FILENO, buf, rr);
  }
  free(buf);
  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)
cmpl   $0x2,-0x8(%rbp)
je     11b6 <main+0x36>
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11de <main+0x5e>
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
mov    $0xfffffffd,%edi
call   1070 <exit@plt>
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1206 <main+0x86>
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
mov    $0xfffffffd,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    1273 <main+0xf3>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdef(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1080 <execlp@plt>
lea    0xdde(%rip),%rdi
call   1040 <printf@plt>
mov    $0xfffffff6,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    12d8 <main+0x158>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xda3(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1080 <execlp@plt>
lea    0xd79(%rip),%rdi
call   1040 <printf@plt>
mov    $0xfffffff6,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1203 <main+0x83>
mov    %rsi,%rbx
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     11ed <main+0x6d>
mov    %rsp,%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     11ed <main+0x6d>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1219 <main+0x99>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     125e <main+0xde>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%edi
call   1070 <exit@plt>
lea    0xe2f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x8(%rbx),%rdx
lea    0xdb0(%rip),%rdi
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1080 <execlp@plt>
jmp    1294 <main+0x114>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
lea    0xd7c(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1080 <execlp@plt>
lea    0xd6e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffff6,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"Error! I must get one argument!\0A\00"
@.str.1 = [29 x i8] c"Error! Could not open pipe!\0A\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [4 x i8] c"cat\00"
@.str.3 = [21 x i8] c"Error calling exec!\0A\00"
@STDIN_FILENO = global i32 0
@.str.4 = [5 x i8] c"sort\00"
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 i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 -3)
unreachable
22: ; preds = %15
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%24 = call i32 @pipe(i32* %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -3)
unreachable
29: ; preds = %22
%30 = call i64 (...) @fork()
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %52
32: ; preds = %29
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%43 = load i32, i32* %42
%44 = load i32, i32* @STDOUT_FILENO
%45 = call i32 @dup2(i32 %43, i32 %44)
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 (i8*, i8*, i8*, ...) @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), i8* %48, i8* null)
%50 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 -10)
unreachable
52: ; preds = %29
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = call i64 (...) @fork()
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %72
58: ; preds = %52
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%66 = load i32, i32* %65
%67 = load i32, i32* @STDIN_FILENO
%68 = call i32 @dup2(i32 %66, i32 %67)
%69 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* null)
%70 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%71 = call i32 @exit(i32 -10)
unreachable
72: ; preds = %52
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%74 = load i32, i32* %73
%75 = call i32 @close(i32 %74)
%76 = call i32 @wait(i32* null)
%77 = call i32 @wait(i32* null)
ret i32 0
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str.2 = [4 x i8] c"cat\00"
@STDIN_FILENO = global i32 0
@.str.4 = [5 x i8] c"sort\00"
@str.5 = [20 x i8] c"Error calling exec!\00"
@str.7 = [28 x i8] c"Error! Could not open pipe!\00"
@str.8 = [32 x i8] c"Error! I must get one argument!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.8, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = bitcast [2 x i32]* %3 to i8*
%11 = bitcast [2 x i32]* %4 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%17 = call i32 @exit(i32 -3)
unreachable
18: ; preds = %9
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%20 = call i32 @pipe(i32* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%24 = call i32 @exit(i32 -3)
unreachable
25: ; preds = %18
%26 = call i64 (...) @fork()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %45
28: ; preds = %25
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %12
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %19
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%37 = load i32, i32* %36
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @dup2(i32 %37, i32 %38)
%40 = getelementptr inbounds i8*, i8** %1, i64 1
%41 = load i8*, i8** %40
%42 = call i32 (i8*, i8*, i8*, ...) @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), i8* %41, i8* null)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%44 = call i32 @exit(i32 -10)
unreachable
45: ; preds = %25
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = call i64 (...) @fork()
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %62
51: ; preds = %45
%52 = load i32, i32* %46
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %12
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %19
%57 = load i32, i32* @STDIN_FILENO
%58 = call i32 @dup2(i32 %56, i32 %57)
%59 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* null)
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%61 = call i32 @exit(i32 -10)
unreachable
62: ; preds = %45
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = call i32 @wait(i32* null)
%67 = call i32 @wait(i32* null)
ret i32 0
}