#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 *);
int /*<<< orphan*/ strlcat(char *, char *, size_t);
int /*<<< orphan*/ strlcpy(char *, char const *, size_t);
int strlen(char const *);
char *xmalloc(size_t);
char *xstrdup(char const *);
void xstrprepend(char **str, const char *prefix) {
  if (!*str) {
    *str = xstrdup(prefix);
    return;
  }
  size_t len = strlen(*str) + strlen(prefix) + 1;
  char *new = xmalloc(len);
  strlcpy(new, prefix, len);
  strlcat(new, *str, len);
  free(*str);
  *str = new;
}


x86 assembly (clang, -O0):
<xstrprepend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1196 <xstrprepend+0x36>
mov    -0x10(%rbp),%rdi
call   1070 <xstrdup@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1207 <xstrprepend+0xa7>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <strlcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strlcat@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrprepend>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
test   %rdi,%rdi
je     11be <xstrprepend+0x5e>
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %ebx,%eax
add    $0x1,%eax
movslq %eax,%r12
mov    %r12,%rdi
call   1060 <xmalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1080 <strlcpy@plt>
mov    (%r15),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
call   1040 <strlcat@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
jmp    11c9 <xstrprepend+0x69>
mov    %r14,%rdi
call   1070 <xstrdup@plt>
mov    %rax,%rbx
mov    %rbx,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @xstrprepend(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%7 = load i8**, i8*** %3
%8 = load i8*, i8** %7
%9 = icmp ne i8* %8, null
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i8* @xstrdup(i8* %11)
%13 = load i8**, i8*** %3
store i8* %12, i8** %13
br label %39
14: ; preds = %2
%15 = load i8**, i8*** %3
%16 = load i8*, i8** %15
%17 = call i32 @strlen(i8* %16)
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %17, %19
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = call i8* @xmalloc(i64 %23)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = call i32 @strlcpy(i8* %25, i8* %26, i64 %27)
%29 = load i8*, i8** %6
%30 = load i8**, i8*** %3
%31 = load i8*, i8** %30
%32 = load i64, i64* %5
%33 = call i32 @strlcat(i8* %29, i8* %31, i64 %32)
%34 = load i8**, i8*** %3
%35 = load i8*, i8** %34
%36 = call i32 @free(i8* %35)
%37 = load i8*, i8** %6
%38 = load i8**, i8*** %3
store i8* %37, i8** %38
br label %39
39: ; preds = %14, %10
ret void
}

LLVM IR (-O2):
define void @xstrprepend(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i8* @xstrdup(i8* %1)
br label %19
7: ; preds = %2
%8 = tail call i32 @strlen(i8* %3)
%9 = tail call i32 @strlen(i8* %1)
%10 = add i32 %8, 1
%11 = add i32 %10, %9
%12 = sext i32 %11 to i64
%13 = tail call i8* @xmalloc(i64 %12)
%14 = tail call i32 @strlcpy(i8* %13, i8* %1, i64 %12)
%15 = load i8*, i8** %0
%16 = tail call i32 @strlcat(i8* %13, i8* %15, i64 %12)
%17 = load i8*, i8** %0
%18 = tail call i32 @free(i8* %17)
br label %19
19: ; preds = %7, %5
%20 = phi i8* [ %6, %5 ], [ %13, %7 ]
store i8* %20, i8** %0
ret void
}