#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 */
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strrchr(char const *, char);
char *get_filename(const char *path) {
  char *result = NULL;
  char *delpos = NULL;
  delpos = strrchr(path, '/');
  if (delpos == NULL) {
    if ((result = (char *)malloc(strlen(path) + 1)) != NULL) {
      strcpy(result, path);
    }
    return result;
  }
  if ((result = (char *)malloc(strlen(delpos) + 1)) != NULL) {
    strcpy(result, delpos + 1);
  }
  return result;
}


x86 assembly (clang, -O0):
<get_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b6 <get_filename+0x76>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11a9 <get_filename+0x69>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f2 <get_filename+0xb2>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11ea <get_filename+0xaa>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filename>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     117f <get_filename+0x3f>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11a3 <get_filename+0x63>
add    $0x1,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
jmp    119e <get_filename+0x5e>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11a3 <get_filename+0x63>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @get_filename(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 %12
4: ; preds = %1
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add nsw i64 %5, 1
%7 = tail call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %21, label %10
10: ; preds = %4
%11 = tail call i32 @strcpy(i8* %8, i8* %0)
br label %21
12: ; preds = %1
%13 = tail call i64 @strlen(i8* %2)
%14 = add nsw i64 %13, 1
%15 = tail call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %2, i64 1
%20 = tail call i32 @strcpy(i8* %16, i8* %19)
br label %21
21: ; preds = %12, %18, %4, %10
%22 = phi i8* [ %8, %10 ], [ %8, %4 ], [ %16, %18 ], [ %16, %12 ]
ret i8* %22
}