#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ flash_erase(int, int);
int /*<<< orphan*/ flash_read(int, int);
int /*<<< orphan*/ flash_write(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getopt(int, char **, char *);
scalar_t__ init_info();
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ stderr;
int strtol(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  int opt;
  char method = '0';
  int offset = 0, count = 0, value = -1, end = 0;
  if (argc < 3)
    usage(argv[0]);
  if (init_info())
    exit(EXIT_FAILURE);
  while ((opt = getopt(argc, argv, "r:w:f:l:o:c:")) != -1) {
    switch (opt) {
    case 'r':
      offset = strtol(optarg, NULL, 16);
      method = 'r';
      break;
    case 'w':
      offset = strtol(optarg, NULL, 16);
      method = 'w';
      break;
    case 'f':
      offset = strtol(optarg, NULL, 16);
      method = 'e';
      break;
    case 'l':
      end = strtol(optarg, NULL, 16);
      break;
    case 'c':
      count = strtol(optarg, NULL, 10);
      if (count > 0x10000) {
        fprintf(stderr, "MAX %d bytes\n", 0x10000);
        return 0;
      }
      break;
    case 'o':
      value = strtol(optarg, NULL, 16);
      break;
    default:
      usage(argv[0]);
    }
  }
  switch (method) {
  case 'r':
    if (count == 0)
      break;
    flash_read(offset, count);
    return 0;
  case 'w':
    if (value == -1)
      break;
    flash_write(offset, value);
    return 0;
  case 'e':
    if (end == 0)
      break;
    flash_erase(offset, end);
    return 0;
  }
  usage(argv[0]);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x30,-0x15(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11dc <main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
mov    $0x0,%al
call   1090 <init_info@plt>
cmp    $0x0,%rax
je     11fb <main+0x6b>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    1200 <main+0x70>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe46(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1352 <main+0x1c2>
mov    -0x14(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x14,%eax
ja     1341 <main+0x1b1>
mov    -0x30(%rbp),%rax
lea    0xdc1(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x1c(%rbp)
movb   $0x72,-0x15(%rbp)
jmp    134d <main+0x1bd>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x1c(%rbp)
movb   $0x77,-0x15(%rbp)
jmp    134d <main+0x1bd>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x1c(%rbp)
movb   $0x65,-0x15(%rbp)
jmp    134d <main+0x1bd>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x28(%rbp)
jmp    134d <main+0x1bd>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x10000,-0x20(%rbp)
jle    131d <main+0x18d>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
lea    0xd5a(%rip),%rsi
mov    $0x10000,%edx
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1407 <main+0x277>
jmp    134d <main+0x1bd>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x24(%rbp)
jmp    134d <main+0x1bd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
jmp    1200 <main+0x70>
movsbl -0x15(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x65,%eax
je     13d5 <main+0x245>
jmp    1367 <main+0x1d7>
mov    -0x34(%rbp),%eax
sub    $0x72,%eax
je     1389 <main+0x1f9>
jmp    1378 <main+0x1e8>
mov    -0x34(%rbp),%eax
sub    $0x77,%eax
je     13af <main+0x21f>
jmp    13fb <main+0x26b>
cmpl   $0x0,-0x20(%rbp)
jne    1398 <main+0x208>
jmp    13fb <main+0x26b>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <flash_read@plt>
movl   $0x0,-0x4(%rbp)
jmp    1407 <main+0x277>
cmpl   $0xffffffff,-0x24(%rbp)
jne    13be <main+0x22e>
jmp    13fb <main+0x26b>
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1080 <flash_write@plt>
movl   $0x0,-0x4(%rbp)
jmp    1407 <main+0x277>
cmpl   $0x0,-0x28(%rbp)
jne    13e4 <main+0x254>
jmp    13fb <main+0x26b>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1060 <flash_erase@plt>
movl   $0x0,-0x4(%rbp)
jmp    1407 <main+0x277>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x2,%edi
jg     11b0 <main+0x20>
mov    (%rbx),%rdi
call   1070 <usage@plt>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
xor    %eax,%eax
call   1090 <init_info@plt>
test   %rax,%rax
jne    1363 <main+0x1d3>
movl   $0xffffffff,0x14(%rsp)
ff
mov    $0x30,%r14b
lea    0xe7b(%rip),%r13
lea    0xe20(%rip),%r15
xor    %r12d,%r12d
movl   $0x0,0x10(%rsp)
jmp    1201 <main+0x71>
nopl   (%rax)
cmp    $0xffffffff,%eax
je     12df <main+0x14f>
mov    (%rbx),%rdi
call   1070 <usage@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   10a0 <getopt@plt>
lea    -0x63(%rax),%ecx
cmp    $0x14,%ecx
ja     11f0 <main+0x60>
movslq (%r15,%rcx,4),%rax
add    %r15,%rax
jmp    *%rax
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,%r12d
cmp    $0x10001,%eax
jl     1201 <main+0x71>
jmp    1347 <main+0x1b7>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,0x8(%rsp)
mov    $0x65,%r14b
jmp    1201 <main+0x71>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,0x10(%rsp)
jmp    1201 <main+0x71>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,0x14(%rsp)
jmp    1201 <main+0x71>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,0x8(%rsp)
mov    $0x72,%r14b
jmp    1201 <main+0x71>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,0x8(%rsp)
mov    $0x77,%r14b
jmp    1201 <main+0x71>
cmp    $0x65,%r14b
je     1305 <main+0x175>
cmp    $0x77,%r14b
je     1319 <main+0x189>
cmp    $0x72,%r14b
jne    132e <main+0x19e>
test   %r12d,%r12d
je     132e <main+0x19e>
mov    0x8(%rsp),%rdi
mov    %r12d,%esi
call   1030 <flash_read@plt>
jmp    1336 <main+0x1a6>
mov    0x10(%rsp),%esi
test   %esi,%esi
je     132e <main+0x19e>
mov    0x8(%rsp),%rdi
call   1060 <flash_erase@plt>
jmp    1336 <main+0x1a6>
mov    0x14(%rsp),%esi
cmp    $0xffffffff,%esi
je     132e <main+0x19e>
mov    0x8(%rsp),%rdi
call   1080 <flash_write@plt>
jmp    1336 <main+0x1a6>
mov    (%rbx),%rdi
call   1070 <usage@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c8a(%rip),%rax
mov    (%rax),%edi
lea    0xd0a(%rip),%rsi
mov    $0x10000,%edx
call   1040 <fprintf@plt>
jmp    1336 <main+0x1a6>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [13 x i8] c"r:w:f:l:o:c:\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"MAX %d bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 48, i8* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 -1, i32* %10
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @usage(i8* %17)
br label %19
19: ; preds = %14, %2
%20 = call i64 (...) @init_info()
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %19
br label %26
26: ; preds = %62, %25
%27 = load i32, i32* %4
%28 = load i8**, i8*** %5
%29 = call i32 @getopt(i32 %27, i8** %28, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %29, i32* %6
%30 = icmp ne i32 %29, -1
br i1 %30, label %31, label %63
31: ; preds = %26
%32 = load i32, i32* %6
switch i32 %32, label %57 [
i32 114, label %33
i32 119, label %36
i32 102, label %39
i32 108, label %42
i32 99, label %45
i32 111, label %54
]
33: ; preds = %31
%34 = load i32, i32* @optarg
%35 = call i32 @strtol(i32 %34, i32* null, i32 16)
store i32 %35, i32* %8
store i8 114, i8* %7
br label %62
36: ; preds = %31
%37 = load i32, i32* @optarg
%38 = call i32 @strtol(i32 %37, i32* null, i32 16)
store i32 %38, i32* %8
store i8 119, i8* %7
br label %62
39: ; preds = %31
%40 = load i32, i32* @optarg
%41 = call i32 @strtol(i32 %40, i32* null, i32 16)
store i32 %41, i32* %8
store i8 101, i8* %7
br label %62
42: ; preds = %31
%43 = load i32, i32* @optarg
%44 = call i32 @strtol(i32 %43, i32* null, i32 16)
store i32 %44, i32* %11
br label %62
45: ; preds = %31
%46 = load i32, i32* @optarg
%47 = call i32 @strtol(i32 %46, i32* null, i32 10)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp sgt i32 %48, 65536
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 65536)
store i32 0, i32* %3
br label %95
53: ; preds = %45
br label %62
54: ; preds = %31
%55 = load i32, i32* @optarg
%56 = call i32 @strtol(i32 %55, i32* null, i32 16)
store i32 %56, i32* %10
br label %62
57: ; preds = %31
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = call i32 @usage(i8* %60)
br label %62
62: ; preds = %57, %54, %53, %42, %39, %36, %33
br label %26
63: ; preds = %26
%64 = load i8, i8* %7
%65 = sext i8 %64 to i32
switch i32 %65, label %90 [
i32 114, label %66
i32 119, label %74
i32 101, label %82
]
66: ; preds = %63
%67 = load i32, i32* %9
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %66
br label %90
70: ; preds = %66
%71 = load i32, i32* %8
%72 = load i32, i32* %9
%73 = call i32 @flash_read(i32 %71, i32 %72)
store i32 0, i32* %3
br label %95
74: ; preds = %63
%75 = load i32, i32* %10
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %78
77: ; preds = %74
br label %90
78: ; preds = %74
%79 = load i32, i32* %8
%80 = load i32, i32* %10
%81 = call i32 @flash_write(i32 %79, i32 %80)
store i32 0, i32* %3
br label %95
82: ; preds = %63
%83 = load i32, i32* %11
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %82
br label %90
86: ; preds = %82
%87 = load i32, i32* %8
%88 = load i32, i32* %11
%89 = call i32 @flash_erase(i32 %87, i32 %88)
store i32 0, i32* %3
br label %95
90: ; preds = %63, %85, %77, %69
%91 = load i8**, i8*** %5
%92 = getelementptr inbounds i8*, i8** %91, i64 0
%93 = load i8*, i8** %92
%94 = call i32 @usage(i8* %93)
br label %95
95: ; preds = %90, %86, %78, %70, %50
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [13 x i8] c"r:w:f:l:o:c:\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"MAX %d bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @usage(i8* %5)
br label %7
7: ; preds = %4, %2
%8 = tail call i64 (...) @init_info()
%9 = icmp eq i64 %8, 0
br i1 %9, label %27, label %10
10: ; preds = %7
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %53, %55
%14 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
switch i32 %14, label %55 [
i32 -1, label %58
i32 114, label %15
i32 119, label %18
i32 102, label %21
i32 108, label %37
i32 99, label %40
i32 111, label %47
]
15: ; preds = %13
%16 = load i32, i32* @optarg
%17 = tail call i32 @strtol(i32 %16, i32* null, i32 16)
br label %24
18: ; preds = %13
%19 = load i32, i32* @optarg
%20 = tail call i32 @strtol(i32 %19, i32* null, i32 16)
br label %24
21: ; preds = %13
%22 = load i32, i32* @optarg
%23 = tail call i32 @strtol(i32 %22, i32* null, i32 16)
br label %24
24: ; preds = %21, %18, %15
%25 = phi i8 [ 114, %15 ], [ 119, %18 ], [ 101, %21 ]
%26 = phi i32 [ %17, %15 ], [ %20, %18 ], [ %23, %21 ]
br label %27
27: ; preds = %7, %24
%28 = phi i8 [ %25, %24 ], [ 48, %7 ]
%29 = phi i32 [ %26, %24 ], [ 0, %7 ]
%30 = phi i32 [ %34, %24 ], [ 0, %7 ]
%31 = phi i32 [ %51, %24 ], [ -1, %7 ]
%32 = phi i32 [ %54, %24 ], [ 0, %7 ]
br label %33
33: ; preds = %27, %40
%34 = phi i32 [ %30, %27 ], [ %42, %40 ]
%35 = phi i32 [ %31, %27 ], [ %51, %40 ]
%36 = phi i32 [ %32, %27 ], [ %54, %40 ]
br label %50
37: ; preds = %13
%38 = load i32, i32* @optarg
%39 = tail call i32 @strtol(i32 %38, i32* null, i32 16)
br label %53
40: ; preds = %13
%41 = load i32, i32* @optarg
%42 = tail call i32 @strtol(i32 %41, i32* null, i32 10)
%43 = icmp sgt i32 %42, 65536
br i1 %43, label %44, label %33
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = tail call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 65536)
br label %74
47: ; preds = %13
%48 = load i32, i32* @optarg
%49 = tail call i32 @strtol(i32 %48, i32* null, i32 16)
br label %50
50: ; preds = %33, %47
%51 = phi i32 [ %35, %33 ], [ %49, %47 ]
%52 = phi i32 [ %36, %33 ], [ %54, %47 ]
br label %53
53: ; preds = %50, %37
%54 = phi i32 [ %52, %50 ], [ %39, %37 ]
br label %13
55: ; preds = %13
%56 = load i8*, i8** %1
%57 = tail call i32 @usage(i8* %56)
br label %13
58: ; preds = %13
switch i8 %28, label %71 [
i8 114, label %59
i8 119, label %63
i8 101, label %67
]
59: ; preds = %58
%60 = icmp eq i32 %34, 0
br i1 %60, label %71, label %61
61: ; preds = %59
%62 = tail call i32 @flash_read(i32 %29, i32 %34)
br label %74
63: ; preds = %58
%64 = icmp eq i32 %51, -1
br i1 %64, label %71, label %65
65: ; preds = %63
%66 = tail call i32 @flash_write(i32 %29, i32 %51)
br label %74
67: ; preds = %58
%68 = icmp eq i32 %54, 0
br i1 %68, label %71, label %69
69: ; preds = %67
%70 = tail call i32 @flash_erase(i32 %29, i32 %54)
br label %74
71: ; preds = %67, %63, %59, %58
%72 = load i8*, i8** %1
%73 = tail call i32 @usage(i8* %72)
br label %74
74: ; preds = %71, %69, %65, %61, %44
ret i32 0
}