#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 *ensure_end_slash(char *pathstr) {
  char *cptr = pathstr + strlen(pathstr) - 1;
  if (*pathstr == 0)
    return pathstr;
  if (*cptr++ != '/') {
    *cptr++ = '/';
    *cptr = 0;
  }
  return cptr;
}


x86 assembly (clang, -O0):
<ensure_end_slash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    115e <ensure_end_slash+0x4e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    119a <ensure_end_slash+0x8a>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1192 <ensure_end_slash+0x82>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x2f,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_end_slash>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmpb   $0x0,(%rbx)
je     1139 <ensure_end_slash+0x29>
cltq
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
lea    0x1(%rax),%rbx
cmpb   $0x2f,(%rax)
je     1139 <ensure_end_slash+0x29>
movw   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @ensure_end_slash(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = sext i32 %2 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = getelementptr inbounds i8, i8* %4, i64 -1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = getelementptr inbounds i8, i8* %5, i64 1
%10 = load i8, i8* %5
%11 = icmp eq i8 %10, 47
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %9, i64 1
store i8 47, i8* %9
store i8 0, i8* %13
br label %14
14: ; preds = %8, %12, %1
%15 = phi i8* [ %0, %1 ], [ %13, %12 ], [ %9, %8 ]
ret i8* %15
}