#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*/ ENOSYS;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *realpath(char const *, int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
size_t strlen(char *);
char const *strndup(char *, size_t const);
const char *getbeastexepath(int *argc, char ***argv) {
  const char *procexe = NULL;
  procexe = "/proc/self/exe";
  errno = ENOSYS;
  char *exe = procexe ? realpath(procexe, NULL) : NULL;
  if (exe) {
    if (0 == access(exe, X_OK)) {
      const size_t l = strlen(exe);
      if (l > 3 && strcmp(exe + l - 3, "-rt") == 0)
        return strndup(exe, l - 3);
    }
    free(exe);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<getbeastexepath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe81(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3d(%rip),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     11b6 <getbeastexepath+0x56>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <realpath@plt>
mov    %rax,-0x38(%rbp)
jmp    11c1 <getbeastexepath+0x61>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    11c1 <getbeastexepath+0x61>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1256 <getbeastexepath+0xf6>
mov    -0x28(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    124d <getbeastexepath+0xed>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x3,-0x30(%rbp)
jbe    1248 <getbeastexepath+0xe8>
mov    -0x28(%rbp),%rdi
add    -0x30(%rbp),%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xdf0(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1248 <getbeastexepath+0xe8>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
sub    $0x3,%rsi
call   1050 <strndup@plt>
mov    %rax,-0x8(%rbp)
jmp    125e <getbeastexepath+0xfe>
jmp    124d <getbeastexepath+0xed>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbeastexepath>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    0x2e5c(%rip),%rcx
mov    %eax,(%rcx)
lea    0xe83(%rip),%rdi
xor    %esi,%esi
call   1070 <realpath@plt>
test   %rax,%rax
je     11d4 <getbeastexepath+0x74>
mov    %rax,%rbx
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
jne    11cc <getbeastexepath+0x6c>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x4,%rax
jb     11cc <getbeastexepath+0x6c>
mov    %rax,%r14
lea    (%rbx,%rax,1),%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xe4d(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11de <getbeastexepath+0x7e>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
add    $0xfffffffffffffffd,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <strndup@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
@ENOSYS = global i32 0
@errno = global i32 0
@X_OK = global i32 0
@.str.1 = [4 x i8] c"-rt\00"
define i8* @getbeastexepath(i32* %0, i8*** %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8***
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i32* %0, i32** %4
store i8*** %1, i8**** %5
store i8* null, i8** %6
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %6
%9 = load i32, i32* @ENOSYS
store i32 %9, i32* @errno
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %6
%14 = call i8* @realpath(i8* %13, i32* null)
br label %16
15: ; preds = %2
br label %16
16: ; preds = %15, %12
%17 = phi i8* [ %14, %12 ], [ null, %15 ]
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %46
20: ; preds = %16
%21 = load i8*, i8** %7
%22 = load i32, i32* @X_OK
%23 = call i64 @access(i8* %21, i32 %22)
%24 = icmp eq i64 0, %23
br i1 %24, label %25, label %43
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = call i64 @strlen(i8* %26)
store i64 %27, i64* %8
%28 = load i64, i64* %8
%29 = icmp ugt i64 %28, 3
br i1 %29, label %30, label %42
30: ; preds = %25
%31 = load i8*, i8** %7
%32 = load i64, i64* %8
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = getelementptr inbounds i8, i8* %33, i64 -3
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i8*, i8** %7
%39 = load i64, i64* %8
%40 = sub i64 %39, 3
%41 = call i8* @strndup(i8* %38, i64 %40)
store i8* %41, i8** %3
br label %47
42: ; preds = %30, %25
br label %43
43: ; preds = %42, %20
%44 = load i8*, i8** %7
%45 = call i32 @free(i8* %44)
br label %46
46: ; preds = %43, %16
store i8* null, i8** %3
br label %47
47: ; preds = %46, %37
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@ENOSYS = global i32 0
@errno = global i32 0
@X_OK = global i32 0
@.str.1 = [4 x i8] c"-rt\00"
define noalias i8* @getbeastexepath(i32* nocapture readnone %0, i8*** nocapture readnone %1) {
%3 = load i32, i32* @ENOSYS
store i32 %3, i32* @errno
%4 = tail call i8* @realpath(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32* null)
%5 = icmp eq i8* %4, null
br i1 %5, label %23, label %6
6: ; preds = %2
%7 = load i32, i32* @X_OK
%8 = tail call i64 @access(i8* %4, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %6
%11 = tail call i64 @strlen(i8* %4)
%12 = icmp ugt i64 %11, 3
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %4, i64 %11
%15 = getelementptr inbounds i8, i8* %14, i64 -3
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = add i64 %11, -3
%20 = tail call i8* @strndup(i8* %4, i64 %19)
br label %23
21: ; preds = %10, %13, %6
%22 = tail call i32 @free(i8* %4)
br label %23
23: ; preds = %18, %2, %21
%24 = phi i8* [ %20, %18 ], [ null, %21 ], [ null, %2 ]
ret i8* %24
}