#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_basename_specific();
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
char *strrchr(char *, char);
char *basename_r(char *path) {
  char *buf = get_basename_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) {
    strncpy(buf, p + 1, PATH_MAX);
  } else {
    strncpy(buf, path, PATH_MAX);
  }
  return buf;
}


x86 assembly (clang, -O0):
<basename_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <get_basename_specific@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a0 <basename_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 <basename_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    124b <basename_r+0xfb>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11f1 <basename_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     122d <basename_r+0xdd>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
jmp    1243 <basename_r+0xf3>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2db4(%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):
<basename_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <get_basename_specific@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %rbx,%rbx
je     11c0 <basename_r+0x70>
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     11c0 <basename_r+0x70>
movslq %ebp,%rax
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    119f <basename_r+0x4f>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11d5 <basename_r+0x85>
add    $0x1,%rax
mov    (%r15),%edx
mov    %r14,%rdi
mov    %rax,%rsi
jmp    11de <basename_r+0x8e>
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
lea    0xe30(%rip),%rsi
mov    %r14,%rdi
jmp    11de <basename_r+0x8e>
mov    (%r15),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
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* @basename_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_basename_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 %55
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 %48
42: ; preds = %37
%43 = load i8*, i8** %4
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i32, i32* @PATH_MAX
%47 = call i32 @strncpy(i8* %43, i8* %45, i32 %46)
br label %53
48: ; preds = %37
%49 = load i8*, i8** %4
%50 = load i8*, i8** %3
%51 = load i32, i32* @PATH_MAX
%52 = call i32 @strncpy(i8* %49, i8* %50, i32 %51)
br label %53
53: ; preds = %48, %42
%54 = load i8*, i8** %4
store i8* %54, i8** %2
br label %55
55: ; preds = %53, %17
%56 = load i8*, i8** %2
ret i8* %56
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@PATH_MAX = global i32 0
@.str.1 = [2 x i8] c".\00"
define i8* @basename_r(i8* %0) {
%2 = tail call i8* (...) @get_basename_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 %29
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 %26, label %22
22: ; preds = %19
%23 = getelementptr inbounds i8, i8* %20, i64 1
%24 = load i32, i32* @PATH_MAX
%25 = tail call i32 @strncpy(i8* %2, i8* %23, i32 %24)
br label %29
26: ; preds = %19
%27 = load i32, i32* @PATH_MAX
%28 = tail call i32 @strncpy(i8* %2, i8* %0, i32 %27)
br label %29
29: ; preds = %22, %26, %9
ret i8* %2
}