#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 */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int ioctl(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  char buf[1024 * 64];
  printf("Reading flash...\n");
  int fd = open("/dev/flash", O_RDONLY);
  if (fd <= 0) {
    perror("Open error ");
    return -1;
  }
  int rc = ioctl(fd, 0x40045201, 0);
  if (rc != 0x8C2) {
    printf("problem with flash\n");
    return -1;
  }
  lseek(fd, 0, SEEK_SET);
  rc = read(fd, buf, 0xE000);
  if (rc != 0xE000) {
    perror("read error ");
    close(fd);
    return -1;
  }
  int out = open("/tmp/boot.rom", O_CREAT | O_WRONLY | O_TRUNC, 0666);
  if (out <= 0) {
    perror("write");
    return -1;
  }
  write(out, buf, 0xE000);
  close(out);
  lseek(fd, 0x10000 + 0x2990, SEEK_SET);
  out = open("/tmp/wma11b.rom", O_CREAT | O_WRONLY | O_TRUNC, 0666);
  if (out <= 0) {
    perror("write");
    return -1;
  }
  int got = 0;
  while (got < (0x200000)) {
    lseek(fd, got, SEEK_SET);
    rc = read(fd, buf, 0x1000);
    if (rc != 0x1000) {
      perror("read");
      return -1;
    }
    got += rc;
    printf("got %d of %d\n", got, 0x200000);
    rc = write(out, buf, rc);
  }
  perror("write ");
  close(out);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x10014(%rbp)
cmpl   $0x0,-0x10014(%rbp)
jg     11e2 <main+0x62>
lea    0xe4c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1460 <main+0x2e0>
mov    -0x10014(%rbp),%edi
mov    $0x40045201,%esi
xor    %edx,%edx
call   1060 <ioctl@plt>
mov    %eax,-0x10018(%rbp)
cmpl   $0x8c2,-0x10018(%rbp)
je     1224 <main+0xa4>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1460 <main+0x2e0>
mov    -0x10014(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x10014(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0xe000,%edx
call   1080 <read@plt>
mov    %eax,-0x10018(%rbp)
cmpl   $0xe000,-0x10018(%rbp)
je     128a <main+0x10a>
lea    0xdcf(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x10014(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1460 <main+0x2e0>
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    0x2d1e(%rip),%rax
or     (%rax),%esi
mov    0x2d4d(%rip),%rax
or     (%rax),%esi
lea    0xd9d(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x1001c(%rbp)
cmpl   $0x0,-0x1001c(%rbp)
jg     12e3 <main+0x163>
lea    0xd85(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1460 <main+0x2e0>
mov    -0x1001c(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0xe000,%edx
call   1030 <write@plt>
mov    -0x1001c(%rbp),%edi
call   1070 <close@plt>
mov    -0x10014(%rbp),%edi
mov    0x2cb6(%rip),%rax
mov    (%rax),%edx
mov    $0x12990,%esi
call   1050 <lseek@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
mov    0x2c8a(%rip),%rax
or     (%rax),%esi
mov    0x2cb9(%rip),%rax
or     (%rax),%esi
lea    0xd1d(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x1001c(%rbp)
cmpl   $0x0,-0x1001c(%rbp)
jg     1377 <main+0x1f7>
lea    0xcf1(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1460 <main+0x2e0>
movl   $0x0,-0x10020(%rbp)
cmpl   $0x200000,-0x10020(%rbp)
jge    143e <main+0x2be>
mov    -0x10014(%rbp),%edi
mov    -0x10020(%rbp),%esi
mov    0x2c24(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0x10014(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x10018(%rbp)
cmpl   $0x1000,-0x10018(%rbp)
je     13f0 <main+0x270>
lea    0xc8e(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1460 <main+0x2e0>
mov    -0x10018(%rbp),%eax
add    -0x10020(%rbp),%eax
mov    %eax,-0x10020(%rbp)
mov    -0x10020(%rbp),%esi
lea    0xc63(%rip),%rdi
mov    $0x200000,%edx
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1001c(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    -0x10018(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x10018(%rbp)
jmp    1381 <main+0x201>
lea    0xc3b(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1001c(%rbp),%edi
call   1070 <close@plt>
mov    -0x10014(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x10020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10008,%rsp
lea    0xeb9(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
jle    1203 <main+0x73>
mov    %eax,%r13d
mov    %eax,%edi
mov    $0x40045201,%esi
xor    %edx,%edx
call   1070 <ioctl@plt>
cmp    $0x8c2,%eax
je     120f <main+0x7f>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x10008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe01(%rip),%rdi
jmp    12c5 <main+0x135>
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rsp,%rsi
mov    %r13d,%edi
mov    $0xe000,%edx
call   1090 <read@plt>
cmp    $0xe000,%eax
jne    12cf <main+0x13f>
mov    0x2db4(%rip),%r14
mov    0x2d6d(%rip),%rbx
mov    (%rbx),%esi
or     (%r14),%esi
mov    0x2d99(%rip),%r15
or     (%r15),%esi
lea    0xdc2(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
jle    12be <main+0x12e>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xe000,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    $0x12990,%esi
call   1060 <lseek@plt>
mov    (%rbx),%esi
or     (%r14),%esi
or     (%r15),%esi
lea    0xd89(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
jg     12e8 <main+0x158>
lea    0xd6c(%rip),%rdi
call   10b0 <perror@plt>
jmp    11ec <main+0x5c>
lea    0xd41(%rip),%rdi
call   10b0 <perror@plt>
mov    %r13d,%edi
call   1080 <close@plt>
jmp    11ec <main+0x5c>
mov    %eax,%r14d
mov    %rsp,%r12
lea    0xd57(%rip),%r15
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    0x2cc1(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    %ebp,%esi
call   1060 <lseek@plt>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1090 <read@plt>
cmp    $0x1000,%eax
jne    137e <main+0x1ee>
lea    0x1000(%rbp),%ebx
mov    %r15,%rdi
mov    %ebx,%esi
mov    $0x200000,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
cmp    $0x1ff000,%ebp
mov    %ebx,%ebp
jb     1300 <main+0x170>
lea    0xcf8(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %r13d,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    11f1 <main+0x61>
lea    0xcc2(%rip),%rdi
jmp    12c5 <main+0x135>

LLVM IR (-O0):
@.str = [18 x i8] c"Reading flash...\0A\00"
@.str.1 = [11 x i8] c"/dev/flash\00"
@O_RDONLY = global i32 0
@.str.2 = [12 x i8] c"Open error \00"
@.str.3 = [20 x i8] c"problem with flash\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [12 x i8] c"read error \00"
@.str.5 = [14 x i8] c"/tmp/boot.rom\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.6 = [6 x i8] c"write\00"
@.str.7 = [16 x i8] c"/tmp/wma11b.rom\00"
@.str.8 = [5 x i8] c"read\00"
@.str.9 = [14 x i8] c"got %d of %d\0A\00"
@.str.10 = [7 x i8] c"write \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [65536 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @O_RDONLY
%9 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %96
14: ; preds = %0
%15 = load i32, i32* %3
%16 = call i32 @ioctl(i32 %15, i32 1074024961, i32 0)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 2242
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %96
21: ; preds = %14
%22 = load i32, i32* %3
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %22, i32 0, i32 %23)
%25 = load i32, i32* %3
%26 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 57344)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp ne i32 %28, 57344
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %1
br label %96
34: ; preds = %21
%35 = load i32, i32* @O_CREAT
%36 = load i32, i32* @O_WRONLY
%37 = or i32 %35, %36
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %39, i32 438)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %1
br label %96
45: ; preds = %34
%46 = load i32, i32* %5
%47 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%48 = call i32 @write(i32 %46, i8* %47, i32 57344)
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %3
%52 = load i32, i32* @SEEK_SET
%53 = call i32 @lseek(i32 %51, i32 76176, i32 %52)
%54 = load i32, i32* @O_CREAT
%55 = load i32, i32* @O_WRONLY
%56 = or i32 %54, %55
%57 = load i32, i32* @O_TRUNC
%58 = or i32 %56, %57
%59 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %58, i32 438)
store i32 %59, i32* %5
%60 = load i32, i32* %5
%61 = icmp sle i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %45
%63 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %1
br label %96
64: ; preds = %45
store i32 0, i32* %6
br label %65
65: ; preds = %80, %64
%66 = load i32, i32* %6
%67 = icmp slt i32 %66, 2097152
br i1 %67, label %68, label %90
68: ; preds = %65
%69 = load i32, i32* %3
%70 = load i32, i32* %6
%71 = load i32, i32* @SEEK_SET
%72 = call i32 @lseek(i32 %69, i32 %70, i32 %71)
%73 = load i32, i32* %3
%74 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%75 = call i32 @read(i32 %73, i8* %74, i32 4096)
store i32 %75, i32* %4
%76 = load i32, i32* %4
%77 = icmp ne i32 %76, 4096
br i1 %77, label %78, label %80
78: ; preds = %68
%79 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %1
br label %96
80: ; preds = %68
%81 = load i32, i32* %4
%82 = load i32, i32* %6
%83 = add nsw i32 %82, %81
store i32 %83, i32* %6
%84 = load i32, i32* %6
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i32 %84, i32 2097152)
%86 = load i32, i32* %5
%87 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%88 = load i32, i32* %4
%89 = call i32 @write(i32 %86, i8* %87, i32 %88)
store i32 %89, i32* %4
br label %65
90: ; preds = %65
%91 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
%92 = load i32, i32* %5
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* %3
%95 = call i32 @close(i32 %94)
br label %96
96: ; preds = %90, %78, %62, %43, %30, %19, %12
%97 = load i32, i32* %1
ret i32 %97
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"/dev/flash\00"
@O_RDONLY = global i32 0
@.str.2 = [12 x i8] c"Open error \00"
@SEEK_SET = global i32 0
@.str.4 = [12 x i8] c"read error \00"
@.str.5 = [14 x i8] c"/tmp/boot.rom\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.6 = [6 x i8] c"write\00"
@.str.7 = [16 x i8] c"/tmp/wma11b.rom\00"
@.str.8 = [5 x i8] c"read\00"
@.str.9 = [14 x i8] c"got %d of %d\0A\00"
@.str.10 = [7 x i8] c"write \00"
@str = [17 x i8] c"Reading flash...\00"
@str.11 = [19 x i8] c"problem with flash\00"
define i32 @main() {
%1 = alloca [65536 x i8]
%2 = getelementptr inbounds [65536 x i8], [65536 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %63
9: ; preds = %0
%10 = tail call i32 @ioctl(i32 %5, i32 1074024961, i32 0)
%11 = icmp eq i32 %10, 2242
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.11, i64 0, i64 0))
br label %63
14: ; preds = %9
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %5, i32 0, i32 %15)
%17 = call i32 @read(i32 %5, i8* %2, i32 57344)
%18 = icmp eq i32 %17, 57344
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%21 = tail call i32 @close(i32 %5)
br label %63
22: ; preds = %14
%23 = load i32, i32* @O_CREAT
%24 = load i32, i32* @O_WRONLY
%25 = or i32 %24, %23
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %27, i32 438)
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %63
32: ; preds = %22
%33 = call i32 @write(i32 %28, i8* %2, i32 57344)
%34 = tail call i32 @close(i32 %28)
%35 = load i32, i32* @SEEK_SET
%36 = tail call i32 @lseek(i32 %5, i32 76176, i32 %35)
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_WRONLY
%39 = or i32 %38, %37
%40 = load i32, i32* @O_TRUNC
%41 = or i32 %39, %40
%42 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %41, i32 438)
%43 = icmp slt i32 %42, 1
br i1 %43, label %44, label %46
44: ; preds = %32
%45 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %63
46: ; preds = %32, %54
%47 = phi i32 [ %55, %54 ], [ 0, %32 ]
%48 = load i32, i32* @SEEK_SET
%49 = tail call i32 @lseek(i32 %5, i32 %47, i32 %48)
%50 = call i32 @read(i32 %5, i8* %2, i32 4096)
%51 = icmp eq i32 %50, 4096
br i1 %51, label %54, label %52
52: ; preds = %46
%53 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %63
54: ; preds = %46
%55 = add nuw nsw i32 %47, 4096
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i32 %55, i32 2097152)
%57 = call i32 @write(i32 %42, i8* %2, i32 4096)
%58 = icmp ult i32 %47, 2093056
br i1 %58, label %46, label %59
59: ; preds = %54
%60 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
%61 = tail call i32 @close(i32 %42)
%62 = tail call i32 @close(i32 %5)
br label %63
63: ; preds = %12, %19, %52, %59, %44, %30, %7
%64 = phi i32 [ -1, %7 ], [ -1, %12 ], [ -1, %19 ], [ -1, %30 ], [ -1, %44 ], [ -1, %52 ], [ 0, %59 ]
ret i32 %64
}