#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 */
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strrchr(char const *, char);
char *xcalloc(size_t, int);
const char *get_plugin(const char *name) {
  if (name == NULL || *name == '\0')
    return NULL;
  char *last_dot = strrchr(name, '.');
  if (!last_dot || strncmp(last_dot, ".so", 3))
    return NULL;
  size_t len = last_dot - name;
  if (!len)
    return NULL;
  char *plugin_name = xcalloc(len + 1, sizeof(char));
  strncpy(plugin_name, name, len);
  return plugin_name;
}


x86 assembly (clang, -O0):
<get_plugin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1167 <get_plugin+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1174 <get_plugin+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1213 <get_plugin+0xd3>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b0 <get_plugin+0x70>
mov    -0x18(%rbp),%rdi
lea    0xe64(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     11bd <get_plugin+0x7d>
movq   $0x0,-0x8(%rbp)
jmp    1213 <get_plugin+0xd3>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e4 <get_plugin+0xa4>
movq   $0x0,-0x8(%rbp)
jmp    1213 <get_plugin+0xd3>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <xcalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_plugin>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1180 <get_plugin+0x40>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1180 <get_plugin+0x40>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1180 <get_plugin+0x40>
mov    %rax,%r14
lea    0xe92(%rip),%rsi
mov    %rax,%rdi
mov    $0x3,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     118c <get_plugin+0x4c>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
sub    %rbx,%r14
je     1180 <get_plugin+0x40>
lea    0x1(%r14),%rdi
mov    $0x1,%esi
call   1060 <xcalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
jmp    1183 <get_plugin+0x43>

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

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