#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ Cbuf_InsertText(char *);
int Cmd_Argc();
char *Cmd_Argv(int);
int /*<<< orphan*/ FS_FileExtension(char *);
int /*<<< orphan*/ FS_ReadFile(char *, char *, char *, int *);
int /*<<< orphan*/ FS_SafePath(char *);
int /*<<< orphan*/ Sys_Printf(char *, ...);
scalar_t__ strcasecmp(char *, int /*<<< orphan*/);
void Cmd_Exec_f(void) {
  char buf[1024 * 100], *name;
  int size;
  if (Cmd_Argc() != 2) {
    Sys_Printf("exec <filename> : execute a script file\n");
    return;
  }
  name = Cmd_Argv(1);
  if (!FS_SafePath(name)) {
    Sys_Printf("exec: absolute paths are prohibited\n");
    return;
  }
  if (strcasecmp(".cfg", FS_FileExtension(name))) {
    Sys_Printf("exec: cfg extension required\n");
    return;
  }
  size = sizeof(buf);
  if (!FS_ReadFile("qtv", name, buf, &size)) {
    size = sizeof(buf);
    if (!FS_ReadFile("qw", name, buf, &size)) {
      Sys_Printf("exec: couldn't exec %s\n", name);
      return;
    }
  }
  Sys_Printf("execing %s\n", name);
  Cbuf_InsertText(buf);
}


x86 assembly (clang, -O0):
<Cmd_Exec_f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x19010,%rsp
mov    $0x0,%al
call   1040 <Cmd_Argc@plt>
cmp    $0x2,%eax
je     11ae <Cmd_Exec_f+0x2e>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1070 <Sys_Printf@plt>
jmp    12c6 <Cmd_Exec_f+0x146>
mov    $0x1,%edi
call   1090 <Cmd_Argv@plt>
mov    %rax,-0x19008(%rbp)
mov    -0x19008(%rbp),%rdi
call   10a0 <FS_SafePath@plt>
cmp    $0x0,%eax
jne    11e7 <Cmd_Exec_f+0x67>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1070 <Sys_Printf@plt>
jmp    12c6 <Cmd_Exec_f+0x146>
mov    -0x19008(%rbp),%rdi
call   1050 <FS_FileExtension@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
je     121e <Cmd_Exec_f+0x9e>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1070 <Sys_Printf@plt>
jmp    12c6 <Cmd_Exec_f+0x146>
movl   $0x19000,-0x1900c(%rbp)
mov    -0x19008(%rbp),%rsi
lea    -0x19000(%rbp),%rdx
lea    0xe34(%rip),%rdi
lea    -0x1900c(%rbp),%rcx
call   1060 <FS_ReadFile@plt>
cmp    $0x0,%eax
jne    12a5 <Cmd_Exec_f+0x125>
movl   $0x19000,-0x1900c(%rbp)
mov    -0x19008(%rbp),%rsi
lea    -0x19000(%rbp),%rdx
lea    0xe04(%rip),%rdi
lea    -0x1900c(%rbp),%rcx
call   1060 <FS_ReadFile@plt>
cmp    $0x0,%eax
jne    12a0 <Cmd_Exec_f+0x120>
mov    -0x19008(%rbp),%rsi
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1070 <Sys_Printf@plt>
jmp    12c6 <Cmd_Exec_f+0x146>
jmp    12a5 <Cmd_Exec_f+0x125>
mov    -0x19008(%rbp),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1070 <Sys_Printf@plt>
lea    -0x19000(%rbp),%rdi
call   1080 <Cbuf_InsertText@plt>
add    $0x19010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Cmd_Exec_f>:
push   %rbx
sub    $0x19010,%rsp
xor    %eax,%eax
call   1040 <Cmd_Argc@plt>
cmp    $0x2,%eax
jne    11d1 <Cmd_Exec_f+0x51>
mov    $0x1,%edi
call   1090 <Cmd_Argv@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <FS_SafePath@plt>
test   %eax,%eax
je     11da <Cmd_Exec_f+0x5a>
mov    %rbx,%rdi
call   1050 <FS_FileExtension@plt>
lea    0xe92(%rip),%rdi
mov    %eax,%esi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     11f0 <Cmd_Exec_f+0x70>
lea    0xe84(%rip),%rdi
jmp    11e1 <Cmd_Exec_f+0x61>
lea    0xe28(%rip),%rdi
jmp    11e1 <Cmd_Exec_f+0x61>
lea    0xe48(%rip),%rdi
xor    %eax,%eax
add    $0x19010,%rsp
pop    %rbx
jmp    1070 <Sys_Printf@plt>
movl   $0x19000,0xc(%rsp)
lea    0xe72(%rip),%rdi
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %rbx,%rsi
call   1060 <FS_ReadFile@plt>
test   %eax,%eax
jne    123a <Cmd_Exec_f+0xba>
movl   $0x19000,0xc(%rsp)
lea    0xe51(%rip),%rdi
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %rbx,%rsi
call   1060 <FS_ReadFile@plt>
test   %eax,%eax
je     125e <Cmd_Exec_f+0xde>
lea    0xe4f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <Sys_Printf@plt>
lea    0x10(%rsp),%rdi
call   1080 <Cbuf_InsertText@plt>
add    $0x19010,%rsp
pop    %rbx
ret
lea    0xe13(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <Sys_Printf@plt>
jmp    1255 <Cmd_Exec_f+0xd5>

LLVM IR (-O0):
@.str = [41 x i8] c"exec <filename> : execute a script file\0A\00"
@.str.1 = [37 x i8] c"exec: absolute paths are prohibited\0A\00"
@.str.2 = [5 x i8] c".cfg\00"
@.str.3 = [30 x i8] c"exec: cfg extension required\0A\00"
@.str.4 = [4 x i8] c"qtv\00"
@.str.5 = [3 x i8] c"qw\00"
@.str.6 = [24 x i8] c"exec: couldn't exec %s\0A\00"
@.str.7 = [12 x i8] c"execing %s\0A\00"
define void @Cmd_Exec_f() {
%1 = alloca [102400 x i8]
%2 = alloca i8*
%3 = alloca i32
%4 = call i32 (...) @Cmd_Argc()
%5 = icmp ne i32 %4, 2
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %41
8: ; preds = %0
%9 = call i8* @Cmd_Argv(i32 1)
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = call i32 @FS_SafePath(i8* %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %41
15: ; preds = %8
%16 = load i8*, i8** %2
%17 = call i32 @FS_FileExtension(i8* %16)
%18 = call i64 @strcasecmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %41
22: ; preds = %15
store i32 102400, i32* %3
%23 = load i8*, i8** %2
%24 = getelementptr inbounds [102400 x i8], [102400 x i8]* %1, i64 0, i64 0
%25 = call i32 @FS_ReadFile(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %23, i8* %24, i32* %3)
%26 = icmp ne i32 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %22
store i32 102400, i32* %3
%28 = load i8*, i8** %2
%29 = getelementptr inbounds [102400 x i8], [102400 x i8]* %1, i64 0, i64 0
%30 = call i32 @FS_ReadFile(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %28, i8* %29, i32* %3)
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %27
%33 = load i8*, i8** %2
%34 = call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %33)
br label %41
35: ; preds = %27
br label %36
36: ; preds = %35, %22
%37 = load i8*, i8** %2
%38 = call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds [102400 x i8], [102400 x i8]* %1, i64 0, i64 0
%40 = call i32 @Cbuf_InsertText(i8* %39)
br label %41
41: ; preds = %36, %32, %20, %13, %6
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"exec <filename> : execute a script file\0A\00"
@.str.1 = [37 x i8] c"exec: absolute paths are prohibited\0A\00"
@.str.2 = [5 x i8] c".cfg\00"
@.str.3 = [30 x i8] c"exec: cfg extension required\0A\00"
@.str.4 = [4 x i8] c"qtv\00"
@.str.5 = [3 x i8] c"qw\00"
@.str.6 = [24 x i8] c"exec: couldn't exec %s\0A\00"
@.str.7 = [12 x i8] c"execing %s\0A\00"
define void @Cmd_Exec_f() {
%1 = alloca [102400 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [102400 x i8], [102400 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @Cmd_Argc()
%6 = icmp eq i32 %5, 2
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %32
9: ; preds = %0
%10 = tail call i8* @Cmd_Argv(i32 1)
%11 = tail call i32 @FS_SafePath(i8* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %32
15: ; preds = %9
%16 = tail call i32 @FS_FileExtension(i8* %10)
%17 = tail call i64 @strcasecmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %32
21: ; preds = %15
store i32 102400, i32* %2
%22 = call i32 @FS_ReadFile(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %10, i8* %3, i32* %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
store i32 102400, i32* %2
%25 = call i32 @FS_ReadFile(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %10, i8* %3, i32* %2)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %10)
br label %32
29: ; preds = %24, %21
%30 = call i32 (i8*, ...) @Sys_Printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i8* %10)
%31 = call i32 @Cbuf_InsertText(i8* %3)
br label %32
32: ; preds = %29, %27, %19, %13, %7
ret void
}