#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 *br_strndup(char const *, size_t);
int /*<<< orphan*/ free(char *);
char *strdup(char *);
char *strrchr(char const *, char);
char *SbCJ22537442193159_br_dirname(const char *path) {
  char *end, *result;
  if (path == (const char *)((void *)0))
    return (char *)((void *)0);
  end = strrchr(path, '/');
  if (end == (const char *)((void *)0))
    return strdup(".");
  while (end > path && *end == '/')
    end--;
  result = br_strndup(path, (size_t)(end - path + 1));
  if (result[0] == 0) {
    free(result);
    return strdup("/");
  } else
    return result;
}


x86 assembly (clang, -O0):
<SbCJ22537442193159_br_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <SbCJ22537442193159_br_dirname+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1231 <SbCJ22537442193159_br_dirname+0xf1>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <SbCJ22537442193159_br_dirname+0x56>
lea    0xe78(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1231 <SbCJ22537442193159_br_dirname+0xf1>
jmp    119b <SbCJ22537442193159_br_dirname+0x5b>
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x21(%rbp)
jbe    11be <SbCJ22537442193159_br_dirname+0x7e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11ce <SbCJ22537442193159_br_dirname+0x8e>
jmp    11df <SbCJ22537442193159_br_dirname+0x9f>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    119b <SbCJ22537442193159_br_dirname+0x5b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rax
sub    %rax,%rsi
add    $0x1,%rsi
call   1050 <br_strndup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1229 <SbCJ22537442193159_br_dirname+0xe9>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xde7(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1231 <SbCJ22537442193159_br_dirname+0xf1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SbCJ22537442193159_br_dirname>:
push   %rbx
test   %rdi,%rdi
je     118a <SbCJ22537442193159_br_dirname+0x4a>
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     118e <SbCJ22537442193159_br_dirname+0x4e>
cmp    %rbx,%rax
jbe    1171 <SbCJ22537442193159_br_dirname+0x31>
nopl   (%rax)
cmpb   $0x2f,(%rax)
jne    1171 <SbCJ22537442193159_br_dirname+0x31>
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
ja     1160 <SbCJ22537442193159_br_dirname+0x20>
mov    %rbx,%rax
sub    %rbx,%rax
add    $0x1,%rax
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <br_strndup@plt>
cmpb   $0x0,(%rax)
je     119b <SbCJ22537442193159_br_dirname+0x5b>
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
lea    0xe6b(%rip),%rdi
pop    %rbx
jmp    1060 <strdup@plt>
mov    %rax,%rdi
call   1030 <free@plt>
lea    0xe58(%rip),%rdi
pop    %rbx
jmp    1060 <strdup@plt>

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

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