#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;
  }
  if (count != 0)
    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    1346 <main+0x1c6>
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    1346 <main+0x1c6>
cmpl   $0x0,-0x20(%rbp)
je     12e1 <main+0x161>
mov    -0x20(%rbp),%esi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    1340 <main+0x1c0>
cmpl   $0x0,-0x14(%rbp)
jne    1323 <main+0x1a3>
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    133b <main+0x1bb>
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    1340 <main+0x1c0>
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    %r15d,%r15d
mov    $0x1,%r12d
cmp    $0x3,%ebx
jb     123f <main+0xaf>
mov    0x10(%r13),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtol@plt>
mov    %eax,%r15d
cmp    $0x3,%ebx
je     1242 <main+0xb2>
mov    0x18(%r13),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtol@plt>
mov    %eax,%r12d
cmp    $0x5,%ebx
jb     1242 <main+0xb2>
mov    0x20(%r13),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtol@plt>
mov    %eax,%r14d
jmp    1242 <main+0xb2>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    $0x10,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
mov    0x8(%r13),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
test   %eax,%eax
js     12ab <main+0x11b>
mov    %eax,%ebx
test   %r12d,%r12d
je     1270 <main+0xe0>
lea    0xdae(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1230 <main+0xa0>
mov    0x4(%rsp),%r8d
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r12d,%edx
mov    %r14d,%ecx
test   %r8d,%r8d
je     12ca <main+0x13a>
call   1090 <region_erase@plt>
jmp    1230 <main+0xa0>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xd48(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x8,%eax
jmp    1230 <main+0xa0>
call   1040 <non_region_erase@plt>
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 %95
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 %95
65: ; preds = %55
%66 = load i32, i32* %9
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* %9
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %68, %65
%72 = load i32, i32* %7
%73 = load i32, i32* @MEMGETREGIONCOUNT
%74 = call i64 @ioctl(i32 %72, i32 %73, i32* %6)
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %93
76: ; preds = %71
%77 = load i32, i32* %6
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %85
79: ; preds = %76
%80 = load i32, i32* %7
%81 = load i32, i32* %8
%82 = load i32, i32* %9
%83 = load i32, i32* %10
%84 = call i32 @non_region_erase(i32 %80, i32 %81, i32 %82, i32 %83)
store i32 %84, i32* %11
br label %92
85: ; preds = %76
%86 = load i32, i32* %7
%87 = load i32, i32* %8
%88 = load i32, i32* %9
%89 = load i32, i32* %10
%90 = load i32, i32* %6
%91 = call i32 @region_erase(i32 %86, i32 %87, i32 %88, i32 %89, i32 %90)
store i32 %91, i32* %11
br label %92
92: ; preds = %85, %79
br label %93
93: ; preds = %92, %71
%94 = load i32, i32* %11
store i32 %94, i32* %3
br label %95
95: ; preds = %93, %62, %26
%96 = load i32, i32* %3
ret i32 %96
}

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 %58
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 %58
43: ; preds = %32
%44 = icmp eq i32 %33, 0
br i1 %44, label %47, label %45
45: ; preds = %43
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %47
47: ; preds = %45, %43
%48 = load i32, i32* @MEMGETREGIONCOUNT
%49 = call i64 @ioctl(i32 %38, i32 %48, i32* %3)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load i32, i32* %3
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @non_region_erase(i32 %38, i32 %34, i32 %33, i32 %35)
br label %58
56: ; preds = %51
%57 = call i32 @region_erase(i32 %38, i32 %34, i32 %33, i32 %35, i32 %52)
br label %58
58: ; preds = %47, %56, %54, %40, %14
%59 = phi i32 [ 16, %14 ], [ 8, %40 ], [ %55, %54 ], [ %57, %56 ], [ 0, %47 ]
ret i32 %59
}