#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 creat(char *, int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 3) {
    write(2, "Usage: red12 <file> <cmd> [<args>]\n", 35);
    return 1;
  }
  int r1, r2;
  if (strcmp(argv[1], "&1") == 0) {
    r1 = 0;
    r2 = 1;
  } else if (strcmp(argv[1], "&2") == 0) {
    r1 = 1;
    r2 = 0;
  } else {
    r1 = 1;
    r2 = 1;
  }
  if (r1) {
    int fd = r2 ? creat(argv[1], 0644) : 2;
    if (fd < 0) {
      write(2, "Could not open ", 15);
      write(2, argv[1], strlen(argv[1]));
      write(2, "\n", 1);
      return 2;
    }
    close(1);
    dup(fd);
    if (r2) {
      close(fd);
    }
  }
  if (r2) {
    close(2);
    dup(1);
  }
  execvp(argv[2], argv + 2);
  write(2, "Could not execute ", 18);
  write(2, argv[2], strlen(argv[2]));
  write(2, "\n", 1);
  return 3;
}


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   $0x3,-0x8(%rbp)
jge    11b2 <main+0x42>
mov    $0x2,%edi
lea    0xe64(%rip),%rsi
mov    $0x23,%edx
call   1030 <write@plt>
movl   $0x1,-0x4(%rbp)
jmp    138b <main+0x21b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe63(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11e3 <main+0x73>
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    1227 <main+0xb7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe35(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1214 <main+0xa4>
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
jmp    1222 <main+0xb2>
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    1227 <main+0xb7>
cmpl   $0x0,-0x14(%rbp)
je     12fc <main+0x18c>
cmpl   $0x0,-0x18(%rbp)
je     1255 <main+0xe5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1a4,%esi
call   1090 <creat@plt>
mov    %eax,-0x20(%rbp)
jmp    1262 <main+0xf2>
mov    $0x2,%eax
mov    %eax,-0x20(%rbp)
jmp    1262 <main+0xf2>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12d3 <main+0x163>
mov    $0x2,%edi
lea    0xdac(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1030 <write@plt>
mov    $0x2,%edi
lea    0xd65(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movl   $0x2,-0x4(%rbp)
jmp    138b <main+0x21b>
mov    $0x1,%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <dup@plt>
cmpl   $0x0,-0x18(%rbp)
je     12f7 <main+0x187>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
jmp    12fc <main+0x18c>
cmpl   $0x0,-0x18(%rbp)
je     131a <main+0x1aa>
mov    $0x2,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1050 <dup@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1080 <execvp@plt>
mov    $0x2,%edi
lea    0xcff(%rip),%rsi
mov    $0x12,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1030 <write@plt>
mov    $0x2,%edi
lea    0xca8(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movl   $0x3,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jg     1199 <main+0x29>
lea    0xe80(%rip),%rsi
mov    $0x2,%edi
mov    $0x23,%edx
call   1030 <write@plt>
mov    $0x1,%ebp
jmp    12c1 <main+0x151>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1254 <main+0xe4>
mov    0x8(%rbx),%rdi
lea    0xe67(%rip),%rsi
call   1070 <strcmp@plt>
mov    %rax,%r14
test   %rax,%rax
je     1232 <main+0xc2>
mov    0x8(%rbx),%rdi
mov    $0x1a4,%esi
call   1090 <creat@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1237 <main+0xc7>
lea    0xe42(%rip),%rsi
mov    $0x2,%ebp
mov    $0x2,%edi
mov    $0xf,%edx
call   1030 <write@plt>
mov    0x8(%rbx),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xe04(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12c1 <main+0x151>
mov    $0x2,%ebp
mov    $0x1,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1050 <dup@plt>
test   %r14,%r14
je     1268 <main+0xf8>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1050 <dup@plt>
lea    0x10(%rbx),%rsi
mov    0x10(%rbx),%rdi
call   1080 <execvp@plt>
lea    0xdbe(%rip),%rsi
mov    $0x2,%edi
mov    $0x12,%edx
call   1030 <write@plt>
mov    0x10(%rbx),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xd75(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x3,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Usage: red12 <file> <cmd> [<args>]\0A\00"
@.str.1 = [3 x i8] c"&1\00"
@.str.2 = [3 x i8] c"&2\00"
@.str.3 = [16 x i8] c"Could not open \00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [19 x i8] c"Could not execute \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca 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 slt i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @write(i32 2, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 35)
store i32 1, i32* %3
br label %89
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 0, i32* %6
store i32 1, i32* %7
br label %29
20: ; preds = %13
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 1, i32* %6
store i32 0, i32* %7
br label %28
27: ; preds = %20
store i32 1, i32* %6
store i32 1, i32* %7
br label %28
28: ; preds = %27, %26
br label %29
29: ; preds = %28, %19
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %66
32: ; preds = %29
%33 = load i32, i32* %7
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @creat(i8* %38, i32 420)
br label %41
40: ; preds = %32
br label %41
41: ; preds = %40, %35
%42 = phi i32 [ %39, %35 ], [ 2, %40 ]
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %41
%46 = call i32 @write(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 15)
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call i32 @strlen(i8* %52)
%54 = call i32 @write(i32 2, i8* %49, i32 %53)
%55 = call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
store i32 2, i32* %3
br label %89
56: ; preds = %41
%57 = call i32 @close(i32 1)
%58 = load i32, i32* %8
%59 = call i32 @dup(i32 %58)
%60 = load i32, i32* %7
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %56
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %56
br label %66
66: ; preds = %65, %29
%67 = load i32, i32* %7
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = call i32 @close(i32 2)
%71 = call i32 @dup(i32 1)
br label %72
72: ; preds = %69, %66
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 2
%75 = load i8*, i8** %74
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 2
%78 = call i32 @execvp(i8* %75, i8** %77)
%79 = call i32 @write(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 18)
%80 = load i8**, i8*** %5
%81 = getelementptr inbounds i8*, i8** %80, i64 2
%82 = load i8*, i8** %81
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 2
%85 = load i8*, i8** %84
%86 = call i32 @strlen(i8* %85)
%87 = call i32 @write(i32 2, i8* %82, i32 %86)
%88 = call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
store i32 3, i32* %3
br label %89
89: ; preds = %72, %45, %11
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
@.str = [36 x i8] c"Usage: red12 <file> <cmd> [<args>]\0A\00"
@.str.1 = [3 x i8] c"&1\00"
@.str.2 = [3 x i8] c"&2\00"
@.str.3 = [16 x i8] c"Could not open \00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [19 x i8] c"Could not execute \00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 35)
br label %43
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %31, label %11
11: ; preds = %6
%12 = load i8*, i8** %7
%13 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i8*, i8** %7
%17 = tail call i32 @creat(i8* %16, i32 420)
%18 = icmp slt i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %11, %15
%20 = phi i32 [ %17, %15 ], [ 2, %11 ]
%21 = tail call i32 @close(i32 1)
%22 = tail call i32 @dup(i32 %20)
br i1 %14, label %34, label %29
23: ; preds = %15
%24 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 15)
%25 = load i8*, i8** %7
%26 = tail call i32 @strlen(i8* %25)
%27 = tail call i32 @write(i32 2, i8* %25, i32 %26)
%28 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
br label %43
29: ; preds = %19
%30 = tail call i32 @close(i32 %20)
br label %31
31: ; preds = %6, %29
%32 = tail call i32 @close(i32 2)
%33 = tail call i32 @dup(i32 1)
br label %34
34: ; preds = %19, %31
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = tail call i32 @execvp(i8* %36, i8** %35)
%38 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 18)
%39 = load i8*, i8** %35
%40 = tail call i32 @strlen(i8* %39)
%41 = tail call i32 @write(i32 2, i8* %39, i32 %40)
%42 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
br label %43
43: ; preds = %23, %34, %4
%44 = phi i32 [ 1, %4 ], [ 3, %34 ], [ 2, %23 ]
ret i32 %44
}