#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 stat {
  int dummy;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ execlp(char *, char *const, char *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ *strstr(char *, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
void execute(char *command, char *args) {
  if (command == NULL)
    return;
  char *path = (char *)malloc(sizeof(char) * 1024);
  memset(path, 0, sizeof(path));
  if (strstr(command, "/") != NULL)
    strcat(path, command);
  else {
    strcat(path, "/usr/bin/");
    strcat(path, command);
    strcat(path, "\0");
  }
  struct stat statRes;
  if (stat(path, &statRes) < 0) {
    printf("%s: command not found\n", path);
    return;
  }
  pid_t pid = fork();
  if (pid == 0) {
    if (args != NULL) {
      execlp(path, path, args, NULL);
    } else {
      char *const str = (char *const)malloc(sizeof(char) * 1024);
      execlp(path, str, NULL);
    }
    exit(127);
  } else {
    waitpid(pid, 0, 0);
    memset(path, 0, sizeof(path));
  }
}


x86 assembly (clang, -O0):
<execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c0 <execute+0x20>
jmp    12e8 <execute+0x148>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     120a <execute+0x6a>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strcat@plt>
jmp    1237 <execute+0x97>
mov    -0x18(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    1263 <execute+0xc3>
mov    -0x18(%rbp),%rsi
lea    0xdb1(%rip),%rdi
call   1030 <printf@plt>
jmp    12e8 <execute+0x148>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12cb <execute+0x12b>
cmpq   $0x0,-0x10(%rbp)
je     12a0 <execute+0x100>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   10a0 <execlp@plt>
jmp    12c1 <execute+0x121>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   10a0 <execlp@plt>
mov    $0x7f,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <waitpid@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1261 <execute+0xc1>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11ee <execute+0x4e>
mov    %r15,%rdi
mov    %rbx,%rsi
jmp    1212 <execute+0x72>
lea    0xe0b(%rip),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
lea    0xe12(%rip),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1060 <stat@plt>
test   %rax,%rax
js     1252 <execute+0xb2>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     126b <execute+0xcb>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
jmp    1261 <execute+0xc1>
lea    0xdb1(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
test   %r14,%r14
jne    1293 <execute+0xf3>
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   10b0 <execlp@plt>
mov    $0x7f,%edi
call   10a0 <exit@plt>
mov    %r15,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   10b0 <execlp@plt>
mov    $0x7f,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/usr/bin/\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [23 x i8] c"%s: command not found\0A\00"
define void @execute(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
br label %64
12: ; preds = %2
%13 = call i64 @malloc(i32 1024)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = call i32 @memset(i8* %15, i32 0, i32 8)
%17 = load i8*, i8** %3
%18 = call i32* @strstr(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = load i8*, i8** %5
%22 = load i8*, i8** %3
%23 = call i32 @strcat(i8* %21, i8* %22)
br label %32
24: ; preds = %12
%25 = load i8*, i8** %5
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = load i8*, i8** %5
%28 = load i8*, i8** %3
%29 = call i32 @strcat(i8* %27, i8* %28)
%30 = load i8*, i8** %5
%31 = call i32 @strcat(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %24, %20
%33 = load i8*, i8** %5
%34 = call i64 @stat(i8* %33, %struct.stat* %6)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %64
39: ; preds = %32
%40 = call i64 (...) @fork()
store i64 %40, i64* %7
%41 = load i64, i64* %7
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %59
43: ; preds = %39
%44 = load i8*, i8** %4
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i8*, i8** %5
%48 = load i8*, i8** %5
%49 = load i8*, i8** %4
%50 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %47, i8* %48, i8* %49, i8* null)
br label %57
51: ; preds = %43
%52 = call i64 @malloc(i32 1024)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %8
%54 = load i8*, i8** %5
%55 = load i8*, i8** %8
%56 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %54, i8* %55, i8* null)
br label %57
57: ; preds = %51, %46
%58 = call i32 @exit(i32 127)
unreachable
59: ; preds = %39
%60 = load i64, i64* %7
%61 = call i32 @waitpid(i64 %60, i32 0, i32 0)
%62 = load i8*, i8** %5
%63 = call i32 @memset(i8* %62, i32 0, i32 8)
br label %64
64: ; preds = %11, %36, %59
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [10 x i8] c"/usr/bin/\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [23 x i8] c"%s: command not found\0A\00"
define void @execute(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = icmp eq i8* %0, null
br i1 %4, label %40, label %5
5: ; preds = %2
%6 = tail call i64 @malloc(i32 1024)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @memset(i8* %7, i32 0, i32 8)
%9 = tail call i8* @strchr(i8* %0, i32 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %5
%12 = tail call i32 @strcat(i8* %7, i8* %0)
br label %17
13: ; preds = %5
%14 = tail call i32 @strcat(i8* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @strcat(i8* %7, i8* %0)
%16 = tail call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %13, %11
%18 = bitcast %struct.stat* %3 to i8*
%19 = call i64 @stat(i8* %7, %struct.stat* %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br label %39
23: ; preds = %17
%24 = call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %23
%27 = icmp eq i8* %1, null
br i1 %27, label %30, label %28
28: ; preds = %26
%29 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %7, i8* %7, i8* %1, i8* null)
br label %34
30: ; preds = %26
%31 = call i64 @malloc(i32 1024)
%32 = inttoptr i64 %31 to i8*
%33 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %7, i8* %32, i8* null)
br label %34
34: ; preds = %30, %28
%35 = call i32 @exit(i32 127)
unreachable
36: ; preds = %23
%37 = call i32 @waitpid(i64 %24, i32 0, i32 0)
%38 = call i32 @memset(i8* %7, i32 0, i32 8)
br label %39
39: ; preds = %36, %21
br label %40
40: ; preds = %2, %39
ret void
}