#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 dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ stderr;
int main(int ac, char **av) {
  int thepipe[2], newfd, pid;
  if (ac != 3) {
    fprintf(stderr, "useage: pipe cmd1 cmd2\n");
    exit(1);
  }
  if (pipe(thepipe) == -1) {
    perror("Can`t get a pipe");
    exit(1);
  };
  if ((pid = fork()) == -1) {
    {
      perror("Can`t fork");
      exit(2);
    };
  }
  if (pid > 0) {
    close(thepipe[1]);
    if (dup2(thepipe[0], 0) == -1) {
      perror("could not redirect stdin");
      exit(3);
    };
    close(thepipe[0]);
    execlp(av[2], av[2], NULL);
    {
      perror(av[2]);
      exit(4);
    };
  }
  close(thepipe[0]);
  if (dup2(thepipe[1], 1) == -1) {
    perror("could not redirect stdout");
    exit(4);
  };
  close(thepipe[1]);
  execlp(av[1], av[1], NULL);
  {
    perror(av[1]);
    exit(5);
  };
}


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   $0x3,-0x8(%rbp)
je     11bf <main+0x3f>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11e7 <main+0x67>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1210 <main+0x90>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jle    1283 <main+0x103>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    124b <main+0xcb>
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execlp@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12b7 <main+0x137>
lea    0xda5(%rip),%rdi
call   1070 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execlp@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
mov    $0x5,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    11b2 <main+0x32>
mov    %rsi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d1 <main+0x51>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
jne    11f3 <main+0x73>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
test   %eax,%eax
jle    1226 <main+0xa6>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    124b <main+0xcb>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
lea    0xe04(%rip),%rdi
jmp    1266 <main+0xe6>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rbx),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
mov    0x10(%rbx),%rdi
call   1070 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rbx),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
mov    0x8(%rbx),%rdi
call   1070 <perror@plt>
mov    $0x5,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"useage: pipe cmd1 cmd2\0A\00"
@.str.1 = [17 x i8] c"Can`t get a pipe\00"
@.str.2 = [11 x i8] c"Can`t fork\00"
@.str.3 = [25 x i8] c"could not redirect stdin\00"
@.str.4 = [26 x i8] c"could not redirect stdout\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
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 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 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = call i32 (...) @fork()
store i32 %23, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %22
%29 = load i32, i32* %8
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %58
31: ; preds = %28
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @dup2(i32 %36, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 3)
unreachable
42: ; preds = %31
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
%51 = load i8*, i8** %50
%52 = call i32 @execlp(i8* %48, i8* %51, i32* null)
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 2
%55 = load i8*, i8** %54
%56 = call i32 @perror(i8* %55)
%57 = call i32 @exit(i32 4)
unreachable
58: ; preds = %28
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @dup2(i32 %63, i32 1)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %58
%67 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 4)
unreachable
69: ; preds = %58
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 1
%75 = load i8*, i8** %74
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = call i32 @execlp(i8* %75, i8* %78, i32* null)
%80 = load i8**, i8*** %5
%81 = getelementptr inbounds i8*, i8** %80, i64 1
%82 = load i8*, i8** %81
%83 = call i32 @perror(i8* %82)
%84 = call i32 @exit(i32 5)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"useage: pipe cmd1 cmd2\0A\00"
@.str.1 = [17 x i8] c"Can`t get a pipe\00"
@.str.2 = [11 x i8] c"Can`t fork\00"
@.str.3 = [25 x i8] c"could not redirect stdin\00"
@.str.4 = [26 x i8] c"could not redirect stdout\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, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = call i32 (...) @fork()
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 2)
unreachable
23: ; preds = %17
%24 = icmp sgt i32 %18, 0
br i1 %24, label %25, label %44
25: ; preds = %23
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %11
%30 = call i32 @dup2(i32 %29, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 3)
unreachable
35: ; preds = %25
%36 = load i32, i32* %11
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds i8*, i8** %1, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @execlp(i8* %39, i8* %39, i32* null)
%41 = load i8*, i8** %38
%42 = call i32 @perror(i8* %41)
%43 = call i32 @exit(i32 4)
unreachable
44: ; preds = %23
%45 = load i32, i32* %11
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @dup2(i32 %48, i32 1)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @exit(i32 4)
unreachable
54: ; preds = %44
%55 = load i32, i32* %47
%56 = call i32 @close(i32 %55)
%57 = getelementptr inbounds i8*, i8** %1, i64 1
%58 = load i8*, i8** %57
%59 = call i32 @execlp(i8* %58, i8* %58, i32* null)
%60 = load i8*, i8** %57
%61 = call i32 @perror(i8* %60)
%62 = call i32 @exit(i32 5)
unreachable
}