#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *realloc(char *, int);
int strlen(char *);
char *head_append(char *appendage, char *tail) {
  int tail_len = 0;
  int appendage_len = 0;
  int total_len = 0;
  char *new_string = 0;
  char *exchange_buffer = 0;
  tail_len = strlen(tail);
  appendage_len = strlen(appendage);
  total_len = tail_len + appendage_len;
  exchange_buffer = (char *)malloc(tail_len + 1);
  memset(exchange_buffer, 0, tail_len + 1);
  memcpy(exchange_buffer, tail, tail_len);
  new_string = realloc(tail, total_len + 1);
  memcpy(new_string, appendage, appendage_len);
  memcpy(new_string + appendage_len, exchange_buffer, tail_len);
  new_string[total_len] = 0;
  free(exchange_buffer);
  return new_string;
}


x86 assembly (clang, -O0):
<head_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
call   1080 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<head_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
lea    (%rbx,%r13,1),%eax
mov    %eax,0x4(%rsp)
lea    0x1(%rbx),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
lea    (%rbx,%r13,1),%esi
add    $0x1,%esi
mov    %r12,%rdi
call   1080 <realloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
movslq %r13d,%rdi
add    %rbp,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
movslq 0x4(%rsp),%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @head_append(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i8* null, i8** %8
store i8* null, i8** %9
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %5
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %6
%14 = load i32, i32* %5
%15 = load i32, i32* %6
%16 = add nsw i32 %14, %15
store i32 %16, i32* %7
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
%24 = call i32 @memset(i8* %21, i32 0, i32 %23)
%25 = load i8*, i8** %9
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = call i32 @memcpy(i8* %25, i8* %26, i32 %27)
%29 = load i8*, i8** %4
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
%32 = call i8* @realloc(i8* %29, i32 %31)
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = load i8*, i8** %3
%35 = load i32, i32* %6
%36 = call i32 @memcpy(i8* %33, i8* %34, i32 %35)
%37 = load i8*, i8** %8
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8*, i8** %9
%42 = load i32, i32* %5
%43 = call i32 @memcpy(i8* %40, i8* %41, i32 %42)
%44 = load i8*, i8** %8
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %9
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %8
ret i8* %50
}

LLVM IR (-O2):
define i8* @head_append(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, %3
%6 = add nsw i32 %3, 1
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = tail call i32 @memset(i8* %8, i32 0, i32 %6)
%10 = tail call i32 @memcpy(i8* %8, i8* %1, i32 %3)
%11 = add nsw i32 %5, 1
%12 = tail call i8* @realloc(i8* %1, i32 %11)
%13 = tail call i32 @memcpy(i8* %12, i8* %0, i32 %4)
%14 = sext i32 %4 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = tail call i32 @memcpy(i8* %15, i8* %8, i32 %3)
%17 = sext i32 %5 to i64
%18 = getelementptr inbounds i8, i8* %12, i64 %17
store i8 0, i8* %18
%19 = tail call i32 @free(i8* %8)
ret i8* %12
}