#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 *);
char *realloc(char *, size_t const);
int /*<<< orphan*/ show_error_msg(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t const strlen(char const *);
char *append_to_expanded(char expanded[], const char str[]) {
  char *t;
  const size_t len = strlen(expanded);
  t = realloc(expanded, len + strlen(str) + 1);
  if (t == NULL) {
    show_error_msg("Memory Error", "Unable to allocate enough memory");
    free(expanded);
    return NULL;
  }
  strcpy(t + len, str);
  return t;
}


x86 assembly (clang, -O0):
<append_to_expanded>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d2 <append_to_expanded+0x82>
lea    0xe50(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1060 <show_error_msg@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11eb <append_to_expanded+0x9b>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_to_expanded>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
mov    %r15,%rdi
call   1070 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1196 <append_to_expanded+0x46>
mov    %rbx,%rdi
add    %r12,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    11b1 <append_to_expanded+0x61>
lea    0xe63(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1060 <show_error_msg@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Memory Error\00"
@.str.1 = [33 x i8] c"Unable to allocate enough memory\00"
define i8* @append_to_expanded(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %7
%10 = load i8*, i8** %4
%11 = load i64, i64* %7
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
%14 = add i64 %11, %13
%15 = add i64 %14, 1
%16 = call i8* @realloc(i8* %10, i64 %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = call i32 @show_error_msg(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = call i32 @free(i8* %21)
store i8* null, i8** %3
br label %30
23: ; preds = %2
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8*, i8** %5
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %6
store i8* %29, i8** %3
br label %30
30: ; preds = %23, %19
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@.str = [13 x i8] c"Memory Error\00"
@.str.1 = [33 x i8] c"Unable to allocate enough memory\00"
define i8* @append_to_expanded(i8* nocapture %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %3, 1
%6 = add i64 %5, %4
%7 = tail call i8* @realloc(i8* %0, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @show_error_msg(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @free(i8* %0)
br label %15
12: ; preds = %2
%13 = getelementptr inbounds i8, i8* %7, i64 %3
%14 = tail call i32 @strcpy(i8* %13, i8* %1)
br label %15
15: ; preds = %12, %9
ret i8* %7
}