#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*/ PATH_MAX;
char *get_dirname_specific();
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
char *strrchr(char *, char);
char *dirname_r(char *path) {
  char *buf = get_dirname_specific();
  int pathlen = strlen(path);
  if (!path || !strncmp(path, "", PATH_MAX)) {
    strncpy(buf, ".", PATH_MAX);
    return buf;
  }
  if (path[pathlen - 1] == '/')
    path[pathlen - 1] = '\0';
  char *p = strrchr(path, '/');
  if (p)
    *p = '\0';
  strncpy(buf, path, PATH_MAX);
  return buf;
}


x86 assembly (clang, -O0):
<dirname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <get_dirname_specific@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a0 <dirname_r+0x50>
mov    -0x10(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
lea    0xe6f(%rip),%rsi
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    11c6 <dirname_r+0x76>
mov    -0x18(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
lea    0xe4c(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1233 <dirname_r+0xe3>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11f1 <dirname_r+0xa1>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1215 <dirname_r+0xc5>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <get_dirname_specific@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %rbx,%rbx
je     11bf <dirname_r+0x6f>
mov    %eax,%ebp
mov    0x2e76(%rip),%r15
mov    (%r15),%edx
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strncmp@plt>
test   %eax,%eax
je     11bf <dirname_r+0x6f>
movslq %ebp,%rax
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    119f <dirname_r+0x4f>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11b4 <dirname_r+0x64>
movb   $0x0,(%rax)
mov    (%r15),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
jmp    11d2 <dirname_r+0x82>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
lea    0xe31(%rip),%rsi
mov    %r14,%rdi
call   1030 <strncpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@PATH_MAX = global i32 0
@.str.1 = [2 x i8] c".\00"
define i8* @dirname_r(i8* %0) {
%2 = tail call i8* (...) @get_dirname_specific()
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp eq i8* %0, null
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = load i32, i32* @PATH_MAX
%7 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %5, %1
%10 = load i32, i32* @PATH_MAX
%11 = tail call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %26
12: ; preds = %5
%13 = add nsw i32 %3, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 47
br i1 %17, label %18, label %19
18: ; preds = %12
store i8 0, i8* %15
br label %19
19: ; preds = %18, %12
%20 = tail call i8* @strrchr(i8* %0, i8 signext 47)
%21 = icmp eq i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %19
store i8 0, i8* %20
br label %23
23: ; preds = %22, %19
%24 = load i32, i32* @PATH_MAX
%25 = tail call i32 @strncpy(i8* %2, i8* %0, i32 %24)
br label %26
26: ; preds = %23, %9
ret i8* %2
}