#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*/ ENOMEM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *_add_slash(char *s) {
  char *ns = NULL;
  size_t l;
  if (!s)
    return NULL;
  l = strlen(s);
  if (s[l - 1] == '/')
    return s;
  if (ns = malloc(l + 2), !ns) {
    errno = ENOMEM;
    goto done;
  }
  memcpy(ns, s, l);
  strcpy(ns + l, "/");
done:
  free(s);
  return ns;
}


x86 assembly (clang, -O0):
<_add_slash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117c <_add_slash+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    1224 <_add_slash+0xd4>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11af <_add_slash+0x5f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1224 <_add_slash+0xd4>
mov    -0x20(%rbp),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d7 <_add_slash+0x87>
jmp    11ee <_add_slash+0x9e>
mov    0x2e02(%rip),%rax
mov    (%rax),%ecx
mov    0x2df1(%rip),%rax
mov    %ecx,(%rax)
jmp    1213 <_add_slash+0xc3>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rdi
add    -0x20(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_add_slash>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     116e <_add_slash+0x1e>
mov    %rdi,%rbx
call   1050 <strlen@plt>
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    1173 <_add_slash+0x23>
mov    %rbx,%r14
jmp    11c3 <_add_slash+0x73>
xor    %r14d,%r14d
jmp    11c3 <_add_slash+0x73>
mov    %rax,%r15
lea    0x2(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11a9 <_add_slash+0x59>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <memcpy@plt>
mov    %r14,%rdi
add    %r15,%rdi
lea    0xe5e(%rip),%rsi
call   1040 <strcpy@plt>
jmp    11bb <_add_slash+0x6b>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    0x2e1f(%rip),%rcx
mov    %eax,(%rcx)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"/\00"
define i8* @_add_slash(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i8* null, i8** %2
br label %43
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %5
%12 = load i8*, i8** %3
%13 = load i64, i64* %5
%14 = sub i64 %13, 1
%15 = getelementptr inbounds i8, i8* %12, 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 %21
19: ; preds = %9
%20 = load i8*, i8** %3
store i8* %20, i8** %2
br label %43
21: ; preds = %9
%22 = load i64, i64* %5
%23 = add i64 %22, 2
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = icmp ne i8* %25, null
%27 = xor i1 %26, true
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = load i32, i32* @ENOMEM
store i32 %29, i32* @errno
br label %39
30: ; preds = %21
%31 = load i8*, i8** %4
%32 = load i8*, i8** %3
%33 = load i64, i64* %5
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i8*, i8** %4
%36 = load i64, i64* %5
%37 = getelementptr inbounds i8, i8* %35, i64 %36
%38 = call i32 @strcpy(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %30, %28
%40 = load i8*, i8** %3
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %4
store i8* %42, i8** %2
br label %43
43: ; preds = %39, %19, %8
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"/\00"
define i8* @_add_slash(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %21, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = add i64 %4, -1
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 47
br i1 %8, label %21, label %9
9: ; preds = %3
%10 = add i64 %4, 2
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load i32, i32* @ENOMEM
store i32 %14, i32* @errno
br label %19
15: ; preds = %9
%16 = tail call i32 @memcpy(i8* %11, i8* %0, i64 %4)
%17 = getelementptr inbounds i8, i8* %11, i64 %4
%18 = tail call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %15, %13
%20 = tail call i32 @free(i8* %0)
br label %21
21: ; preds = %3, %1, %19
%22 = phi i8* [ %11, %19 ], [ null, %1 ], [ %0, %3 ]
ret i8* %22
}