#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);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *savePath(char *path) {
  int len = strlen(path);
  if (path[len - 1] == '/') {
    return strdup(path);
  } else {
    char *result = ((char *)malloc(sizeof(char) * (len + 2)));
    strncpy(result, path, len + 1);
    result[len] = '/';
    result[len + 1] = '\0';
    return result;
  }
}


x86 assembly (clang, -O0):
<savePath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1184 <savePath+0x44>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11d3 <savePath+0x93>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<savePath>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
movslq %eax,%r15
cmpb   $0x2f,-0x1(%r15,%rbx,1)
jne    1165 <savePath+0x25>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strdup@plt>
lea    0x2(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0x1(%r15),%edx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
movw   $0x2f,(%r14,%r15,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @savePath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %4
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %8, 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 %19
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %2
br label %42
19: ; preds = %1
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 2
%22 = sext i32 %21 to i64
%23 = mul i64 1, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = load i8*, i8** %3
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
%31 = call i32 @strncpy(i8* %27, i8* %28, i32 %30)
%32 = load i8*, i8** %5
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 47, i8* %35
%36 = load i8*, i8** %5
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %5
store i8* %41, i8** %2
br label %42
42: ; preds = %19, %16
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
define i8* @savePath(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 47
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i8* @strdup(i8* %0)
br label %20
10: ; preds = %1
%11 = add nsw i32 %2, 2
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = add nsw i32 %2, 1
%15 = tail call i32 @strncpy(i8* %13, i8* %0, i32 %14)
%16 = sext i32 %2 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8 47, i8* %17
%18 = sext i32 %14 to i64
%19 = getelementptr inbounds i8, i8* %13, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %10, %8
%21 = phi i8* [ %9, %8 ], [ %13, %10 ]
ret i8* %21
}