char *concat(str1, str2)
char *str1, *str2;
{
  char *newstr;
  int str1_length = strlen(str1);
  newstr = malloc(str1_length + strlen(str2) + 1);
  if (newstr == 0)
    return 0;
  strcpy(newstr, str1);
  strcpy(newstr + str1_length, str2);
  return newstr;
}


x86 assembly (clang, -O0):
<concat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1189 <concat+0x59>
movq   $0x0,-0x8(%rbp)
jmp    11b2 <concat+0x82>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
movslq %eax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r12,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117c <concat+0x4c>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
add    %r12,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @concat(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = shl i64 %3, 32
%5 = ashr exact i64 %4, 32
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = add i64 %6, 1
%8 = add i64 %7, %5
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = tail call i8* @strcpy(i8* %9, i8* dereferenceable(1) %0)
%13 = getelementptr inbounds i8, i8* %9, i64 %5
%14 = tail call i8* @strcpy(i8* %13, i8* dereferenceable(1) %1)
br label %15
15: ; preds = %2, %11
ret i8* %9
}