#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*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
char *realloc(char *, int);
char *malloc_copy_prefix(char ignore_nulls_flag, char **dst, char *src,
                         int end) {
  int free_buffer_flag = 0;
  int n = 0;
  char *array = malloc(sizeof(char) * 8);
  int size = 10;
  if (dst == 0) {
    printf("Error:\t%s\n", "Null Pointer Exception - copy_prefix_safely");
    exit(-1);
  }
  if (src == 0) {
    printf("Error:\t%s\n", "Null Pointer Exception - copy_prefix_safely");
    exit(-1);
  }
  if (end < 0) {
    printf("Error:\t%s\n", "Negative Length Exception - copy_prefix_safely");
    exit(-1);
  }
  for (n = 0; n < end && (src[n] || ignore_nulls_flag); n++) {
    if (n > size - 1) {
      array = realloc(array, size * 2);
    }
    array[n] = src[n];
  }
  array[n] = '\0';
  dst[0] = array;
  if (src[n]) {
    return src + n;
  } else {
    return src + n + 1;
  }
}


x86 assembly (clang, -O0):
<malloc_copy_prefix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dil,%al
mov    %al,-0x9(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0xa,-0x3c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a4 <malloc_copy_prefix+0x64>
lea    0xe72(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11cc <malloc_copy_prefix+0x8c>
lea    0xe4a(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jge    11f3 <malloc_copy_prefix+0xb3>
lea    0xe23(%rip),%rdi
lea    0xe53(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x24(%rbp),%ecx
mov    %al,-0x3d(%rbp)
jge    1238 <malloc_copy_prefix+0xf8>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x3e(%rbp)
jne    1232 <malloc_copy_prefix+0xf2>
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x3e(%rbp)
mov    -0x3e(%rbp),%al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    1248 <malloc_copy_prefix+0x108>
jmp    1290 <malloc_copy_prefix+0x150>
mov    -0x2c(%rbp),%eax
mov    -0x3c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    126c <malloc_copy_prefix+0x12c>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
shl    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11fa <malloc_copy_prefix+0xba>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     12cd <malloc_copy_prefix+0x18d>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    12e0 <malloc_copy_prefix+0x1a0>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc_copy_prefix>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r12d
mov    $0x8,%edi
call   1040 <malloc@plt>
test   %r14,%r14
je     11d5 <malloc_copy_prefix+0x95>
test   %rbx,%rbx
je     11d5 <malloc_copy_prefix+0x95>
test   %r15d,%r15d
js     11e5 <malloc_copy_prefix+0xa5>
je     11b0 <malloc_copy_prefix+0x70>
mov    %r15d,%r15d
xor    %ebp,%ebp
jmp    118c <malloc_copy_prefix+0x4c>
cs nopw 0x0(%rax,%rax,1)
mov    %cl,(%rax,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%r15
je     11b4 <malloc_copy_prefix+0x74>
movzbl (%rbx,%rbp,1),%ecx
mov    %ecx,%edx
or     %r12b,%dl
je     11b7 <malloc_copy_prefix+0x77>
cmp    $0xa,%rbp
jb     1180 <malloc_copy_prefix+0x40>
mov    %rax,%rdi
mov    $0x14,%esi
call   1050 <realloc@plt>
movzbl (%rbx,%rbp,1),%ecx
jmp    1180 <malloc_copy_prefix+0x40>
xor    %ebp,%ebp
jmp    11b7 <malloc_copy_prefix+0x77>
mov    %r15d,%ebp
mov    %ebp,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    %rax,(%r14)
lea    (%rbx,%rcx,1),%rax
cmpb   $0x1,(%rbx,%rcx,1)
adc    $0x0,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe24(%rip),%rdi
lea    0xe28(%rip),%rsi
jmp    11f3 <malloc_copy_prefix+0xb3>
lea    0xe14(%rip),%rdi
lea    0xe44(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"Error:\09%s\0A\00"
@.str.1 = [44 x i8] c"Null Pointer Exception - copy_prefix_safely\00"
@.str.2 = [47 x i8] c"Negative Length Exception - copy_prefix_safely\00"
define i8* @malloc_copy_prefix(i8 signext %0, i8** %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
store i8 %0, i8* %6
store i8** %1, i8*** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
%14 = call i8* @malloc(i32 8)
store i8* %14, i8** %12
store i32 10, i32* %13
%15 = load i8**, i8*** %7
%16 = icmp eq i8** %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %4
%21 = load i8*, i8** %8
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %20
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %26
store i32 0, i32* %11
br label %33
33: ; preds = %73, %32
%34 = load i32, i32* %11
%35 = load i32, i32* %9
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %51
37: ; preds = %33
%38 = load i8*, i8** %8
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %37
%46 = load i8, i8* %6
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 0
br label %49
49: ; preds = %45, %37
%50 = phi i1 [ true, %37 ], [ %48, %45 ]
br label %51
51: ; preds = %49, %33
%52 = phi i1 [ false, %33 ], [ %50, %49 ]
br i1 %52, label %53, label %76
53: ; preds = %51
%54 = load i32, i32* %11
%55 = load i32, i32* %13
%56 = sub nsw i32 %55, 1
%57 = icmp sgt i32 %54, %56
br i1 %57, label %58, label %63
58: ; preds = %53
%59 = load i8*, i8** %12
%60 = load i32, i32* %13
%61 = mul nsw i32 %60, 2
%62 = call i8* @realloc(i8* %59, i32 %61)
store i8* %62, i8** %12
br label %63
63: ; preds = %58, %53
%64 = load i8*, i8** %8
%65 = load i32, i32* %11
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i8, i8* %67
%69 = load i8*, i8** %12
%70 = load i32, i32* %11
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 %68, i8* %72
br label %73
73: ; preds = %63
%74 = load i32, i32* %11
%75 = add nsw i32 %74, 1
store i32 %75, i32* %11
br label %33
76: ; preds = %51
%77 = load i8*, i8** %12
%78 = load i32, i32* %11
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %77, i64 %79
store i8 0, i8* %80
%81 = load i8*, i8** %12
%82 = load i8**, i8*** %7
%83 = getelementptr inbounds i8*, i8** %82, i64 0
store i8* %81, i8** %83
%84 = load i8*, i8** %8
%85 = load i32, i32* %11
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
%88 = load i8, i8* %87
%89 = icmp ne i8 %88, 0
br i1 %89, label %90, label %95
90: ; preds = %76
%91 = load i8*, i8** %8
%92 = load i32, i32* %11
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i8, i8* %91, i64 %93
store i8* %94, i8** %5
br label %101
95: ; preds = %76
%96 = load i8*, i8** %8
%97 = load i32, i32* %11
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds i8, i8* %96, i64 %98
%100 = getelementptr inbounds i8, i8* %99, i64 1
store i8* %100, i8** %5
br label %101
101: ; preds = %95, %90
%102 = load i8*, i8** %5
ret i8* %102
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error:\09%s\0A\00"
@.str.1 = [44 x i8] c"Null Pointer Exception - copy_prefix_safely\00"
@.str.2 = [47 x i8] c"Negative Length Exception - copy_prefix_safely\00"
define i8* @malloc_copy_prefix(i8 signext %0, i8** writeonly %1, i8* %2, i32 %3) {
%5 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%6 = icmp eq i8** %1, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %4
%11 = icmp eq i8* %2, null
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %10
%16 = icmp slt i32 %3, 0
br i1 %16, label %21, label %17
17: ; preds = %15
%18 = icmp eq i32 %3, 0
br i1 %18, label %44, label %19
19: ; preds = %17
%20 = zext i32 %3 to i64
br label %24
21: ; preds = %15
%22 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %19, %36
%25 = phi i64 [ 0, %19 ], [ %40, %36 ]
%26 = phi i8* [ %5, %19 ], [ %38, %36 ]
%27 = getelementptr inbounds i8, i8* %2, i64 %25
%28 = load i8, i8* %27
%29 = or i8 %28, %0
%30 = icmp eq i8 %29, 0
br i1 %30, label %42, label %31
31: ; preds = %24
%32 = icmp ugt i64 %25, 9
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = tail call i8* @realloc(i8* %26, i32 20)
%35 = load i8, i8* %27
br label %36
36: ; preds = %33, %31
%37 = phi i8 [ %35, %33 ], [ %28, %31 ]
%38 = phi i8* [ %34, %33 ], [ %26, %31 ]
%39 = getelementptr inbounds i8, i8* %38, i64 %25
store i8 %37, i8* %39
%40 = add nuw nsw i64 %25, 1
%41 = icmp eq i64 %40, %20
br i1 %41, label %44, label %24
42: ; preds = %24
%43 = trunc i64 %25 to i32
br label %44
44: ; preds = %36, %42, %17
%45 = phi i32 [ 0, %17 ], [ %43, %42 ], [ %3, %36 ]
%46 = phi i8* [ %5, %17 ], [ %26, %42 ], [ %38, %36 ]
%47 = zext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
store i8* %46, i8** %1
%49 = getelementptr inbounds i8, i8* %2, i64 %47
%50 = load i8, i8* %49
%51 = icmp eq i8 %50, 0
%52 = zext i1 %51 to i64
%53 = getelementptr i8, i8* %49, i64 %52
ret i8* %53
}