#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 /*<<< orphan*/ buf;

/* Variables and functions */
char *path_last_sep(char const *);
scalar_t__ strcmp(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *base_name(const char *name, const char *suffix) {
  static char buf[256];
  char *base;
  if (!name)
    return NULL;
  base = path_last_sep(name);
  strncpy(buf, base ? base + 1 : name, sizeof(buf) - 1);
  if (suffix) {
    int l = strlen(buf) - strlen(suffix);
    if (l >= 0 && strcmp(buf + l, suffix) == 0)
      buf[l] = '\0';
  }
  return buf;
}


x86 assembly (clang, -O0):
<base_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1168 <base_name+0x28>
movq   $0x0,-0x8(%rbp)
jmp    1225 <base_name+0xe5>
mov    -0x10(%rbp),%rdi
call   1050 <path_last_sep@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1191 <base_name+0x51>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1199 <base_name+0x59>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0x2eac(%rip),%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x18(%rbp)
je     121a <base_name+0xda>
lea    0x2e90(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1215 <base_name+0xd5>
movslq -0x24(%rbp),%rax
lea    0x2e60(%rip),%rdi
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1215 <base_name+0xd5>
movslq -0x24(%rbp),%rcx
lea    0x2e3f(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
jmp    121a <base_name+0xda>
lea    0x2e2f(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11bb <base_name+0x7b>
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <path_last_sep@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %rbx,%rsi
lea    0x2ee7(%rip),%rbx
mov    %rbx,%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
test   %r14,%r14
je     11bd <base_name+0x7d>
lea    0x2ece(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
js     11bd <base_name+0x7d>
mov    %ebp,%r15d
lea    0x2eae(%rip),%rbx
add    %rbx,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11bd <base_name+0x7d>
movb   $0x0,(%r15)
jmp    11bd <base_name+0x7d>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@base_name.buf = internal global [256 x i8] zeroinitializer
define i8* @base_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %2
store i8* null, i8** %3
br label %46
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i8* @path_last_sep(i8* %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 1
br label %21
19: ; preds = %11
%20 = load i8*, i8** %4
br label %21
21: ; preds = %19, %16
%22 = phi i8* [ %18, %16 ], [ %20, %19 ]
%23 = call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0), i8* %22, i32 255)
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %45
26: ; preds = %21
%27 = call i32 @strlen(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0))
%28 = load i8*, i8** %5
%29 = call i32 @strlen(i8* %28)
%30 = sub nsw i32 %27, %29
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp sge i32 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %26
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0), i64 %35
%37 = load i8*, i8** %5
%38 = call i64 @strcmp(i8* %36, i8* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %40, %33, %26
br label %45
45: ; preds = %44, %21
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0), i8** %3
br label %46
46: ; preds = %45, %10
%47 = load i8*, i8** %3
ret i8* %47
}

LLVM IR (-O2):
@base_name.buf = internal global [256 x i8] zeroinitializer
define i8* @base_name(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %22, label %4
4: ; preds = %2
%5 = tail call i8* @path_last_sep(i8* %0)
%6 = icmp eq i8* %5, null
%7 = getelementptr inbounds i8, i8* %5, i64 1
%8 = select i1 %6, i8* %0, i8* %7
%9 = tail call i32 @strncpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0), i8* %8, i32 255)
%10 = icmp eq i8* %1, null
br i1 %10, label %22, label %11
11: ; preds = %4
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0))
%13 = tail call i32 @strlen(i8* %1)
%14 = sub nsw i32 %12, %13
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = zext i32 %14 to i64
%18 = getelementptr inbounds [256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 %17
%19 = tail call i64 @strcmp(i8* %18, i8* %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i8 0, i8* %18
br label %22
22: ; preds = %4, %21, %16, %11, %2
%23 = phi i8* [ null, %2 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0), %11 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0), %16 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0), %21 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @base_name.buf, i64 0, i64 0), %4 ]
ret i8* %23
}