#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(int);
int /*<<< orphan*/ strcpy(char *, char *);
char *strrchr(char *, char);
char *without_dir(char *filepath) {
  char *filename = (strrchr(filepath, '/')) + 1;
  char *c = filename;
  int requiredSize = 1;
  while (*c++)
    requiredSize++;
  char *result = (char *)malloc(requiredSize * sizeof(char));
  strcpy(result, filename);
  return result;
}


x86 assembly (clang, -O0):
<without_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
cmpb   $0x0,(%rax)
je     1187 <without_dir+0x57>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1161 <without_dir+0x31>
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<without_dir>:
push   %r14
push   %rbx
push   %rax
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%r14
cmpb   $0x0,0x1(%rax)
je     115f <without_dir+0x2f>
mov    $0x2,%edi
nopl   (%rax)
cmpb   $0x0,(%rax,%rdi,1)
lea    0x1(%rdi),%rdi
jne    1150 <without_dir+0x20>
add    $0xffffffff,%edi
jmp    1164 <without_dir+0x34>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @without_dir(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = getelementptr inbounds i8, i8* %2, i64 1
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %1, %6
%7 = phi i8* [ %9, %6 ], [ %3, %1 ]
%8 = phi i32 [ %10, %6 ], [ 1, %1 ]
%9 = getelementptr inbounds i8, i8* %7, i64 1
%10 = add nuw nsw i32 %8, 1
%11 = load i8, i8* %9
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %6
13: ; preds = %6, %1
%14 = phi i32 [ 1, %1 ], [ %10, %6 ]
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32 @strcpy(i8* %16, i8* %3)
ret i8* %16
}