#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ eeprom_read(int, int, unsigned char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, char *);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int i, j, fd;
  FILE *f;
  unsigned char data[0x10000];
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    printf("Cannot open %s: %s\n", argv[1], strerror(errno));
    exit(-1);
  }
  f = fopen("dump.bin", "w");
  for (i = 0; i < (4160 * 1024); i += 0x8000) {
    eeprom_read(fd, i, data, 0x8000);
    fwrite(data, 1, 0x8000, f);
  }
  fclose(f);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1209 <main+0x79>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x10038(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x10038(%rbp),%rsi
mov    %rax,%rdx
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe04(%rip),%rdi
lea    0xe06(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x410000,-0x14(%rbp)
jge    1275 <main+0xe5>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x10030(%rbp),%rdx
mov    $0x8000,%ecx
call   1080 <eeprom_read@plt>
lea    -0x10030(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8000,%edx
call   10a0 <fwrite@plt>
mov    -0x14(%rbp),%eax
add    $0x8000,%eax
mov    %eax,-0x14(%rbp)
jmp    1227 <main+0x97>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x10008,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1235 <main+0xa5>
mov    %eax,%r14d
lea    0xe54(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
mov    $0xffff8000,%ebp
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
add    $0x8000,%ebp
mov    %r14d,%edi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    $0x8000,%ecx
call   1080 <eeprom_read@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x8000,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    $0x408000,%ebp
jb     11e0 <main+0x50>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rbx
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb2(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"dump.bin\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [65536 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %23)
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %2
%27 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %27, i32** %9
store i32 0, i32* %6
br label %28
28: ; preds = %39, %26
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 4259840
br i1 %30, label %31, label %42
31: ; preds = %28
%32 = load i32, i32* %8
%33 = load i32, i32* %6
%34 = getelementptr inbounds [65536 x i8], [65536 x i8]* %10, i64 0, i64 0
%35 = call i32 @eeprom_read(i32 %32, i32 %33, i8* %34, i32 32768)
%36 = getelementptr inbounds [65536 x i8], [65536 x i8]* %10, i64 0, i64 0
%37 = load i32*, i32** %9
%38 = call i32 @fwrite(i8* %36, i32 1, i32 32768, i32* %37)
br label %39
39: ; preds = %31
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 32768
store i32 %41, i32* %6
br label %28
42: ; preds = %28
%43 = load i32*, i32** %9
%44 = call i32 @fclose(i32* %43)
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"dump.bin\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %13)
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %18
%19 = phi i32 [ 0, %16 ], [ %22, %18 ]
%20 = call i32 @eeprom_read(i32 %8, i32 %19, i8* %4, i32 32768)
%21 = call i32 @fwrite(i8* %4, i32 1, i32 32768, i32* %17)
%22 = add nuw nsw i32 %19, 32768
%23 = icmp ult i32 %19, 4227072
br i1 %23, label %18, label %24
24: ; preds = %18
%25 = call i32 @fclose(i32* %17)
%26 = call i32 @close(i32 %8)
ret i32 0
}