#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 */
int strlen(char *);
char *strrchr(char *, char);
char *dirname(char *buf) {
  if (buf == NULL || buf[0] == 0) {
    return (char *)".";
  };
  char *end = &buf[strlen(buf) - 1];
  while (*end == '/' && end != (buf - 1))
    *end-- = 0;
  if (buf[0] == 0) {
    return (char *)"/";
  };
  char *finalSlash = strrchr(buf, '/');
  if (finalSlash == NULL) {
    return (char *)".";
  };
  if (finalSlash == buf) {
    return (char *)"/";
  };
  *finalSlash = 0;
  return buf;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1147 <dirname+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1157 <dirname+0x37>
lea    0xeb2(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1246 <dirname+0x126>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x29(%rbp)
jne    11a5 <dirname+0x85>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11b5 <dirname+0x95>
jmp    11cc <dirname+0xac>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
jmp    117b <dirname+0x5b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ec <dirname+0xcc>
lea    0xe1f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1246 <dirname+0x126>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1219 <dirname+0xf9>
lea    0xdf0(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1246 <dirname+0x126>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1237 <dirname+0x117>
lea    0xdd4(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1246 <dirname+0x126>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r15
push   %r14
push   %rbx
lea    0xed4(%rip),%r14
test   %rdi,%rdi
je     11aa <dirname+0x8a>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     11aa <dirname+0x8a>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rcx
cmpb   $0x2f,-0x1(%rbx,%rcx,1)
jne    1179 <dirname+0x59>
test   %eax,%eax
je     1179 <dirname+0x59>
add    $0xffffffffffffffff,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rcx
setb   %al
movb   $0x0,(%rbx,%rdx,1)
cmpb   $0x2f,-0x1(%rbx,%rdx,1)
jne    1179 <dirname+0x59>
test   %al,%al
jne    1160 <dirname+0x40>
lea    0xe82(%rip),%r15
cmpb   $0x0,(%rbx)
je     11a7 <dirname+0x87>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     11aa <dirname+0x8a>
mov    %r15,%r14
cmp    %rbx,%rax
je     11aa <dirname+0x8a>
movb   $0x0,(%rax)
mov    %rbx,%r14
jmp    11aa <dirname+0x8a>
mov    %r15,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @dirname(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 = icmp eq i8* %6, null
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %8, %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %58
15: ; preds = %8
%16 = load i8*, i8** %3
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %16, i64 %20
store i8* %21, i8** %4
br label %22
22: ; preds = %34, %15
%23 = load i8*, i8** %4
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 47
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 -1
%31 = icmp ne i8* %28, %30
br label %32
32: ; preds = %27, %22
%33 = phi i1 [ false, %22 ], [ %31, %27 ]
br i1 %33, label %34, label %37
34: ; preds = %32
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i32 -1
store i8* %36, i8** %4
store i8 0, i8* %35
br label %22
37: ; preds = %32
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %58
44: ; preds = %37
%45 = load i8*, i8** %3
%46 = call i8* @strrchr(i8* %45, i8 signext 47)
store i8* %46, i8** %5
%47 = load i8*, i8** %5
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %50
49: ; preds = %44
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %58
50: ; preds = %44
%51 = load i8*, i8** %5
%52 = load i8*, i8** %3
%53 = icmp eq i8* %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %58
55: ; preds = %50
%56 = load i8*, i8** %5
store i8 0, i8* %56
%57 = load i8*, i8** %3
store i8* %57, i8** %2
br label %58
58: ; preds = %55, %54, %49, %43, %14
%59 = load i8*, i8** %2
ret i8* %59
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @dirname(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %33, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %33, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 47
%13 = icmp ne i32 %7, 0
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %15, label %24
15: ; preds = %6, %15
%16 = phi i8* [ %19, %15 ], [ %10, %6 ]
%17 = phi i64 [ %18, %15 ], [ %9, %6 ]
%18 = add nsw i64 %17, -1
store i8 0, i8* %16
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 47
%22 = icmp ne i64 %17, 0
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %15, label %24
24: ; preds = %15, %6
%25 = load i8, i8* %0
%26 = icmp eq i8 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = tail call i8* @strrchr(i8* %0, i8 signext 47)
%29 = icmp eq i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = icmp eq i8* %28, %0
br i1 %31, label %33, label %32
32: ; preds = %30
store i8 0, i8* %28
br label %33
33: ; preds = %24, %30, %27, %32, %1, %3
%34 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %24 ], [ %0, %32 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %27 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %30 ]
ret i8* %34
}