#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
char *strrchr(char *, char);
int mprGetDirName(char *buf, int bufsize, char *path) {
  char *cp;
  int dlen;
  cp = strrchr(path, '/');
  if (cp == 0) {
    {
      buf[0] = '\0';
      return 0;
    }
  }
  if (cp == path && cp[1] == '\0') {
    strcpy(buf, ".");
    return 0;
  }
  dlen = cp - path;
  if (dlen < bufsize) {
    if (dlen == 0) {
      dlen++;
    }
    memcpy(buf, path, dlen);
    buf[dlen] = '\0';
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<mprGetDirName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1173 <mprGetDirName+0x43>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    120a <mprGetDirName+0xda>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11ae <mprGetDirName+0x7e>
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    11ae <mprGetDirName+0x7e>
mov    -0x10(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    120a <mprGetDirName+0xda>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1203 <mprGetDirName+0xd3>
cmpl   $0x0,-0x2c(%rbp)
jne    11db <mprGetDirName+0xab>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    120a <mprGetDirName+0xda>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mprGetDirName>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rdx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1185 <mprGetDirName+0x55>
cmp    %rbx,%rax
jne    1159 <mprGetDirName+0x29>
cmpb   $0x0,0x1(%rbx)
je     118b <mprGetDirName+0x5b>
sub    %ebx,%eax
mov    $0xffffffff,%ecx
cmp    %ebp,%eax
jge    119c <mprGetDirName+0x6c>
test   %eax,%eax
mov    $0x1,%ebp
cmovne %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
jmp    119a <mprGetDirName+0x6a>
movb   $0x0,(%r14)
jmp    119a <mprGetDirName+0x6a>
lea    0xe6e(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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