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


x86 assembly (clang, -O0):
<filename_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <filename_unix@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1181 <filename_path+0x41>
movq   $0x0,-0x8(%rbp)
jmp    123a <filename_path+0xfa>
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    121f <filename_path+0xdf>
lea    0xe5c(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    11db <filename_path+0x9b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11ed <filename_path+0xad>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x40(%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)
jmp    1232 <filename_path+0xf2>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filename_path>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <filename_unix@plt>
test   %rax,%rax
je     116c <filename_path+0x2c>
mov    %rax,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1170 <filename_path+0x30>
add    $0x1,%rax
jmp    11b2 <filename_path+0x72>
xor    %ebx,%ebx
jmp    11b5 <filename_path+0x75>
lea    0xe89(%rip),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r14,%rax
ja     1198 <filename_path+0x58>
lea    0xe73(%rip),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r14
lea    0xe61(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
lea    (%r14,%rbx,1),%rax
add    $0xffffffffffffffff,%rax
movb   $0x0,(%rax)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
define i8* @filename_path(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** %5
%11 = load i64, i64* %6
%12 = load i8*, i8** %7
%13 = call i8* @filename_unix(i8* %10, i64 %11, 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 eq i8* %19, null
br i1 %20, label %21, label %46
21: ; preds = %17
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %9
%22 = load i64, i64* %6
%23 = load i8*, i8** %9
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = icmp ult i64 %22, %26
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = load i64, i64* %6
br label %35
30: ; preds = %21
%31 = load i8*, i8** %9
%32 = call i32 @strlen(i8* %31)
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
br label %35
35: ; preds = %30, %28
%36 = phi i64 [ %29, %28 ], [ %34, %30 ]
%37 = sub i64 %36, 1
store i64 %37, i64* %8
%38 = load i8*, i8** %5
%39 = load i8*, i8** %9
%40 = load i64, i64* %8
%41 = add i64 %40, 1
%42 = call i32 @strncpy(i8* %38, i8* %39, i64 %41)
%43 = load i8*, i8** %5
%44 = load i64, i64* %8
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 0, i8* %45
br label %50
46: ; preds = %17
%47 = load i8*, i8** %9
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %9
%49 = load i8*, i8** %9
store i8 0, i8* %49
br label %50
50: ; preds = %46, %35
%51 = load i8*, i8** %5
store i8* %51, i8** %4
br label %52
52: ; preds = %50, %16
%53 = load i8*, i8** %4
ret i8* %53
}

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