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


x86 assembly (clang, -O0):
<str_get_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <str_get_file@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <str_last_index@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    119f <str_get_path+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    11fd <str_get_path+0xad>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
sub    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e0 <str_get_path+0x90>
movq   $0x0,-0x8(%rbp)
jmp    11fd <str_get_path+0xad>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_get_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1060 <str_get_file@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x2f,%esi
call   1070 <str_last_index@plt>
test   %rax,%rax
je     11bb <str_get_path+0x6b>
mov    %rax,%r14
mov    %r12,%rdi
call   1040 <strlen@plt>
sub    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11bb <str_get_path+0x6b>
mov    %rax,%rbx
add    $0xffffffffffffffff,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
jmp    11bd <str_get_path+0x6d>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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