char *basename(const char *path) {
  static char bname[1024];
  register const char *endp, *startp;
  if (path == ((void *)0) || *path == '\0') {
    (void)strlcpy(bname, ".", sizeof bname);
    return (bname);
  }
  endp = path + strlen(path) - 1;
  while (endp > path && *endp == '/')
    endp--;
  if (endp == path && *endp == '/') {
    (void)strlcpy(bname, "/", sizeof bname);
    return (bname);
  }
  startp = endp;
  while (startp > path && *(startp - 1) != '/')
    startp--;
  if (endp - startp + 2 > sizeof(bname)) {
    return (((void *)0));
  }
  strlcpy(bname, startp, endp - startp + 2);
  return (bname);
}


x86 assembly (clang, -O0):
<basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1147 <basename+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1171 <basename+0x51>
lea    0x2ef2(%rip),%rdi
lea    0xeab(%rip),%rsi
mov    $0x400,%edx
mov    $0x0,%al
call   1040 <strlcpy@plt>
lea    0x2ed8(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12c1 <basename+0x1a1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x29(%rbp)
jbe    11b7 <basename+0x97>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11c7 <basename+0xa7>
jmp    11d8 <basename+0xb8>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1194 <basename+0x74>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1220 <basename+0x100>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1220 <basename+0x100>
lea    0x2e43(%rip),%rdi
lea    0xdfe(%rip),%rsi
mov    $0x400,%edx
mov    $0x0,%al
call   1040 <strlcpy@plt>
lea    0x2e29(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12c1 <basename+0x1a1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x2a(%rbp)
jbe    124c <basename+0x12c>
mov    -0x20(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    125c <basename+0x13c>
jmp    126d <basename+0x14d>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1228 <basename+0x108>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
add    $0x2,%rax
cmp    $0x400,%rax
jbe    1295 <basename+0x175>
movq   $0x0,-0x8(%rbp)
jmp    12c1 <basename+0x1a1>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rax
sub    %rax,%rdx
add    $0x2,%rdx
lea    0x2d91(%rip),%rdi
mov    $0x0,%al
call   1040 <strlcpy@plt>
lea    0x2d83(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basename>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1170 <basename+0x50>
mov    %rdi,%rbx
mov    (%rdi),%bpl
test   %bpl,%bpl
je     1170 <basename+0x50>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0xffffffffffffffff,%rax
nop
mov    %rax,%rdx
lea    (%rbx,%rax,1),%rcx
cmp    %rbx,%rcx
jbe    1155 <basename+0x35>
lea    -0x1(%rdx),%rax
cmpb   $0x2f,(%rcx)
je     1140 <basename+0x20>
test   %rdx,%rdx
jne    1197 <basename+0x77>
cmp    $0x2f,%bpl
jne    1197 <basename+0x77>
lea    0x2ed9(%rip),%rbx
lea    0xe94(%rip),%rsi
jmp    117e <basename+0x5e>
lea    0x2ec9(%rip),%rbx
lea    0xe82(%rip),%rsi
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <strlcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x2,%eax
nopl   0x0(%rax)
mov    %rcx,%rsi
mov    %rax,%rdx
cmp    %rbx,%rcx
jbe    11b9 <basename+0x99>
lea    -0x1(%rsi),%rcx
lea    0x1(%rdx),%rax
cmpb   $0x2f,-0x1(%rsi)
jne    11a0 <basename+0x80>
cmp    $0x400,%rdx
jbe    11c6 <basename+0xa6>
xor    %ebx,%ebx
jmp    118d <basename+0x6d>
lea    0x2e73(%rip),%rbx
jmp    1183 <basename+0x63>

LLVM IR (-O0):
@basename.bname = internal global [1024 x i8] zeroinitializer
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @basename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8, %1
%14 = call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @strlcpy to i32 (i8*, i8*, i64, ...)*)(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 1024)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8** %2
br label %81
15: ; preds = %8
%16 = load i8*, i8** %3
%17 = load i8*, i8** %3
%18 = call i64 @strlen(i8* %17)
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 -1
store i8* %20, i8** %4
br label %21
21: ; preds = %32, %15
%22 = load i8*, i8** %4
%23 = load i8*, i8** %3
%24 = icmp ugt i8* %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 47
br label %30
30: ; preds = %25, %21
%31 = phi i1 [ false, %21 ], [ %29, %25 ]
br i1 %31, label %32, label %35
32: ; preds = %30
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i32 -1
store i8* %34, i8** %4
br label %21
35: ; preds = %30
%36 = load i8*, i8** %4
%37 = load i8*, i8** %3
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i8*, i8** %4
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 47
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @strlcpy to i32 (i8*, i8*, i64, ...)*)(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i64 1024)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8** %2
br label %81
46: ; preds = %39, %35
%47 = load i8*, i8** %4
store i8* %47, i8** %5
br label %48
48: ; preds = %60, %46
%49 = load i8*, i8** %5
%50 = load i8*, i8** %3
%51 = icmp ugt i8* %49, %50
br i1 %51, label %52, label %58
52: ; preds = %48
%53 = load i8*, i8** %5
%54 = getelementptr inbounds i8, i8* %53, i64 -1
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp ne i32 %56, 47
br label %58
58: ; preds = %52, %48
%59 = phi i1 [ false, %48 ], [ %57, %52 ]
br i1 %59, label %60, label %63
60: ; preds = %58
%61 = load i8*, i8** %5
%62 = getelementptr inbounds i8, i8* %61, i32 -1
store i8* %62, i8** %5
br label %48
63: ; preds = %58
%64 = load i8*, i8** %4
%65 = load i8*, i8** %5
%66 = ptrtoint i8* %64 to i64
%67 = ptrtoint i8* %65 to i64
%68 = sub i64 %66, %67
%69 = add nsw i64 %68, 2
%70 = icmp ugt i64 %69, 1024
br i1 %70, label %71, label %72
71: ; preds = %63
store i8* null, i8** %2
br label %81
72: ; preds = %63
%73 = load i8*, i8** %5
%74 = load i8*, i8** %4
%75 = load i8*, i8** %5
%76 = ptrtoint i8* %74 to i64
%77 = ptrtoint i8* %75 to i64
%78 = sub i64 %76, %77
%79 = add nsw i64 %78, 2
%80 = call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @strlcpy to i32 (i8*, i8*, i64, ...)*)(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8* %73, i64 %79)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8** %2
br label %81
81: ; preds = %72, %71, %44, %13
%82 = load i8*, i8** %2
ret i8* %82
}

LLVM IR (-O2):
@basename.bname = internal global [1024 x i8] zeroinitializer
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @basename(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3, %1
%7 = tail call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @strlcpy to i32 (i8*, i8*, i64, ...)*)(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 1024)
br label %39
8: ; preds = %3
%9 = tail call i64 @strlen(i8* %0)
%10 = getelementptr inbounds i8, i8* %0, i64 %9
br label %11
11: ; preds = %15, %8
%12 = phi i8* [ %10, %8 ], [ %13, %15 ]
%13 = getelementptr inbounds i8, i8* %12, i64 -1
%14 = icmp ugt i8* %13, %0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i8, i8* %13
%17 = icmp eq i8 %16, 47
br i1 %17, label %11, label %18
18: ; preds = %11, %15
%19 = icmp eq i8* %13, %0
%20 = icmp eq i8 %4, 47
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @strlcpy to i32 (i8*, i8*, i64, ...)*)(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i64 1024)
br label %39
24: ; preds = %18, %27
%25 = phi i8* [ %28, %27 ], [ %13, %18 ]
%26 = icmp ugt i8* %25, %0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = getelementptr inbounds i8, i8* %25, i64 -1
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 47
br i1 %30, label %31, label %24
31: ; preds = %24, %27
%32 = ptrtoint i8* %13 to i64
%33 = ptrtoint i8* %25 to i64
%34 = sub i64 %32, %33
%35 = add nsw i64 %34, 2
%36 = icmp ugt i64 %35, 1024
br i1 %36, label %39, label %37
37: ; preds = %31
%38 = tail call i32 (i8*, i8*, i64, ...) bitcast (i32 (...)* @strlcpy to i32 (i8*, i8*, i64, ...)*)(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), i8* %25, i64 %35)
br label %39
39: ; preds = %31, %37, %22, %6
%40 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), %6 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), %22 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @basename.bname, i64 0, i64 0), %37 ], [ null, %31 ]
ret i8* %40
}