#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *file_path(const char *filename) {
  int ct = strlen(filename) - 1;
  char sl = '/';
  while (filename[ct] != sl) {
    ct--;
    if (ct == 0)
      return ".";
  }
  char *s = malloc(ct + 1);
  if (s == NULL) {
    fprintf(stderr, "Not enough memory\n");
    exit(1);
  }
  strncpy(s, filename, ct);
  s[ct + 1] = '\0';
  return s;
}


x86 assembly (clang, -O0):
<file_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
movb   $0x2f,-0x15(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movsbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
je     11af <file_path+0x5f>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11aa <file_path+0x5a>
lea    0xe5f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1211 <file_path+0xc1>
jmp    116f <file_path+0x1f>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e8 <file_path+0x98>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    -0x1(%rbx),%eax
cltq
neg    %ebx
add    %r14,%rax
lea    0xe8e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpb   $0x2f,(%rax)
je     1193 <file_path+0x43>
add    $0x1,%ebx
add    $0xffffffffffffffff,%rax
cmp    $0xffffffff,%ebx
jne    1180 <file_path+0x30>
jmp    11bd <file_path+0x6d>
mov    %ebx,%ebp
neg    %ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11cb <file_path+0x7b>
mov    %rax,%r15
not    %ebx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Not enough memory\0A\00"
define i8* @file_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = sub nsw i32 %8, 1
store i32 %9, i32* %4
store i8 47, i8* %5
br label %10
10: ; preds = %26, %1
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = load i8, i8* %5
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %16, %18
br i1 %19, label %20, label %27
20: ; preds = %10
%21 = load i32, i32* %4
%22 = add nsw i32 %21, -1
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %48
26: ; preds = %20
br label %10
27: ; preds = %10
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = load i8*, i8** %6
%39 = load i8*, i8** %3
%40 = load i32, i32* %4
%41 = call i32 @strncpy(i8* %38, i8* %39, i32 %40)
%42 = load i8*, i8** %6
%43 = load i32, i32* %4
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %6
store i8* %47, i8** %2
br label %48
48: ; preds = %37, %25
%49 = load i8*, i8** %2
ret i8* %49
}

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