#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 *lisys_malloc(int);
char *private_strdup(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char const *, char);
char *lisys_path_pathname(const char *path) {
  char *ret;
  const char *ptr;
  ptr = strrchr(path, '/');
  if (ptr == NULL)
    return private_strdup(".");
  if (ptr == path)
    return private_strdup("/");
  ret = lisys_malloc(ptr - path + 1);
  if (ret == NULL)
    return NULL;
  strncpy(ret, path, ptr - path);
  ret[ptr - path] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<lisys_path_pathname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117e <lisys_path_pathname+0x3e>
lea    0xe90(%rip),%rdi
call   1040 <private_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1208 <lisys_path_pathname+0xc8>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11a1 <lisys_path_pathname+0x61>
lea    0xe6f(%rip),%rdi
call   1040 <private_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1208 <lisys_path_pathname+0xc8>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <lisys_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d3 <lisys_path_pathname+0x93>
movq   $0x0,-0x8(%rbp)
jmp    1208 <lisys_path_pathname+0xc8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lisys_path_pathname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1183 <lisys_path_pathname+0x43>
mov    %rax,%rbx
sub    %r14,%rbx
je     118c <lisys_path_pathname+0x4c>
lea    0x1(%rbx),%edi
call   1060 <lisys_malloc@plt>
test   %rax,%rax
je     119d <lisys_path_pathname+0x5d>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    11a0 <lisys_path_pathname+0x60>
lea    0xe76(%rip),%rdi
jmp    1193 <lisys_path_pathname+0x53>
lea    0xe6f(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <private_strdup@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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