#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 *calloc(size_t, int);
size_t str_last_index(char const *, char);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *str_get_file(const char *ptr) {
  char *a;
  size_t ptr_sz = strlen(ptr);
  size_t index;
  if (!(index = str_last_index(ptr, '/'))) {
    return NULL;
  }
  if (!(a = calloc((ptr_sz - index) + 1, sizeof(char)))) {
    return NULL;
  }
  strncpy(a, &ptr[index], ptr_sz - index);
  return a;
}


x86 assembly (clang, -O0):
<str_get_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <str_last_index@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1182 <str_get_file+0x42>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <str_get_file+0x94>
mov    -0x20(%rbp),%rdi
sub    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b3 <str_get_file+0x73>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <str_get_file+0x94>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_get_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <str_last_index@plt>
test   %rax,%rax
je     1194 <str_get_file+0x54>
mov    %rax,%r12
sub    %rax,%r14
lea    0x1(%r14),%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     1194 <str_get_file+0x54>
mov    %rax,%r15
add    %r12,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
jmp    1197 <str_get_file+0x57>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_get_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %5
%9 = load i8*, i8** %3
%10 = call i64 @str_last_index(i8* %9, i8 signext 47)
store i64 %10, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %31
13: ; preds = %1
%14 = load i64, i64* %5
%15 = load i64, i64* %6
%16 = sub i64 %14, %15
%17 = add i64 %16, 1
%18 = call i8* @calloc(i64 %17, i32 1)
store i8* %18, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %13
store i8* null, i8** %2
br label %31
21: ; preds = %13
%22 = load i8*, i8** %4
%23 = load i8*, i8** %3
%24 = load i64, i64* %6
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = load i64, i64* %5
%27 = load i64, i64* %6
%28 = sub i64 %26, %27
%29 = call i32 @strncpy(i8* %22, i8* %25, i64 %28)
%30 = load i8*, i8** %4
store i8* %30, i8** %2
br label %31
31: ; preds = %21, %20, %12
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
define i8* @str_get_file(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = tail call i64 @str_last_index(i8* %0, i8 signext 47)
%4 = icmp eq i64 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %1
%6 = sub i64 %2, %3
%7 = add i64 %6, 1
%8 = tail call i8* @calloc(i64 %7, i32 1)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %0, i64 %3
%12 = tail call i32 @strncpy(i8* %8, i8* %11, i64 %6)
br label %13
13: ; preds = %5, %1, %10
%14 = phi i8* [ %8, %10 ], [ null, %1 ], [ null, %5 ]
ret i8* %14
}