char *_getdir(const char *p) {
  char *path = _xstrdup((p), __FUNCTION__, 69);
  char *c = (char *)_xmalloc((2), __FUNCTION__, 70);
  *c = '/';
  if (!path)
    return c;
  if (((*path == '/') ? 0 : 1))
    return c;
  if ((c = strrchr(path, '/')) && (c + 1))
    *(c + 1) = 0;
  return (path);
}


x86 assembly (clang, -O0):
<_getdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb9(%rip),%rsi
mov    $0x45,%edx
mov    $0x0,%al
call   1030 <_xstrdup@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    $0x2,%edi
lea    0xe9b(%rip),%rsi
mov    $0x46,%edx
mov    $0x0,%al
call   1050 <_xmalloc@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <_getdir+0x66>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120f <_getdir+0xdf>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11b5 <_getdir+0x85>
xor    %eax,%eax
test   $0x1,%al
jne    11c4 <_getdir+0x94>
jmp    11d1 <_getdir+0xa1>
mov    $0x1,%al
test   $0x1,%al
jne    11c4 <_getdir+0x94>
jmp    11d1 <_getdir+0xa1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120f <_getdir+0xdf>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1207 <_getdir+0xd7>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
cmp    $0x0,%rax
je     1207 <_getdir+0xd7>
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getdir>:
push   %r14
push   %rbx
push   %rax
lea    0xec5(%rip),%r14
mov    %r14,%rsi
mov    $0x45,%edx
xor    %eax,%eax
call   1030 <_xstrdup@plt>
mov    %eax,%ebx
mov    $0x2,%edi
mov    %r14,%rsi
mov    $0x46,%edx
xor    %eax,%eax
call   1050 <_xmalloc@plt>
cltq
movb   $0x2f,(%rax)
test   %ebx,%ebx
je     118a <_getdir+0x5a>
movslq %ebx,%rbx
cmpb   $0x2f,(%rbx)
jne    118a <_getdir+0x5a>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1187 <_getdir+0x57>
movb   $0x0,0x1(%rax)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__FUNCTION__._getdir = [8 x i8] c"_getdir\00"
define i8* @_getdir(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 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @_xstrdup to i32 (i8*, i8*, i32, ...)*)(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__FUNCTION__._getdir, i64 0, i64 0), i32 69)
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %4
%10 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @_xmalloc to i32 (i32, i8*, i32, ...)*)(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__FUNCTION__._getdir, i64 0, i64 0), i32 70)
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %5
%13 = load i8*, i8** %5
store i8 47, i8* %13
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %1
%17 = load i8*, i8** %5
store i8* %17, i8** %2
br label %40
18: ; preds = %1
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 47
br i1 %22, label %23, label %24
23: ; preds = %18
br i1 false, label %25, label %27
24: ; preds = %18
br i1 true, label %25, label %27
25: ; preds = %24, %23
%26 = load i8*, i8** %5
store i8* %26, i8** %2
br label %40
27: ; preds = %24, %23
%28 = load i8*, i8** %4
%29 = call i8* @strrchr(i8* %28, i32 47)
store i8* %29, i8** %5
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 0, i8* %37
br label %38
38: ; preds = %35, %31, %27
%39 = load i8*, i8** %4
store i8* %39, i8** %2
br label %40
40: ; preds = %38, %25, %16
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
@__FUNCTION__._getdir = [8 x i8] c"_getdir\00"
define i8* @_getdir(i8* %0) {
%2 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @_xstrdup to i32 (i8*, i8*, i32, ...)*)(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__FUNCTION__._getdir, i64 0, i64 0), i32 69)
%3 = sext i32 %2 to i64
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @_xmalloc to i32 (i32, i8*, i32, ...)*)(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__FUNCTION__._getdir, i64 0, i64 0), i32 70)
%6 = sext i32 %5 to i64
%7 = inttoptr i64 %6 to i8*
store i8 47, i8* %7
%8 = icmp eq i32 %2, 0
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = load i8, i8* %4
%11 = icmp eq i8 %10, 47
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = tail call i8* @strrchr(i8* %4, i32 47)
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %13, i64 1
store i8 0, i8* %16
br label %17
17: ; preds = %12, %15, %9, %1
%18 = phi i8* [ %7, %1 ], [ %7, %9 ], [ %4, %15 ], [ %4, %12 ]
ret i8* %18
}