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


x86 assembly (clang, -O0):
<KSRM16162742943295_br_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <KSRM16162742943295_br_dirname+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1233 <KSRM16162742943295_br_dirname+0xf3>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <KSRM16162742943295_br_dirname+0x56>
lea    0xe78(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1233 <KSRM16162742943295_br_dirname+0xf3>
jmp    119b <KSRM16162742943295_br_dirname+0x5b>
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x21(%rbp)
jbe    11be <KSRM16162742943295_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 <KSRM16162742943295_br_dirname+0x8e>
jmp    11df <KSRM16162742943295_br_dirname+0x9f>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    119b <KSRM16162742943295_br_dirname+0x5b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1050 <br_strndup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    122b <KSRM16162742943295_br_dirname+0xeb>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xde5(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1233 <KSRM16162742943295_br_dirname+0xf3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<KSRM16162742943295_br_dirname>:
push   %rbx
test   %rdi,%rdi
je     1187 <KSRM16162742943295_br_dirname+0x47>
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     118b <KSRM16162742943295_br_dirname+0x4b>
cmp    %rbx,%rax
jbe    1171 <KSRM16162742943295_br_dirname+0x31>
nopl   (%rax)
cmpb   $0x2f,(%rax)
jne    1171 <KSRM16162742943295_br_dirname+0x31>
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
ja     1160 <KSRM16162742943295_br_dirname+0x20>
mov    %rbx,%rax
sub    %ebx,%eax
add    $0x1,%eax
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <br_strndup@plt>
cmpb   $0x0,(%rax)
je     1198 <KSRM16162742943295_br_dirname+0x58>
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
lea    0xe6e(%rip),%rdi
pop    %rbx
jmp    1060 <strdup@plt>
mov    %rax,%rdi
call   1030 <free@plt>
lea    0xe5b(%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* @KSRM16162742943295_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 %52
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 %52
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 = trunc i64 %38 to i32
%40 = call i8* @br_strndup(i8* %32, i32 %39)
store i8* %40, i8** %5
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %31
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
%49 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %49, i8** %2
br label %52
50: ; preds = %31
%51 = load i8*, i8** %5
store i8* %51, i8** %2
br label %52
52: ; preds = %50, %46, %14, %8
%53 = load i8*, i8** %2
ret i8* %53
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @KSRM16162742943295_br_dirname(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = icmp eq i8* %0, null
br i1 %3, label %36, 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 %33, 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 %24, %2
%26 = trunc i64 %25 to i32
%27 = add i32 %26, 1
%28 = tail call i8* @br_strndup(i8* %0, i32 %27)
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %23
%32 = tail call i32 @free(i8* %28)
br label %33
33: ; preds = %4, %31
%34 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %31 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %4 ]
%35 = tail call dereferenceable_or_null(2) i8* @strdup(i8* %34)
br label %36
36: ; preds = %33, %23, %1
%37 = phi i8* [ null, %1 ], [ %28, %23 ], [ %35, %33 ]
ret i8* %37
}