#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*/ com_strncpy(char *, int, char const *, size_t);
size_t strlen(char const *);
char *strrchr(char const *, char);
void file_basename(const char *filename, char *buff) {
  const char *startp;
  const char *endp;
  size_t len;
  startp = strrchr(filename, '/');
  if (startp) {
    startp++;
  } else {
    startp = filename;
  }
  endp = strrchr(filename, '.');
  if (endp > startp) {
    len = endp - startp;
  } else {
    len = strlen(startp);
  }
  com_strncpy(buff, 256, startp, len);
  buff[len] = '\0';
}


x86 assembly (clang, -O0):
<file_basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116e <file_basename+0x3e>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1176 <file_basename+0x46>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11aa <file_basename+0x7a>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    11b7 <file_basename+0x87>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x100,%esi
call   1050 <com_strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_basename>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%r15
test   %rax,%rax
cmove  %rbx,%r15
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,%rbx
sub    %r15,%rbx
ja     1170 <file_basename+0x40>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1050 <com_strncpy@plt>
movb   $0x0,(%r14,%rbx,1)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @file_basename(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%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 = %2
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %5
br label %17
15: ; preds = %2
%16 = load i8*, i8** %3
store i8* %16, i8** %5
br label %17
17: ; preds = %15, %12
%18 = load i8*, i8** %3
%19 = call i8* @strrchr(i8* %18, i8 signext 46)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i8*, i8** %5
%22 = icmp ugt i8* %20, %21
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = load i8*, i8** %6
%25 = load i8*, i8** %5
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
store i64 %28, i64* %7
br label %32
29: ; preds = %17
%30 = load i8*, i8** %5
%31 = call i64 @strlen(i8* %30)
store i64 %31, i64* %7
br label %32
32: ; preds = %29, %23
%33 = load i8*, i8** %4
%34 = load i8*, i8** %5
%35 = load i64, i64* %7
%36 = call i32 @com_strncpy(i8* %33, i32 256, i8* %34, i64 %35)
%37 = load i8*, i8** %4
%38 = load i64, i64* %7
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
ret void
}

LLVM IR (-O2):
define void @file_basename(i8* %0, i8* %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%4 = icmp eq i8* %3, null
%5 = getelementptr inbounds i8, i8* %3, i64 1
%6 = select i1 %4, i8* %0, i8* %5
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%8 = icmp ugt i8* %7, %6
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = ptrtoint i8* %7 to i64
%11 = ptrtoint i8* %6 to i64
%12 = sub i64 %10, %11
br label %15
13: ; preds = %2
%14 = tail call i64 @strlen(i8* dereferenceable(1) %6)
br label %15
15: ; preds = %13, %9
%16 = phi i64 [ %12, %9 ], [ %14, %13 ]
%17 = tail call i32 @com_strncpy(i8* %1, i32 256, i8* %6, i64 %16)
%18 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 0, i8* %18
ret void
}