#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*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *);
int execlp(char *, char *, char *, ...);
int fork();
int pipe(int *);
int main(int argc, char **argv) {
  int pd[2], pid;
  if (argc != 2) {
    err(1, "Exactly one argument must be passed!");
  }
  if (pipe(pd) == -1) {
    err(1, "Error creating pipe!");
  }
  pid = fork();
  if (pid < 0) {
    close(pd[0]);
    close(pd[1]);
    err(1, "Error in first fork!");
  }
  if (pid > 0) {
    close(pd[0]);
    if (-1 == execlp("cat", "cat", argv[1], (char *)NULL)) {
      err(1, "Failed to execute cat!");
    }
  } else {
    close(pd[1]);
    dup2(pd[0], 0);
    if (-1 == execlp("sort", "sort", (char *)NULL)) {
      err(1, "Failed to execute sort!");
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1191 <main+0x31>
mov    $0x1,%edi
lea    0xe74(%rip),%rsi
call   1060 <err@plt>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11b4 <main+0x54>
mov    $0x1,%edi
lea    0xe76(%rip),%rsi
call   1060 <err@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e9 <main+0x89>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
lea    0xe56(%rip),%rsi
call   1060 <err@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    123d <main+0xdd>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe45(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1238 <main+0xd8>
mov    $0x1,%edi
lea    0xe20(%rip),%rsi
call   1060 <err@plt>
jmp    1289 <main+0x129>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    0xe14(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1284 <main+0x124>
mov    $0x1,%edi
lea    0xdf0(%rip),%rsi
call   1060 <err@plt>
jmp    1289 <main+0x129>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     117e <main+0x1e>
lea    0xe8c(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    119e <main+0x3e>
lea    0xe91(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11d9 <main+0x79>
je     11fc <main+0x9c>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rbx),%rdx
lea    0xe90(%rip),%rdi
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <execlp@plt>
cmp    $0xffffffff,%eax
jne    1239 <main+0xd9>
lea    0xe7c(%rip),%rsi
jmp    122f <main+0xcf>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xe48(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    0xe53(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1070 <execlp@plt>
cmp    $0xffffffff,%eax
jne    1239 <main+0xd9>
lea    0xe40(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Exactly one argument must be passed!\00"
@.str.1 = [21 x i8] c"Error creating pipe!\00"
@.str.2 = [21 x i8] c"Error in first fork!\00"
@.str.3 = [4 x i8] c"cat\00"
@.str.4 = [23 x i8] c"Failed to execute cat!\00"
@.str.5 = [5 x i8] c"sort\00"
@.str.6 = [24 x i8] c"Failed to execute 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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @err(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %6, 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 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = call i32 (...) @fork()
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %22, %18
%31 = load i32, i32* %7
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %30
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32 (i8*, i8*, i8*, ...) @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* %39, i8* null)
%41 = icmp eq i32 -1, %40
br i1 %41, label %42, label %44
42: ; preds = %33
%43 = call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %33
br label %57
45: ; preds = %30
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%50 = load i32, i32* %49
%51 = call i32 @dup2(i32 %50, i32 0)
%52 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* null)
%53 = icmp eq i32 -1, %52
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %54, %45
br label %57
57: ; preds = %56, %44
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [37 x i8] c"Exactly one argument must be passed!\00"
@.str.1 = [21 x i8] c"Error creating pipe!\00"
@.str.2 = [21 x i8] c"Error in first fork!\00"
@.str.3 = [4 x i8] c"cat\00"
@.str.4 = [23 x i8] c"Failed to execute cat!\00"
@.str.5 = [5 x i8] c"sort\00"
@.str.6 = [24 x i8] c"Failed to execute sort!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = call i32 (...) @fork()
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32, i32* %9
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %33
24: ; preds = %14
%25 = icmp eq i32 %15, 0
br i1 %25, label %33, label %26
26: ; preds = %24
%27 = load i32, i32* %9
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = call i32 (i8*, i8*, i8*, ...) @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* %30, i8* null)
%32 = icmp eq i32 %31, -1
br i1 %32, label %41, label %44
33: ; preds = %17, %24
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %9
%38 = call i32 @dup2(i32 %37, i32 0)
%39 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* null)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %33, %26
%42 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), %26 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), %33 ]
%43 = call i32 @err(i32 1, i8* %42)
br label %44
44: ; preds = %41, %33, %26
ret i32 0
}