#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 *);
int append_slash(char *buf, int buf_size) {
  int len;
  len = strlen(buf);
  if (len > 0 && buf[len - 1] != '/' && len + 1 < buf_size) {
    buf[len++] = '/';
    buf[len] = '\0';
  }
  return len;
}


x86 assembly (clang, -O0):
<append_slash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1180 <append_slash+0x70>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1180 <append_slash+0x70>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jge    1180 <append_slash+0x70>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x10(%rbp)
movslq %ecx,%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_slash>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    113e <append_slash+0x2e>
lea    -0x1(%rax),%ecx
cmpb   $0x2f,(%rbx,%rcx,1)
je     113e <append_slash+0x2e>
mov    %eax,%edx
lea    0x1(%rdx),%rcx
cmp    %ebp,%ecx
jge    113e <append_slash+0x2e>
movb   $0x2f,(%rbx,%rdx,1)
movb   $0x0,(%rbx,%rcx,1)
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @append_slash(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %34
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = load i32, i32* %5
%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 ne i32 %17, 47
br i1 %18, label %19, label %34
19: ; preds = %10
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 47, i8* %29
%30 = load i8*, i8** %3
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %24, %19, %10, %2
%35 = load i32, i32* %5
ret i32 %35
}

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