#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 *malloc(int);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strstr(char *, char *);
char *get_basename(const char *path) {
  char *basename;
  char *copy;
  int l = 0;
  char *ssc;
  basename = malloc(500);
  strcpy(basename, path);
  while (ssc = strstr(basename, "/")) {
    l = strlen(ssc) + 1;
    copy = basename;
    basename = &basename[strlen(basename) - l + 2];
  }
  if (!strcmp(basename, "")) {
    char *temp = malloc(500);
    size_t length = (strlen(copy) - 1);
    strncpy(temp, copy, length);
    temp[length] = '\0';
    return temp;
  }
  return basename;
}


x86 assembly (clang, -O0):
<get_basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x1f4,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11ef <get_basename+0x8f>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    -0x24(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    118e <get_basename+0x2e>
mov    -0x18(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1252 <get_basename+0xf2>
mov    $0x1f4,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125a <get_basename+0xfa>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_basename>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x1f4,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11d0 <get_basename+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
sub    %ebx,%eax
add    $0x1,%eax
movslq %eax,%rbx
add    %r14,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
jne    1190 <get_basename+0x40>
cmpb   $0x0,(%rbx)
je     11d5 <get_basename+0x85>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
cmpb   $0x0,(%rbx)
jne    11c7 <get_basename+0x77>
mov    $0x1f4,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
movslq %eax,%r15
lea    -0x1(%r15),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r15,1)
jmp    11c7 <get_basename+0x77>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @get_basename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %3
store i32 0, i32* %6
%10 = call i8* @malloc(i32 500)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = load i8*, i8** %3
%13 = call i32 @strcpy(i8* %11, i8* %12)
br label %14
14: ; preds = %18, %1
%15 = load i8*, i8** %4
%16 = call i8* @strstr(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = load i8*, i8** %7
%20 = call i32 @strlen(i8* %19)
%21 = add nsw i32 %20, 1
store i32 %21, i32* %6
%22 = load i8*, i8** %4
store i8* %22, i8** %5
%23 = load i8*, i8** %4
%24 = load i8*, i8** %4
%25 = call i32 @strlen(i8* %24)
%26 = load i32, i32* %6
%27 = sub nsw i32 %25, %26
%28 = add nsw i32 %27, 2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %23, i64 %29
store i8* %30, i8** %4
br label %14
31: ; preds = %14
%32 = load i8*, i8** %4
%33 = call i32 @strcmp(i8* %32, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp ne i32 %33, 0
br i1 %34, label %49, label %35
35: ; preds = %31
%36 = call i8* @malloc(i32 500)
store i8* %36, i8** %8
%37 = load i8*, i8** %5
%38 = call i32 @strlen(i8* %37)
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
store i64 %40, i64* %9
%41 = load i8*, i8** %8
%42 = load i8*, i8** %5
%43 = load i64, i64* %9
%44 = call i32 @strncpy(i8* %41, i8* %42, i64 %43)
%45 = load i8*, i8** %8
%46 = load i64, i64* %9
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %8
store i8* %48, i8** %2
br label %51
49: ; preds = %31
%50 = load i8*, i8** %4
store i8* %50, i8** %2
br label %51
51: ; preds = %49, %35
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
define i8* @get_basename(i8* %0) {
%2 = tail call dereferenceable_or_null(500) i8* @malloc(i32 500)
%3 = tail call i32 @strcpy(i8* %2, i8* %0)
%4 = tail call i8* @strchr(i8* dereferenceable(1) %2, i32 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %1, %6
%7 = phi i8* [ %15, %6 ], [ %4, %1 ]
%8 = phi i8* [ %14, %6 ], [ %2, %1 ]
%9 = tail call i32 @strlen(i8* %7)
%10 = tail call i32 @strlen(i8* %8)
%11 = sub i32 1, %9
%12 = add i32 %11, %10
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %8, i64 %13
%15 = tail call i8* @strchr(i8* dereferenceable(1) %14, i32 47)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %6
17: ; preds = %6, %1
%18 = phi i8* [ undef, %1 ], [ %8, %6 ]
%19 = phi i8* [ %2, %1 ], [ %14, %6 ]
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = tail call dereferenceable_or_null(500) i8* @malloc(i32 500)
%24 = tail call i32 @strlen(i8* %18)
%25 = add nsw i32 %24, -1
%26 = sext i32 %25 to i64
%27 = tail call i32 @strncpy(i8* %23, i8* %18, i64 %26)
%28 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 0, i8* %28
br label %29
29: ; preds = %17, %22
%30 = phi i8* [ %23, %22 ], [ %19, %17 ]
ret i8* %30
}