#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*/ change_dir(char const *);
int /*<<< orphan*/ check_env(char const **);
int fork_exec_wait(char const **, int, int, int);
int /*<<< orphan*/ put_fg(char const *);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ term_all();
int exec_cmd(char const **args, int no_args, int cmd, int no_cmds) {
  int background = 0;
  if (no_cmds == 1) {
    if (strcmp(args[0], "cd") == 0) {
      if (no_args == 1) {
        change_dir("~");
        return 0;
      }
      if (no_args == 2) {
        change_dir(args[1]);
        return 0;
      }
      return -1;
    }
    if (strcmp(args[0], "checkEnv") == 0) {
      if (no_args <= 2) {
        check_env(args);
        return 0;
      }
      return -1;
    }
    if (strcmp(args[0], "exit") == 0) {
      if (no_args == 1) {
        term_all();
        return 0;
      }
      return -1;
    }
    if (strcmp(args[0], "fg") == 0) {
      if (no_args == 2) {
        put_fg(args[1]);
        return 0;
      }
      return -1;
    }
    if (strcmp(args[no_args - 1], "&") == 0) {
      if (no_args >= 2) {
        args[no_args - 1] = NULL;
        background = 1;
      } else {
        return -1;
      }
    }
  }
  return fork_exec_wait(args, cmd, no_cmds, background);
}


x86 assembly (clang, -O0):
<exec_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    1336 <exec_cmd+0x1d6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe6c(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11f4 <exec_cmd+0x94>
cmpl   $0x1,-0x14(%rbp)
jne    11c5 <exec_cmd+0x65>
lea    0xe4f(%rip),%rdi
call   1070 <change_dir@plt>
movl   $0x0,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
cmpl   $0x2,-0x14(%rbp)
jne    11e8 <exec_cmd+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <change_dir@plt>
movl   $0x0,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe03(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    123c <exec_cmd+0xdc>
cmpl   $0x2,-0x14(%rbp)
jg     1230 <exec_cmd+0xd0>
mov    -0x10(%rbp),%rdi
call   1030 <check_env@plt>
movl   $0x0,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc4(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1282 <exec_cmd+0x122>
cmpl   $0x1,-0x14(%rbp)
jne    1276 <exec_cmd+0x116>
mov    $0x0,%al
call   1040 <term_all@plt>
movl   $0x0,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xd83(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12ce <exec_cmd+0x16e>
cmpl   $0x2,-0x14(%rbp)
jne    12c2 <exec_cmd+0x162>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <put_fg@plt>
movl   $0x0,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd30(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1331 <exec_cmd+0x1d1>
cmpl   $0x2,-0x14(%rbp)
jl     1320 <exec_cmd+0x1c0>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
movl   $0x1,-0x20(%rbp)
jmp    132c <exec_cmd+0x1cc>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <exec_cmd+0x1eb>
jmp    1331 <exec_cmd+0x1d1>
jmp    1336 <exec_cmd+0x1d6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1080 <fork_exec_wait@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rdi,%rbx
xor    %r12d,%r12d
cmp    $0x1,%ecx
jne    121d <exec_cmd+0xbd>
mov    %esi,%r15d
mov    (%rbx),%rdi
lea    0xe74(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     123b <exec_cmd+0xdb>
mov    (%rbx),%rdi
lea    0xe61(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     125b <exec_cmd+0xfb>
mov    (%rbx),%rdi
lea    0xe52(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1271 <exec_cmd+0x111>
mov    (%rbx),%rdi
lea    0xe3f(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1294 <exec_cmd+0x134>
movslq %r15d,%r13
mov    -0x8(%rbx,%r13,8),%rdi
lea    0xe25(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    121d <exec_cmd+0xbd>
mov    $0xffffffff,%r12d
cmp    $0x2,%r15d
jl     12ac <exec_cmd+0x14c>
add    $0xffffffffffffffff,%r13
movq   $0x0,(%rbx,%r13,8)
mov    $0x1,%r12d
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %r12d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <fork_exec_wait@plt>
cmp    $0x2,%r15d
je     1289 <exec_cmd+0x129>
mov    $0xffffffff,%r12d
cmp    $0x1,%r15d
jne    12ac <exec_cmd+0x14c>
lea    0xdaf(%rip),%rdi
call   1070 <change_dir@plt>
jmp    12a9 <exec_cmd+0x149>
mov    $0xffffffff,%r12d
cmp    $0x2,%r15d
jg     12ac <exec_cmd+0x14c>
mov    %rbx,%rdi
call   1030 <check_env@plt>
jmp    12a9 <exec_cmd+0x149>
mov    $0xffffffff,%r12d
cmp    $0x1,%r15d
jne    12ac <exec_cmd+0x14c>
xor    %r12d,%r12d
xor    %eax,%eax
call   1040 <term_all@plt>
jmp    12ac <exec_cmd+0x14c>
mov    0x8(%rbx),%rdi
call   1070 <change_dir@plt>
jmp    12a9 <exec_cmd+0x149>
mov    $0xffffffff,%r12d
cmp    $0x2,%r15d
jne    12ac <exec_cmd+0x14c>
mov    0x8(%rbx),%rdi
call   1060 <put_fg@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"cd\00"
@.str.1 = [2 x i8] c"~\00"
@.str.2 = [9 x i8] c"checkEnv\00"
@.str.3 = [5 x i8] c"exit\00"
@.str.4 = [3 x i8] c"fg\00"
@.str.5 = [2 x i8] c"&\00"
define i32 @exec_cmd(i8** %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8** %0, i8*** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
%11 = load i32, i32* %9
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %94
13: ; preds = %4
%14 = load i8**, i8*** %6
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %13
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @change_dir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %100
24: ; preds = %19
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 2
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i8**, i8*** %6
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @change_dir(i8* %30)
store i32 0, i32* %5
br label %100
32: ; preds = %24
store i32 -1, i32* %5
br label %100
33: ; preds = %13
%34 = load i8**, i8*** %6
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %33
%40 = load i32, i32* %7
%41 = icmp sle i32 %40, 2
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i8**, i8*** %6
%44 = call i32 @check_env(i8** %43)
store i32 0, i32* %5
br label %100
45: ; preds = %39
store i32 -1, i32* %5
br label %100
46: ; preds = %33
%47 = load i8**, i8*** %6
%48 = getelementptr inbounds i8*, i8** %47, i64 0
%49 = load i8*, i8** %48
%50 = call i64 @strcmp(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %46
%53 = load i32, i32* %7
%54 = icmp eq i32 %53, 1
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 (...) @term_all()
store i32 0, i32* %5
br label %100
57: ; preds = %52
store i32 -1, i32* %5
br label %100
58: ; preds = %46
%59 = load i8**, i8*** %6
%60 = getelementptr inbounds i8*, i8** %59, i64 0
%61 = load i8*, i8** %60
%62 = call i64 @strcmp(i8* %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %73
64: ; preds = %58
%65 = load i32, i32* %7
%66 = icmp eq i32 %65, 2
br i1 %66, label %67, label %72
67: ; preds = %64
%68 = load i8**, i8*** %6
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = call i32 @put_fg(i8* %70)
store i32 0, i32* %5
br label %100
72: ; preds = %64
store i32 -1, i32* %5
br label %100
73: ; preds = %58
%74 = load i8**, i8*** %6
%75 = load i32, i32* %7
%76 = sub nsw i32 %75, 1
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8*, i8** %74, i64 %77
%79 = load i8*, i8** %78
%80 = call i64 @strcmp(i8* %79, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%81 = icmp eq i64 %80, 0
br i1 %81, label %82, label %93
82: ; preds = %73
%83 = load i32, i32* %7
%84 = icmp sge i32 %83, 2
br i1 %84, label %85, label %91
85: ; preds = %82
%86 = load i8**, i8*** %6
%87 = load i32, i32* %7
%88 = sub nsw i32 %87, 1
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i8*, i8** %86, i64 %89
store i8* null, i8** %90
store i32 1, i32* %10
br label %92
91: ; preds = %82
store i32 -1, i32* %5
br label %100
92: ; preds = %85
br label %93
93: ; preds = %92, %73
br label %94
94: ; preds = %93, %4
%95 = load i8**, i8*** %6
%96 = load i32, i32* %8
%97 = load i32, i32* %9
%98 = load i32, i32* %10
%99 = call i32 @fork_exec_wait(i8** %95, i32 %96, i32 %97, i32 %98)
store i32 %99, i32* %5
br label %100
100: ; preds = %94, %91, %72, %67, %57, %55, %45, %42, %32, %27, %22
%101 = load i32, i32* %5
ret i32 %101
}

LLVM IR (-O2):
@.str = [3 x i8] c"cd\00"
@.str.1 = [2 x i8] c"~\00"
@.str.2 = [9 x i8] c"checkEnv\00"
@.str.3 = [5 x i8] c"exit\00"
@.str.4 = [3 x i8] c"fg\00"
@.str.5 = [2 x i8] c"&\00"
define i32 @exec_cmd(i8** %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 1
br i1 %5, label %6, label %53
6: ; preds = %4
%7 = load i8*, i8** %0
%8 = tail call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %6
switch i32 %1, label %56 [
i32 1, label %11
i32 2, label %13
]
11: ; preds = %10
%12 = tail call i32 @change_dir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %56
13: ; preds = %10
%14 = getelementptr inbounds i8*, i8** %0, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @change_dir(i8* %15)
br label %56
17: ; preds = %6
%18 = load i8*, i8** %0
%19 = tail call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = icmp slt i32 %1, 3
br i1 %22, label %23, label %56
23: ; preds = %21
%24 = tail call i32 @check_env(i8** %0)
br label %56
25: ; preds = %17
%26 = load i8*, i8** %0
%27 = tail call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = icmp eq i32 %1, 1
br i1 %30, label %31, label %56
31: ; preds = %29
%32 = tail call i32 (...) @term_all()
br label %56
33: ; preds = %25
%34 = load i8*, i8** %0
%35 = tail call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = icmp eq i32 %1, 2
br i1 %38, label %39, label %56
39: ; preds = %37
%40 = getelementptr inbounds i8*, i8** %0, i64 1
%41 = load i8*, i8** %40
%42 = tail call i32 @put_fg(i8* %41)
br label %56
43: ; preds = %33
%44 = add nsw i32 %1, -1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8*, i8** %0, i64 %45
%47 = load i8*, i8** %46
%48 = tail call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %43
%51 = icmp sgt i32 %1, 1
br i1 %51, label %52, label %56
52: ; preds = %50
store i8* null, i8** %46
br label %53
53: ; preds = %43, %52, %4
%54 = phi i32 [ 1, %52 ], [ 0, %43 ], [ 0, %4 ]
%55 = tail call i32 @fork_exec_wait(i8** %0, i32 %2, i32 %3, i32 %54)
br label %56
56: ; preds = %50, %37, %29, %21, %10, %53, %39, %31, %23, %13, %11
%57 = phi i32 [ 0, %11 ], [ 0, %13 ], [ 0, %23 ], [ 0, %31 ], [ 0, %39 ], [ %55, %53 ], [ -1, %10 ], [ -1, %21 ], [ -1, %29 ], [ -1, %37 ], [ -1, %50 ]
ret i32 %57
}