#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 */
scalar_t__ malloc(unsigned int);
int /*<<< orphan*/ memcpy(char *, char *, unsigned int);
char *strdup(char const *);
size_t strlen(char const *);
char *strrchr(char const *, char);
char *fsfuse_basename(const char *path) {
  size_t path_len = strlen(path);
  char *loc = strrchr(path, '/');
  unsigned base_len;
  char *base = NULL;
  if (!loc)
    return strdup(path);
  base_len = path_len - (loc - path) + 1;
  if (!base_len)
    return strdup("");
  base = (char *)malloc((base_len + 1) * sizeof(char));
  if (base) {
    memcpy(base, loc + 1, base_len);
    base[base_len] = '\0';
  }
  return base;
}


x86 assembly (clang, -O0):
<fsfuse_basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a0 <fsfuse_basename+0x50>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1221 <fsfuse_basename+0xd1>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11d8 <fsfuse_basename+0x88>
lea    0xe36(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1221 <fsfuse_basename+0xd1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1219 <fsfuse_basename+0xc9>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsfuse_basename>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1186 <fsfuse_basename+0x36>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
sub    %r14d,%ebx
add    %ebx,%eax
mov    %eax,%ebx
inc    %ebx
jne    1193 <fsfuse_basename+0x43>
lea    0xe7c(%rip),%rdi
jmp    1189 <fsfuse_basename+0x39>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>
add    $0x2,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11bd <fsfuse_basename+0x6d>
add    $0x1,%r14
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @fsfuse_basename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %4
%10 = load i8*, i8** %3
%11 = call i8* @strrchr(i8* %10, i8 signext 47)
store i8* %11, i8** %5
store i8* null, i8** %7
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %2
br label %53
17: ; preds = %1
%18 = load i64, i64* %4
%19 = load i8*, i8** %5
%20 = load i8*, i8** %3
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %20 to i64
%23 = sub i64 %21, %22
%24 = sub i64 %18, %23
%25 = add i64 %24, 1
%26 = trunc i64 %25 to i32
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %17
%30 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %30, i8** %2
br label %53
31: ; preds = %17
%32 = load i32, i32* %6
%33 = add i32 %32, 1
%34 = zext i32 %33 to i64
%35 = mul i64 %34, 1
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %51
41: ; preds = %31
%42 = load i8*, i8** %7
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 1
%45 = load i32, i32* %6
%46 = call i32 @memcpy(i8* %42, i8* %44, i32 %45)
%47 = load i8*, i8** %7
%48 = load i32, i32* %6
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
br label %51
51: ; preds = %41, %31
%52 = load i8*, i8** %7
store i8* %52, i8** %2
br label %53
53: ; preds = %51, %29, %14
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @fsfuse_basename(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i8* @strdup(i8* %0)
br label %27
6: ; preds = %1
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = ptrtoint i8* %2 to i64
%9 = ptrtoint i8* %0 to i64
%10 = sub i64 %9, %8
%11 = add i64 %10, %7
%12 = trunc i64 %11 to i32
%13 = add i32 %12, 1
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %6
%16 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %27
17: ; preds = %6
%18 = add i32 %12, 2
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = getelementptr inbounds i8, i8* %2, i64 1
%24 = tail call i32 @memcpy(i8* %20, i8* %23, i32 %13)
%25 = zext i32 %13 to i64
%26 = getelementptr inbounds i8, i8* %20, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %17, %22, %15, %4
%28 = phi i8* [ %16, %15 ], [ %5, %4 ], [ %20, %22 ], [ %20, %17 ]
ret i8* %28
}