#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
char *memchr(char *, int, char);
char *strrchr(char *, char);
char *dirname(char *path) {
  static const char dot[] = ".";
  char *last_slash;
  last_slash = path != NULL ? strrchr(path, '/') : NULL;
  if (last_slash == path)
    ++last_slash;
  else if (last_slash != NULL && last_slash[1] == '\0')
    last_slash = memchr(path, last_slash - path, '/');
  if (last_slash != NULL)
    last_slash[0] = '\0';
  else
    path = (char *)dot;
  return path;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     114e <dirname+0x2e>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
jmp    1159 <dirname+0x39>
xor    %eax,%eax
mov    %rax,-0x18(%rbp)
jmp    1159 <dirname+0x39>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1180 <dirname+0x60>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11c0 <dirname+0xa0>
cmpq   $0x0,-0x10(%rbp)
je     11bb <dirname+0x9b>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    11bb <dirname+0x9b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    $0x2f,%edx
call   1040 <memchr@plt>
mov    %rax,-0x10(%rbp)
jmp    11c0 <dirname+0xa0>
cmpq   $0x0,-0x10(%rbp)
je     11d7 <dirname+0xb7>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11e2 <dirname+0xc2>
lea    0xe22(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
je     1168 <dirname+0x48>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
cmp    %rbx,%rax
je     1168 <dirname+0x48>
lea    0xebb(%rip),%r14
test   %rax,%rax
je     1172 <dirname+0x52>
cmpb   $0x0,0x1(%rax)
jne    116c <dirname+0x4c>
sub    %ebx,%eax
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x2f,%edx
call   1040 <memchr@plt>
test   %rax,%rax
jne    116c <dirname+0x4c>
jmp    1172 <dirname+0x52>
lea    0x1(%rbx),%rax
movb   $0x0,(%rax)
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@dirname.dot = internal constant [2 x i8] c".\00"
define i8* @dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i8*, i8** %2
%8 = call i8* @strrchr(i8* %7, i8 signext 47)
br label %10
9: ; preds = %1
br label %10
10: ; preds = %9, %6
%11 = phi i8* [ %8, %6 ], [ null, %9 ]
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = load i8*, i8** %2
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %3
br label %37
18: ; preds = %10
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %36
21: ; preds = %18
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %21
%28 = load i8*, i8** %2
%29 = load i8*, i8** %3
%30 = load i8*, i8** %2
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
%35 = call i8* @memchr(i8* %28, i32 %34, i8 signext 47)
store i8* %35, i8** %3
br label %36
36: ; preds = %27, %21, %18
br label %37
37: ; preds = %36, %15
%38 = load i8*, i8** %3
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %3
%42 = getelementptr inbounds i8, i8* %41, i64 0
store i8 0, i8* %42
br label %44
43: ; preds = %37
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @dirname.dot, i64 0, i64 0), i8** %2
br label %44
44: ; preds = %43, %40
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
@dirname.dot = internal constant [2 x i8] c".\00"
define i8* @dirname(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = tail call i8* @strrchr(i8* %0, i8 signext 47)
%5 = icmp eq i8* %4, %0
br i1 %5, label %6, label %8
6: ; preds = %1, %3
%7 = getelementptr inbounds i8, i8* %0, i64 1
br label %21
8: ; preds = %3
%9 = icmp eq i8* %4, null
br i1 %9, label %23, label %10
10: ; preds = %8
%11 = getelementptr inbounds i8, i8* %4, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = ptrtoint i8* %4 to i64
%16 = ptrtoint i8* %0 to i64
%17 = sub i64 %15, %16
%18 = trunc i64 %17 to i32
%19 = tail call i8* @memchr(i8* %0, i32 %18, i8 signext 47)
%20 = icmp eq i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %10, %6, %14
%22 = phi i8* [ %19, %14 ], [ %4, %10 ], [ %7, %6 ]
store i8 0, i8* %22
br label %23
23: ; preds = %8, %14, %21
%24 = phi i8* [ %0, %21 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @dirname.dot, i64 0, i64 0), %14 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @dirname.dot, i64 0, i64 0), %8 ]
ret i8* %24
}