char *dirname(path)
char *path;
{
  static char dn[256];
  register char *p;
  strcpy(dn, path);
  for (p = dn; *p; ++p)
    ;
  for (--p; (p >= dn) && (*p == '/'); --p)
    ;
  for (; (p >= dn) && (*p != '/'); --p)
    ;
  for (; (p >= dn) && (*p == '/'); --p)
    *p = '\0';
  if (p < dn)
    dn[0] = '\0';
  return (dn);
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0x2f19(%rip),%rdi
call   1030 <strcpy@plt>
lea    0x2f0d(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     115a <dirname+0x4a>
jmp    1149 <dirname+0x39>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1137 <dirname+0x27>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
lea    0x2ed1(%rip),%rcx
cmp    %rcx,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jb     118c <dirname+0x7c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    119c <dirname+0x8c>
jmp    11b2 <dirname+0xa2>
jmp    11a1 <dirname+0x91>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1166 <dirname+0x56>
jmp    11b7 <dirname+0xa7>
xor    %eax,%eax
lea    0x2e80(%rip),%rcx
cmp    %rcx,-0x10(%rbp)
mov    %al,-0x12(%rbp)
jb     11dd <dirname+0xcd>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    11ed <dirname+0xdd>
jmp    1203 <dirname+0xf3>
jmp    11f2 <dirname+0xe2>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    11b7 <dirname+0xa7>
jmp    1208 <dirname+0xf8>
xor    %eax,%eax
lea    0x2e2f(%rip),%rcx
cmp    %rcx,-0x10(%rbp)
mov    %al,-0x13(%rbp)
jb     122e <dirname+0x11e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
test   $0x1,%al
jne    123e <dirname+0x12e>
jmp    1256 <dirname+0x146>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1208 <dirname+0xf8>
lea    0x2de3(%rip),%rax
cmp    %rax,-0x10(%rbp)
jae    126e <dirname+0x15e>
movb   $0x0,0x2dd2(%rip)
lea    0x2dcb(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %rbx
mov    %rdi,%rsi
lea    0x2f25(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0x2f14(%rip),%rax
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,0x2(%rax)
lea    0x1(%rax),%rax
jne    1130 <dirname+0x20>
nopw   0x0(%rax,%rax,1)
cmp    %rbx,%rax
jb     118c <dirname+0x7c>
lea    -0x1(%rax),%rcx
cmpb   $0x2f,(%rax)
mov    %rcx,%rax
je     1140 <dirname+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rcx,%rax
cmp    %rbx,%rcx
jb     118c <dirname+0x7c>
lea    -0x1(%rax),%rcx
cmpb   $0x2f,(%rax)
jne    1160 <dirname+0x50>
jmp    118c <dirname+0x7c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x2f,(%rax)
jne    1198 <dirname+0x88>
movb   $0x0,(%rax)
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jae    1180 <dirname+0x70>
movb   $0x0,0x2ea8(%rip)
lea    0x2ea1(%rip),%rax
pop    %rbx
ret

LLVM IR (-O0):
@dirname.dn = internal global [256 x i8] zeroinitializer
define i8* @dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i8* @strcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0), i8* %4)
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0), i8** %3
br label %6
6: ; preds = %11, %1
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = icmp ne i8 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
br label %11
11: ; preds = %10
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %3
br label %6
14: ; preds = %6
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i32 -1
store i8* %16, i8** %3
br label %17
17: ; preds = %28, %14
%18 = load i8*, i8** %3
%19 = icmp uge i8* %18, getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8*, i8** %3
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 47
br label %25
25: ; preds = %20, %17
%26 = phi i1 [ false, %17 ], [ %24, %20 ]
br i1 %26, label %27, label %31
27: ; preds = %25
br label %28
28: ; preds = %27
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %3
br label %17
31: ; preds = %25
br label %32
32: ; preds = %43, %31
%33 = load i8*, i8** %3
%34 = icmp uge i8* %33, getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i8*, i8** %3
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 47
br label %40
40: ; preds = %35, %32
%41 = phi i1 [ false, %32 ], [ %39, %35 ]
br i1 %41, label %42, label %46
42: ; preds = %40
br label %43
43: ; preds = %42
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i32 -1
store i8* %45, i8** %3
br label %32
46: ; preds = %40
br label %47
47: ; preds = %59, %46
%48 = load i8*, i8** %3
%49 = icmp uge i8* %48, getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i8*, i8** %3
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 47
br label %55
55: ; preds = %50, %47
%56 = phi i1 [ false, %47 ], [ %54, %50 ]
br i1 %56, label %57, label %62
57: ; preds = %55
%58 = load i8*, i8** %3
store i8 0, i8* %58
br label %59
59: ; preds = %57
%60 = load i8*, i8** %3
%61 = getelementptr inbounds i8, i8* %60, i32 -1
store i8* %61, i8** %3
br label %47
62: ; preds = %55
%63 = load i8*, i8** %3
%64 = icmp ult i8* %63, getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br i1 %64, label %65, label %66
65: ; preds = %62
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br label %66
66: ; preds = %65, %62
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
}

LLVM IR (-O2):
@dirname.dn = internal global [256 x i8] zeroinitializer
define i8* @dirname(i8* nocapture %0) {
%2 = tail call i8* @strcpy(i8* dereferenceable(1) getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0), i8* dereferenceable(1) %0)
br label %3
3: ; preds = %3, %1
%4 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0), %1 ], [ %7, %3 ]
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
%7 = getelementptr inbounds i8, i8* %4, i64 1
br i1 %6, label %8, label %3
8: ; preds = %3, %12
%9 = phi i8* [ %10, %12 ], [ %4, %3 ]
%10 = getelementptr inbounds i8, i8* %9, i64 -1
%11 = icmp ult i8* %10, getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br i1 %11, label %22, label %12
12: ; preds = %8
%13 = load i8, i8* %10
%14 = icmp eq i8 %13, 47
br i1 %14, label %8, label %18
15: ; preds = %18
%16 = load i8, i8* %20
%17 = icmp eq i8 %16, 47
br i1 %17, label %22, label %18
18: ; preds = %12, %15
%19 = phi i8* [ %20, %15 ], [ %10, %12 ]
%20 = getelementptr inbounds i8, i8* %19, i64 -1
%21 = icmp ult i8* %20, getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br i1 %21, label %22, label %15
22: ; preds = %8, %18, %15
%23 = phi i8* [ %20, %15 ], [ %20, %18 ], [ %10, %8 ]
%24 = icmp ult i8* %23, getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br i1 %24, label %32, label %25
25: ; preds = %22, %29
%26 = phi i8* [ %30, %29 ], [ %23, %22 ]
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 47
br i1 %28, label %29, label %33
29: ; preds = %25
store i8 0, i8* %26
%30 = getelementptr inbounds i8, i8* %26, i64 -1
%31 = icmp ult i8* %30, getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br i1 %31, label %32, label %25
32: ; preds = %29, %22
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
br label %33
33: ; preds = %25, %32
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dn, i64 0, i64 0)
}