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

/* Variables and functions */
scalar_t__ errno;
char *malloc(int const);
int /*<<< orphan*/ memset(char *, char, int const);
int readlink(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
void *strlen(char const *);
char *jep_get_import(const char *path) {
  errno = 0;
  char *import_path = NULL;
  if (path == NULL) {
    return import_path;
  }
  size_t p_len = strlen(path);
  if (p_len < 1) {
    return import_path;
  }
  char app_path[1024];
  ssize_t len;
  len = readlink("/proc/self/exe", app_path, sizeof(app_path) - 1);
  if (len == -1) {
    return import_path;
  }
  app_path[len] = '\0';
  int i = strlen(app_path);
  for (; app_path[i] != '/' && i > 0; i--) {
    app_path[i] = '\0';
  }
  size_t a_len = strlen(app_path);
  const int i_len = a_len + p_len + 10;
  import_path = malloc(i_len);
  memset(import_path, '\0', i_len);
  strcpy(import_path, app_path);
  strcat(import_path, "imports/");
  strcat(import_path, path);
  return import_path;
}


x86 assembly (clang, -O0):
<jep_get_import>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6a(%rip),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119d <jep_get_import+0x3d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12fe <jep_get_import+0x19e>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jae    11c2 <jep_get_import+0x62>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12fe <jep_get_import+0x19e>
lea    -0x420(%rbp),%rsi
lea    0xe30(%rip),%rdi
mov    $0x3ff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    11fa <jep_get_import+0x9a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12fe <jep_get_import+0x19e>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x428(%rbp)
movslq -0x428(%rbp),%rax
movsbl -0x420(%rbp,%rax,1),%ecx
ff
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x435(%rbp)
je     124b <jep_get_import+0xeb>
cmpl   $0x0,-0x428(%rbp)
setg   %al
mov    %al,-0x435(%rbp)
mov    -0x435(%rbp),%al
test   $0x1,%al
jne    125e <jep_get_import+0xfe>
jmp    1281 <jep_get_import+0x121>
movslq -0x428(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x428(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x428(%rbp)
jmp    121b <jep_get_import+0xbb>
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rax
add    -0x20(%rbp),%rax
add    $0xa,%rax
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x434(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xd2b(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jep_get_import>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    0x2e6a(%rip),%rax
movq   $0x0,(%rax)
test   %rdi,%rdi
je     1247 <jep_get_import+0xe7>
mov    %rdi,%r15
call   1050 <strlen@plt>
test   %rax,%rax
je     1247 <jep_get_import+0xe7>
mov    %rax,%r14
lea    0xe5f(%rip),%rdi
mov    %rsp,%rsi
mov    $0x3ff,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     1247 <jep_get_import+0xe7>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
cmpb   $0x2f,(%rsp,%rcx,1)
je     11f8 <jep_get_import+0x98>
test   %eax,%eax
jle    11f8 <jep_get_import+0x98>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x0,(%rsp,%rcx,1)
cmpb   $0x2f,-0x1(%rsp,%rcx,1)
je     11f8 <jep_get_import+0x98>
lea    -0x1(%rcx),%rax
cmp    $0x1,%rcx
mov    %rax,%rcx
jg     11e0 <jep_get_import+0x80>
mov    %rsp,%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r14,1),%ebp
add    $0xa,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
lea    0xddd(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
jmp    1249 <jep_get_import+0xe9>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [9 x i8] c"imports/\00"
define i8* @jep_get_import(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %3
store i64 0, i64* @errno
store i8* null, i8** %4
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i8*, i8** %4
store i8* %14, i8** %2
br label %79
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i8* @strlen(i8* %16)
%18 = ptrtoint i8* %17 to i64
store i64 %18, i64* %5
%19 = load i64, i64* %5
%20 = icmp ult i64 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = load i8*, i8** %4
store i8* %22, i8** %2
br label %79
23: ; preds = %15
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %24, i32 1023)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i8*, i8** %4
store i8* %29, i8** %2
br label %79
30: ; preds = %23
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%35 = call i8* @strlen(i8* %34)
%36 = ptrtoint i8* %35 to i32
store i32 %36, i32* %8
br label %37
37: ; preds = %53, %30
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 47
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = load i32, i32* %8
%46 = icmp sgt i32 %45, 0
br label %47
47: ; preds = %44, %37
%48 = phi i1 [ false, %37 ], [ %46, %44 ]
br i1 %48, label %49, label %56
49: ; preds = %47
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %51
store i8 0, i8* %52
br label %53
53: ; preds = %49
%54 = load i32, i32* %8
%55 = add nsw i32 %54, -1
store i32 %55, i32* %8
br label %37
56: ; preds = %47
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%58 = call i8* @strlen(i8* %57)
%59 = ptrtoint i8* %58 to i64
store i64 %59, i64* %9
%60 = load i64, i64* %9
%61 = load i64, i64* %5
%62 = add i64 %60, %61
%63 = add i64 %62, 10
%64 = trunc i64 %63 to i32
store i32 %64, i32* %10
%65 = load i32, i32* %10
%66 = call i8* @malloc(i32 %65)
store i8* %66, i8** %4
%67 = load i8*, i8** %4
%68 = load i32, i32* %10
%69 = call i32 @memset(i8* %67, i8 signext 0, i32 %68)
%70 = load i8*, i8** %4
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%72 = call i32 @strcpy(i8* %70, i8* %71)
%73 = load i8*, i8** %4
%74 = call i32 @strcat(i8* %73, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%75 = load i8*, i8** %4
%76 = load i8*, i8** %3
%77 = call i32 @strcat(i8* %75, i8* %76)
%78 = load i8*, i8** %4
store i8* %78, i8** %2
br label %79
79: ; preds = %56, %28, %21, %13
%80 = load i8*, i8** %2
ret i8* %80
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [9 x i8] c"imports/\00"
define i8* @jep_get_import(i8* %0) {
%2 = alloca [1024 x i8]
store i64 0, i64* @errno
%3 = icmp eq i8* %0, null
br i1 %3, label %47, label %4
4: ; preds = %1
%5 = tail call i8* @strlen(i8* %0)
%6 = ptrtoint i8* %5 to i64
%7 = icmp eq i8* %5, null
br i1 %7, label %47, label %8
8: ; preds = %4
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%10 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9, i32 1023)
%11 = icmp eq i32 %10, -1
br i1 %11, label %45, label %12
12: ; preds = %8
%13 = sext i32 %10 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i8* @strlen(i8* %9)
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = shl i64 %16, 32
%19 = ashr exact i64 %18, 32
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 47
%23 = icmp sgt i32 %17, 0
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %25, label %34
25: ; preds = %12, %25
%26 = phi i64 [ %28, %25 ], [ %19, %12 ]
%27 = phi i8* [ %29, %25 ], [ %20, %12 ]
store i8 0, i8* %27
%28 = add nsw i64 %26, -1
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 47
%32 = icmp sgt i64 %26, 1
%33 = select i1 %31, i1 %32, i1 false
br i1 %33, label %25, label %34
34: ; preds = %25, %12
%35 = call i8* @strlen(i8* %9)
%36 = ptrtoint i8* %35 to i64
%37 = add i64 %36, %6
%38 = trunc i64 %37 to i32
%39 = add i32 %38, 10
%40 = call i8* @malloc(i32 %39)
%41 = call i32 @memset(i8* %40, i8 signext 0, i32 %39)
%42 = call i32 @strcpy(i8* %40, i8* %9)
%43 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @strcat(i8* %40, i8* %0)
br label %45
45: ; preds = %8, %34
%46 = phi i8* [ %40, %34 ], [ null, %8 ]
br label %47
47: ; preds = %45, %4, %1
%48 = phi i8* [ null, %1 ], [ %46, %45 ], [ null, %4 ]
ret i8* %48
}