#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 */
size_t strlen(char *);
char *path_final_slash(char *path) {
  size_t path_len = strlen(path);
  char *c;
  c = path + path_len - 1;
  while (*c == '/') {
    *c-- = '\0';
    path_len--;
  }
  while (path_len > 0 && *c != '/') {
    c--;
    path_len--;
  }
  if (path_len > 0) {
    return c;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<path_final_slash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    116c <path_final_slash+0x5c>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1139 <path_final_slash+0x29>
jmp    1171 <path_final_slash+0x61>
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jbe    1191 <path_final_slash+0x81>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11a1 <path_final_slash+0x91>
jmp    11be <path_final_slash+0xae>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1171 <path_final_slash+0x61>
cmpq   $0x0,-0x18(%rbp)
jbe    11d6 <path_final_slash+0xc6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11de <path_final_slash+0xce>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_final_slash>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    -0x1(%rbx,%rax,1),%cl
cmp    $0x2f,%cl
jne    1143 <path_final_slash+0x33>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x0,-0x1(%rbx,%rax,1)
movzbl -0x2(%rbx,%rax,1),%ecx
add    $0xffffffffffffffff,%rax
cmp    $0x2f,%cl
je     1130 <path_final_slash+0x20>
lea    (%rbx,%rax,1),%rdx
add    $0xffffffffffffffff,%rdx
test   %rax,%rax
je     1175 <path_final_slash+0x65>
cmp    $0x2f,%cl
jne    115a <path_final_slash+0x4a>
mov    %rdx,%rcx
jmp    1177 <path_final_slash+0x67>
add    $0xffffffffffffffff,%rax
xchg   %ax,%ax
sub    $0x1,%rax
jb     1175 <path_final_slash+0x65>
lea    -0x1(%rdx),%rcx
cmpb   $0x2f,-0x1(%rdx)
mov    %rcx,%rdx
jne    1160 <path_final_slash+0x50>
jmp    1177 <path_final_slash+0x67>
xor    %ecx,%ecx
mov    %rcx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @path_final_slash(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %4
%8 = load i8*, i8** %3
%9 = load i64, i64* %4
%10 = getelementptr inbounds i8, i8* %8, i64 %9
%11 = getelementptr inbounds i8, i8* %10, i64 -1
store i8* %11, i8** %5
br label %12
12: ; preds = %17, %1
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 47
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i32 -1
store i8* %19, i8** %5
store i8 0, i8* %18
%20 = load i64, i64* %4
%21 = add i64 %20, -1
store i64 %21, i64* %4
br label %12
22: ; preds = %12
br label %23
23: ; preds = %33, %22
%24 = load i64, i64* %4
%25 = icmp ugt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i8*, i8** %5
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 47
br label %31
31: ; preds = %26, %23
%32 = phi i1 [ false, %23 ], [ %30, %26 ]
br i1 %32, label %33, label %38
33: ; preds = %31
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i32 -1
store i8* %35, i8** %5
%36 = load i64, i64* %4
%37 = add i64 %36, -1
store i64 %37, i64* %4
br label %23
38: ; preds = %31
%39 = load i64, i64* %4
%40 = icmp ugt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load i8*, i8** %5
store i8* %42, i8** %2
br label %44
43: ; preds = %38
store i8* null, i8** %2
br label %44
44: ; preds = %43, %41
%45 = load i8*, i8** %2
ret i8* %45
}

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