#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 /*<<< orphan*/ malloc(int);
char *memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
char *strcat(char *, char const *);
char *strdup(char const *);
int strlen(char const *);
char *ending_slash(const char *src) {
  int len;
  if (!src)
    return NULL;
  len = strlen(src);
  if (src[len - 1] == '/') {
    return strdup(src);
  } else {
    return strcat(strcat(memset(malloc((len + 2) * sizeof(char)), 0,
                                (len + 2) * sizeof(char)),
                         src),
                  "/");
  }
}


x86 assembly (clang, -O0):
<ending_slash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <ending_slash+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11f5 <ending_slash+0xa5>
mov    -0x10(%rbp),%rdi
call   1030 <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    11ac <ending_slash+0x5c>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11f5 <ending_slash+0xa5>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,%rdi
lea    0xe14(%rip),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ending_slash>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1178 <ending_slash+0x28>
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ebp
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    117d <ending_slash+0x2d>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <strdup@plt>
xor    %r14d,%r14d
jmp    11af <ending_slash+0x5f>
add    $0x2,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
movw   $0x2f,(%r14,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i8* @ending_slash(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i8* null, i8** %2
br label %38
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %4
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = sub nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 47
br i1 %18, label %19, label %22
19: ; preds = %8
%20 = load i8*, i8** %3
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %2
br label %38
22: ; preds = %8
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 2
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 1
%27 = trunc i64 %26 to i32
%28 = call i32 @malloc(i32 %27)
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 2
%31 = sext i32 %30 to i64
%32 = mul i64 %31, 1
%33 = trunc i64 %32 to i32
%34 = call i8* @memset(i32 %28, i32 0, i32 %33)
%35 = load i8*, i8** %3
%36 = call i8* @strcat(i8* %34, i8* %35)
%37 = call i8* @strcat(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %37, i8** %2
br label %38
38: ; preds = %22, %19, %7
%39 = load i8*, i8** %2
ret i8* %39
}

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