#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 st_mode;
};

/* Variables and functions */
int S_IXUSR;
char *getenv(char *);
int /*<<< orphan*/ log_printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, ...);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ system(char *);
int call_ext(char *cmd, char *d1, char *d2) {
  int pid, status;
  char *argv[4];
  char command[1024];
  char file[128];
  struct stat fs;
  snprintf(file, 128, "%s/.msn/%s", getenv("HOME"), cmd);
  if (stat(file, &fs) == 0 || fs.st_mode & S_IXUSR) {
    log_printf("eERROR: external command (%s) not found.", file);
    return -1;
  }
  snprintf(command, 1024, "%s %s \"%s\"", cmd, d1, d2);
  system(command);
  return 0;
}


x86 assembly (clang, -O0):
<call_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4e0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x4d0(%rbp),%rax
mov    %rax,-0x4e0(%rbp)
lea    0xe8f(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x4e0(%rbp),%rdi
mov    %rax,%rcx
mov    -0x10(%rbp),%r8
mov    $0x80,%esi
lea    0xe65(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x4d0(%rbp),%rdi
lea    -0x4d8(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11d7 <call_ext+0x87>
mov    -0x4d8(%rbp),%eax
mov    0x2e1c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f6 <call_ext+0xa6>
lea    -0x4d0(%rbp),%rsi
lea    0xe2b(%rip),%rdi
call   1060 <log_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <call_ext+0xdf>
lea    -0x450(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x400,%esi
lea    0xe24(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x450(%rbp),%rdi
call   1040 <system@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<call_ext>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x490,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe9c(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe85(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %rax,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     11b6 <call_ext+0x66>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%rsp)
je     11ce <call_ext+0x7e>
lea    0xe53(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1060 <log_printf@plt>
mov    $0xffffffff,%ebp
jmp    11ff <call_ext+0xaf>
lea    0xe64(%rip),%rdx
xor    %ebp,%ebp
lea    0x90(%rsp),%r12
mov    %r12,%rdi
mov    $0x400,%esi
mov    %rbx,%rcx
mov    %r15,%r8
mov    %r14,%r9
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1040 <system@plt>
mov    %ebp,%eax
add    $0x490,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"%s/.msn/%s\00"
@.str.1 = [5 x i8] c"HOME\00"
@S_IXUSR = global i32 0
@.str.2 = [41 x i8] c"eERROR: external command (%s) not found.\00"
@.str.3 = [11 x i8] c"%s %s \22%s\22\00"
define i32 @call_ext(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4 x i8*]
%11 = alloca [1024 x i8]
%12 = alloca [128 x i8]
%13 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%15 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = load i8*, i8** %5
%17 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %14, i32 128, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%19 = call i64 @stat(i8* %18, %struct.stat* %13)
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %3
%22 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @S_IXUSR
%25 = and i32 %23, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21, %3
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%29 = call i32 @log_printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 -1, i32* %4
br label %38
30: ; preds = %21
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%32 = load i8*, i8** %5
%33 = load i8*, i8** %6
%34 = load i8*, i8** %7
%35 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %31, i32 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %32, i8* %33, i8* %34)
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%37 = call i32 @system(i8* %36)
store i32 0, i32* %4
br label %38
38: ; preds = %30, %27
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"%s/.msn/%s\00"
@.str.1 = [5 x i8] c"HOME\00"
@S_IXUSR = global i32 0
@.str.2 = [41 x i8] c"eERROR: external command (%s) not found.\00"
@.str.3 = [11 x i8] c"%s %s \22%s\22\00"
define i32 @call_ext(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca [128 x i8]
%6 = alloca %struct.stat
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%9 = bitcast %struct.stat* %6 to i8*
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %0)
%12 = call i64 @stat(i8* %8, %struct.stat* %6)
%13 = icmp eq i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %3
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @S_IXUSR
%18 = and i32 %17, %16
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %14, %3
%21 = call i32 @log_printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %25
22: ; preds = %14
%23 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %1, i8* %2)
%24 = call i32 @system(i8* %7)
br label %25
25: ; preds = %22, %20
%26 = phi i32 [ -1, %20 ], [ 0, %22 ]
ret i32 %26
}