#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 *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *string_append(const char *s1, const char *s2) {
  char *temp_string = NULL;
  char *appended_string = NULL;
  appended_string = malloc(strlen(s1) + strlen(s2) + 1);
  if (appended_string == NULL) {
    printf("Malloc Error!\n");
    return 0;
  }
  memset(appended_string, 0, strlen(s1) + strlen(s2) + 1);
  temp_string = appended_string;
  strcpy(appended_string, s1);
  temp_string += strlen(s1) - 1;
  if (*temp_string == 47) {
    strcpy(temp_string, s2);
  } else {
    strcat(temp_string, s2);
  }
  return appended_string;
}


x86 assembly (clang, -O0):
<string_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ca <string_append+0x6a>
lea    0xe48(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1262 <string_append+0x102>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rdi
add    %eax,%edx
add    $0x1,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
sub    $0x1,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    124d <string_append+0xed>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    125a <string_append+0xfa>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_append>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e2 <string_append+0x82>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%r12,%rax,1),%edx
add    $0x1,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rax,%rbx,1),%rdi
add    $0xffffffffffffffff,%rdi
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    11f0 <string_append+0x90>
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    11f8 <string_append+0x98>
lea    0xe17(%rip),%rdi
call   1040 <puts@plt>
jmp    11f8 <string_append+0x98>
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Malloc Error!\0A\00"
define i8* @string_append(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %9, %11
%13 = add nsw i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %52
19: ; preds = %2
%20 = load i8*, i8** %7
%21 = load i8*, i8** %4
%22 = call i32 @strlen(i8* %21)
%23 = load i8*, i8** %5
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %22, %24
%26 = add nsw i32 %25, 1
%27 = call i32 @memset(i8* %20, i32 0, i32 %26)
%28 = load i8*, i8** %7
store i8* %28, i8** %6
%29 = load i8*, i8** %7
%30 = load i8*, i8** %4
%31 = call i32 @strcpy(i8* %29, i8* %30)
%32 = load i8*, i8** %4
%33 = call i32 @strlen(i8* %32)
%34 = sub nsw i32 %33, 1
%35 = load i8*, i8** %6
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 47
br i1 %41, label %42, label %46
42: ; preds = %19
%43 = load i8*, i8** %6
%44 = load i8*, i8** %5
%45 = call i32 @strcpy(i8* %43, i8* %44)
br label %50
46: ; preds = %19
%47 = load i8*, i8** %6
%48 = load i8*, i8** %5
%49 = call i32 @strcat(i8* %47, i8* %48)
br label %50
50: ; preds = %46, %42
%51 = load i8*, i8** %7
store i8* %51, i8** %3
br label %52
52: ; preds = %50, %17
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
@str = [14 x i8] c"Malloc Error!\00"
define i8* @string_append(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %3, 1
%6 = add i32 %5, %4
%7 = tail call i8* @malloc(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %28
11: ; preds = %2
%12 = tail call i32 @strlen(i8* %0)
%13 = tail call i32 @strlen(i8* %1)
%14 = add i32 %12, 1
%15 = add i32 %14, %13
%16 = tail call i32 @memset(i8* %7, i32 0, i32 %15)
%17 = tail call i32 @strcpy(i8* %7, i8* %0)
%18 = tail call i32 @strlen(i8* %0)
%19 = add nsw i32 %18, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %7, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 47
br i1 %23, label %24, label %26
24: ; preds = %11
%25 = tail call i32 @strcpy(i8* %21, i8* %1)
br label %28
26: ; preds = %11
%27 = tail call i32 @strcat(i8* %21, i8* %1)
br label %28
28: ; preds = %24, %26, %9
ret i8* %7
}