char *lua_strconc(char *l, char *r) {
  static char *buffer = 0;
  static int buffer_size = 0;
  int nl = strlen(l);
  int n = nl + strlen(r) + 1;
  if (n > buffer_size) {
    buffer_size = n;
    if (buffer != 0)
      luaI_free(buffer);
    buffer = ((char *)luaI_malloc((buffer_size) * sizeof(char)));
  }
  strcpy(buffer, l);
  strcpy(buffer + nl, r);
  return buffer;
}


x86 assembly (clang, -O0):
<lua_strconc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
add    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    0x2ec9(%rip),%eax
jle    11cd <lua_strconc+0x8d>
mov    -0x18(%rbp),%eax
mov    %eax,0x2eba(%rip)
cmpq   $0x0,0x2eaa(%rip)
je     11b2 <lua_strconc+0x72>
mov    0x2e9d(%rip),%rdi
mov    $0x0,%al
call   1060 <luaI_free@plt>
movslq 0x2e97(%rip),%rdi
shl    $0x0,%rdi
mov    $0x0,%al
call   1050 <luaI_malloc@plt>
cltq
mov    %rax,0x2e7b(%rip)
mov    0x2e74(%rip),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e64(%rip),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e4d(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lua_strconc>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %r12d,%eax
add    $0x1,%eax
cmp    0x2ee5(%rip),%eax
jle    11a2 <lua_strconc+0x62>
mov    %eax,0x2edd(%rip)
mov    0x2ece(%rip),%rdi
test   %rdi,%rdi
je     118c <lua_strconc+0x4c>
xor    %eax,%eax
call   1060 <luaI_free@plt>
mov    0x2ec4(%rip),%eax
movslq %eax,%rdi
xor    %eax,%eax
call   1050 <luaI_malloc@plt>
movslq %eax,%r13
mov    %r13,0x2ea8(%rip)
jmp    11a9 <lua_strconc+0x69>
mov    0x2e9f(%rip),%r13
movslq %r12d,%rbx
mov    %r13,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@lua_strconc.buffer = internal global i8* null
@lua_strconc.buffer_size = internal global i32 0
define i8* @lua_strconc(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
%9 = trunc i64 %8 to i32
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
%14 = add i64 %11, %13
%15 = add i64 %14, 1
%16 = trunc i64 %15 to i32
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = load i32, i32* @lua_strconc.buffer_size
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %34
20: ; preds = %2
%21 = load i32, i32* %6
store i32 %21, i32* @lua_strconc.buffer_size
%22 = load i8*, i8** @lua_strconc.buffer
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i8*, i8** @lua_strconc.buffer
%26 = call i32 (i8*, ...) bitcast (i32 (...)* @luaI_free to i32 (i8*, ...)*)(i8* %25)
br label %27
27: ; preds = %24, %20
%28 = load i32, i32* @lua_strconc.buffer_size
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 1
%31 = call i32 (i64, ...) bitcast (i32 (...)* @luaI_malloc to i32 (i64, ...)*)(i64 %30)
%32 = sext i32 %31 to i64
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** @lua_strconc.buffer
br label %34
34: ; preds = %27, %2
%35 = load i8*, i8** @lua_strconc.buffer
%36 = load i8*, i8** %3
%37 = call i8* @strcpy(i8* %35, i8* %36)
%38 = load i8*, i8** @lua_strconc.buffer
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8*, i8** %4
%43 = call i8* @strcpy(i8* %41, i8* %42)
%44 = load i8*, i8** @lua_strconc.buffer
ret i8* %44
}

LLVM IR (-O2):
@lua_strconc.buffer = internal unnamed_addr global i8* null
@lua_strconc.buffer_size = internal unnamed_addr global i32 0
define i8* @lua_strconc(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %4, %3
%6 = trunc i64 %5 to i32
%7 = add i32 %6, 1
%8 = load i32, i32* @lua_strconc.buffer_size
%9 = icmp sgt i32 %7, %8
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = load i8*, i8** @lua_strconc.buffer
br label %24
12: ; preds = %2
store i32 %7, i32* @lua_strconc.buffer_size
%13 = load i8*, i8** @lua_strconc.buffer
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i32 (i8*, ...) bitcast (i32 (...)* @luaI_free to i32 (i8*, ...)*)(i8* %13)
%17 = load i32, i32* @lua_strconc.buffer_size
br label %18
18: ; preds = %15, %12
%19 = phi i32 [ %17, %15 ], [ %7, %12 ]
%20 = sext i32 %19 to i64
%21 = tail call i32 (i64, ...) bitcast (i32 (...)* @luaI_malloc to i32 (i64, ...)*)(i64 %20)
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** @lua_strconc.buffer
br label %24
24: ; preds = %10, %18
%25 = phi i8* [ %11, %10 ], [ %23, %18 ]
%26 = shl i64 %3, 32
%27 = ashr exact i64 %26, 32
%28 = tail call i8* @strcpy(i8* dereferenceable(1) %25, i8* dereferenceable(1) %0)
%29 = getelementptr inbounds i8, i8* %25, i64 %27
%30 = tail call i8* @strcpy(i8* dereferenceable(1) %29, i8* dereferenceable(1) %1)
ret i8* %25
}