#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(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *strrchr(char const *, char);
char *baseleaf_from_fnm(const char *fnm) {
  const char *p;
  char *q;
  size_t len;
  p = fnm;
  q = strrchr(p, '/');
  if (q)
    p = q + 1;
  q = strrchr(p, '\\');
  if (q)
    p = q + 1;
  q = strrchr(p, '.');
  if (q)
    len = (const char *)q - p;
  else
    len = strlen(p);
  q = malloc((len + 1));
  if (!q)
    return NULL;
  memcpy(q, p, len);
  q[len] = '\0';
  return q;
}


x86 assembly (clang, -O0):
<baseleaf_from_fnm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117d <baseleaf_from_fnm+0x3d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x5c,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a6 <baseleaf_from_fnm+0x66>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d7 <baseleaf_from_fnm+0x97>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    11e4 <baseleaf_from_fnm+0xa4>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120d <baseleaf_from_fnm+0xcd>
movq   $0x0,-0x8(%rbp)
jmp    1232 <baseleaf_from_fnm+0xf2>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<baseleaf_from_fnm>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %r14,%rbx
mov    %rbx,%rdi
mov    $0x5c,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%r14
test   %rax,%rax
cmove  %rbx,%r14
mov    %r14,%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     118f <baseleaf_from_fnm+0x4f>
mov    %rax,%r15
sub    %r14,%r15
jmp    119a <baseleaf_from_fnm+0x5a>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
lea    0x1(%r15),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11be <baseleaf_from_fnm+0x7e>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @baseleaf_from_fnm(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 1
store i8* %14, i8** %4
br label %15
15: ; preds = %12, %1
%16 = load i8*, i8** %4
%17 = call i8* @strrchr(i8* %16, i8 signext 92)
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8* %22, i8** %4
br label %23
23: ; preds = %20, %15
%24 = load i8*, i8** %4
%25 = call i8* @strrchr(i8* %24, i8 signext 46)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i8*, i8** %5
%30 = load i8*, i8** %4
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
store i64 %33, i64* %6
br label %37
34: ; preds = %23
%35 = load i8*, i8** %4
%36 = call i64 @strlen(i8* %35)
store i64 %36, i64* %6
br label %37
37: ; preds = %34, %28
%38 = load i64, i64* %6
%39 = add i64 %38, 1
%40 = call i8* @malloc(i64 %39)
store i8* %40, i8** %5
%41 = load i8*, i8** %5
%42 = icmp ne i8* %41, null
br i1 %42, label %44, label %43
43: ; preds = %37
store i8* null, i8** %2
br label %53
44: ; preds = %37
%45 = load i8*, i8** %5
%46 = load i8*, i8** %4
%47 = load i64, i64* %6
%48 = call i32 @memcpy(i8* %45, i8* %46, i64 %47)
%49 = load i8*, i8** %5
%50 = load i64, i64* %6
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %5
store i8* %52, i8** %2
br label %53
53: ; preds = %44, %43
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
define i8* @baseleaf_from_fnm(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) %5, i8 signext 92)
%7 = icmp eq i8* %6, null
%8 = getelementptr inbounds i8, i8* %6, i64 1
%9 = select i1 %7, i8* %5, i8* %8
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %9, i8 signext 46)
%11 = icmp eq i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = ptrtoint i8* %10 to i64
%14 = ptrtoint i8* %9 to i64
%15 = sub i64 %13, %14
br label %18
16: ; preds = %1
%17 = tail call i64 @strlen(i8* dereferenceable(1) %9)
br label %18
18: ; preds = %16, %12
%19 = phi i64 [ %15, %12 ], [ %17, %16 ]
%20 = add i64 %19, 1
%21 = tail call i8* @malloc(i64 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = tail call i32 @memcpy(i8* %21, i8* %9, i64 %19)
%25 = getelementptr inbounds i8, i8* %21, i64 %19
store i8 0, i8* %25
br label %26
26: ; preds = %18, %23
ret i8* %21
}