#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 */
struct tokens {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ cmd_exec_helper(char *, struct tokens *);
int /*<<< orphan*/ cmd_input_direction(char **, char *);
int /*<<< orphan*/ cmd_output_direction(char **, char *);
int detect_in_direction(struct tokens *);
int detect_out_direction(struct tokens *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
char *strtok(char *, char *);
char *tokens_get_token(struct tokens *, int);
int cmd_exec(struct tokens *tokens) {
  int out = detect_out_direction(tokens);
  int in = detect_in_direction(tokens);
  if (out != -1) {
    char *argv[out + 1];
    int j;
    for (j = 0; j < out; j++) {
      argv[j] = tokens_get_token(tokens, j);
    }
    argv[out] = NULL;
    char *filename = tokens_get_token(tokens, out + 1);
    cmd_output_direction(argv, filename);
  } else if (in != -1) {
    char *argv[in + 1];
    int j;
    for (j = 0; j < in; j++) {
      argv[j] = tokens_get_token(tokens, j);
    }
    argv[in] = NULL;
    char *filename = tokens_get_token(tokens, in + 1);
    cmd_input_direction(argv, filename);
  } else {
    cmd_exec_helper(tokens_get_token(tokens, 0), tokens);
    char *path = getenv("PATH");
    char *pch;
    pch = strtok(path, ":");
    while (pch != NULL) {
      char *str = malloc(100);
      strcat(str, pch);
      strcat(str, "/");
      strcat(str, tokens_get_token(tokens, 0));
      pch = strtok(NULL, ":");
      cmd_exec_helper(str, tokens);
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<cmd_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   10c0 <detect_out_direction@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <detect_in_direction@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     1273 <cmd_exec+0xd3>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1237 <cmd_exec+0x97>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <tokens_get_token@plt>
mov    %rax,%rdx
mov    -0x70(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1202 <cmd_exec+0x62>
mov    -0x70(%rbp),%rax
movslq -0xc(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1040 <tokens_get_token@plt>
mov    -0x70(%rbp),%rdi
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
call   1070 <cmd_output_direction@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
jmp    13df <cmd_exec+0x23f>
cmpl   $0xffffffff,-0x10(%rbp)
je     131f <cmd_exec+0x17f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12e3 <cmd_exec+0x143>
mov    -0x8(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   1040 <tokens_get_token@plt>
mov    %rax,%rdx
mov    -0x78(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    12ae <cmd_exec+0x10e>
mov    -0x78(%rbp),%rax
movslq -0x10(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
add    $0x1,%esi
call   1040 <tokens_get_token@plt>
mov    -0x78(%rbp),%rdi
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rsi
call   1090 <cmd_input_direction@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
jmp    13da <cmd_exec+0x23a>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1040 <tokens_get_token@plt>
mov    %rax,%rdi
mov    -0x8(%rbp),%rsi
call   1050 <cmd_exec_helper@plt>
lea    0xcc3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
lea    0xcb4(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     13d5 <cmd_exec+0x235>
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x68(%rbp),%rdi
lea    0xc7c(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x68(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1040 <tokens_get_token@plt>
mov    -0x80(%rbp),%rdi
mov    %rax,%rsi
call   10b0 <strcat@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xc4b(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x60(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <cmd_exec_helper@plt>
jmp    135a <cmd_exec+0x1ba>
jmp    13da <cmd_exec+0x23a>
jmp    13df <cmd_exec+0x23f>
mov    $0x1,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_exec>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r12
call   10c0 <detect_out_direction@plt>
mov    %eax,%r14d
mov    %r12,%rdi
call   1080 <detect_in_direction@plt>
cmp    $0xffffffff,%r14d
je     123c <cmd_exec+0x9c>
mov    %rsp,-0x38(%rbp)
lea    0x1(%r14),%eax
mov    %eax,-0x2c(%rbp)
mov    %r14d,%r15d
mov    %rsp,%r13
lea    0x17(,%r15,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
test   %r14d,%r14d
jle    1218 <cmd_exec+0x78>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    %ebx,%esi
call   1040 <tokens_get_token@plt>
mov    %rax,0x0(%r13,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1200 <cmd_exec+0x60>
movslq %r14d,%rax
movq   $0x0,0x0(%r13,%rax,8)
mov    %r12,%rdi
mov    -0x2c(%rbp),%esi
call   1040 <tokens_get_token@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1070 <cmd_output_direction@plt>
jmp    12ba <cmd_exec+0x11a>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     12d5 <cmd_exec+0x135>
mov    %rsp,-0x38(%rbp)
lea    0x1(%r15),%eax
mov    %eax,-0x2c(%rbp)
mov    %r15d,%r14d
mov    %rsp,%r13
lea    0x17(,%r14,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
test   %r15d,%r15d
jle    1298 <cmd_exec+0xf8>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %ebx,%esi
call   1040 <tokens_get_token@plt>
mov    %rax,0x0(%r13,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1280 <cmd_exec+0xe0>
movslq %r15d,%rax
movq   $0x0,0x0(%r13,%rax,8)
mov    %r12,%rdi
mov    -0x2c(%rbp),%esi
call   1040 <tokens_get_token@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1090 <cmd_input_direction@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
xor    %esi,%esi
call   1040 <tokens_get_token@plt>
mov    %rax,%rdi
mov    %r12,%rsi
call   1050 <cmd_exec_helper@plt>
lea    0xd0f(%rip),%rdi
call   1030 <getenv@plt>
lea    0xd08(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     12c1 <cmd_exec+0x121>
mov    %rax,%rbx
lea    0xcf3(%rip),%r14
lea    0xcea(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbx,%rsi
call   10b0 <strcat@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1040 <tokens_get_token@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   10b0 <strcat@plt>
xor    %edi,%edi
mov    %r15,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <cmd_exec_helper@plt>
test   %rbx,%rbx
jne    1320 <cmd_exec+0x180>
jmp    12c1 <cmd_exec+0x121>

LLVM IR (-O0):
%struct.tokens = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
define i32 @cmd_exec(%struct.tokens* %0) {
%2 = alloca %struct.tokens*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
store %struct.tokens* %0, %struct.tokens** %2
%16 = load %struct.tokens*, %struct.tokens** %2
%17 = call i32 @detect_out_direction(%struct.tokens* %16)
store i32 %17, i32* %3
%18 = load %struct.tokens*, %struct.tokens** %2
%19 = call i32 @detect_in_direction(%struct.tokens* %18)
store i32 %19, i32* %4
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %53
22: ; preds = %1
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
%25 = zext i32 %24 to i64
%26 = call i8* @llvm.stacksave()
store i8* %26, i8** %5
%27 = alloca i8*, i64 %25
store i64 %25, i64* %6
store i32 0, i32* %7
br label %28
28: ; preds = %39, %22
%29 = load i32, i32* %7
%30 = load i32, i32* %3
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %42
32: ; preds = %28
%33 = load %struct.tokens*, %struct.tokens** %2
%34 = load i32, i32* %7
%35 = call i8* @tokens_get_token(%struct.tokens* %33, i32 %34)
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %27, i64 %37
store i8* %35, i8** %38
br label %39
39: ; preds = %32
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %28
42: ; preds = %28
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8*, i8** %27, i64 %44
store i8* null, i8** %45
%46 = load %struct.tokens*, %struct.tokens** %2
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
%49 = call i8* @tokens_get_token(%struct.tokens* %46, i32 %48)
store i8* %49, i8** %8
%50 = load i8*, i8** %8
%51 = call i32 @cmd_output_direction(i8** %27, i8* %50)
%52 = load i8*, i8** %5
br label %115
53: ; preds = %1
%54 = load i32, i32* %4
%55 = icmp ne i32 %54, -1
br i1 %55, label %56, label %87
56: ; preds = %53
%57 = load i32, i32* %4
%58 = add nsw i32 %57, 1
%59 = zext i32 %58 to i64
%60 = call i8* @llvm.stacksave()
store i8* %60, i8** %9
%61 = alloca i8*, i64 %59
store i64 %59, i64* %10
store i32 0, i32* %11
br label %62
62: ; preds = %73, %56
%63 = load i32, i32* %11
%64 = load i32, i32* %4
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %76
66: ; preds = %62
%67 = load %struct.tokens*, %struct.tokens** %2
%68 = load i32, i32* %11
%69 = call i8* @tokens_get_token(%struct.tokens* %67, i32 %68)
%70 = load i32, i32* %11
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8*, i8** %61, i64 %71
store i8* %69, i8** %72
br label %73
73: ; preds = %66
%74 = load i32, i32* %11
%75 = add nsw i32 %74, 1
store i32 %75, i32* %11
br label %62
76: ; preds = %62
%77 = load i32, i32* %4
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8*, i8** %61, i64 %78
store i8* null, i8** %79
%80 = load %struct.tokens*, %struct.tokens** %2
%81 = load i32, i32* %4
%82 = add nsw i32 %81, 1
%83 = call i8* @tokens_get_token(%struct.tokens* %80, i32 %82)
store i8* %83, i8** %12
%84 = load i8*, i8** %12
%85 = call i32 @cmd_input_direction(i8** %61, i8* %84)
%86 = load i8*, i8** %9
br label %114
87: ; preds = %53
%88 = load %struct.tokens*, %struct.tokens** %2
%89 = call i8* @tokens_get_token(%struct.tokens* %88, i32 0)
%90 = load %struct.tokens*, %struct.tokens** %2
%91 = call i32 @cmd_exec_helper(i8* %89, %struct.tokens* %90)
%92 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %92, i8** %13
%93 = load i8*, i8** %13
%94 = call i8* @strtok(i8* %93, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %94, i8** %14
br label %95
95: ; preds = %98, %87
%96 = load i8*, i8** %14
%97 = icmp ne i8* %96, null
br i1 %97, label %98, label %113
98: ; preds = %95
%99 = call i8* @malloc(i32 100)
store i8* %99, i8** %15
%100 = load i8*, i8** %15
%101 = load i8*, i8** %14
%102 = call i32 @strcat(i8* %100, i8* %101)
%103 = load i8*, i8** %15
%104 = call i32 @strcat(i8* %103, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%105 = load i8*, i8** %15
%106 = load %struct.tokens*, %struct.tokens** %2
%107 = call i8* @tokens_get_token(%struct.tokens* %106, i32 0)
%108 = call i32 @strcat(i8* %105, i8* %107)
%109 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %109, i8** %14
%110 = load i8*, i8** %15
%111 = load %struct.tokens*, %struct.tokens** %2
%112 = call i32 @cmd_exec_helper(i8* %110, %struct.tokens* %111)
br label %95
113: ; preds = %95
br label %114
114: ; preds = %113, %76
br label %115
115: ; preds = %114, %42
ret i32 1
}

LLVM IR (-O2):
%struct.tokens = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
define i32 @cmd_exec(%struct.tokens* %0) {
%2 = tail call i32 @detect_out_direction(%struct.tokens* %0)
%3 = tail call i32 @detect_in_direction(%struct.tokens* %0)
%4 = icmp eq i32 %2, -1
br i1 %4, label %25, label %5
5: ; preds = %1
%6 = add nuw nsw i32 %2, 1
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8*, i64 %7
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %20
11: ; preds = %5
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ 0, %11 ], [ %18, %13 ]
%15 = trunc i64 %14 to i32
%16 = tail call i8* @tokens_get_token(%struct.tokens* %0, i32 %15)
%17 = getelementptr inbounds i8*, i8** %9, i64 %14
store i8* %16, i8** %17
%18 = add nuw nsw i64 %14, 1
%19 = icmp eq i64 %18, %12
br i1 %19, label %20, label %13
20: ; preds = %13, %5
%21 = sext i32 %2 to i64
%22 = getelementptr inbounds i8*, i8** %9, i64 %21
store i8* null, i8** %22
%23 = tail call i8* @tokens_get_token(%struct.tokens* %0, i32 %6)
%24 = call i32 @cmd_output_direction(i8** %9, i8* %23)
br label %63
25: ; preds = %1
%26 = icmp eq i32 %3, -1
br i1 %26, label %47, label %27
27: ; preds = %25
%28 = add nuw nsw i32 %3, 1
%29 = zext i32 %28 to i64
%30 = tail call i8* @llvm.stacksave()
%31 = alloca i8*, i64 %29
%32 = icmp sgt i32 %3, 0
br i1 %32, label %33, label %42
33: ; preds = %27
%34 = zext i32 %3 to i64
br label %35
35: ; preds = %33, %35
%36 = phi i64 [ 0, %33 ], [ %40, %35 ]
%37 = trunc i64 %36 to i32
%38 = tail call i8* @tokens_get_token(%struct.tokens* %0, i32 %37)
%39 = getelementptr inbounds i8*, i8** %31, i64 %36
store i8* %38, i8** %39
%40 = add nuw nsw i64 %36, 1
%41 = icmp eq i64 %40, %34
br i1 %41, label %42, label %35
42: ; preds = %35, %27
%43 = sext i32 %3 to i64
%44 = getelementptr inbounds i8*, i8** %31, i64 %43
store i8* null, i8** %44
%45 = tail call i8* @tokens_get_token(%struct.tokens* %0, i32 %28)
%46 = call i32 @cmd_input_direction(i8** %31, i8* %45)
br label %63
47: ; preds = %25
%48 = tail call i8* @tokens_get_token(%struct.tokens* %0, i32 0)
%49 = tail call i32 @cmd_exec_helper(i8* %48, %struct.tokens* %0)
%50 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%51 = tail call i8* @strtok(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%52 = icmp eq i8* %51, null
br i1 %52, label %65, label %53
53: ; preds = %47, %53
%54 = phi i8* [ %60, %53 ], [ %51, %47 ]
%55 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%56 = tail call i32 @strcat(i8* %55, i8* %54)
%57 = tail call i32 @strcat(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%58 = tail call i8* @tokens_get_token(%struct.tokens* %0, i32 0)
%59 = tail call i32 @strcat(i8* %55, i8* %58)
%60 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%61 = tail call i32 @cmd_exec_helper(i8* %55, %struct.tokens* %0)
%62 = icmp eq i8* %60, null
br i1 %62, label %65, label %53
63: ; preds = %20, %42
%64 = phi i8* [ %30, %42 ], [ %8, %20 ]
br label %65
65: ; preds = %53, %63, %47
ret i32 1
}