#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*/ enough_mem(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *);
char *strdup(char *);
char *strrchr(char const *, char);
char *extract_path(const char *pathfile) {
  const char *last_slash = strrchr(pathfile, '/');
  char *path;
  size_t size;
  if (!last_slash) {
    return strdup(".");
  }
  size = last_slash - pathfile + 1 + 1;
  path = (char *)malloc(size);
  enough_mem(path);
  snprintf(path, size, "%s", pathfile);
  return path;
}


x86 assembly (clang, -O0):
<extract_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118e <extract_path+0x3e>
lea    0xe80(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11db <extract_path+0x8b>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <enough_mem@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0xe34(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_path>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     11a2 <extract_path+0x52>
mov    %rax,%rbx
sub    %r14,%rbx
add    $0x2,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <enough_mem@plt>
lea    0xe77(%rip),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe57(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"%s\00"
define i8* @extract_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strrchr(i8* %7, i8 signext 47)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %2
br label %31
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = load i8*, i8** %3
%16 = ptrtoint i8* %14 to i64
%17 = ptrtoint i8* %15 to i64
%18 = sub i64 %16, %17
%19 = add nsw i64 %18, 1
%20 = add nsw i64 %19, 1
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = call i32 @enough_mem(i8* %24)
%26 = load i8*, i8** %5
%27 = load i64, i64* %6
%28 = load i8*, i8** %3
%29 = call i32 @snprintf(i8* %26, i64 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %5
store i8* %30, i8** %2
br label %31
31: ; preds = %13, %11
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"%s\00"
define i8* @extract_path(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %15
6: ; preds = %1
%7 = ptrtoint i8* %2 to i64
%8 = ptrtoint i8* %0 to i64
%9 = sub i64 %7, %8
%10 = add nsw i64 %9, 2
%11 = tail call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @enough_mem(i8* %12)
%14 = tail call i32 @snprintf(i8* %12, i64 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %6, %4
%16 = phi i8* [ %12, %6 ], [ %5, %4 ]
ret i8* %16
}