#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 */
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *plugin_get_name(char *filename) {
  char *last_slash = strrchr(filename, '/');
  char *name_start = last_slash ? last_slash + 1 : filename;
  char *last_dot = strrchr(filename, '.');
  if (!last_dot || strcmp(last_dot, ".so")) {
    return NULL;
  }
  char *dest = malloc((last_dot - name_start) + 2);
  memset(dest, 0x00, ((last_dot - name_start) + 2));
  strncpy(dest, name_start, last_dot - name_start);
  return dest;
}


x86 assembly (clang, -O0):
<plugin_get_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118a <plugin_get_name+0x3a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1192 <plugin_get_name+0x42>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d1 <plugin_get_name+0x81>
mov    -0x28(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11de <plugin_get_name+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    1236 <plugin_get_name+0xe6>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
add    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
add    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plugin_get_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%r14
test   %rax,%rax
cmove  %rbx,%r14
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1198 <plugin_get_name+0x48>
mov    %rax,%rbx
lea    0xe75(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     119d <plugin_get_name+0x4d>
xor    %r15d,%r15d
jmp    11c6 <plugin_get_name+0x76>
sub    %r14d,%ebx
lea    0x2(%rbx),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c".so\00"
define i8* @plugin_get_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 1
br label %17
15: ; preds = %1
%16 = load i8*, i8** %3
br label %17
17: ; preds = %15, %12
%18 = phi i8* [ %14, %12 ], [ %16, %15 ]
store i8* %18, i8** %5
%19 = load i8*, i8** %3
%20 = call i8* @strrchr(i8* %19, i8 signext 46)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i8*, i8** %6
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23, %17
store i8* null, i8** %2
br label %56
28: ; preds = %23
%29 = load i8*, i8** %6
%30 = load i8*, i8** %5
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = add nsw i64 %33, 2
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = load i8*, i8** %6
%39 = load i8*, i8** %5
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = add nsw i64 %42, 2
%44 = trunc i64 %43 to i32
%45 = call i32 @memset(i8* %37, i32 0, i32 %44)
%46 = load i8*, i8** %7
%47 = load i8*, i8** %5
%48 = load i8*, i8** %6
%49 = load i8*, i8** %5
%50 = ptrtoint i8* %48 to i64
%51 = ptrtoint i8* %49 to i64
%52 = sub i64 %50, %51
%53 = trunc i64 %52 to i32
%54 = call i32 @strncpy(i8* %46, i8* %47, i32 %53)
%55 = load i8*, i8** %7
store i8* %55, i8** %2
br label %56
56: ; preds = %28, %27
%57 = load i8*, i8** %2
ret i8* %57
}

LLVM IR (-O2):
@.str = [4 x i8] c".so\00"
define i8* @plugin_get_name(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
%4 = getelementptr inbounds i8, i8* %2, i64 1
%5 = select i1 %3, i8* %0, i8* %4
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%7 = icmp eq i8* %6, null
br i1 %7, label %20, label %8
8: ; preds = %1
%9 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = ptrtoint i8* %6 to i64
%13 = ptrtoint i8* %5 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = add i32 %15, 2
%17 = tail call i8* @malloc(i32 %16)
%18 = tail call i32 @memset(i8* %17, i32 0, i32 %16)
%19 = tail call i32 @strncpy(i8* %17, i8* %5, i32 %15)
br label %20
20: ; preds = %1, %8, %11
%21 = phi i8* [ %17, %11 ], [ null, %8 ], [ null, %1 ]
ret i8* %21
}