#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__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *end_path_with_separator(const char *path) {
  int i, n;
  char *p;
  i = strlen(path);
  n = i + 1;
  if (path[i - 1] != '/')
    n++;
  p = (char *)malloc(n);
  memcpy(p, path, i);
  if (n > (i + 1))
    p[i++] = '/';
  p[i] = '\0';
  return p;
}


x86 assembly (clang, -O0):
<end_path_with_separator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1174 <end_path_with_separator+0x44>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jle    11b7 <end_path_with_separator+0x87>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<end_path_with_separator>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%r14d
movslq %eax,%r12
mov    %eax,%ebp
add    $0x1,%ebp
lea    0x2(%r12),%ebx
cmpb   $0x2f,-0x1(%r12,%r15,1)
cmove  %ebp,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
cmp    %ebp,%ebx
jle    1181 <end_path_with_separator+0x51>
movb   $0x2f,0x0(%r13,%r12,1)
mov    %ebp,%r14d
movslq %r14d,%rax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @end_path_with_separator(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = add nsw i32 %2, -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
%9 = add nsw i32 %2, 2
%10 = select i1 %8, i32 %3, i32 %9
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @memcpy(i8* %12, i8* %0, i32 %2)
%14 = icmp sgt i32 %10, %3
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = sext i32 %2 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
store i8 47, i8* %17
br label %18
18: ; preds = %15, %1
%19 = phi i32 [ %3, %15 ], [ %2, %1 ]
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %12, i64 %20
store i8 0, i8* %21
ret i8* %12
}