#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(size_t const);
int /*<<< orphan*/ memcpy(char *, char const *const, size_t);
size_t strlen(char const *const);
__attribute__((__malloc__, __nonnull__, __warn_unused_result__)) char *
cheat_allocate_truncated(char const *const literal, size_t const length,
                         char const *const marker) {
  size_t literal_length;
  char *result;
  literal_length = strlen(literal);
  if (literal_length > length) {
    size_t marker_length;
    size_t paste_length;
    marker_length = strlen(marker);
    if (marker_length > length)
      return NULL;
    result = (malloc(length + 1));
    if (result == NULL)
      return NULL;
    paste_length = length - marker_length;
    memcpy(result, literal, paste_length);
    memcpy(&result[paste_length], marker, marker_length + 1);
  } else {
    result = (malloc(literal_length + 1));
    if (result == NULL)
      return NULL;
    memcpy(result, literal, literal_length + 1);
  }
  return result;
}


x86 assembly (clang, -O0):
<cheat_allocate_truncated>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11eb <cheat_allocate_truncated+0xbb>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1187 <cheat_allocate_truncated+0x57>
movq   $0x0,-0x8(%rbp)
jmp    1231 <cheat_allocate_truncated+0x101>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b0 <cheat_allocate_truncated+0x80>
movq   $0x0,-0x8(%rbp)
jmp    1231 <cheat_allocate_truncated+0x101>
mov    -0x18(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
jmp    1229 <cheat_allocate_truncated+0xf9>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1214 <cheat_allocate_truncated+0xe4>
movq   $0x0,-0x8(%rbp)
jmp    1231 <cheat_allocate_truncated+0x101>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cheat_allocate_truncated>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    %rbp,%rax
jbe    1192 <cheat_allocate_truncated+0x62>
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    %rbp,%rax
ja     11b9 <cheat_allocate_truncated+0x89>
mov    %rax,%r13
lea    0x1(%rbp),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b9 <cheat_allocate_truncated+0x89>
mov    %rax,%r12
sub    %r13,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1040 <memcpy@plt>
add    %r12,%rbp
add    $0x1,%r13
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
jmp    11b2 <cheat_allocate_truncated+0x82>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b9 <cheat_allocate_truncated+0x89>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    11bc <cheat_allocate_truncated+0x8c>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define noalias i8* @cheat_allocate_truncated(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = load i64, i64* %6
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %46
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %10
%20 = load i64, i64* %10
%21 = load i64, i64* %6
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %17
store i8* null, i8** %4
br label %61
24: ; preds = %17
%25 = load i64, i64* %6
%26 = add i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %24
store i8* null, i8** %4
br label %61
31: ; preds = %24
%32 = load i64, i64* %6
%33 = load i64, i64* %10
%34 = sub i64 %32, %33
store i64 %34, i64* %11
%35 = load i8*, i8** %9
%36 = load i8*, i8** %5
%37 = load i64, i64* %11
%38 = call i32 @memcpy(i8* %35, i8* %36, i64 %37)
%39 = load i8*, i8** %9
%40 = load i64, i64* %11
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = load i8*, i8** %7
%43 = load i64, i64* %10
%44 = add i64 %43, 1
%45 = call i32 @memcpy(i8* %41, i8* %42, i64 %44)
br label %59
46: ; preds = %3
%47 = load i64, i64* %8
%48 = add i64 %47, 1
%49 = call i8* @malloc(i64 %48)
store i8* %49, i8** %9
%50 = load i8*, i8** %9
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %53
52: ; preds = %46
store i8* null, i8** %4
br label %61
53: ; preds = %46
%54 = load i8*, i8** %9
%55 = load i8*, i8** %5
%56 = load i64, i64* %8
%57 = add i64 %56, 1
%58 = call i32 @memcpy(i8* %54, i8* %55, i64 %57)
br label %59
59: ; preds = %53, %31
%60 = load i8*, i8** %9
store i8* %60, i8** %4
br label %61
61: ; preds = %59, %52, %30, %23
%62 = load i8*, i8** %4
ret i8* %62
}

LLVM IR (-O2):
define noalias i8* @cheat_allocate_truncated(i8* %0, i64 %1, i8* %2) {
%4 = tail call i64 @strlen(i8* %0)
%5 = icmp ugt i64 %4, %1
br i1 %5, label %6, label %19
6: ; preds = %3
%7 = tail call i64 @strlen(i8* %2)
%8 = icmp ugt i64 %7, %1
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = add nuw i64 %1, 1
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %25, label %13
13: ; preds = %9
%14 = sub i64 %1, %7
%15 = tail call i32 @memcpy(i8* %11, i8* %0, i64 %14)
%16 = getelementptr inbounds i8, i8* %11, i64 %14
%17 = add i64 %7, 1
%18 = tail call i32 @memcpy(i8* %16, i8* %2, i64 %17)
br label %25
19: ; preds = %3
%20 = add i64 %4, 1
%21 = tail call i8* @malloc(i64 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @memcpy(i8* %21, i8* %0, i64 %20)
br label %25
25: ; preds = %9, %6, %23, %13, %19
%26 = phi i8* [ null, %19 ], [ %11, %13 ], [ %21, %23 ], [ null, %6 ], [ null, %9 ]
ret i8* %26
}