#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*/ F_DUPFD;
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  if (argc < 4) {
    printf("Uso: %s <programa> <<|>> <fichero>.\n", argv[0]);
    exit(-1);
  }
  char redireccion = argv[2][0];
  int fd;
  switch (redireccion) {
  case '<':
    close(STDIN_FILENO);
    if ((fd = open(argv[3], O_RDONLY)) == -1) {
      perror("open");
      exit(-1);
    }
    if (fcntl(fd, F_DUPFD, STDIN_FILENO) == -1) {
      perror("fcntl falló");
      exit(-1);
    }
    if ((execlp(argv[1], argv[1], NULL) < 0)) {
      perror("Error en el execl\n");
      exit(-1);
    }
    break;
  case '>':
    close(STDOUT_FILENO);
    if ((fd = open(argv[3], O_WRONLY | O_TRUNC | O_CREAT)) == -1) {
      perror("open");
      exit(-1);
    }
    if (fcntl(fd, F_DUPFD, STDOUT_FILENO) == -1) {
      perror("fcntl falló");
      exit(-1);
    }
    if ((execlp(argv[1], argv[1], NULL) < 0)) {
      perror("Error en el execl\n");
      exit(-1);
    }
    break;
  }
  exit(0);
}


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   $0x4,-0x8(%rbp)
jge    11ad <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x3c,%eax
je     11e0 <main+0x70>
jmp    11cf <main+0x5f>
mov    -0x1c(%rbp),%eax
sub    $0x3e,%eax
je     129c <main+0x12c>
jmp    136a <main+0x1fa>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1226 <main+0xb6>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    125f <main+0xef>
lea    0xdda(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@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>
cmp    $0x0,%eax
jge    1297 <main+0x127>
lea    0xdaf(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    136a <main+0x1fa>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
mov    0x2d2e(%rip),%rax
or     (%rax),%esi
mov    0x2d2d(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12f4 <main+0x184>
lea    0xd40(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
mov    0x2cd9(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    132d <main+0x1bd>
lea    0xd0c(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@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>
cmp    $0x0,%eax
jge    1365 <main+0x1f5>
lea    0xce1(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    136a <main+0x1fa>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jg     1195 <main+0x25>
mov    (%rbx),%rsi
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x10(%rbx),%rax
movsbl (%rax),%eax
cmp    $0x3e,%eax
je     11c8 <main+0x58>
cmp    $0x3c,%eax
jne    1252 <main+0xe2>
mov    0x2e07(%rip),%r14
mov    (%r14),%edi
call   1050 <close@plt>
mov    0x18(%rbx),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
jmp    11f6 <main+0x86>
mov    0x2e11(%rip),%r14
mov    (%r14),%edi
call   1050 <close@plt>
mov    0x2dca(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x18(%rbx),%rdi
mov    0x2e04(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
jne    1209 <main+0x99>
lea    0xe1e(%rip),%rdi
jmp    1243 <main+0xd3>
mov    0x2db8(%rip),%rcx
mov    (%rcx),%esi
mov    (%r14),%edx
mov    %eax,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    122a <main+0xba>
lea    0xe02(%rip),%rdi
jmp    1243 <main+0xd3>
mov    0x8(%rbx),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
test   %eax,%eax
jns    1252 <main+0xe2>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"Uso: %s <programa> <<|>> <fichero>.\0A\00"
@STDIN_FILENO = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@F_DUPFD = global i32 0
@.str.2 = [13 x i8] c"fcntl fall\C3\B3\00"
@.str.3 = [19 x i8] c"Error en el execl\0A\00"
@STDOUT_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%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 slt i32 %8, 4
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
store i8 %21, i8* %6
%22 = load i8, i8* %6
%23 = sext i8 %22 to i32
switch i32 %23, label %96 [
i32 60, label %24
i32 62, label %58
]
24: ; preds = %16
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @close(i32 %25)
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 3
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %24
%37 = load i32, i32* %7
%38 = load i32, i32* @F_DUPFD
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @fcntl(i32 %37, i32 %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %36
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call i32 @execlp(i8* %48, i8* %51, i32* null)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %45
%55 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %45
br label %96
58: ; preds = %16
%59 = load i32, i32* @STDOUT_FILENO
%60 = call i32 @close(i32 %59)
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 3
%63 = load i8*, i8** %62
%64 = load i32, i32* @O_WRONLY
%65 = load i32, i32* @O_TRUNC
%66 = or i32 %64, %65
%67 = load i32, i32* @O_CREAT
%68 = or i32 %66, %67
%69 = call i32 @open(i8* %63, i32 %68)
store i32 %69, i32* %7
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %58
%72 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%73 = call i32 @exit(i32 -1)
unreachable
74: ; preds = %58
%75 = load i32, i32* %7
%76 = load i32, i32* @F_DUPFD
%77 = load i32, i32* @STDOUT_FILENO
%78 = call i32 @fcntl(i32 %75, i32 %76, i32 %77)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %83
80: ; preds = %74
%81 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%82 = call i32 @exit(i32 -1)
unreachable
83: ; preds = %74
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = load i8**, i8*** %5
%88 = getelementptr inbounds i8*, i8** %87, i64 1
%89 = load i8*, i8** %88
%90 = call i32 @execlp(i8* %86, i8* %89, i32* null)
%91 = icmp slt i32 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %83
%93 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%94 = call i32 @exit(i32 -1)
unreachable
95: ; preds = %83
br label %96
96: ; preds = %16, %95, %57
%97 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [37 x i8] c"Uso: %s <programa> <<|>> <fichero>.\0A\00"
@STDIN_FILENO = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@F_DUPFD = global i32 0
@.str.2 = [13 x i8] c"fcntl fall\C3\B3\00"
@.str.3 = [19 x i8] c"Error en el execl\0A\00"
@STDOUT_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
switch i32 %12, label %71 [
i32 60, label %13
i32 62, label %40
]
13: ; preds = %8
%14 = load i32, i32* @STDIN_FILENO
%15 = tail call i32 @close(i32 %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 3
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %13
%25 = load i32, i32* @F_DUPFD
%26 = load i32, i32* @STDIN_FILENO
%27 = tail call i32 @fcntl(i32 %19, i32 %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds i8*, i8** %1, i64 1
%34 = load i8*, i8** %33
%35 = tail call i32 @execlp(i8* %34, i8* %34, i32* null)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %71
37: ; preds = %32
%38 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%39 = tail call i32 @exit(i32 -1)
unreachable
40: ; preds = %8
%41 = load i32, i32* @STDOUT_FILENO
%42 = tail call i32 @close(i32 %41)
%43 = getelementptr inbounds i8*, i8** %1, i64 3
%44 = load i8*, i8** %43
%45 = load i32, i32* @O_WRONLY
%46 = load i32, i32* @O_TRUNC
%47 = or i32 %46, %45
%48 = load i32, i32* @O_CREAT
%49 = or i32 %47, %48
%50 = tail call i32 @open(i8* %44, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %40
%53 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%54 = tail call i32 @exit(i32 -1)
unreachable
55: ; preds = %40
%56 = load i32, i32* @F_DUPFD
%57 = load i32, i32* @STDOUT_FILENO
%58 = tail call i32 @fcntl(i32 %50, i32 %56, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%62 = tail call i32 @exit(i32 -1)
unreachable
63: ; preds = %55
%64 = getelementptr inbounds i8*, i8** %1, i64 1
%65 = load i8*, i8** %64
%66 = tail call i32 @execlp(i8* %65, i8* %65, i32* null)
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %63
%69 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%70 = tail call i32 @exit(i32 -1)
unreachable
71: ; preds = %63, %32, %8
%72 = tail call i32 @exit(i32 0)
unreachable
}