#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 */
long countBytesInFile(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(long);
int /*<<< orphan*/ perror(char *);
int readFileIntoString(char *, char *, long);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *readFileIntoStringWrapper(char *file_name) {
  int size = strlen("./scripts/") + strlen(file_name) + strlen(".rpt") + 1;
  char *file = malloc(size * sizeof(char));
  if (file == NULL) {
    perror("malloc");
    return NULL;
  }
  strcpy(file, "./scripts/");
  strcat(file, file_name);
  strcat(file, ".rpt");
  long int num_bytes = countBytesInFile(file);
  if (num_bytes == -1) {
    free(file);
    return NULL;
  }
  char *file_str = malloc(num_bytes + 1);
  if (file_str == NULL) {
    perror("malloc");
    free(file);
    return NULL;
  }
  int read = readFileIntoString(file_str, file, num_bytes);
  if (read == -1) {
    fprintf(stderr, "Failed to read file into string");
    free(file);
    free(file_str);
    return NULL;
  }
  free(file);
  return file_str;
}


x86 assembly (clang, -O0):
<readFileIntoStringWrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe5d(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
lea    0xe46(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rdi
shl    $0x0,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120c <readFileIntoStringWrapper+0x7c>
lea    0xe16(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1308 <readFileIntoStringWrapper+0x178>
mov    -0x20(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1050 <countBytesInFile@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1267 <readFileIntoStringWrapper+0xd7>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1308 <readFileIntoStringWrapper+0x178>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12a5 <readFileIntoStringWrapper+0x115>
lea    0xd86(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1308 <readFileIntoStringWrapper+0x178>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1090 <readFileIntoString@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12f7 <readFileIntoStringWrapper+0x167>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xd44(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1308 <readFileIntoStringWrapper+0x178>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFileIntoStringWrapper>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe5f(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
lea    0xe52(%rip),%rdi
call   1060 <strlen@plt>
add    %ebp,%ebx
add    %ebx,%eax
add    $0x1,%eax
movslq %eax,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1263 <readFileIntoStringWrapper+0xd3>
mov    %rax,%rbx
lea    0xe20(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
lea    0xe11(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
call   1050 <countBytesInFile@plt>
cmp    $0xffffffffffffffff,%rax
je     127d <readFileIntoStringWrapper+0xed>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1271 <readFileIntoStringWrapper+0xe1>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <readFileIntoString@plt>
cmp    $0xffffffff,%eax
je     1241 <readFileIntoStringWrapper+0xb1>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1288 <readFileIntoStringWrapper+0xf8>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
jmp    1280 <readFileIntoStringWrapper+0xf0>
lea    0xda6(%rip),%rdi
call   10a0 <perror@plt>
jmp    1285 <readFileIntoStringWrapper+0xf5>
lea    0xd98(%rip),%rdi
call   10a0 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"./scripts/\00"
@.str.1 = [5 x i8] c".rpt\00"
@.str.2 = [7 x i8] c"malloc\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Failed to read file into string\00"
define i8* @readFileIntoStringWrapper(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %9, %11
%13 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = add nsw i32 %12, %13
%15 = add nsw i32 %14, 1
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %67
24: ; preds = %1
%25 = load i8*, i8** %5
%26 = call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%27 = load i8*, i8** %5
%28 = load i8*, i8** %3
%29 = call i32 @strcat(i8* %27, i8* %28)
%30 = load i8*, i8** %5
%31 = call i32 @strcat(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = load i8*, i8** %5
%33 = call i64 @countBytesInFile(i8* %32)
store i64 %33, i64* %6
%34 = load i64, i64* %6
%35 = icmp eq i64 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %24
%37 = load i8*, i8** %5
%38 = call i32 @free(i8* %37)
store i8* null, i8** %2
br label %67
39: ; preds = %24
%40 = load i64, i64* %6
%41 = add nsw i64 %40, 1
%42 = call i8* @malloc(i64 %41)
store i8* %42, i8** %7
%43 = load i8*, i8** %7
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
store i8* null, i8** %2
br label %67
49: ; preds = %39
%50 = load i8*, i8** %7
%51 = load i8*, i8** %5
%52 = load i64, i64* %6
%53 = call i32 @readFileIntoString(i8* %50, i8* %51, i64 %52)
store i32 %53, i32* %8
%54 = load i32, i32* %8
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %63
56: ; preds = %49
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%59 = load i8*, i8** %5
%60 = call i32 @free(i8* %59)
%61 = load i8*, i8** %7
%62 = call i32 @free(i8* %61)
store i8* null, i8** %2
br label %67
63: ; preds = %49
%64 = load i8*, i8** %5
%65 = call i32 @free(i8* %64)
%66 = load i8*, i8** %7
store i8* %66, i8** %2
br label %67
67: ; preds = %63, %56, %45, %36, %22
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
@.str = [11 x i8] c"./scripts/\00"
@.str.1 = [5 x i8] c".rpt\00"
@.str.2 = [7 x i8] c"malloc\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Failed to read file into string\00"
define i8* @readFileIntoStringWrapper(i8* %0) {
%2 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%5 = add i32 %2, 1
%6 = add i32 %5, %3
%7 = add i32 %6, %4
%8 = sext i32 %7 to i64
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %38
13: ; preds = %1
%14 = tail call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @strcat(i8* %9, i8* %0)
%16 = tail call i32 @strcat(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i64 @countBytesInFile(i8* %9)
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @free(i8* %9)
br label %38
21: ; preds = %13
%22 = add nuw nsw i64 %17, 1
%23 = tail call i8* @malloc(i64 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @free(i8* %9)
br label %38
28: ; preds = %21
%29 = tail call i32 @readFileIntoString(i8* %23, i8* %9, i64 %17)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 @free(i8* %9)
%35 = tail call i32 @free(i8* %23)
br label %38
36: ; preds = %28
%37 = tail call i32 @free(i8* %9)
br label %38
38: ; preds = %19, %31, %36, %25, %11
%39 = phi i8* [ null, %11 ], [ null, %19 ], [ null, %25 ], [ null, %31 ], [ %23, %36 ]
ret i8* %39
}