#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*/ memcpy(char *, char *, size_t);
int strlen(char *);
char *basename(char *path) {
  static char result[5000];
  char *p, *lastp;
  size_t len;
  if ((path == NULL) || (*path == '\0')) {
    result[0] = '.';
    result[1] = '\0';
    return (result);
  }
  lastp = path + strlen(path) - 1;
  while (lastp != path && (*lastp == '/'))
    lastp--;
  p = lastp;
  while (p != path && !(*(p - 1) == '/'))
    p--;
  len = (lastp - p) + 1;
  if (len > (5000 - 1))
    len = 5000 - 1;
  memcpy(result, p, len);
  result[len] = '\0';
  return (result);
}


x86 assembly (clang, -O0):
<basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1147 <basename+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1165 <basename+0x45>
movb   $0x2e,0x2ef2(%rip)
movb   $0x0,0x2eec(%rip)
lea    0x2ee4(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1274 <basename+0x154>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x31(%rbp)
je     11ad <basename+0x8d>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11bd <basename+0x9d>
jmp    11ce <basename+0xae>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    118a <basename+0x6a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x32(%rbp)
je     11fc <basename+0xdc>
mov    -0x18(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2f,%eax
sete   %al
xor    $0xff,%al
mov    %al,-0x32(%rbp)
mov    -0x32(%rbp),%al
test   $0x1,%al
jne    120c <basename+0xec>
jmp    121d <basename+0xfd>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11d6 <basename+0xb6>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x1387,-0x28(%rbp)
jbe    1246 <basename+0x126>
movq   $0x1387,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0x2deb(%rip),%rdi
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rcx
lea    0x2ddb(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2dd0(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basename>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     116f <basename+0x4f>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     116f <basename+0x4f>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdx
lea    (%rbx,%rdx,1),%rax
neg    %rdx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0xffffffffffffffff,%rdx
je     117f <basename+0x5f>
lea    -0x1(%rax),%rcx
add    $0x1,%rdx
cmpb   $0x2f,-0x1(%rax)
mov    %rcx,%rax
je     1150 <basename+0x30>
mov    %rbx,%rax
sub    %rdx,%rax
jmp    1185 <basename+0x65>
movb   $0x2e,0x2eca(%rip)
lea    0x2ec4(%rip),%rbx
jmp    11d4 <basename+0xb4>
mov    %rbx,%rax
mov    %rbx,%rcx
mov    %rbx,%rsi
sub    %rax,%rsi
add    %rcx,%rsi
xchg   %ax,%ax
cmp    %rbx,%rcx
je     11a9 <basename+0x89>
lea    -0x1(%rcx),%rdx
cmpb   $0x2f,-0x1(%rcx)
mov    %rdx,%rcx
jne    1190 <basename+0x70>
add    $0x1,%rdx
mov    %rdx,%rsi
sub    %rsi,%rax
add    $0x1,%rax
cmp    $0x1387,%rax
mov    $0x1387,%ebx
cmovb  %rax,%rbx
lea    0x2e7a(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
add    %r14,%rbx
movb   $0x0,(%rbx)
lea    0x2e62(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@basename.result = internal global [5000 x i8] zeroinitializer
define i8* @basename(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 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9, %1
store i8 46, i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 0)
store i8 0, i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 1)
store i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 0), i8** %2
br label %70
15: ; preds = %9
%16 = load i8*, i8** %3
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 -1
store i8* %21, i8** %5
br label %22
22: ; preds = %33, %15
%23 = load i8*, i8** %5
%24 = load i8*, i8** %3
%25 = icmp ne i8* %23, %24
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 47
br label %31
31: ; preds = %26, %22
%32 = phi i1 [ false, %22 ], [ %30, %26 ]
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i32 -1
store i8* %35, i8** %5
br label %22
36: ; preds = %31
%37 = load i8*, i8** %5
store i8* %37, i8** %4
br label %38
38: ; preds = %51, %36
%39 = load i8*, i8** %4
%40 = load i8*, i8** %3
%41 = icmp ne i8* %39, %40
br i1 %41, label %42, label %49
42: ; preds = %38
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i64 -1
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 47
%48 = xor i1 %47, true
br label %49
49: ; preds = %42, %38
%50 = phi i1 [ false, %38 ], [ %48, %42 ]
br i1 %50, label %51, label %54
51: ; preds = %49
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i32 -1
store i8* %53, i8** %4
br label %38
54: ; preds = %49
%55 = load i8*, i8** %5
%56 = load i8*, i8** %4
%57 = ptrtoint i8* %55 to i64
%58 = ptrtoint i8* %56 to i64
%59 = sub i64 %57, %58
%60 = add nsw i64 %59, 1
store i64 %60, i64* %6
%61 = load i64, i64* %6
%62 = icmp ugt i64 %61, 4999
br i1 %62, label %63, label %64
63: ; preds = %54
store i64 4999, i64* %6
br label %64
64: ; preds = %63, %54
%65 = load i8*, i8** %4
%66 = load i64, i64* %6
%67 = call i32 @memcpy(i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 0), i8* %65, i64 %66)
%68 = load i64, i64* %6
%69 = getelementptr inbounds [5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 %68
store i8 0, i8* %69
store i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 0), i8** %2
br label %70
70: ; preds = %64, %14
%71 = load i8*, i8** %2
ret i8* %71
}

LLVM IR (-O2):
@basename.result = internal global [5000 x i8] zeroinitializer
define i8* @basename(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = icmp eq i8* %0, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %4, %1
store i8 46, i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 0)
br label %42
8: ; preds = %4
%9 = tail call i32 @strlen(i8* %0)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
br label %12
12: ; preds = %16, %8
%13 = phi i8* [ %11, %8 ], [ %14, %16 ]
%14 = getelementptr inbounds i8, i8* %13, i64 -1
%15 = icmp eq i8* %14, %0
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i8, i8* %14
%18 = icmp eq i8 %17, 47
br i1 %18, label %12, label %19
19: ; preds = %16
%20 = ptrtoint i8* %14 to i64
br label %21
21: ; preds = %12, %19
%22 = phi i64 [ %20, %19 ], [ %2, %12 ]
%23 = phi i8* [ %14, %19 ], [ %0, %12 ]
%24 = sub i64 %2, %22
%25 = getelementptr i8, i8* %23, i64 %24
br label %26
26: ; preds = %29, %21
%27 = phi i8* [ %23, %21 ], [ %30, %29 ]
%28 = icmp eq i8* %27, %0
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = getelementptr inbounds i8, i8* %27, i64 -1
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 47
br i1 %32, label %33, label %26
33: ; preds = %26, %29
%34 = phi i8* [ %25, %26 ], [ %27, %29 ]
%35 = ptrtoint i8* %34 to i64
%36 = sub i64 %22, %35
%37 = add nsw i64 %36, 1
%38 = icmp ult i64 %37, 4999
%39 = select i1 %38, i64 %37, i64 4999
%40 = tail call i32 @memcpy(i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 0), i8* %34, i64 %39)
%41 = getelementptr inbounds [5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 %39
br label %42
42: ; preds = %33, %7
%43 = phi i8* [ %41, %33 ], [ getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 1), %7 ]
store i8 0, i8* %43
ret i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @basename.result, i64 0, i64 0)
}