#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 const *, unsigned int);
char *strdup(char *);
char *strrchr(char const *, char);
char *fsfuse_dirname(const char *path) {
  char *loc = strrchr(path, '/');
  unsigned dir_len;
  char *dir = NULL;
  if (!loc)
    return strdup("");
  dir_len = loc - path;
  if (!dir_len)
    return strdup("/");
  dir = (char *)malloc((dir_len + 1) * sizeof(char));
  if (dir) {
    memcpy(dir, path, dir_len);
    dir[dir_len] = '\0';
  }
  return dir;
}


x86 assembly (clang, -O0):
<fsfuse_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1186 <fsfuse_dirname+0x46>
lea    0xe89(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11f8 <fsfuse_dirname+0xb8>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11b3 <fsfuse_dirname+0x73>
lea    0xe5b(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11f8 <fsfuse_dirname+0xb8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f0 <fsfuse_dirname+0xb0>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsfuse_dirname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     118e <fsfuse_dirname+0x4e>
mov    %rax,%rbx
sub    %r14,%rbx
test   %ebx,%ebx
je     1197 <fsfuse_dirname+0x57>
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1185 <fsfuse_dirname+0x45>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe6c(%rip),%rdi
jmp    119e <fsfuse_dirname+0x5e>
lea    0xe62(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strdup@plt>

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
define i8* @fsfuse_dirname(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 dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %23
6: ; preds = %1
%7 = ptrtoint i8* %2 to i64
%8 = ptrtoint i8* %0 to i64
%9 = sub i64 %7, %8
%10 = trunc i64 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %23
14: ; preds = %6
%15 = add i32 %10, 1
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = tail call i32 @memcpy(i8* %17, i8* %0, i32 %10)
%21 = and i64 %9, 4294967295
%22 = getelementptr inbounds i8, i8* %17, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %14, %19, %12, %4
%24 = phi i8* [ %13, %12 ], [ %5, %4 ], [ %17, %19 ], [ %17, %14 ]
ret i8* %24
}