#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 */
char *CR_EnsureCapacity(char *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t sSizeAdd(size_t, size_t);
size_t strlen(char const *);
size_t pathBuilderAppend(char **buffer_ptr, size_t length, const char *path) {
  size_t path_length = strlen(path);
  size_t required_capacity = sSizeAdd(sSizeAdd(length, 2), path_length);
  *buffer_ptr = CR_EnsureCapacity(*buffer_ptr, required_capacity);
  (*buffer_ptr)[length] = '/';
  (*buffer_ptr)[required_capacity - 1] = '\0';
  memcpy(&(*buffer_ptr)[length + 1], path, path_length);
  return required_capacity - 1;
}


x86 assembly (clang, -O0):
<pathBuilderAppend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1060 <sSizeAdd@plt>
mov    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1060 <sSizeAdd@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <CR_EnsureCapacity@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
add    $0x1,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pathBuilderAppend>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
mov    $0x2,%esi
mov    %r15,%rdi
call   1060 <sSizeAdd@plt>
mov    %rax,%rdi
mov    %r12,%rsi
call   1060 <sSizeAdd@plt>
mov    %rax,%rbx
mov    0x0(%r13),%rdi
mov    %rax,%rsi
call   1050 <CR_EnsureCapacity@plt>
mov    %rax,0x0(%r13)
movb   $0x2f,(%rax,%r15,1)
mov    0x0(%r13),%rax
movb   $0x0,-0x1(%rax,%rbx,1)
add    $0xffffffffffffffff,%rbx
mov    0x0(%r13),%rax
lea    (%r15,%rax,1),%rdi
add    $0x1,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @pathBuilderAppend(i8** %0, i64 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8** %0, i8*** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %7
%11 = load i64, i64* %5
%12 = call i64 @sSizeAdd(i64 %11, i64 2)
%13 = load i64, i64* %7
%14 = call i64 @sSizeAdd(i64 %12, i64 %13)
store i64 %14, i64* %8
%15 = load i8**, i8*** %4
%16 = load i8*, i8** %15
%17 = load i64, i64* %8
%18 = call i8* @CR_EnsureCapacity(i8* %16, i64 %17)
%19 = load i8**, i8*** %4
store i8* %18, i8** %19
%20 = load i8**, i8*** %4
%21 = load i8*, i8** %20
%22 = load i64, i64* %5
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 47, i8* %23
%24 = load i8**, i8*** %4
%25 = load i8*, i8** %24
%26 = load i64, i64* %8
%27 = sub i64 %26, 1
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = load i64, i64* %5
%32 = add i64 %31, 1
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8*, i8** %6
%35 = load i64, i64* %7
%36 = call i32 @memcpy(i8* %33, i8* %34, i64 %35)
%37 = load i64, i64* %8
%38 = sub i64 %37, 1
ret i64 %38
}

LLVM IR (-O2):
define i64 @pathBuilderAppend(i8** nocapture %0, i64 %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%5 = tail call i64 @sSizeAdd(i64 %1, i64 2)
%6 = tail call i64 @sSizeAdd(i64 %5, i64 %4)
%7 = load i8*, i8** %0
%8 = tail call i8* @CR_EnsureCapacity(i8* %7, i64 %6)
store i8* %8, i8** %0
%9 = getelementptr inbounds i8, i8* %8, i64 %1
store i8 47, i8* %9
%10 = load i8*, i8** %0
%11 = add i64 %6, -1
%12 = getelementptr inbounds i8, i8* %10, i64 %11
store i8 0, i8* %12
%13 = load i8*, i8** %0
%14 = add i64 %1, 1
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = tail call i32 @memcpy(i8* %15, i8* %2, i64 %4)
ret i64 %11
}