#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__ strcmp(char *, char const *);
char *strdup(char const *);
void *strrchr(char const *, char);
extern char *cptname(const char *path, const char *ext) {
  const char *pb;
  if ((pb = strrchr(path, '/')) != NULL)
    pb++;
  else
    pb = path;
  char *pc;
  if ((pc = strdup(pb)) == NULL)
    return NULL;
  if (ext) {
    char *px;
    if ((px = strrchr(pc, '.')) != NULL) {
      if ((strcmp("*", ext) == 0) || (strcmp(px + 1, ext) == 0))
        *px = '\0';
    }
  }
  return pc;
}


x86 assembly (clang, -O0):
<cptname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     116d <cptname+0x3d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1175 <cptname+0x45>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1199 <cptname+0x69>
movq   $0x0,-0x8(%rbp)
jmp    120e <cptname+0xde>
cmpq   $0x0,-0x18(%rbp)
je     1206 <cptname+0xd6>
mov    -0x28(%rbp),%rdi
mov    $0x2e,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1201 <cptname+0xd1>
mov    -0x18(%rbp),%rsi
lea    0xe35(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11f5 <cptname+0xc5>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11fc <cptname+0xcc>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
jmp    1201 <cptname+0xd1>
jmp    1206 <cptname+0xd6>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cptname>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1030 <strrchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
cmove  %rbx,%rdi
call   1050 <strdup@plt>
mov    %rax,%rbx
test   %r14,%r14
je     11a0 <cptname+0x70>
test   %rbx,%rbx
je     11a0 <cptname+0x70>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     11a0 <cptname+0x70>
mov    %rax,%r15
lea    0xe82(%rip),%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     119c <cptname+0x6c>
lea    0x1(%r15),%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    11a0 <cptname+0x70>
movb   $0x0,(%r15)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"*\00"
define i8* @cptname(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* @strdup(i8* %6)
%8 = icmp eq i8* %7, null
%9 = icmp eq i8* %1, null
%10 = or i1 %9, %8
br i1 %10, label %22, label %11
11: ; preds = %2
%12 = tail call i8* @strrchr(i8* %7, i8 signext 46)
%13 = icmp eq i8* %12, null
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %12, i64 1
%19 = tail call i64 @strcmp(i8* %18, i8* %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17, %14
store i8 0, i8* %12
br label %22
22: ; preds = %17, %21, %11, %2
ret i8* %7
}