#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*/ BG_AnimParseError(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *BG_CopyStringIntoBuffer(char *string, char *buffer, unsigned int bufSize,
                              unsigned int *offset) {
  char *pch;
  if (*offset + strlen(string) + 1 >= bufSize) {
    BG_AnimParseError("BG_CopyStringIntoBuffer: out of buffer space");
  }
  pch = &buffer[*offset];
  strcpy(pch, string);
  *offset += strlen(string) + 1;
  return pch;
}


x86 assembly (clang, -O0):
<BG_CopyStringIntoBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jb     1178 <BG_CopyStringIntoBuffer+0x48>
lea    0xe8d(%rip),%rdi
call   1050 <BG_AnimParseError@plt>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BG_CopyStringIntoBuffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r15
mov    (%rcx),%ebp
call   1040 <strlen@plt>
add    %ebp,%eax
add    $0x1,%eax
cmp    %r12d,%eax
jb     1161 <BG_CopyStringIntoBuffer+0x31>
lea    0xea4(%rip),%rdi
call   1050 <BG_AnimParseError@plt>
mov    (%r14),%eax
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    (%r14),%ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [45 x i8] c"BG_CopyStringIntoBuffer: out of buffer space\00"
define i8* @BG_CopyStringIntoBuffer(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%10 = load i32*, i32** %8
%11 = load i32, i32* %10
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
%14 = add i32 %11, %13
%15 = add i32 %14, 1
%16 = load i32, i32* %7
%17 = icmp uge i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 @BG_AnimParseError(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %4
%21 = load i8*, i8** %6
%22 = load i32*, i32** %8
%23 = load i32, i32* %22
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8* %25, i8** %9
%26 = load i8*, i8** %9
%27 = load i8*, i8** %5
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %5
%30 = call i32 @strlen(i8* %29)
%31 = add nsw i32 %30, 1
%32 = load i32*, i32** %8
%33 = load i32, i32* %32
%34 = add i32 %33, %31
store i32 %34, i32* %32
%35 = load i8*, i8** %9
ret i8* %35
}

LLVM IR (-O2):
@.str = [45 x i8] c"BG_CopyStringIntoBuffer: out of buffer space\00"
define i8* @BG_CopyStringIntoBuffer(i8* %0, i8* %1, i32 %2, i32* nocapture %3) {
%5 = load i32, i32* %3
%6 = tail call i32 @strlen(i8* %0)
%7 = add i32 %5, 1
%8 = add i32 %7, %6
%9 = icmp ult i32 %8, %2
br i1 %9, label %12, label %10
10: ; preds = %4
%11 = tail call i32 @BG_AnimParseError(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %4
%13 = load i32, i32* %3
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = tail call i32 @strcpy(i8* %15, i8* %0)
%17 = tail call i32 @strlen(i8* %0)
%18 = add nsw i32 %17, 1
%19 = load i32, i32* %3
%20 = add i32 %18, %19
store i32 %20, i32* %3
ret i8* %15
}