#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 const *, int);
int /*<<< orphan*/ pstrcpy(char *, int, char const *);
char *strrchr(char const *, char);
const char *pathname(char *buf, int buf_size, const char *filename) {
  const char *p;
  int len;
  p = strrchr(filename, '/');
  if (!p) {
    pstrcpy(buf, buf_size, filename);
  } else {
    len = p - filename + 1;
    if (len > buf_size - 1)
      len = buf_size - 1;
    memcpy(buf, filename, len);
    buf[len] = '\0';
  }
  return buf;
}


x86 assembly (clang, -O0):
<pathname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1175 <pathname+0x45>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1030 <pstrcpy@plt>
jmp    11bd <pathname+0x8d>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11a1 <pathname+0x71>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pathname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    %rdx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1178 <pathname+0x48>
sub    %ebx,%eax
add    $0x1,%eax
lea    -0x1(%r15),%ebp
cmp    %r15d,%eax
cmovl  %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
jmp    1186 <pathname+0x56>
mov    %r14,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
call   1030 <pstrcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @pathname(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
%10 = call i8* @strrchr(i8* %9, i8 signext 47)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = call i32 @pstrcpy(i8* %14, i32 %15, i8* %16)
br label %42
18: ; preds = %3
%19 = load i8*, i8** %7
%20 = load i8*, i8** %6
%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
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = load i32, i32* %5
%28 = sub nsw i32 %27, 1
%29 = icmp sgt i32 %26, %28
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = load i32, i32* %5
%32 = sub nsw i32 %31, 1
store i32 %32, i32* %8
br label %33
33: ; preds = %30, %18
%34 = load i8*, i8** %4
%35 = load i8*, i8** %6
%36 = load i32, i32* %8
%37 = call i32 @memcpy(i8* %34, i8* %35, i32 %36)
%38 = load i8*, i8** %4
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %33, %13
%43 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
define i8* @pathname(i8* returned %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 %8
6: ; preds = %3
%7 = tail call i32 @pstrcpy(i8* %0, i32 %1, i8* %2)
br label %20
8: ; preds = %3
%9 = ptrtoint i8* %4 to i64
%10 = ptrtoint i8* %2 to i64
%11 = sub i64 %9, %10
%12 = trunc i64 %11 to i32
%13 = add i32 %12, 1
%14 = icmp slt i32 %13, %1
%15 = add nsw i32 %1, -1
%16 = select i1 %14, i32 %13, i32 %15
%17 = tail call i32 @memcpy(i8* %0, i8* %2, i32 %16)
%18 = sext i32 %16 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %8, %6
ret i8* %0
}