#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 *copy_prefix_safely(char, char *, char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ print_string(char, char *, int, char);
int /*<<< orphan*/ printf(char *, char *);
void print_string_safely(char ignore_nulls_flag, char *buffer, char *str,
                         int len, char hex_flag) {
  int free_buffer_flag = 0;
  int tmp_len = 0;
  int n = 0;
  char c = 0;
  char *tmp;
  if (str == 0) {
    printf("Error:\t%s\n", "Null Pointer Exception - print_string_safely");
    exit(-1);
  }
  if (len < 0) {
    printf("Error:\t%s\n", "Negative Length Exception - print_string_safely");
    exit(-1);
  }
  if (buffer == 0) {
    buffer = malloc(sizeof(char) * (len + 2));
    free_buffer_flag = 1;
  }
  tmp = copy_prefix_safely(ignore_nulls_flag, buffer, str, len);
  tmp_len = (int)(tmp - str);
  buffer[tmp_len] = '\0';
  print_string(ignore_nulls_flag, buffer, len, hex_flag);
  if (free_buffer_flag) {
    free(buffer);
  }
}


x86 assembly (clang, -O0):
<print_string_safely>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %r8b,%al
mov    %dil,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movb   $0x0,-0x2d(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11be <print_string_safely+0x5e>
lea    0xe58(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    11e5 <print_string_safely+0x85>
lea    0xe31(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    120e <print_string_safely+0xae>
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x1(%rbp),%al
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
movsbl %al,%edi
call   1040 <copy_prefix_safely@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1(%rbp),%al
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
movsbl %al,%edi
movsbl -0x1d(%rbp),%ecx
call   1060 <print_string@plt>
cmpl   $0x0,-0x24(%rbp)
je     126b <print_string_safely+0x10b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_string_safely>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11fb <print_string_safely+0x9b>
mov    %ecx,%r12d
test   %ecx,%ecx
js     120b <print_string_safely+0xab>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%r13d
mov    %r8d,0x4(%rsp)
mov    %rsi,%rbx
test   %rsi,%rsi
jne    11a2 <print_string_safely+0x42>
lea    0x2(%r12),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
movsbl %r13b,%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r12d,%ecx
call   1040 <copy_prefix_safely@plt>
sub    %r15d,%eax
cltq
movb   $0x0,(%rbx,%rax,1)
movsbl 0x4(%rsp),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1060 <print_string@plt>
test   %r14,%r14
je     11e5 <print_string_safely+0x85>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xdfe(%rip),%rdi
lea    0xe02(%rip),%rsi
jmp    1219 <print_string_safely+0xb9>
lea    0xdee(%rip),%rdi
lea    0xe1f(%rip),%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"Error:\09%s\0A\00"
@.str.1 = [45 x i8] c"Null Pointer Exception - print_string_safely\00"
@.str.2 = [48 x i8] c"Negative Length Exception - print_string_safely\00"
define void @print_string_safely(i8 signext %0, i8* %1, i8* %2, i32 %3, i8 signext %4) {
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8
%15 = alloca i8*
store i8 %0, i8* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8 %4, i8* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i8 0, i8* %14
%16 = load i8*, i8** %8
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %5
%19 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %5
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %21
%28 = load i8*, i8** %7
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i32, i32* %9
%32 = add nsw i32 %31, 2
%33 = sext i32 %32 to i64
%34 = mul i64 1, %33
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %7
store i32 1, i32* %11
br label %37
37: ; preds = %30, %27
%38 = load i8, i8* %6
%39 = load i8*, i8** %7
%40 = load i8*, i8** %8
%41 = load i32, i32* %9
%42 = call i8* @copy_prefix_safely(i8 signext %38, i8* %39, i8* %40, i32 %41)
store i8* %42, i8** %15
%43 = load i8*, i8** %15
%44 = load i8*, i8** %8
%45 = ptrtoint i8* %43 to i64
%46 = ptrtoint i8* %44 to i64
%47 = sub i64 %45, %46
%48 = trunc i64 %47 to i32
store i32 %48, i32* %12
%49 = load i8*, i8** %7
%50 = load i32, i32* %12
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i8, i8* %6
%54 = load i8*, i8** %7
%55 = load i32, i32* %9
%56 = load i8, i8* %10
%57 = call i32 @print_string(i8 signext %53, i8* %54, i32 %55, i8 signext %56)
%58 = load i32, i32* %11
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %37
%61 = load i8*, i8** %7
%62 = call i32 @free(i8* %61)
br label %63
63: ; preds = %60, %37
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error:\09%s\0A\00"
@.str.1 = [45 x i8] c"Null Pointer Exception - print_string_safely\00"
@.str.2 = [48 x i8] c"Negative Length Exception - print_string_safely\00"
define void @print_string_safely(i8 signext %0, i8* %1, i8* %2, i32 %3, i8 signext %4) {
%6 = icmp eq i8* %2, null
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %5
%11 = icmp slt i32 %3, 0
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 ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %10
%16 = icmp eq i8* %1, null
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = add nuw nsw i32 %3, 2
%19 = tail call i8* @malloc(i32 %18)
br label %20
20: ; preds = %17, %15
%21 = phi i8* [ %19, %17 ], [ %1, %15 ]
%22 = tail call i8* @copy_prefix_safely(i8 signext %0, i8* %21, i8* %2, i32 %3)
%23 = ptrtoint i8* %22 to i64
%24 = ptrtoint i8* %2 to i64
%25 = sub i64 %23, %24
%26 = shl i64 %25, 32
%27 = ashr exact i64 %26, 32
%28 = getelementptr inbounds i8, i8* %21, i64 %27
store i8 0, i8* %28
%29 = tail call i32 @print_string(i8 signext %0, i8* %21, i32 %3, i8 signext %4)
br i1 %16, label %30, label %32
30: ; preds = %20
%31 = tail call i32 @free(i8* %21)
br label %32
32: ; preds = %30, %20
ret void
}