char *directory(name, dir)
char *name;
char *dir;
{
  extern char *strchr();
  char *ptr, *q;
  strcpy(dir, name);
  q = dir;
  while (ptr = strchr(q, '/')) {
    q = ptr + 1;
  }
  if (q == dir) {
    strcpy(dir, ".");
  } else {
    *(q - 1) = 0;
  }
  return (dir);
}


x86 assembly (clang, -O0):
<directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1172 <directory+0x52>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1145 <directory+0x25>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1195 <directory+0x75>
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1030 <strcpy@plt>
jmp    119d <directory+0x7d>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<directory>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rcx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rcx,%rbx
mov    %rcx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
lea    0x1(%rax),%rcx
test   %rax,%rax
jne    1140 <directory+0x20>
cmp    %r14,%rbx
je     1164 <directory+0x44>
movb   $0x0,-0x1(%rbx)
jmp    116a <directory+0x4a>
movw   $0x2e,(%r14)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i8* @directory(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = load i8*, i8** %3
%9 = call i8* @strcpy(i8* %7, i8* %8)
%10 = load i8*, i8** %4
store i8* %10, i8** %6
br label %11
11: ; preds = %15, %2
%12 = load i8*, i8** %6
%13 = call i8* @strchr(i8* %12, i32 47)
store i8* %13, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %17, i8** %6
br label %11
18: ; preds = %11
%19 = load i8*, i8** %6
%20 = load i8*, i8** %4
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = call i8* @strcpy(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %28
25: ; preds = %18
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i64 -1
store i8 0, i8* %27
br label %28
28: ; preds = %25, %22
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
define i8* @directory(i8* nocapture %0, i8* returned %1) {
%3 = tail call i8* @strcpy(i8* dereferenceable(1) %1, i8* dereferenceable(1) %0)
br label %4
4: ; preds = %4, %2
%5 = phi i8* [ %1, %2 ], [ %8, %4 ]
%6 = tail call i8* @strchr(i8* dereferenceable(1) %5, i32 47)
%7 = icmp eq i8* %6, null
%8 = getelementptr inbounds i8, i8* %6, i64 1
br i1 %7, label %9, label %4
9: ; preds = %4
%10 = icmp eq i8* %5, %1
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = bitcast i8* %1 to i16*
store i16 46, i16* %12
br label %15
13: ; preds = %9
%14 = getelementptr inbounds i8, i8* %5, i64 -1
store i8 0, i8* %14
br label %15
15: ; preds = %13, %11
ret i8* %1
}