#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*/ memmove(char *, char *, size_t);
char *memrchr(char *, char, size_t);
char *strdup(char *);
size_t strlen(char *);
char *basefile(char *path) {
  char *base;
  char *p;
  base = strdup(path);
  p = memrchr(base, '/', strlen(base));
  if (p != NULL) {
    if (base + strlen(p) == p) {
      base[0] = '\0';
    } else {
      memmove(base, p + 1, strlen(p + 1));
      base[strlen(p + 1)] = '\0';
    }
  }
  return base;
}


x86 assembly (clang, -O0):
<basefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rdx
mov    $0x2f,%esi
call   1050 <memrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1211 <basefile+0xd1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jne    11bb <basefile+0x7b>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    120c <basefile+0xcc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   1040 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
jmp    1211 <basefile+0xd1>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basefile>:
push   %r14
push   %rbx
push   %rax
call   1060 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
mov    %rax,%rdx
call   1050 <memrchr@plt>
test   %rax,%rax
je     11aa <basefile+0x6a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <strlen@plt>
add    %r14,%rax
mov    %r14,%rcx
cmp    %rbx,%rax
je     11a7 <basefile+0x67>
add    $0x1,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <memmove@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r14,%rcx
add    %rax,%rcx
movb   $0x0,(%rcx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @basefile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i8* @strdup(i8* %5)
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
%10 = call i8* @memrchr(i8* %7, i8 signext 47, i64 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %37
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = load i8*, i8** %4
%16 = call i64 @strlen(i8* %15)
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8*, i8** %4
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 0, i8* %22
br label %36
23: ; preds = %13
%24 = load i8*, i8** %3
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = call i64 @strlen(i8* %28)
%30 = call i32 @memmove(i8* %24, i8* %26, i64 %29)
%31 = load i8*, i8** %3
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = call i64 @strlen(i8* %33)
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %23, %20
br label %37
37: ; preds = %36, %1
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
define i8* @basefile(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%4 = tail call i8* @memrchr(i8* %2, i8 signext 47, i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %18, label %6
6: ; preds = %1
%7 = tail call i64 @strlen(i8* %4)
%8 = getelementptr inbounds i8, i8* %2, i64 %7
%9 = icmp eq i8* %8, %4
br i1 %9, label %16, label %10
10: ; preds = %6
%11 = getelementptr inbounds i8, i8* %4, i64 1
%12 = tail call i64 @strlen(i8* %11)
%13 = tail call i32 @memmove(i8* %2, i8* %11, i64 %12)
%14 = tail call i64 @strlen(i8* %11)
%15 = getelementptr inbounds i8, i8* %2, i64 %14
br label %16
16: ; preds = %6, %10
%17 = phi i8* [ %15, %10 ], [ %2, %6 ]
store i8 0, i8* %17
br label %18
18: ; preds = %16, %1
ret i8* %2
}