#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*/ copyString(char *, char *);
int stringLength(char *);
int concatString(char *dest, int destSize, char *source) {
  int destLengthWithoutTerminator = stringLength(dest) - 1;
  int sourceLength = stringLength(source);
  if (destLengthWithoutTerminator + sourceLength > destSize) {
    return -1;
  }
  char *editDestPtr = dest + destLengthWithoutTerminator;
  copyString(editDestPtr, source);
  return 0;
}


x86 assembly (clang, -O0):
<concatString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <stringLength@plt>
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <stringLength@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1169 <concatString+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    118c <concatString+0x6c>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <copyString@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concatString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
call   1040 <stringLength@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <stringLength@plt>
lea    (%rbx,%rax,1),%ecx
add    $0xffffffff,%ecx
mov    $0xffffffff,%eax
cmp    %ebp,%ecx
jg     1163 <concatString+0x43>
add    $0xffffffff,%ebx
movslq %ebx,%rax
add    %rax,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <copyString@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @concatString(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = call i32 @stringLength(i8* %11)
%13 = sub nsw i32 %12, 1
store i32 %13, i32* %8
%14 = load i8*, i8** %7
%15 = call i32 @stringLength(i8* %14)
store i32 %15, i32* %9
%16 = load i32, i32* %8
%17 = load i32, i32* %9
%18 = add nsw i32 %16, %17
%19 = load i32, i32* %6
%20 = icmp sgt i32 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 -1, i32* %4
br label %30
22: ; preds = %3
%23 = load i8*, i8** %5
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8* %26, i8** %10
%27 = load i8*, i8** %10
%28 = load i8*, i8** %7
%29 = call i32 @copyString(i8* %27, i8* %28)
store i32 0, i32* %4
br label %30
30: ; preds = %22, %21
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
define i32 @concatString(i8* %0, i32 %1, i8* %2) {
%4 = tail call i32 @stringLength(i8* %0)
%5 = add nsw i32 %4, -1
%6 = tail call i32 @stringLength(i8* %2)
%7 = add nsw i32 %5, %6
%8 = icmp sgt i32 %7, %1
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = sext i32 %5 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i32 @copyString(i8* %11, i8* %2)
br label %13
13: ; preds = %3, %9
%14 = phi i32 [ 0, %9 ], [ -1, %3 ]
ret i32 %14
}