#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fsReadNumber(unsigned char *, long, long *, int *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ stderr;
int fsReadString(unsigned char *buffer, long fileSize, long *contentOffset,
                 unsigned char **string) {
  int r = 0;
  int nameSize = 0;
  if ((r = fsReadNumber(buffer, fileSize, contentOffset, &nameSize)) != 0) {
    return r;
  }
  if (*contentOffset >= fileSize) {
    fprintf(stderr, "Error: Out of bounds (offset(%ld) >= fileSize(%ld)).\n",
            *contentOffset, fileSize);
    return -1;
  }
  if (*contentOffset + nameSize >= fileSize) {
    fprintf(stderr,
            "Error: Going out of bounds (offset(%ld) >= fileSize(%ld)).\n",
            *contentOffset + nameSize, fileSize);
    return -1;
  }
  unsigned char *_buffer =
      (unsigned char *)calloc(nameSize + 1, sizeof(unsigned char));
  if (!_buffer) {
    fprintf(stderr, "Error: not enough memory.\n");
    return -1;
  }
  memcpy(_buffer, &(buffer[*contentOffset]), nameSize);
  *contentOffset += nameSize;
  *string = _buffer;
  return 0;
}


x86 assembly (clang, -O0):
<fsReadString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <fsReadNumber@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     1192 <fsReadString+0x52>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1297 <fsReadString+0x157>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x18(%rbp),%rax
jl     11d1 <fsReadString+0x91>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <fsReadString+0x157>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jl     121e <fsReadString+0xde>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
movslq -0x30(%rbp),%rax
add    %rax,%rdx
mov    -0x18(%rbp),%rcx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <fsReadString+0x157>
mov    -0x30(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1260 <fsReadString+0x120>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <fsReadString+0x157>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
add    (%rax),%rsi
mov    -0x30(%rbp),%edx
call   1050 <memcpy@plt>
movslq -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsReadString>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rcx
call   1060 <fsReadNumber@plt>
test   %eax,%eax
jne    11e6 <fsReadString+0xa6>
mov    (%r15),%rdx
cmp    %rbx,%rdx
jge    11b5 <fsReadString+0x75>
movslq 0x4(%rsp),%rdi
add    %rdi,%rdx
cmp    %rbx,%rdx
jge    11c7 <fsReadString+0x87>
add    $0x1,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
test   %rax,%rax
je     11f2 <fsReadString+0xb2>
mov    %rax,%rbx
add    (%r15),%r12
mov    0x4(%rsp),%edx
mov    %rax,%rdi
mov    %r12,%rsi
call   1050 <memcpy@plt>
movslq 0x4(%rsp),%rax
add    %rax,(%r15)
mov    %rbx,(%r14)
xor    %eax,%eax
jmp    11e6 <fsReadString+0xa6>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
jmp    11d7 <fsReadString+0x97>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11e1 <fsReadString+0xa1>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [54 x i8] c"Error: Out of bounds (offset(%ld) >= fileSize(%ld)).\0A\00"
@.str.1 = [60 x i8] c"Error: Going out of bounds (offset(%ld) >= fileSize(%ld)).\0A\00"
@.str.2 = [27 x i8] c"Error: not enough memory.\0A\00"
define i32 @fsReadString(i8* %0, i64 %1, i64* %2, i8** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64* %2, i64** %8
store i8** %3, i8*** %9
store i32 0, i32* %10
store i32 0, i32* %11
%13 = load i8*, i8** %6
%14 = load i64, i64* %7
%15 = load i64*, i64** %8
%16 = call i32 @fsReadNumber(i8* %13, i64 %14, i64* %15, i32* %11)
store i32 %16, i32* %10
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = load i32, i32* %10
store i32 %19, i32* %5
br label %73
20: ; preds = %4
%21 = load i64*, i64** %8
%22 = load i64, i64* %21
%23 = load i64, i64* %7
%24 = icmp sge i64 %22, %23
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i64*, i64** %8
%28 = load i64, i64* %27
%29 = load i64, i64* %7
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i64 %28, i64 %29)
store i32 -1, i32* %5
br label %73
31: ; preds = %20
%32 = load i64*, i64** %8
%33 = load i64, i64* %32
%34 = load i32, i32* %11
%35 = sext i32 %34 to i64
%36 = add nsw i64 %33, %35
%37 = load i64, i64* %7
%38 = icmp sge i64 %36, %37
br i1 %38, label %39, label %48
39: ; preds = %31
%40 = load i32, i32* @stderr
%41 = load i64*, i64** %8
%42 = load i64, i64* %41
%43 = load i32, i32* %11
%44 = sext i32 %43 to i64
%45 = add nsw i64 %42, %44
%46 = load i64, i64* %7
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i64 %45, i64 %46)
store i32 -1, i32* %5
br label %73
48: ; preds = %31
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
%51 = call i64 @calloc(i32 %50, i32 1)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %12
%53 = load i8*, i8** %12
%54 = icmp ne i8* %53, null
br i1 %54, label %58, label %55
55: ; preds = %48
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %73
58: ; preds = %48
%59 = load i8*, i8** %12
%60 = load i8*, i8** %6
%61 = load i64*, i64** %8
%62 = load i64, i64* %61
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = load i32, i32* %11
%65 = call i32 @memcpy(i8* %59, i8* %63, i32 %64)
%66 = load i32, i32* %11
%67 = sext i32 %66 to i64
%68 = load i64*, i64** %8
%69 = load i64, i64* %68
%70 = add nsw i64 %69, %67
store i64 %70, i64* %68
%71 = load i8*, i8** %12
%72 = load i8**, i8*** %9
store i8* %71, i8** %72
store i32 0, i32* %5
br label %73
73: ; preds = %58, %55, %39, %25, %18
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [54 x i8] c"Error: Out of bounds (offset(%ld) >= fileSize(%ld)).\0A\00"
@.str.1 = [60 x i8] c"Error: Going out of bounds (offset(%ld) >= fileSize(%ld)).\0A\00"
@.str.2 = [27 x i8] c"Error: not enough memory.\0A\00"
define i32 @fsReadString(i8* %0, i64 %1, i64* %2, i8** nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%7 = call i32 @fsReadNumber(i8* %0, i64 %1, i64* %2, i32* %5)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %40
9: ; preds = %4
%10 = load i64, i64* %2
%11 = icmp slt i64 %10, %1
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i64 %10, i64 %1)
br label %40
15: ; preds = %9
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = add nsw i64 %10, %17
%19 = icmp slt i64 %18, %1
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i64 %18, i64 %1)
br label %40
23: ; preds = %15
%24 = add nsw i32 %16, 1
%25 = call i64 @calloc(i32 %24, i32 1)
%26 = inttoptr i64 %25 to i8*
%27 = icmp eq i64 %25, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %40
31: ; preds = %23
%32 = load i64, i64* %2
%33 = getelementptr inbounds i8, i8* %0, i64 %32
%34 = load i32, i32* %5
%35 = call i32 @memcpy(i8* %26, i8* %33, i32 %34)
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = load i64, i64* %2
%39 = add nsw i64 %38, %37
store i64 %39, i64* %2
store i8* %26, i8** %3
br label %40
40: ; preds = %28, %31, %4, %20, %12
%41 = phi i32 [ -1, %12 ], [ -1, %20 ], [ %7, %4 ], [ 0, %31 ], [ -1, %28 ]
ret i32 %41
}