#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*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ eprintf(char *);
int /*<<< orphan*/ execlp(char const *, char const *, char const *,
                          int /*<<< orphan*/ *);
int fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ weprintf(char *, char const *);
int comp(int fd, const char *tool, const char *flags) {
  int fds[2];
  if (pipe(fds) < 0)
    eprintf("pipe:");
  switch (fork()) {
  case -1:
    eprintf("fork:");
  case 0:
    dup2(fd, 1);
    dup2(fds[0], 0);
    close(fds[0]);
    close(fds[1]);
    execlp(tool, tool, flags, NULL);
    weprintf("execlp %s:", tool);
    _exit(1);
  }
  close(fds[0]);
  return fds[1];
}


x86 assembly (clang, -O0):
<comp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11b2 <comp+0x32>
lea    0xe53(%rip),%rdi
call   1080 <eprintf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     11dc <comp+0x5c>
jmp    11cc <comp+0x4c>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     11e8 <comp+0x68>
jmp    123e <comp+0xbe>
lea    0xe23(%rip),%rdi
call   1080 <eprintf@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <execlp@plt>
mov    -0x10(%rbp),%rsi
lea    0xddd(%rip),%rdi
call   1070 <weprintf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jns    11ab <comp+0x2b>
lea    0xe5a(%rip),%rdi
call   1080 <eprintf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11dd <comp+0x5d>
cmp    $0xffffffff,%eax
je     11d1 <comp+0x51>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe2e(%rip),%rdi
call   1080 <eprintf@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    %r14,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1090 <execlp@plt>
lea    0xdef(%rip),%rdi
mov    %r14,%rsi
call   1070 <weprintf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"pipe:\00"
@.str.1 = [6 x i8] c"fork:\00"
@.str.2 = [11 x i8] c"execlp %s:\00"
define i32 @comp(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [2 x i32]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @eprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = call i32 (...) @fork()
switch i32 %14, label %36 [
i32 -1, label %15
i32 0, label %17
]
15: ; preds = %13
%16 = call i32 @eprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %13, %15
%18 = load i32, i32* %4
%19 = call i32 @dup2(i32 %18, i32 1)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @dup2(i32 %21, i32 0)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = load i8*, i8** %5
%30 = load i8*, i8** %5
%31 = load i8*, i8** %6
%32 = call i32 @execlp(i8* %29, i8* %30, i8* %31, i32* null)
%33 = load i8*, i8** %5
%34 = call i32 @weprintf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32 @_exit(i32 1)
unreachable
36: ; preds = %13
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%41 = load i32, i32* %40
ret i32 %41
}

LLVM IR (-O2):
@.str = [6 x i8] c"pipe:\00"
@.str.1 = [6 x i8] c"fork:\00"
@.str.2 = [11 x i8] c"execlp %s:\00"
define i32 @comp(i32 %0, i8* %1, i8* %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 @eprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = call i32 (...) @fork()
switch i32 %12, label %27 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %11
%14 = call i32 @eprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %11, %13
%16 = call i32 @dup2(i32 %0, i32 1)
%17 = load i32, i32* %6
%18 = call i32 @dup2(i32 %17, i32 0)
%19 = load i32, i32* %6
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = call i32 @execlp(i8* %1, i8* %1, i8* %2, i32* null)
%25 = call i32 @weprintf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%26 = call i32 @_exit(i32 1)
unreachable
27: ; preds = %11
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%31 = load i32, i32* %30
ret i32 %31
}