#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*/ MEMGETREGIONCOUNT;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int non_region_erase(int, int, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int region_erase(int, int, int, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int regcount;
  int Fd;
  int start;
  int count;
  int unlock;
  int res = 0;
  if (1 >= argc || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
    printf(
        "Usage: flash_erase MTD-device [start] [cnt (# erase blocks)] [lock]\n"
        "       flash_erase -h | --help\n");
    return 16;
  }
  if (argc > 2)
    start = strtol(argv[2], NULL, 0);
  else
    start = 0;
  if (argc > 3)
    count = strtol(argv[3], NULL, 0);
  else
    count = 1;
  if (argc > 4)
    unlock = strtol(argv[4], NULL, 0);
  else
    unlock = 0;
  if ((Fd = open(argv[1], O_RDWR)) < 0) {
    fprintf(stderr, "File open error\n");
    return 8;
  }
  printf("Erase Total %d Units\n", count);
  if (ioctl(Fd, MEMGETREGIONCOUNT, &regcount) == 0) {
    if (regcount == 0) {
      res = non_region_erase(Fd, start, count, unlock);
    } else {
      res = region_erase(Fd, start, count, unlock, regcount);
    }
  }
  return res;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x1,%eax
cmp    -0x8(%rbp),%eax
jge    11e5 <main+0x65>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe46(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     11e5 <main+0x65>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2c(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11ff <main+0x7f>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x10,-0x4(%rbp)
jmp    133c <main+0x1bc>
cmpl   $0x2,-0x8(%rbp)
jle    1224 <main+0xa4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtol@plt>
mov    %eax,-0x1c(%rbp)
jmp    122b <main+0xab>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1250 <main+0xd0>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtol@plt>
mov    %eax,-0x20(%rbp)
jmp    1257 <main+0xd7>
movl   $0x1,-0x20(%rbp)
cmpl   $0x4,-0x8(%rbp)
jle    127c <main+0xfc>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtol@plt>
mov    %eax,-0x24(%rbp)
jmp    1283 <main+0x103>
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12c6 <main+0x146>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x8,-0x4(%rbp)
jmp    133c <main+0x1bc>
mov    -0x20(%rbp),%esi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    1336 <main+0x1b6>
cmpl   $0x0,-0x14(%rbp)
jne    1319 <main+0x199>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
call   1030 <non_region_erase@plt>
mov    %eax,-0x28(%rbp)
jmp    1331 <main+0x1b1>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%r8d
call   1080 <region_erase@plt>
mov    %eax,-0x28(%rbp)
jmp    1336 <main+0x1b6>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     121f <main+0x8f>
mov    %rsi,%r13
mov    %edi,%ebx
mov    0x8(%rsi),%rbp
lea    0xe50(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     121f <main+0x8f>
lea    0xec3(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     121f <main+0x8f>
xor    %r14d,%r14d
mov    $0x1,%r12d
cmp    $0x3,%ebx
jb     1241 <main+0xb1>
mov    0x10(%r13),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtol@plt>
mov    %eax,%r14d
cmp    $0x3,%ebx
je     1244 <main+0xb4>
mov    0x18(%r13),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtol@plt>
mov    %eax,%r12d
cmp    $0x5,%ebx
jb     1244 <main+0xb4>
mov    0x20(%r13),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtol@plt>
mov    %eax,%r15d
jmp    1244 <main+0xb4>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    $0x10,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
mov    0x8(%r13),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
test   %eax,%eax
js     12a5 <main+0x115>
mov    %eax,%ebx
lea    0xdb1(%rip),%rdi
xor    %ebp,%ebp
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
jne    1230 <main+0xa0>
mov    0x4(%rsp),%r8d
mov    %ebx,%edi
mov    %r14d,%esi
mov    %r12d,%edx
mov    %r15d,%ecx
test   %r8d,%r8d
je     12c4 <main+0x134>
call   1090 <region_erase@plt>
jmp    12c9 <main+0x139>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x8,%ebp
jmp    1230 <main+0xa0>
call   1040 <non_region_erase@plt>
mov    %eax,%ebp
jmp    1230 <main+0xa0>

LLVM IR (-O0):
@.str = [3 x i8] c"-h\00"
@.str.1 = [7 x i8] c"--help\00"
@.str.2 = [100 x i8] c"Usage: flash_erase MTD-device [start] [cnt (# erase blocks)] [lock]\0A flash_erase -h | --help\0A\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.3 = [17 x i8] c"File open error\0A\00"
@.str.4 = [22 x i8] c"Erase Total %d Units\0A\00"
@MEMGETREGIONCOUNT = global i32 0
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 i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp sge i32 1, %12
br i1 %13, label %26, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @strcmp(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %20, %14, %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.2, i64 0, i64 0))
store i32 16, i32* %3
br label %91
28: ; preds = %20
%29 = load i32, i32* %4
%30 = icmp sgt i32 %29, 2
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @strtol(i8* %34, i32* null, i32 0)
store i32 %35, i32* %8
br label %37
36: ; preds = %28
store i32 0, i32* %8
br label %37
37: ; preds = %36, %31
%38 = load i32, i32* %4
%39 = icmp sgt i32 %38, 3
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 3
%43 = load i8*, i8** %42
%44 = call i32 @strtol(i8* %43, i32* null, i32 0)
store i32 %44, i32* %9
br label %46
45: ; preds = %37
store i32 1, i32* %9
br label %46
46: ; preds = %45, %40
%47 = load i32, i32* %4
%48 = icmp sgt i32 %47, 4
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 4
%52 = load i8*, i8** %51
%53 = call i32 @strtol(i8* %52, i32* null, i32 0)
store i32 %53, i32* %10
br label %55
54: ; preds = %46
store i32 0, i32* %10
br label %55
55: ; preds = %54, %49
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 1
%58 = load i8*, i8** %57
%59 = load i32, i32* @O_RDWR
%60 = call i32 @open(i8* %58, i32 %59)
store i32 %60, i32* %7
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %55
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 8, i32* %3
br label %91
65: ; preds = %55
%66 = load i32, i32* %9
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %7
%69 = load i32, i32* @MEMGETREGIONCOUNT
%70 = call i64 @ioctl(i32 %68, i32 %69, i32* %6)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %89
72: ; preds = %65
%73 = load i32, i32* %6
%74 = icmp eq i32 %73, 0
br i1 %74, label %75, label %81
75: ; preds = %72
%76 = load i32, i32* %7
%77 = load i32, i32* %8
%78 = load i32, i32* %9
%79 = load i32, i32* %10
%80 = call i32 @non_region_erase(i32 %76, i32 %77, i32 %78, i32 %79)
store i32 %80, i32* %11
br label %88
81: ; preds = %72
%82 = load i32, i32* %7
%83 = load i32, i32* %8
%84 = load i32, i32* %9
%85 = load i32, i32* %10
%86 = load i32, i32* %6
%87 = call i32 @region_erase(i32 %82, i32 %83, i32 %84, i32 %85, i32 %86)
store i32 %87, i32* %11
br label %88
88: ; preds = %81, %75
br label %89
89: ; preds = %88, %65
%90 = load i32, i32* %11
store i32 %90, i32* %3
br label %91
91: ; preds = %89, %62, %26
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str = [3 x i8] c"-h\00"
@.str.1 = [7 x i8] c"--help\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.3 = [17 x i8] c"File open error\0A\00"
@.str.4 = [22 x i8] c"Erase Total %d Units\0A\00"
@MEMGETREGIONCOUNT = global i32 0
@str = [99 x i8] c"Usage: flash_erase MTD-device [start] [cnt (# erase blocks)] [lock]\0A flash_erase -h | --help\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = tail call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11, %6, %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([99 x i8], [99 x i8]* @str, i64 0, i64 0))
br label %55
16: ; preds = %11
%17 = icmp ugt i32 %0, 2
br i1 %17, label %18, label %32
18: ; preds = %16
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i32 @strtol(i8* nocapture %20, i32* null, i32 0)
%22 = icmp eq i32 %0, 3
br i1 %22, label %32, label %23
23: ; preds = %18
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = tail call i32 @strtol(i8* nocapture %25, i32* null, i32 0)
%27 = icmp ugt i32 %0, 4
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = getelementptr inbounds i8*, i8** %1, i64 4
%30 = load i8*, i8** %29
%31 = tail call i32 @strtol(i8* nocapture %30, i32* null, i32 0)
br label %32
32: ; preds = %16, %18, %23, %28
%33 = phi i32 [ %26, %28 ], [ %26, %23 ], [ 1, %18 ], [ 1, %16 ]
%34 = phi i32 [ %21, %28 ], [ %21, %23 ], [ %21, %18 ], [ 0, %16 ]
%35 = phi i32 [ %31, %28 ], [ 0, %23 ], [ 0, %18 ], [ 0, %16 ]
%36 = load i8*, i8** %7
%37 = load i32, i32* @O_RDWR
%38 = tail call i32 @open(i8* %36, i32 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %55
43: ; preds = %32
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%45 = load i32, i32* @MEMGETREGIONCOUNT
%46 = call i64 @ioctl(i32 %38, i32 %45, i32* %3)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load i32, i32* %3
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @non_region_erase(i32 %38, i32 %34, i32 %33, i32 %35)
br label %55
53: ; preds = %48
%54 = call i32 @region_erase(i32 %38, i32 %34, i32 %33, i32 %35, i32 %49)
br label %55
55: ; preds = %43, %53, %51, %40, %14
%56 = phi i32 [ 16, %14 ], [ 8, %40 ], [ %52, %51 ], [ %54, %53 ], [ 0, %43 ]
ret i32 %56
}