#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(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strrchr(char const *, char);
char *getDirectory(const char *path) {
  char *root = 0;
  const char *lastSlash = strrchr(path, '/');
  if (lastSlash != 0) {
    root = (char *)malloc(lastSlash - path + 2);
    memcpy(root, path, lastSlash - path + 1);
    root[lastSlash - path + 1] = '\0';
  }
  return root;
}


x86 assembly (clang, -O0):
<getDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ad <getDirectory+0x7d>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getDirectory>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     116f <getDirectory+0x3f>
mov    %rax,%rbx
sub    %r14,%rbx
lea    0x2(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
lea    0x1(%rbx),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
movb   $0x0,0x1(%r15,%rbx,1)
jmp    1172 <getDirectory+0x42>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @getDirectory(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
%5 = load i8*, i8** %2
%6 = call i8* @strrchr(i8* %5, i8 signext 47)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %37
9: ; preds = %1
%10 = load i8*, i8** %4
%11 = load i8*, i8** %2
%12 = ptrtoint i8* %10 to i64
%13 = ptrtoint i8* %11 to i64
%14 = sub i64 %12, %13
%15 = add nsw i64 %14, 2
%16 = trunc i64 %15 to i32
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = load i8*, i8** %2
%21 = load i8*, i8** %4
%22 = load i8*, i8** %2
%23 = ptrtoint i8* %21 to i64
%24 = ptrtoint i8* %22 to i64
%25 = sub i64 %23, %24
%26 = add nsw i64 %25, 1
%27 = trunc i64 %26 to i32
%28 = call i32 @memcpy(i8* %19, i8* %20, i32 %27)
%29 = load i8*, i8** %3
%30 = load i8*, i8** %4
%31 = load i8*, i8** %2
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = add nsw i64 %34, 1
%36 = getelementptr inbounds i8, i8* %29, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %9, %1
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
define i8* @getDirectory(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
%8 = trunc i64 %7 to i32
%9 = add i32 %8, 2
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = add nsw i64 %7, 1
%13 = trunc i64 %12 to i32
%14 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %13)
%15 = getelementptr inbounds i8, i8* %11, i64 %12
store i8 0, i8* %15
br label %16
16: ; preds = %4, %1
%17 = phi i8* [ %11, %4 ], [ null, %1 ]
ret i8* %17
}