#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*/ ENOMEM;
int /*<<< orphan*/ errno;
char *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, char *);
int strlen(char *);
char *str_prepend(char *str, char *prepend) {
  size_t strsize;
  char *new;
  if ((!str) || (!prepend)) {
    return NULL;
  }
  strsize = strlen(str) + strlen(prepend) + 1;
  new = malloc(strsize);
  if (!new) {
    errno = ENOMEM;
    return NULL;
  }
  snprintf(new, strsize, "%s%s", prepend, str);
  return new;
}


x86 assembly (clang, -O0):
<str_prepend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <str_prepend+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <str_prepend+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11e3 <str_prepend+0xb3>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bf <str_prepend+0x8f>
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    0x2e28(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11e3 <str_prepend+0xb3>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe2a(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_prepend>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     119f <str_prepend+0x6f>
mov    %rsi,%r14
test   %rsi,%rsi
je     119f <str_prepend+0x6f>
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %ebx,%eax
add    $0x1,%eax
movslq %eax,%r12
mov    %r12,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     118b <str_prepend+0x5b>
mov    %rax,%rbx
lea    0xe88(%rip),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r14,%rcx
mov    %r15,%r8
call   1040 <snprintf@plt>
jmp    119f <str_prepend+0x6f>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    0x2e3d(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [5 x i8] c"%s%s\00"
define i8* @str_prepend(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %10, %2
store i8* null, i8** %3
br label %35
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %16, %18
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %28, label %26
26: ; preds = %14
%27 = load i32, i32* @ENOMEM
store i32 %27, i32* @errno
store i8* null, i8** %3
br label %35
28: ; preds = %14
%29 = load i8*, i8** %7
%30 = load i64, i64* %6
%31 = load i8*, i8** %5
%32 = load i8*, i8** %4
%33 = call i32 @snprintf(i8* %29, i64 %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32)
%34 = load i8*, i8** %7
store i8* %34, i8** %3
br label %35
35: ; preds = %28, %26, %13
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [5 x i8] c"%s%s\00"
define i8* @str_prepend(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %18
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i32 @strlen(i8* %1)
%9 = add i32 %7, 1
%10 = add i32 %9, %8
%11 = sext i32 %10 to i64
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = load i32, i32* @ENOMEM
store i32 %15, i32* @errno
br label %18
16: ; preds = %6
%17 = tail call i32 @snprintf(i8* %12, i64 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
br label %18
18: ; preds = %2, %16, %14
%19 = phi i8* [ %12, %16 ], [ null, %14 ], [ null, %2 ]
ret i8* %19
}