#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__ libc_sch_count(int /*<<< orphan*/, char);
int /*<<< orphan*/ libc_strcpy(char *, char *);
int /*<<< orphan*/ libc_strlen(char *);
char *malloc(int /*<<< orphan*/);
char *libc_filename(char *file) {
  int n = 0;
  while (libc_sch_count(file++, '/') > 0) {
    n++;
  }
  n--;
  file--;
  char *out = malloc(libc_strlen(file));
  libc_strcpy(out, file);
  file = file - n;
  return out;
}


x86 assembly (clang, -O0):
<libc_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    %eax,%edi
mov    $0x2f,%esi
call   1050 <libc_sch_count@plt>
cmp    $0x0,%rax
jle    1186 <libc_filename+0x46>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1153 <libc_filename+0x13>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <libc_strlen@plt>
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <libc_strcpy@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libc_filename>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    -0x1(%rdi),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    $0x2f,%esi
call   1050 <libc_sch_count@plt>
add    $0x1,%r14
add    $0x1,%ebx
test   %rax,%rax
jg     1150 <libc_filename+0x10>
mov    %r14,%rdi
call   1040 <libc_strlen@plt>
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <libc_strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @libc_filename(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i8* [ %0, %1 ], [ %8, %2 ]
%4 = ptrtoint i8* %3 to i64
%5 = trunc i64 %4 to i32
%6 = tail call i64 @libc_sch_count(i32 %5, i8 signext 47)
%7 = icmp sgt i64 %6, 0
%8 = getelementptr inbounds i8, i8* %3, i64 1
br i1 %7, label %2, label %9
9: ; preds = %2
%10 = tail call i32 @libc_strlen(i8* %3)
%11 = tail call i8* @malloc(i32 %10)
%12 = tail call i32 @libc_strcpy(i8* %11, i8* %3)
ret i8* %11
}