main(argc, argv) int argc;
char *argv[];
{
  int j, suflen;
  char *c;
  char *d;
  extern char *rindex();
  if (argc < 2) {
    std_err("Usage: basename string [suffix]  \n");
    exit(1);
  }
  c = argv[1];
  d = rindex(argv[1], '/');
  if (d == 0)
    d = argv[1];
  else
    d++;
  if (argc == 2) {
    prints("%s\n", d);
    exit(0);
  } else {
    c = d;
    suflen = strlen(argv[2]);
    j = strlen(c) - suflen;
    if (strcmp(c + j, argv[2]) == 0)
      *(c + j) = 0;
  }
  prints("%s\n", c);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1198 <main+0x38>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1080 <std_err@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2f,%esi
call   1060 <rindex@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d6 <main+0x76>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11e2 <main+0x82>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1205 <main+0xa5>
mov    -0x28(%rbp),%rsi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1050 <prints@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
movslq -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    125d <main+0xfd>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1262 <main+0x102>
mov    -0x20(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <prints@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jle    11fc <main+0x9c>
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
mov    $0x2f,%esi
call   1060 <rindex@plt>
test   %rax,%rax
je     11db <main+0x7b>
mov    %rax,%rbx
add    $0x1,%rbx
cmp    $0x2,%ebp
je     11e4 <main+0x84>
mov    0x10(%r14),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
sub    %ebp,%eax
movslq %eax,%rbp
add    %rbx,%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
jne    11c3 <main+0x63>
movb   $0x0,0x0(%rbp)
lea    0xe59(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <prints@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x8(%r14),%rbx
cmp    $0x2,%ebp
jne    1191 <main+0x31>
lea    0xe38(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <prints@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1080 <std_err@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"Usage: basename string [suffix] \0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) bitcast (i32 (...)* @std_err to i32 (i8*, ...)*)(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %8
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i8* @rindex(i8* %20, i32 47)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
store i8* %27, i8** %9
br label %31
28: ; preds = %14
%29 = load i8*, i8** %9
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %9
br label %31
31: ; preds = %28, %24
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i8*, i8** %9
%36 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @prints to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %35)
unreachable
37: ; preds = %31
%38 = load i8*, i8** %9
store i8* %38, i8** %8
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = call i64 @strlen(i8* %41)
%43 = trunc i64 %42 to i32
store i32 %43, i32* %7
%44 = load i8*, i8** %8
%45 = call i64 @strlen(i8* %44)
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = sub i64 %45, %47
%49 = trunc i64 %48 to i32
store i32 %49, i32* %6
%50 = load i8*, i8** %8
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = load i8*, i8** %55
%57 = call i32 @strcmp(i8* %53, i8* %56)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %37
%60 = load i8*, i8** %8
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
br label %64
64: ; preds = %59, %37
br label %65
65: ; preds = %64
%66 = load i8*, i8** %8
%67 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @prints to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %66)
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [35 x i8] c"Usage: basename string [suffix] \0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (i8*, ...) bitcast (i32 (...)* @std_err to i32 (i8*, ...)*)(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
tail call void @exit(i32 1)
unreachable
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i8* @rindex(i8* %8, i32 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = load i8*, i8** %7
br label %15
13: ; preds = %6
%14 = getelementptr inbounds i8, i8* %9, i64 1
br label %15
15: ; preds = %13, %11
%16 = phi i8* [ %12, %11 ], [ %14, %13 ]
%17 = icmp eq i32 %0, 2
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @prints to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %16)
tail call void @exit(i32 0)
unreachable
20: ; preds = %15
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i64 @strlen(i8* dereferenceable(1) %22)
%24 = tail call i64 @strlen(i8* dereferenceable(1) %16)
%25 = sub i64 %24, %23
%26 = shl i64 %25, 32
%27 = ashr exact i64 %26, 32
%28 = getelementptr inbounds i8, i8* %16, i64 %27
%29 = tail call i32 @strcmp(i8* dereferenceable(1) %28, i8* dereferenceable(1) %22)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %20
store i8 0, i8* %28
br label %32
32: ; preds = %20, %31
%33 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @prints to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %16)
ret i32 0
}