#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 strlen(char *);
char *get_filename(char *path) {
  int last_delim = -1;
  for (int i = 0; i < strlen(path); ++i) {
    if (path[i] == '/')
      last_delim = i;
  }
  char *filename = path + last_delim + 1;
  return filename;
}


x86 assembly (clang, -O0):
<get_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    1174 <get_filename+0x64>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1161 <get_filename+0x51>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1166 <get_filename+0x56>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112a <get_filename+0x1a>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filename>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    1150 <get_filename+0x40>
mov    $0xffffffff,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
cmpb   $0x2f,(%r14,%rbx,1)
cmove  %ebx,%ebp
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1130 <get_filename+0x20>
movslq %ebp,%rax
jmp    1157 <get_filename+0x47>
mov    $0xffffffffffffffff,%rax
add    %r14,%rax
add    $0x1,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @get_filename(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %10, label %6
4: ; preds = %10
%5 = sext i32 %17 to i64
br label %6
6: ; preds = %4, %1
%7 = phi i64 [ -1, %1 ], [ %5, %4 ]
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 1
ret i8* %9
10: ; preds = %1, %10
%11 = phi i64 [ %18, %10 ], [ 0, %1 ]
%12 = phi i32 [ %17, %10 ], [ -1, %1 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %11
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 47
%16 = trunc i64 %11 to i32
%17 = select i1 %15, i32 %16, i32 %12
%18 = add nuw nsw i64 %11, 1
%19 = tail call i32 @strlen(i8* %0)
%20 = sext i32 %19 to i64
%21 = icmp slt i64 %18, %20
br i1 %21, label %10, label %4
}