#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ writeln(unsigned char *, size_t, int, int);
void dump(FILE *file, int offset, int bytesToRead, int bytesPerLine) {
  if (offset != 0) {
    if (fseek(file, offset, 0) != 0) {
      fprintf(stderr, "error: cannot locate offset in file\n");
      exit(1);
    }
  }
  unsigned char *buffer = (unsigned char *)malloc(bytesPerLine);
  if (buffer == NULL) {
    fprintf(stderr, "error: insufficient memory\n");
    exit(1);
  }
  size_t numBytes;
  size_t maxBytes;
  while (true) {
    if (bytesToRead < 0) {
      maxBytes = bytesPerLine;
    } else if (bytesPerLine < bytesToRead) {
      maxBytes = bytesPerLine;
    } else {
      maxBytes = bytesToRead;
    }
    numBytes = fread(buffer, sizeof(char), maxBytes, file);
    if (numBytes > 0) {
      writeln(buffer, numBytes, offset, bytesPerLine);
      offset += numBytes;
      bytesToRead -= numBytes;
    } else {
      break;
    }
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11cb <dump+0x5b>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11c6 <dump+0x56>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11cb <dump+0x5b>
mov    -0x14(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1201 <dump+0x91>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1206 <dump+0x96>
cmpl   $0x0,-0x10(%rbp)
jge    121d <dump+0xad>
movslq -0x14(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1243 <dump+0xd3>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1236 <dump+0xc6>
movslq -0x14(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    123e <dump+0xce>
movslq -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1243 <dump+0xd3>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    129c <dump+0x12c>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1050 <writeln@plt>
mov    -0x28(%rbp),%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%rcx
movslq -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x10(%rbp)
jmp    12a1 <dump+0x131>
jmp    12a6 <dump+0x136>
jmp    1206 <dump+0x96>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %esi,%r13d
mov    %rdi,%r15
test   %esi,%esi
je     11a0 <dump+0x30>
mov    %r15,%rdi
mov    %r13d,%esi
xor    %edx,%edx
call   1080 <fseek@plt>
test   %rax,%rax
jne    1248 <dump+0xd8>
mov    %r14d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1236 <dump+0xc6>
mov    %rax,%r12
cmp    %r14d,%ebp
mov    %ebp,%eax
cmovg  %r14d,%eax
test   %ebp,%ebp
cmovs  %r14d,%eax
movslq %eax,%rdx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1220 <dump+0xb0>
mov    %rax,%rbx
xchg   %ax,%ax
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
mov    %r14d,%ecx
call   1050 <writeln@plt>
add    %ebx,%r13d
sub    %ebx,%ebp
cmp    %r14d,%ebp
mov    %ebp,%eax
cmovg  %r14d,%eax
test   %ebp,%ebp
cmovs  %r14d,%eax
movslq %eax,%rdx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e0 <dump+0x70>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
jmp    1258 <dump+0xe8>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"error: cannot locate offset in file\0A\00"
@.str.1 = [28 x i8] c"error: insufficient memory\0A\00"
define void @dump(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %4
%15 = load i32*, i32** %5
%16 = load i32, i32* %6
%17 = call i64 @fseek(i32* %15, i32 %16, i32 0)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
br label %24
24: ; preds = %23, %4
%25 = load i32, i32* %8
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %24
br label %35
35: ; preds = %34, %76
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
store i64 %40, i64* %11
br label %52
41: ; preds = %35
%42 = load i32, i32* %8
%43 = load i32, i32* %7
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
store i64 %47, i64* %11
br label %51
48: ; preds = %41
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
store i64 %50, i64* %11
br label %51
51: ; preds = %48, %45
br label %52
52: ; preds = %51, %38
%53 = load i8*, i8** %9
%54 = load i64, i64* %11
%55 = load i32*, i32** %5
%56 = call i64 @fread(i8* %53, i32 1, i64 %54, i32* %55)
store i64 %56, i64* %10
%57 = load i64, i64* %10
%58 = icmp ugt i64 %57, 0
br i1 %58, label %59, label %75
59: ; preds = %52
%60 = load i8*, i8** %9
%61 = load i64, i64* %10
%62 = load i32, i32* %6
%63 = load i32, i32* %8
%64 = call i32 @writeln(i8* %60, i64 %61, i32 %62, i32 %63)
%65 = load i64, i64* %10
%66 = load i32, i32* %6
%67 = sext i32 %66 to i64
%68 = add i64 %67, %65
%69 = trunc i64 %68 to i32
store i32 %69, i32* %6
%70 = load i64, i64* %10
%71 = load i32, i32* %7
%72 = sext i32 %71 to i64
%73 = sub i64 %72, %70
%74 = trunc i64 %73 to i32
store i32 %74, i32* %7
br label %76
75: ; preds = %52
br label %77
76: ; preds = %59
br label %35
77: ; preds = %75
%78 = load i8*, i8** %9
%79 = call i32 @free(i8* %78)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"error: cannot locate offset in file\0A\00"
@.str.1 = [28 x i8] c"error: insufficient memory\0A\00"
define void @dump(i32* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %1, 0
br i1 %5, label %13, label %6
6: ; preds = %4
%7 = tail call i64 @fseek(i32* %0, i32 %1, i32 0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6, %4
%14 = tail call i64 @malloc(i32 %3)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %25, label %17
17: ; preds = %13
%18 = icmp slt i32 %2, 0
%19 = icmp sgt i32 %2, %3
%20 = or i1 %18, %19
%21 = select i1 %20, i32 %3, i32 %2
%22 = sext i32 %21 to i64
%23 = tail call i64 @fread(i8* %15, i32 1, i64 %22, i32* %0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %44, label %29
25: ; preds = %13
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %17, %29
%30 = phi i64 [ %42, %29 ], [ %23, %17 ]
%31 = phi i32 [ %35, %29 ], [ %1, %17 ]
%32 = phi i32 [ %36, %29 ], [ %2, %17 ]
%33 = tail call i32 @writeln(i8* %15, i64 %30, i32 %31, i32 %3)
%34 = trunc i64 %30 to i32
%35 = add i32 %31, %34
%36 = sub i32 %32, %34
%37 = icmp slt i32 %36, 0
%38 = icmp sgt i32 %36, %3
%39 = or i1 %37, %38
%40 = select i1 %39, i32 %3, i32 %36
%41 = sext i32 %40 to i64
%42 = tail call i64 @fread(i8* %15, i32 1, i64 %41, i32* %0)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %29
44: ; preds = %29, %17
%45 = tail call i32 @free(i8* %15)
ret void
}