#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);
int /*<<< orphan*/ printf(char *, char *);
char *copy_prefix_safely(char ignore_nulls_flag, char *buffer, char *str,
                         int end) {
  int free_buffer_flag = 0;
  int n = 0;
  if (buffer == 0) {
    printf("Error:\t%s\n", "Null Pointer Exception - copy_prefix_safely");
    exit(-1);
  }
  if (str == 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 && (str[n] || ignore_nulls_flag); n++) {
    buffer[n] = str[n];
  }
  buffer[n] = '\0';
  if (str[n]) {
    return str + n;
  } else {
    return str + n + 1;
  }
}


x86 assembly (clang, -O0):
<copy_prefix_safely>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
cmpq   $0x0,-0x18(%rbp)
jne    116f <copy_prefix_safely+0x4f>
lea    0xea7(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1197 <copy_prefix_safely+0x77>
lea    0xe7f(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jge    11be <copy_prefix_safely+0x9e>
lea    0xe58(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x24(%rbp),%ecx
mov    %al,-0x2d(%rbp)
jge    1203 <copy_prefix_safely+0xe3>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x2e(%rbp)
jne    11fd <copy_prefix_safely+0xdd>
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x2e(%rbp)
mov    -0x2e(%rbp),%al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1213 <copy_prefix_safely+0xf3>
jmp    1237 <copy_prefix_safely+0x117>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11c5 <copy_prefix_safely+0xa5>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     1269 <copy_prefix_safely+0x149>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    127c <copy_prefix_safely+0x15c>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_prefix_safely>:
push   %rax
test   %rsi,%rsi
je     1175 <copy_prefix_safely+0x55>
test   %rdx,%rdx
je     1175 <copy_prefix_safely+0x55>
test   %ecx,%ecx
js     1185 <copy_prefix_safely+0x65>
je     115f <copy_prefix_safely+0x3f>
mov    %ecx,%r8d
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
movzbl (%rdx,%rax,1),%r9d
mov    %r9d,%ecx
or     %dil,%cl
je     1161 <copy_prefix_safely+0x41>
mov    %r9b,(%rsi,%rax,1)
add    $0x1,%rax
cmp    %rax,%r8
jne    1140 <copy_prefix_safely+0x20>
mov    %r8d,%eax
jmp    1161 <copy_prefix_safely+0x41>
xor    %eax,%eax
mov    %eax,%ecx
movb   $0x0,(%rsi,%rcx,1)
lea    (%rdx,%rcx,1),%rax
cmpb   $0x1,(%rdx,%rcx,1)
adc    $0x0,%rax
pop    %rcx
ret
lea    0xe84(%rip),%rdi
lea    0xe88(%rip),%rsi
jmp    1193 <copy_prefix_safely+0x73>
lea    0xe74(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <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* @copy_prefix_safely(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
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
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %4
%15 = 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))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %4
%18 = load i8*, i8** %8
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = 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))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %17
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = 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))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %23
store i32 0, i32* %11
br label %30
30: ; preds = %60, %29
%31 = load i32, i32* %11
%32 = load i32, i32* %9
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %48
34: ; preds = %30
%35 = load i8*, i8** %8
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %34
%43 = load i8, i8* %6
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 0
br label %46
46: ; preds = %42, %34
%47 = phi i1 [ true, %34 ], [ %45, %42 ]
br label %48
48: ; preds = %46, %30
%49 = phi i1 [ false, %30 ], [ %47, %46 ]
br i1 %49, label %50, label %63
50: ; preds = %48
%51 = load i8*, i8** %8
%52 = load i32, i32* %11
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i8, i8* %54
%56 = load i8*, i8** %7
%57 = load i32, i32* %11
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 %55, i8* %59
br label %60
60: ; preds = %50
%61 = load i32, i32* %11
%62 = add nsw i32 %61, 1
store i32 %62, i32* %11
br label %30
63: ; preds = %48
%64 = load i8*, i8** %7
%65 = load i32, i32* %11
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 0, i8* %67
%68 = load i8*, i8** %8
%69 = load i32, i32* %11
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i8, i8* %71
%73 = icmp ne i8 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %63
%75 = load i8*, i8** %8
%76 = load i32, i32* %11
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
store i8* %78, i8** %5
br label %85
79: ; preds = %63
%80 = load i8*, i8** %8
%81 = load i32, i32* %11
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = getelementptr inbounds i8, i8* %83, i64 1
store i8* %84, i8** %5
br label %85
85: ; preds = %79, %74
%86 = load i8*, i8** %5
ret i8* %86
}

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* @copy_prefix_safely(i8 signext %0, i8* writeonly %1, i8* %2, i32 %3) {
%5 = icmp eq i8* %1, null
br i1 %5, label %6, label %9
6: ; preds = %4
%7 = 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))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %4
%10 = icmp eq i8* %2, null
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = 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))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %9
%15 = icmp slt i32 %3, 0
br i1 %15, label %20, label %16
16: ; preds = %14
%17 = icmp eq i32 %3, 0
br i1 %17, label %35, label %18
18: ; preds = %16
%19 = zext i32 %3 to i64
br label %23
20: ; preds = %14
%21 = 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))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %18, %29
%24 = phi i64 [ 0, %18 ], [ %31, %29 ]
%25 = getelementptr inbounds i8, i8* %2, i64 %24
%26 = load i8, i8* %25
%27 = or i8 %26, %0
%28 = icmp eq i8 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %23
%30 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 %26, i8* %30
%31 = add nuw nsw i64 %24, 1
%32 = icmp eq i64 %31, %19
br i1 %32, label %35, label %23
33: ; preds = %23
%34 = trunc i64 %24 to i32
br label %35
35: ; preds = %29, %33, %16
%36 = phi i32 [ 0, %16 ], [ %34, %33 ], [ %3, %29 ]
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %1, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds i8, i8* %2, i64 %37
%40 = load i8, i8* %39
%41 = icmp eq i8 %40, 0
%42 = zext i1 %41 to i64
%43 = getelementptr i8, i8* %39, i64 %42
ret i8* %43
}