#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 uint8_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, char *);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int write(int, int *, int);
int dump_eeprom(int fd, int out) {
  uint8_t buf[8192 + 2];
  int rc = 0;
  int i;
  for (i = 0; i < 8192; i += 8192) {
    buf[0] = i >> 8;
    buf[1] = i;
    rc = write(fd, buf, 2);
    if (rc != 2) {
      fprintf(stderr, "writing address failed: %d/%d/%s\n", rc, errno,
              strerror(errno));
      return 1;
    }
    rc = read(fd, buf, 8192);
    if (rc != 8192) {
      fprintf(stderr, "Failed to read: %d/%d/%s\n", rc, errno, strerror(errno));
      return 1;
    }
    write(out, buf, 8192);
  }
  return 0;
}


x86 assembly (clang, -O0):
<dump_eeprom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8040,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x8024(%rbp)
movl   $0x0,-0x8028(%rbp)
cmpl   $0x2000,-0x8028(%rbp)
jge    12d5 <dump_eeprom+0x195>
mov    -0x8028(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x8020(%rbp)
mov    -0x8028(%rbp),%eax
mov    %eax,-0x801c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x8020(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x8024(%rbp)
cmpl   $0x2,-0x8024(%rbp)
je     121c <dump_eeprom+0xdc>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8034(%rbp)
mov    -0x8024(%rbp),%eax
mov    %eax,-0x8030(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x802c(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x8034(%rbp),%edi
mov    -0x8030(%rbp),%edx
mov    -0x802c(%rbp),%ecx
mov    %rax,%r8
lea    0xdf5(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <dump_eeprom+0x19c>
mov    -0x8(%rbp),%edi
lea    -0x8020(%rbp),%rsi
mov    $0x2000,%edx
call   1040 <read@plt>
mov    %eax,-0x8024(%rbp)
cmpl   $0x2000,-0x8024(%rbp)
je     12ab <dump_eeprom+0x16b>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8040(%rbp)
mov    -0x8024(%rbp),%eax
mov    %eax,-0x803c(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8038(%rbp)
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x8040(%rbp),%edi
mov    -0x803c(%rbp),%edx
mov    -0x8038(%rbp),%ecx
mov    %rax,%r8
lea    0xd88(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <dump_eeprom+0x19c>
mov    -0xc(%rbp),%edi
lea    -0x8020(%rbp),%rsi
mov    $0x2000,%edx
call   1030 <write@plt>
mov    -0x8028(%rbp),%eax
add    $0x2000,%eax
mov    %eax,-0x8028(%rbp)
jmp    1165 <dump_eeprom+0x25>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x8040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_eeprom>:
push   %rbp
push   %r14
push   %rbx
sub    $0x8010,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    1194 <dump_eeprom+0x54>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x2000,%edx
call   1040 <read@plt>
cmp    $0x2000,%eax
jne    11b9 <dump_eeprom+0x79>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x2000,%edx
call   1030 <write@plt>
xor    %eax,%eax
jmp    11f0 <dump_eeprom+0xb0>
mov    %eax,%ebp
mov    0x2e43(%rip),%rax
mov    (%rax),%r14d
mov    0x2e31(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe49(%rip),%rsi
jmp    11dc <dump_eeprom+0x9c>
mov    %eax,%ebp
mov    0x2e1e(%rip),%rax
mov    (%rax),%r14d
mov    0x2e0c(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe46(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %rax,%r8
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x8010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"writing address failed: %d/%d/%s\0A\00"
@errno = global i32 0
@.str.1 = [26 x i8] c"Failed to read: %d/%d/%s\0A\00"
define i32 @dump_eeprom(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [8194 x i32]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %47, %2
%10 = load i32, i32* %8
%11 = icmp slt i32 %10, 8192
br i1 %11, label %12, label %50
12: ; preds = %9
%13 = load i32, i32* %8
%14 = ashr i32 %13, 8
%15 = getelementptr inbounds [8194 x i32], [8194 x i32]* %6, i64 0, i64 0
store i32 %14, i32* %15
%16 = load i32, i32* %8
%17 = getelementptr inbounds [8194 x i32], [8194 x i32]* %6, i64 0, i64 1
store i32 %16, i32* %17
%18 = load i32, i32* %4
%19 = getelementptr inbounds [8194 x i32], [8194 x i32]* %6, i64 0, i64 0
%20 = call i32 @write(i32 %18, i32* %19, i32 2)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 2
br i1 %22, label %23, label %30
23: ; preds = %12
%24 = load i32, i32* @stderr
%25 = load i32, i32* %7
%26 = load i32, i32* @errno
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26, i8* %28)
store i32 1, i32* %3
br label %51
30: ; preds = %12
%31 = load i32, i32* %4
%32 = getelementptr inbounds [8194 x i32], [8194 x i32]* %6, i64 0, i64 0
%33 = call i32 @read(i32 %31, i32* %32, i32 8192)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 8192
br i1 %35, label %36, label %43
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = load i32, i32* %7
%39 = load i32, i32* @errno
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %39, i8* %41)
store i32 1, i32* %3
br label %51
43: ; preds = %30
%44 = load i32, i32* %5
%45 = getelementptr inbounds [8194 x i32], [8194 x i32]* %6, i64 0, i64 0
%46 = call i32 @write(i32 %44, i32* %45, i32 8192)
br label %47
47: ; preds = %43
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 8192
store i32 %49, i32* %8
br label %9
50: ; preds = %9
store i32 0, i32* %3
br label %51
51: ; preds = %50, %36, %23
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"writing address failed: %d/%d/%s\0A\00"
@errno = global i32 0
@.str.1 = [26 x i8] c"Failed to read: %d/%d/%s\0A\00"
define i32 @dump_eeprom(i32 %0, i32 %1) {
%3 = alloca [8194 x i32]
%4 = bitcast [8194 x i32]* %3 to i8*
%5 = getelementptr inbounds [8194 x i32], [8194 x i32]* %3, i64 0, i64 0
%6 = getelementptr inbounds [8194 x i32], [8194 x i32]* %3, i64 0, i64 1
store i32 0, i32* %5
store i32 0, i32* %6
%7 = call i32 @write(i32 %0, i32* %5, i32 2)
%8 = icmp eq i32 %7, 2
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %11, i8* %12)
br label %24
14: ; preds = %2
%15 = call i32 @read(i32 %0, i32* %5, i32 8192)
%16 = icmp eq i32 %15, 8192
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %19, i8* %20)
br label %24
22: ; preds = %14
%23 = call i32 @write(i32 %1, i32* %5, i32 8192)
br label %24
24: ; preds = %22, %17, %9
%25 = phi i32 [ 1, %9 ], [ 1, %17 ], [ 0, %22 ]
ret i32 %25
}