#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 /*<<< orphan*/ free(char *);
char *strdup(char *);
int /*<<< orphan*/ stripslash(char *);
char *strrchr(int /*<<< orphan*/, char);
char *dirname(char *pathname) {
  char *dup = strdup(pathname);
  char *ptr = strrchr(stripslash(dup), '/');
  if (ptr == NULL) {
    free(dup);
    return strdup(".");
  }
  if (ptr == dup && dup[0] == '/')
    ptr++;
  *ptr = '\0';
  return dup;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <stripslash@plt>
mov    %eax,%edi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119b <dirname+0x5b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe73(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11d4 <dirname+0x94>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11c5 <dirname+0x85>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11c5 <dirname+0x85>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %rbx
call   1060 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <stripslash@plt>
mov    %eax,%edi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     117d <dirname+0x3d>
cmp    %rbx,%rax
jne    1175 <dirname+0x35>
xor    %ecx,%ecx
cmpb   $0x2f,(%rbx)
sete   %cl
mov    %rbx,%rax
add    %rcx,%rax
movb   $0x0,(%rax)
mov    %rbx,%rax
pop    %rbx
ret
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe74(%rip),%rdi
pop    %rbx
jmp    1060 <strdup@plt>

LLVM IR (-O0):
@.str = [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 = call i8* @strdup(i8* %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 @stripslash(i8* %8)
%10 = call i8* @strrchr(i32 %9, i8 signext 47)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i32 @free(i8* %14)
%16 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %2
br label %33
17: ; preds = %1
%18 = load i8*, i8** %5
%19 = load i8*, i8** %4
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 47
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
br label %30
30: ; preds = %27, %21, %17
%31 = load i8*, i8** %5
store i8 0, i8* %31
%32 = load i8*, i8** %4
store i8* %32, i8** %2
br label %33
33: ; preds = %30, %13
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i8* @dirname(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i32 @stripslash(i8* %2)
%4 = tail call i8* @strrchr(i32 %3, i8 signext 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @free(i8* %2)
%8 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %18
9: ; preds = %1
%10 = icmp eq i8* %4, %2
br i1 %10, label %11, label %16
11: ; preds = %9
%12 = load i8, i8* %2
%13 = icmp eq i8 %12, 47
%14 = zext i1 %13 to i64
%15 = getelementptr i8, i8* %2, i64 %14
br label %16
16: ; preds = %11, %9
%17 = phi i8* [ %4, %9 ], [ %15, %11 ]
store i8 0, i8* %17
br label %18
18: ; preds = %16, %6
%19 = phi i8* [ %8, %6 ], [ %2, %16 ]
ret i8* %19
}