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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ dump_buffer(unsigned char *, int /*<<< orphan*/, size_t);
scalar_t__ feof(int /*<<< orphan*/ *);
int ferror(int /*<<< orphan*/ *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned char *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
void dump(FILE *fp, int64_t offset, size_t sz) {
  if (sz) {
    fseek(fp, offset, SEEK_SET);
    unsigned char *buffer = malloc(sz);
    size_t actual = fread(buffer, 1, sz, fp);
    if (actual != sz) {
      if (feof(fp)) {
        printf("*** Unexpected EOF\n");
      } else {
        int err = ferror(fp);
        printf("*** Read error code: %d\n", err);
      }
    }
    dump_buffer(buffer, offset, actual);
    free(buffer);
  }
}


x86 assembly (clang, -O0):
<dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1249 <dump+0xc9>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1230 <dump+0xb0>
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     120e <dump+0x8e>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    122b <dump+0xab>
mov    -0x8(%rbp),%rdi
call   1040 <ferror@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1230 <dump+0xb0>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   10a0 <dump_buffer@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump>:
test   %rdx,%rdx
je     11f5 <dump+0x65>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   1060 <fread@plt>
mov    %rax,%r15
cmp    %rbx,%rax
je     120e <dump+0x7e>
mov    %rbp,%rdi
call   1080 <feof@plt>
test   %rax,%rax
je     11f6 <dump+0x66>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
jmp    120e <dump+0x7e>
ret
mov    %rbp,%rdi
call   1050 <ferror@plt>
lea    0xdfb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
call   10b0 <dump_buffer@plt>
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [20 x i8] c"*** Unexpected EOF\0A\00"
@.str.1 = [25 x i8] c"*** Read error code: %d\0A\00"
define void @dump(i32* %0, i32 %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %45
12: ; preds = %3
%13 = load i32*, i32** %4
%14 = load i32, i32* %5
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseek(i32* %13, i32 %14, i32 %15)
%17 = load i64, i64* %6
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i64, i64* %6
%21 = load i32*, i32** %4
%22 = call i64 @fread(i8* %19, i32 1, i64 %20, i32* %21)
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = load i64, i64* %6
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %38
26: ; preds = %12
%27 = load i32*, i32** %4
%28 = call i64 @feof(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %37
32: ; preds = %26
%33 = load i32*, i32** %4
%34 = call i32 @ferror(i32* %33)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %32, %30
br label %38
38: ; preds = %37, %12
%39 = load i8*, i8** %7
%40 = load i32, i32* %5
%41 = load i64, i64* %8
%42 = call i32 @dump_buffer(i8* %39, i32 %40, i64 %41)
%43 = load i8*, i8** %7
%44 = call i32 @free(i8* %43)
br label %45
45: ; preds = %38, %3
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str.1 = [25 x i8] c"*** Read error code: %d\0A\00"
@str = [19 x i8] c"*** Unexpected EOF\00"
define void @dump(i32* nocapture %0, i32 %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %22, label %5
5: ; preds = %3
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @fseek(i32* %0, i32 %1, i32 %6)
%8 = tail call i8* @malloc(i64 %2)
%9 = tail call i64 @fread(i8* %8, i32 1, i64 %2, i32* %0)
%10 = icmp eq i64 %9, %2
br i1 %10, label %19, label %11
11: ; preds = %5
%12 = tail call i64 @feof(i32* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %19
16: ; preds = %11
%17 = tail call i32 @ferror(i32* %0)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %14, %16, %5
%20 = tail call i32 @dump_buffer(i8* %8, i32 %1, i64 %9)
%21 = tail call i32 @free(i8* %8)
br label %22
22: ; preds = %19, %3
ret void
}