#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*/ getcwd(char *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, int);
size_t strlen(char const *);
char *strrchr(char const *, int);
char *getpath_edf(char *buffer, size_t buflen, const char *filename) {
  size_t str_len;
  char *ps;
  str_len = strlen(filename);
  if (str_len >= buflen)
    return ((char *)NULL);
  if ((ps = strrchr(filename, (int)'/'))) {
    memcpy(buffer, filename, (ps - filename));
    buffer[(ps - filename)] = '\0';
  } else
    getcwd(buffer, buflen);
  str_len = strlen(buffer);
  if (str_len < buflen) {
    buffer[str_len] = '/';
    buffer[str_len + 1] = '\0';
  } else
    buffer[0] = '\0';
  return (buffer);
}


x86 assembly (clang, -O0):
<getpath_edf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     117c <getpath_edf+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    121f <getpath_edf+0xdf>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11ca <getpath_edf+0x8a>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11d7 <getpath_edf+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <getcwd@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1210 <getpath_edf+0xd0>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
jmp    1217 <getpath_edf+0xd7>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpath_edf>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
call   1030 <strlen@plt>
cmp    %r14,%rax
jae    118b <getpath_edf+0x4b>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1190 <getpath_edf+0x50>
mov    %rax,%r12
sub    %rbx,%r12
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
movb   $0x0,(%r15,%r12,1)
jmp    119b <getpath_edf+0x5b>
xor    %r15d,%r15d
jmp    11bb <getpath_edf+0x7b>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <getcwd@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %r15,%rcx
cmp    %r14,%rax
jae    11b8 <getpath_edf+0x78>
movb   $0x2f,(%r15,%rax,1)
lea    (%r15,%rax,1),%rcx
add    $0x1,%rcx
movb   $0x0,(%rcx)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @getpath_edf(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %7
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %8
%12 = load i64, i64* %8
%13 = load i64, i64* %6
%14 = icmp uge i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %3
store i8* null, i8** %4
br label %60
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = call i8* @strrchr(i8* %17, i32 47)
store i8* %18, i8** %9
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %37
20: ; preds = %16
%21 = load i8*, i8** %5
%22 = load i8*, i8** %7
%23 = load i8*, i8** %9
%24 = load i8*, i8** %7
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
%28 = trunc i64 %27 to i32
%29 = call i32 @memcpy(i8* %21, i8* %22, i32 %28)
%30 = load i8*, i8** %5
%31 = load i8*, i8** %9
%32 = load i8*, i8** %7
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = getelementptr inbounds i8, i8* %30, i64 %35
store i8 0, i8* %36
br label %41
37: ; preds = %16
%38 = load i8*, i8** %5
%39 = load i64, i64* %6
%40 = call i32 @getcwd(i8* %38, i64 %39)
br label %41
41: ; preds = %37, %20
%42 = load i8*, i8** %5
%43 = call i64 @strlen(i8* %42)
store i64 %43, i64* %8
%44 = load i64, i64* %8
%45 = load i64, i64* %6
%46 = icmp ult i64 %44, %45
br i1 %46, label %47, label %55
47: ; preds = %41
%48 = load i8*, i8** %5
%49 = load i64, i64* %8
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 47, i8* %50
%51 = load i8*, i8** %5
%52 = load i64, i64* %8
%53 = add i64 %52, 1
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
br label %58
55: ; preds = %41
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i64 0
store i8 0, i8* %57
br label %58
58: ; preds = %55, %47
%59 = load i8*, i8** %5
store i8* %59, i8** %4
br label %60
60: ; preds = %58, %15
%61 = load i8*, i8** %4
ret i8* %61
}

LLVM IR (-O2):
define i8* @getpath_edf(i8* %0, i64 %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%5 = icmp ult i64 %4, %1
br i1 %5, label %6, label %27
6: ; preds = %3
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %2, i32 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %16, label %9
9: ; preds = %6
%10 = ptrtoint i8* %7 to i64
%11 = ptrtoint i8* %2 to i64
%12 = sub i64 %10, %11
%13 = trunc i64 %12 to i32
%14 = tail call i32 @memcpy(i8* %0, i8* %2, i32 %13)
%15 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %15
br label %18
16: ; preds = %6
%17 = tail call i32 @getcwd(i8* %0, i64 %1)
br label %18
18: ; preds = %16, %9
%19 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%20 = icmp ult i64 %19, %1
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 47, i8* %22
%23 = add nuw i64 %19, 1
%24 = getelementptr inbounds i8, i8* %0, i64 %23
br label %25
25: ; preds = %18, %21
%26 = phi i8* [ %24, %21 ], [ %0, %18 ]
store i8 0, i8* %26
br label %27
27: ; preds = %25, %3
%28 = phi i8* [ null, %3 ], [ %0, %25 ]
ret i8* %28
}