#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 */
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
void filename(char *path, char *dest, uint32_t size) {
  char *start = strrchr(path, '/');
  if (start != NULL) {
    start++;
  } else {
    start = path;
  }
  char *end = strrchr(path, '.');
  if (end == NULL) {
    end = strrchr(path, '\0');
  }
  uint32_t len = (uint32_t)(end - start);
  if (len > size - 1) {
    len = size - 1;
  }
  strncpy(dest, start, len);
  dest[len] = 0;
}


x86 assembly (clang, -O0):
<filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1161 <filename+0x41>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1169 <filename+0x49>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1195 <filename+0x75>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11bd <filename+0x9d>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%r12
test   %rax,%rax
cmove  %rbx,%r12
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
jne    1173 <filename+0x43>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %rbx,%rax
sub    %r12d,%eax
lea    -0x1(%r15),%ebx
cmp    %r15d,%eax
cmovl  %eax,%ebx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @filename(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%5 = icmp eq i8* %4, null
%6 = getelementptr inbounds i8, i8* %4, i64 1
%7 = select i1 %5, i8* %0, i8* %6
%8 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%12 = getelementptr i8, i8* %0, i64 %11
br label %13
13: ; preds = %10, %3
%14 = phi i8* [ %12, %10 ], [ %8, %3 ]
%15 = ptrtoint i8* %14 to i64
%16 = ptrtoint i8* %7 to i64
%17 = sub i64 %15, %16
%18 = trunc i64 %17 to i32
%19 = icmp slt i32 %18, %2
%20 = add nsw i32 %2, -1
%21 = select i1 %19, i32 %18, i32 %20
%22 = tail call i32 @strncpy(i8* %1, i8* %7, i32 %21)
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
ret void
}