#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*/ strcpy(char *, char const *);
size_t strlen(char const *);
scalar_t__ xmalloc(size_t);
char *xstrdup(char const *);
char *lt_extend_str(const char *orig_value, const char *add, int to_end) {
  char *new_value;
  if (orig_value && *orig_value) {
    size_t orig_value_len = strlen(orig_value);
    size_t add_len = strlen(add);
    new_value =
        ((char *)xmalloc((add_len + orig_value_len + 1) * sizeof(char)));
    if (to_end) {
      strcpy(new_value, orig_value);
      strcpy(new_value + orig_value_len, add);
    } else {
      strcpy(new_value, add);
      strcpy(new_value + add_len, orig_value);
    }
  } else {
    new_value = xstrdup(add);
  }
  return new_value;
}


x86 assembly (clang, -O0):
<lt_extend_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11f1 <lt_extend_str+0xb1>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f1 <lt_extend_str+0xb1>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ce <lt_extend_str+0x8e>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11ec <lt_extend_str+0xac>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11fe <lt_extend_str+0xbe>
mov    -0x10(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lt_extend_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     119d <lt_extend_str+0x5d>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     119d <lt_extend_str+0x5d>
mov    %edx,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
lea    (%r12,%rax,1),%rdi
add    $0x1,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
test   %ebp,%ebp
je     11b3 <lt_extend_str+0x73>
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
add    %r12,%rdi
mov    %r14,%rsi
jmp    11c4 <lt_extend_str+0x84>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <xstrdup@plt>
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
add    %r13,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @lt_extend_str(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %50
12: ; preds = %3
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %50
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %8
%20 = load i8*, i8** %5
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %9
%22 = load i64, i64* %9
%23 = load i64, i64* %8
%24 = add i64 %22, %23
%25 = add i64 %24, 1
%26 = mul i64 %25, 1
%27 = call i64 @xmalloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %7
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %17
%32 = load i8*, i8** %7
%33 = load i8*, i8** %4
%34 = call i32 @strcpy(i8* %32, i8* %33)
%35 = load i8*, i8** %7
%36 = load i64, i64* %8
%37 = getelementptr inbounds i8, i8* %35, i64 %36
%38 = load i8*, i8** %5
%39 = call i32 @strcpy(i8* %37, i8* %38)
br label %49
40: ; preds = %17
%41 = load i8*, i8** %7
%42 = load i8*, i8** %5
%43 = call i32 @strcpy(i8* %41, i8* %42)
%44 = load i8*, i8** %7
%45 = load i64, i64* %9
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = load i8*, i8** %4
%48 = call i32 @strcpy(i8* %46, i8* %47)
br label %49
49: ; preds = %40, %31
br label %53
50: ; preds = %12, %3
%51 = load i8*, i8** %5
%52 = call i8* @xstrdup(i8* %51)
store i8* %52, i8** %7
br label %53
53: ; preds = %50, %49
%54 = load i8*, i8** %7
ret i8* %54
}

LLVM IR (-O2):
define i8* @lt_extend_str(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %24, label %5
5: ; preds = %3
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %5
%9 = tail call i64 @strlen(i8* %0)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%11 = add i64 %9, 1
%12 = add i64 %11, %10
%13 = tail call i64 @xmalloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i32 %2, 0
br i1 %15, label %20, label %16
16: ; preds = %8
%17 = tail call i32 @strcpy(i8* %14, i8* %0)
%18 = getelementptr inbounds i8, i8* %14, i64 %9
%19 = tail call i32 @strcpy(i8* %18, i8* %1)
br label %26
20: ; preds = %8
%21 = tail call i32 @strcpy(i8* %14, i8* %1)
%22 = getelementptr inbounds i8, i8* %14, i64 %10
%23 = tail call i32 @strcpy(i8* %22, i8* %0)
br label %26
24: ; preds = %5, %3
%25 = tail call i8* @xstrdup(i8* %1)
br label %26
26: ; preds = %16, %20, %24
%27 = phi i8* [ %25, %24 ], [ %14, %20 ], [ %14, %16 ]
ret i8* %27
}