#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_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ executecommand(char *, char **, int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ findfilepath(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
void handlecommand(char *cmd, char **args, int argslen) {
  char filepath[100];
  if (strcmp(cmd, "cd") == 0) {
    if (chdir(args[0]) != 0) {
      printf("cd: %s: No such file or directory\n", cmd);
    }
  } else if (findfilepath(cmd, filepath)) {
    executecommand(cmd, args, argslen, filepath);
  } else if ((strcmp(cmd, "quit") == 0 || strcmp(cmd, "exit") == 0) &&
             argslen == 0) {
    printf("bye\n");
    exit(EXIT_SUCCESS);
  } else {
    printf("ERROR: command '%s' not found.\n", cmd);
  }
}


x86 assembly (clang, -O0):
<handlecommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11bd <handlecommand+0x5d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     11b8 <handlecommand+0x58>
mov    -0x8(%rbp),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1263 <handlecommand+0x103>
mov    -0x8(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1070 <findfilepath@plt>
cmp    $0x0,%rax
je     11ed <handlecommand+0x8d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x80(%rbp),%rcx
call   1050 <executecommand@plt>
jmp    125e <handlecommand+0xfe>
mov    -0x8(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1221 <handlecommand+0xc1>
mov    -0x8(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1247 <handlecommand+0xe7>
cmpl   $0x0,-0x14(%rbp)
jne    1247 <handlecommand+0xe7>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125e <handlecommand+0xfe>
jmp    1263 <handlecommand+0x103>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handlecommand>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe78(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11b8 <handlecommand+0x48>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <findfilepath@plt>
test   %rax,%rax
je     11df <handlecommand+0x6f>
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1060 <executecommand@plt>
jmp    123c <handlecommand+0xcc>
mov    0x0(%rbp),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     123c <handlecommand+0xcc>
lea    0xe36(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <printf@plt>
lea    0xe40(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1226 <handlecommand+0xb6>
lea    0xe31(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %r14d,%r14d
jne    122b <handlecommand+0xbb>
test   %rax,%rax
jne    122b <handlecommand+0xbb>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
test   %r14d,%r14d
je     120c <handlecommand+0x9c>
lea    0xdfe(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"cd\00"
@.str.1 = [35 x i8] c"cd: %s: No such file or directory\0A\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [5 x i8] c"exit\00"
@.str.4 = [5 x i8] c"bye\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [32 x i8] c"ERROR: command '%s' not found.\0A\00"
define void @handlecommand(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [100 x i8]
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %3
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i64 @chdir(i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %11
br label %52
21: ; preds = %3
%22 = load i8*, i8** %4
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%24 = call i64 @findfilepath(i8* %22, i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i8*, i8** %4
%28 = load i8**, i8*** %5
%29 = load i32, i32* %6
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%31 = call i32 @executecommand(i8* %27, i8** %28, i32 %29, i8* %30)
br label %51
32: ; preds = %21
%33 = load i8*, i8** %4
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i8*, i8** %4
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %36, %32
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32, i32* @EXIT_SUCCESS
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %40, %36
%48 = load i8*, i8** %4
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %47
br label %51
51: ; preds = %50, %26
br label %52
52: ; preds = %51, %20
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"cd\00"
@.str.1 = [35 x i8] c"cd: %s: No such file or directory\0A\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [5 x i8] c"exit\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [32 x i8] c"ERROR: command '%s' not found.\0A\00"
@str = [4 x i8] c"bye\00"
define void @handlecommand(i8* %0, i8** %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %3
%9 = load i8*, i8** %1
%10 = tail call i64 @chdir(i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %35, label %12
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %35
14: ; preds = %3
%15 = call i64 @findfilepath(i8* %0, i8* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @executecommand(i8* %0, i8** %1, i32 %2, i8* %5)
br label %35
19: ; preds = %14
%20 = call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
%25 = icmp eq i32 %2, 0
%26 = and i1 %25, %24
br i1 %26, label %29, label %33
27: ; preds = %19
%28 = icmp eq i32 %2, 0
br i1 %28, label %29, label %33
29: ; preds = %22, %27
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%31 = load i32, i32* @EXIT_SUCCESS
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %27, %22
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %35
35: ; preds = %17, %33, %8, %12
ret void
}