#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*/ path;

/* Variables and functions */
int RTLD_LOCAL;
int RTLD_NOW;
scalar_t__ dlclose(void *);
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int snprintf(char *, int, char *, char *, char const *);
char *strrchr(char const *, char);
int /*<<< orphan*/ warnx(char *, char const *, ...);
int run_command(const char *cmd, int argc, char *argv[]) {
  char *p, path[128];
  void *h, *f;
  int (*func)(int, char *[]), r;
  p = strrchr(cmd, '/');
  if (p != NULL)
    cmd = p + 1;
  if (snprintf(path, sizeof(path), "%s/%s.so", "/fat-dragon/lib/resh", cmd) >
      sizeof(path)) {
    warnx("%s: command too long.", cmd);
    return (-1);
  }
  h = dlopen(path, RTLD_NOW | RTLD_LOCAL);
  if (h == NULL) {
    warnx("%s: command not found: %s", cmd, dlerror());
    return (-1);
  }
  f = dlsym(h, "main");
  if (f == NULL) {
    perror(dlerror());
    dlclose(h);
    return (-1);
  }
  func = (int (*)(int, char *[]))f;
  r = func(argc, argv);
  if (dlclose(h) != 0)
    perror(dlerror());
  return (r);
}


x86 assembly (clang, -O0):
<run_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11bf <run_command+0x3f>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%r8
mov    $0x80,%esi
lea    0xe2a(%rip),%rdx
lea    0xe2c(%rip),%rcx
call   1050 <snprintf@plt>
cltq
cmp    $0x80,%rax
jbe    120e <run_command+0x8e>
mov    -0x10(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <run_command+0x195>
lea    -0xb0(%rbp),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
call   1070 <dlopen@plt>
mov    %rax,-0xb8(%rbp)
cmpq   $0x0,-0xb8(%rbp)
jne    1276 <run_command+0xf6>
mov    -0x10(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0xd8(%rbp),%rsi
mov    %eax,%edx
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <run_command+0x195>
mov    -0xb8(%rbp),%rdi
lea    0xdca(%rip),%rsi
call   1090 <dlsym@plt>
mov    %rax,-0xc0(%rbp)
cmpq   $0x0,-0xc0(%rbp)
jne    12c4 <run_command+0x144>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1080 <perror@plt>
mov    -0xb8(%rbp),%rdi
call   10a0 <dlclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <run_command+0x195>
mov    -0xc0(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0xc8(%rbp),%rax
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   *%rax
mov    %eax,-0xcc(%rbp)
mov    -0xb8(%rbp),%rdi
call   10a0 <dlclose@plt>
cmp    $0x0,%rax
je     130c <run_command+0x18c>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1080 <perror@plt>
mov    -0xcc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_command>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbp
mov    $0x2f,%esi
call   1060 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %rbp,%rbx
lea    0xe4e(%rip),%rdx
lea    0xe50(%rip),%rcx
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rbx,%r8
call   1050 <snprintf@plt>
cmp    $0x81,%eax
jb     11e6 <run_command+0x66>
lea    0xe47(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <warnx@plt>
jmp    1275 <run_command+0xf5>
mov    0x2dfb(%rip),%rax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rsp,%rdi
call   1070 <dlopen@plt>
test   %rax,%rax
je     1243 <run_command+0xc3>
mov    %rax,%rbp
lea    0xe3f(%rip),%rsi
mov    %rax,%rdi
call   1090 <dlsym@plt>
test   %rax,%rax
je     125f <run_command+0xdf>
mov    %r15d,%edi
mov    %r14,%rsi
call   *%rax
mov    %eax,%ebx
mov    %rbp,%rdi
call   10a0 <dlclose@plt>
test   %rax,%rax
je     127a <run_command+0xfa>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1080 <perror@plt>
jmp    127a <run_command+0xfa>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xde3(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <warnx@plt>
jmp    1275 <run_command+0xf5>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1080 <perror@plt>
mov    %rbp,%rdi
call   10a0 <dlclose@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%s/%s.so\00"
@.str.1 = [21 x i8] c"/fat-dragon/lib/resh\00"
@.str.2 = [22 x i8] c"%s: command too long.\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.3 = [26 x i8] c"%s: command not found: %s\00"
@.str.4 = [5 x i8] c"main\00"
define i32 @run_command(i8* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca [128 x i8]
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32 (i32, i8**)*
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%14 = load i8*, i8** %5
%15 = call i8* @strrchr(i8* %14, i8 signext 47)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i8*, i8** %8
%20 = getelementptr inbounds i8, i8* %19, i64 1
store i8* %20, i8** %5
br label %21
21: ; preds = %18, %3
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%23 = load i8*, i8** %5
%24 = call i32 @snprintf(i8* %22, i32 128, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = sext i32 %24 to i64
%26 = icmp ugt i64 %25, 128
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i8*, i8** %5
%29 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 -1, i32* %4
br label %67
30: ; preds = %21
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%32 = load i32, i32* @RTLD_NOW
%33 = load i32, i32* @RTLD_LOCAL
%34 = or i32 %32, %33
%35 = call i8* @dlopen(i8* %31, i32 %34)
store i8* %35, i8** %10
%36 = load i8*, i8** %10
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %42
38: ; preds = %30
%39 = load i8*, i8** %5
%40 = call i32 (...) @dlerror()
%41 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %39, i32 %40)
store i32 -1, i32* %4
br label %67
42: ; preds = %30
%43 = load i8*, i8** %10
%44 = call i8* @dlsym(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i8* %44, i8** %11
%45 = load i8*, i8** %11
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = call i32 (...) @dlerror()
%49 = call i32 @perror(i32 %48)
%50 = load i8*, i8** %10
%51 = call i64 @dlclose(i8* %50)
store i32 -1, i32* %4
br label %67
52: ; preds = %42
%53 = load i8*, i8** %11
%54 = bitcast i8* %53 to i32 (i32, i8**)*
store i32 (i32, i8**)* %54, i32 (i32, i8**)** %12
%55 = load i32 (i32, i8**)*, i32 (i32, i8**)** %12
%56 = load i32, i32* %6
%57 = load i8**, i8*** %7
%58 = call i32 %55(i32 %56, i8** %57)
store i32 %58, i32* %13
%59 = load i8*, i8** %10
%60 = call i64 @dlclose(i8* %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %52
%63 = call i32 (...) @dlerror()
%64 = call i32 @perror(i32 %63)
br label %65
65: ; preds = %62, %52
%66 = load i32, i32* %13
store i32 %66, i32* %4
br label %67
67: ; preds = %65, %47, %38, %27
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s/%s.so\00"
@.str.1 = [21 x i8] c"/fat-dragon/lib/resh\00"
@.str.2 = [22 x i8] c"%s: command too long.\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.3 = [26 x i8] c"%s: command not found: %s\00"
@.str.4 = [5 x i8] c"main\00"
define i32 @run_command(i8* %0, i32 %1, i8** %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%7 = icmp eq i8* %6, null
%8 = getelementptr inbounds i8, i8* %6, i64 1
%9 = select i1 %7, i8* %0, i8* %8
%10 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = icmp ugt i32 %10, 128
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %38
14: ; preds = %3
%15 = load i32, i32* @RTLD_NOW
%16 = load i32, i32* @RTLD_LOCAL
%17 = or i32 %16, %15
%18 = call i8* @dlopen(i8* %5, i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 (...) @dlerror()
%22 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %9, i32 %21)
br label %38
23: ; preds = %14
%24 = call i8* @dlsym(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = call i32 (...) @dlerror()
%28 = call i32 @perror(i32 %27)
%29 = call i64 @dlclose(i8* %18)
br label %38
30: ; preds = %23
%31 = bitcast i8* %24 to i32 (i32, i8**)*
%32 = call i32 %31(i32 %1, i8** %2)
%33 = call i64 @dlclose(i8* %18)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = call i32 (...) @dlerror()
%37 = call i32 @perror(i32 %36)
br label %38
38: ; preds = %30, %35, %26, %20, %12
%39 = phi i32 [ -1, %12 ], [ -1, %20 ], [ -1, %26 ], [ %32, %35 ], [ %32, %30 ]
ret i32 %39
}