#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 *strrchr(char *, char);
int pathnamed(char *path) {
  char *p;
  p = strrchr(path, '/');
  if (path[strlen(path) - 1] == '/') {
    p--;
    p = strrchr(path, '/');
  }
  if (p == NULL)
    return 0;
  return p - path;
}


x86 assembly (clang, -O0):
<pathnamed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1186 <pathnamed+0x66>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119d <pathnamed+0x7d>
movl   $0x0,-0x4(%rbp)
jmp    11ab <pathnamed+0x8b>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pathnamed>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    1155 <pathnamed+0x35>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,%r14
mov    %r14d,%ecx
sub    %ebx,%ecx
xor    %eax,%eax
test   %r14,%r14
cmovne %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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