#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 *filename_unix(char *, int, char const *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strrchr(char *, char);
char *filename_name(char *buffer, size_t buflen, const char *pattern) {
  size_t len;
  char *str, Temp[260];
  str = filename_unix(Temp, 260, pattern);
  if (str == (char *)NULL)
    return ((char *)NULL);
  if ((str = strrchr(str, '/')) != (char *)NULL) {
    str++;
  } else {
    str = Temp;
  }
  len = ((buflen) < (strlen(str) + 1) ? (buflen) : (strlen(str) + 1)) - 1;
  (void)strncpy(buffer, str, len + 1);
  buffer[len] = '\0';
  return (buffer);
}


x86 assembly (clang, -O0):
<filename_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x104,%esi
call   1060 <filename_unix@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1188 <filename_name+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1249 <filename_name+0x109>
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11b5 <filename_name+0x75>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11c0 <filename_name+0x80>
lea    -0x140(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x148(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    11fc <filename_name+0xbc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x150(%rbp)
jmp    1211 <filename_name+0xd1>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x150(%rbp)
mov    -0x150(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filename_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x104,%esi
call   1060 <filename_unix@plt>
test   %rax,%rax
je     11b7 <filename_name+0x77>
mov    %rax,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %r12,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r15,%rax
ja     11a1 <filename_name+0x61>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r15,%r14,1)
jmp    11ba <filename_name+0x7a>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @filename_name(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca [260 x i8]
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [260 x i8], [260 x i8]* %10, i64 0, i64 0
%12 = load i8*, i8** %7
%13 = call i8* @filename_unix(i8* %11, i32 260, i8* %12)
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i8* null, i8** %4
br label %52
17: ; preds = %3
%18 = load i8*, i8** %9
%19 = call i8* @strrchr(i8* %18, i8 signext 47)
store i8* %19, i8** %9
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i8*, i8** %9
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %9
br label %26
24: ; preds = %17
%25 = getelementptr inbounds [260 x i8], [260 x i8]* %10, i64 0, i64 0
store i8* %25, i8** %9
br label %26
26: ; preds = %24, %21
%27 = load i64, i64* %6
%28 = load i8*, i8** %9
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = icmp ult i64 %27, %31
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = load i64, i64* %6
br label %40
35: ; preds = %26
%36 = load i8*, i8** %9
%37 = call i32 @strlen(i8* %36)
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
br label %40
40: ; preds = %35, %33
%41 = phi i64 [ %34, %33 ], [ %39, %35 ]
%42 = sub i64 %41, 1
store i64 %42, i64* %8
%43 = load i8*, i8** %5
%44 = load i8*, i8** %9
%45 = load i64, i64* %8
%46 = add i64 %45, 1
%47 = call i32 @strncpy(i8* %43, i8* %44, i64 %46)
%48 = load i8*, i8** %5
%49 = load i64, i64* %8
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %5
store i8* %51, i8** %4
br label %52
52: ; preds = %40, %16
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
define i8* @filename_name(i8* %0, i64 %1, i8* %2) {
%4 = alloca [260 x i8]
%5 = getelementptr inbounds [260 x i8], [260 x i8]* %4, i64 0, i64 0
%6 = call i8* @filename_unix(i8* %5, i32 260, i8* %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %26, label %8
8: ; preds = %3
%9 = call i8* @strrchr(i8* %6, i8 signext 47)
%10 = icmp eq i8* %9, null
%11 = getelementptr inbounds i8, i8* %9, i64 1
%12 = select i1 %10, i8* %5, i8* %11
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = icmp ugt i64 %15, %1
br i1 %16, label %21, label %17
17: ; preds = %8
%18 = call i32 @strlen(i8* %12)
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
br label %21
21: ; preds = %8, %17
%22 = phi i64 [ %20, %17 ], [ %1, %8 ]
%23 = add i64 %22, -1
%24 = call i32 @strncpy(i8* %0, i8* %12, i64 %22)
%25 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %25
br label %26
26: ; preds = %3, %21
%27 = phi i8* [ %0, %21 ], [ null, %3 ]
ret i8* %27
}