#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 *calloc(size_t, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *string_cat(const char *s1, const char *s2) {
  size_t size = strlen(s1) + strlen(s2);
  char *retVal = calloc(size + 1, sizeof(char));
  if (retVal == NULL) {
    printf("ERROR: Out of memeory!\n");
    exit(1);
  }
  strncpy(retVal, s1, size);
  strncat(retVal, s2, size - strlen(retVal));
  return retVal;
}


x86 assembly (clang, -O0):
<string_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cb <string_cat+0x6b>
lea    0xe44(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
sub    %rax,%rdx
call   1060 <strncat@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_cat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
lea    (%rax,%r13,1),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     11d3 <string_cat+0x73>
mov    %rax,%r12
add    %r13,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
sub    %rax,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <strncat@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe26(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"ERROR: Out of memeory!\0A\00"
define i8* @string_cat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
%11 = add i64 %8, %10
store i64 %11, i64* %5
%12 = load i64, i64* %5
%13 = add i64 %12, 1
%14 = call i8* @calloc(i64 %13, i32 1)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8*, i8** %6
%22 = load i8*, i8** %3
%23 = load i64, i64* %5
%24 = call i32 @strncpy(i8* %21, i8* %22, i64 %23)
%25 = load i8*, i8** %6
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = load i8*, i8** %6
%29 = call i64 @strlen(i8* %28)
%30 = sub i64 %27, %29
%31 = call i32 @strncat(i8* %25, i8* %26, i64 %30)
%32 = load i8*, i8** %6
ret i8* %32
}

LLVM IR (-O2):
@str = [23 x i8] c"ERROR: Out of memeory!\00"
define i8* @string_cat(i8* %0, i8* %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 = add i64 %5, 1
%7 = tail call i8* @calloc(i64 %6, i32 1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32 @strncpy(i8* %7, i8* %0, i64 %5)
%14 = tail call i64 @strlen(i8* %7)
%15 = sub i64 %5, %14
%16 = tail call i32 @strncat(i8* %7, i8* %1, i64 %15)
ret i8* %7
}