#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*/ PATH_MAX;
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
char *strrchr(char *, char);
char *path2dir(char *path, char *dir) {
  char *p;
  strncpy(dir, path, PATH_MAX);
  p = strrchr(dir, '/');
  if (!p) {
    return dir;
  } else if (p == dir) {
    *(++p) = '\0';
  } else {
    *p = '\0';
  }
  return dir;
}


x86 assembly (clang, -O0):
<path2dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2eb1(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1170 <path2dir+0x50>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11aa <path2dir+0x8a>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1196 <path2dir+0x76>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,0x1(%rax)
jmp    119d <path2dir+0x7d>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    11a2 <path2dir+0x82>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path2dir>:
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rsi
mov    0x2ec2(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1158 <path2dir+0x38>
lea    0x1(%rbx),%rcx
cmp    %rbx,%rax
cmovne %rax,%rcx
movb   $0x0,(%rcx)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
define i8* @path2dir(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @PATH_MAX
%10 = call i32 @strncpy(i8* %7, i8* %8, i32 %9)
%11 = load i8*, i8** %5
%12 = call i8* @strrchr(i8* %11, i8 signext 47)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = load i8*, i8** %5
store i8* %16, i8** %3
br label %29
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = load i8*, i8** %5
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %6
store i8 0, i8* %23
br label %26
24: ; preds = %17
%25 = load i8*, i8** %6
store i8 0, i8* %25
br label %26
26: ; preds = %24, %21
br label %27
27: ; preds = %26
%28 = load i8*, i8** %5
store i8* %28, i8** %3
br label %29
29: ; preds = %27, %15
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
define i8* @path2dir(i8* %0, i8* returned %1) {
%3 = load i32, i32* @PATH_MAX
%4 = tail call i32 @strncpy(i8* %1, i8* %0, i32 %3)
%5 = tail call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 47)
%6 = icmp eq i8* %5, null
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = icmp eq i8* %5, %1
%9 = getelementptr inbounds i8, i8* %1, i64 1
%10 = select i1 %8, i8* %9, i8* %5
store i8 0, i8* %10
br label %11
11: ; preds = %7, %2
ret i8* %1
}