#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 */
char *malloc(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *getDirectoryName(char *pathname) {
  char *tempStr = NULL, *directoryPath = NULL;
  int len;
  tempStr = strrchr(pathname, '\\');
  len = tempStr - pathname;
  if (tempStr != NULL) {
    directoryPath = malloc(len + 1);
    strncpy(directoryPath, pathname, len);
    directoryPath[len] = '\0';
  }
  return directoryPath;
}


x86 assembly (clang, -O0):
<getDirectoryName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x5c,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a2 <getDirectoryName+0x72>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

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

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

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