#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*/ E2BIG;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *strrchr(char const *, char);
int c0x_getfilename(char *file, size_t max, const char *path) {
  const char *cp;
  size_t len;
  cp = strrchr(path, '/');
  cp = (cp == NULL ? path : cp + 1);
  len = strlen(cp);
  if (len >= max) {
    errno = E2BIG;
    return (-1);
  }
  memcpy(file, cp, len);
  file[len] = '\0';
  return (len);
}


x86 assembly (clang, -O0):
<c0x_getfilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%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    116e <c0x_getfilename+0x3e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    117a <c0x_getfilename+0x4a>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11bb <c0x_getfilename+0x8b>
mov    0x2e44(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <c0x_getfilename+0xaf>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c0x_getfilename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rdx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %r15,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %r12,%rax
jae    117e <c0x_getfilename+0x4e>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r14,%r15,1)
jmp    1196 <c0x_getfilename+0x66>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    0x2e4a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@E2BIG = global i32 0
@errno = global i32 0
define i32 @c0x_getfilename(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %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 %16
14: ; preds = %3
%15 = load i8*, i8** %7
br label %19
16: ; preds = %3
%17 = load i8*, i8** %8
%18 = getelementptr inbounds i8, i8* %17, i64 1
br label %19
19: ; preds = %16, %14
%20 = phi i8* [ %15, %14 ], [ %18, %16 ]
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = call i64 @strlen(i8* %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = load i64, i64* %6
%25 = icmp uge i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = load i32, i32* @E2BIG
store i32 %27, i32* @errno
store i32 -1, i32* %4
br label %38
28: ; preds = %19
%29 = load i8*, i8** %5
%30 = load i8*, i8** %8
%31 = load i64, i64* %9
%32 = call i32 @memcpy(i8* %29, i8* %30, i64 %31)
%33 = load i8*, i8** %5
%34 = load i64, i64* %9
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i64, i64* %9
%37 = trunc i64 %36 to i32
store i32 %37, i32* %4
br label %38
38: ; preds = %28, %26
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@E2BIG = global i32 0
@errno = global i32 0
define i32 @c0x_getfilename(i8* %0, i64 %1, i8* %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 47)
%5 = icmp eq i8* %4, null
%6 = getelementptr inbounds i8, i8* %4, i64 1
%7 = select i1 %5, i8* %2, i8* %6
%8 = tail call i64 @strlen(i8* dereferenceable(1) %7)
%9 = icmp ult i64 %8, %1
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = load i32, i32* @E2BIG
store i32 %11, i32* @errno
br label %16
12: ; preds = %3
%13 = tail call i32 @memcpy(i8* %0, i8* %7, i64 %8)
%14 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 0, i8* %14
%15 = trunc i64 %8 to i32
br label %16
16: ; preds = %12, %10
%17 = phi i32 [ -1, %10 ], [ %15, %12 ]
ret i32 %17
}