#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);
int region_erase(int, int, int, int, int);
int /*<<< orphan*/ stderr;
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) {
    fprintf(stderr, "You must specify a device\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
jl     11bc <main+0x4c>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x10,-0x4(%rbp)
jmp    12f7 <main+0x187>
cmpl   $0x2,-0x8(%rbp)
jle    11e1 <main+0x71>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,-0x1c(%rbp)
jmp    11e8 <main+0x78>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    120d <main+0x9d>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,-0x20(%rbp)
jmp    1214 <main+0xa4>
movl   $0x1,-0x20(%rbp)
cmpl   $0x4,-0x8(%rbp)
jle    1239 <main+0xc9>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,-0x24(%rbp)
jmp    1240 <main+0xd0>
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1283 <main+0x113>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x8,-0x4(%rbp)
jmp    12f7 <main+0x187>
mov    -0x20(%rbp),%esi
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    12f1 <main+0x181>
cmpl   $0x0,-0x14(%rbp)
jne    12d4 <main+0x164>
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    12ec <main+0x17c>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%r8d
call   1070 <region_erase@plt>
mov    %eax,-0x28(%rbp)
jmp    12f1 <main+0x181>
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   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     11a0 <main+0x30>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x10,%eax
jmp    127b <main+0x10b>
mov    %rsi,%rbp
mov    %edi,%ebx
xor    %r15d,%r15d
mov    $0x1,%r12d
cmp    $0x2,%edi
jne    11b8 <main+0x48>
xor    %r14d,%r14d
jmp    11f8 <main+0x88>
mov    0x10(%rbp),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,%r15d
cmp    $0x4,%ebx
jb     11f8 <main+0x88>
mov    0x18(%rbp),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,%r12d
cmp    $0x4,%ebx
je     11f8 <main+0x88>
mov    0x20(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,%r14d
mov    0x8(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     125a <main+0xea>
mov    %eax,%ebp
lea    0xe15(%rip),%rdi
mov    %r12d,%esi
call   1040 <printf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    127b <main+0x10b>
mov    0xc(%rsp),%r8d
mov    %ebp,%edi
mov    %r15d,%esi
mov    %r12d,%edx
mov    %r14d,%ecx
test   %r8d,%r8d
je     1276 <main+0x106>
call   1070 <region_erase@plt>
jmp    127b <main+0x10b>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x8,%eax
jmp    127b <main+0x10b>
call   1030 <non_region_erase@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"You must specify a device\0A\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"File open error\0A\00"
@.str.2 = [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 %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 16, i32* %3
br label %80
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 2
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @strtol(i8* %23, i32* null, i32 0)
store i32 %24, i32* %8
br label %26
25: ; preds = %17
store i32 0, i32* %8
br label %26
26: ; preds = %25, %20
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 3
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = call i32 @strtol(i8* %32, i32* null, i32 0)
store i32 %33, i32* %9
br label %35
34: ; preds = %26
store i32 1, i32* %9
br label %35
35: ; preds = %34, %29
%36 = load i32, i32* %4
%37 = icmp sgt i32 %36, 4
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 4
%41 = load i8*, i8** %40
%42 = call i32 @strtol(i8* %41, i32* null, i32 0)
store i32 %42, i32* %10
br label %44
43: ; preds = %35
store i32 0, i32* %10
br label %44
44: ; preds = %43, %38
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = load i32, i32* @O_RDWR
%49 = call i32 @open(i8* %47, i32 %48)
store i32 %49, i32* %7
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 8, i32* %3
br label %80
54: ; preds = %44
%55 = load i32, i32* %9
%56 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %7
%58 = load i32, i32* @MEMGETREGIONCOUNT
%59 = call i64 @ioctl(i32 %57, i32 %58, i32* %6)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %78
61: ; preds = %54
%62 = load i32, i32* %6
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %70
64: ; preds = %61
%65 = load i32, i32* %7
%66 = load i32, i32* %8
%67 = load i32, i32* %9
%68 = load i32, i32* %10
%69 = call i32 @non_region_erase(i32 %65, i32 %66, i32 %67, i32 %68)
store i32 %69, i32* %11
br label %77
70: ; preds = %61
%71 = load i32, i32* %7
%72 = load i32, i32* %8
%73 = load i32, i32* %9
%74 = load i32, i32* %10
%75 = load i32, i32* %6
%76 = call i32 @region_erase(i32 %71, i32 %72, i32 %73, i32 %74, i32 %75)
store i32 %76, i32* %11
br label %77
77: ; preds = %70, %64
br label %78
78: ; preds = %77, %54
%79 = load i32, i32* %11
store i32 %79, i32* %3
br label %80
80: ; preds = %78, %51, %14
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"You must specify a device\0A\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"File open error\0A\00"
@.str.2 = [22 x i8] c"Erase Total %d Units\0A\00"
@MEMGETREGIONCOUNT = global i32 0
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 %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %49
9: ; preds = %2
%10 = icmp eq i32 %0, 2
br i1 %10, label %25, label %11
11: ; preds = %9
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @strtol(i8* nocapture %13, i32* null, i32 0)
%15 = icmp ugt i32 %0, 3
br i1 %15, label %16, label %25
16: ; preds = %11
%17 = getelementptr inbounds i8*, i8** %1, i64 3
%18 = load i8*, i8** %17
%19 = tail call i32 @strtol(i8* nocapture %18, i32* null, i32 0)
%20 = icmp eq i32 %0, 4
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = getelementptr inbounds i8*, i8** %1, i64 4
%23 = load i8*, i8** %22
%24 = tail call i32 @strtol(i8* nocapture %23, i32* null, i32 0)
br label %25
25: ; preds = %9, %11, %16, %21
%26 = phi i32 [ %19, %21 ], [ %19, %16 ], [ 1, %11 ], [ 1, %9 ]
%27 = phi i32 [ %14, %21 ], [ %14, %16 ], [ %14, %11 ], [ 0, %9 ]
%28 = phi i32 [ %24, %21 ], [ 0, %16 ], [ 0, %11 ], [ 0, %9 ]
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_RDWR
%32 = tail call i32 @open(i8* %30, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %49
37: ; preds = %25
%38 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%39 = load i32, i32* @MEMGETREGIONCOUNT
%40 = call i64 @ioctl(i32 %32, i32 %39, i32* %3)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %37
%43 = load i32, i32* %3
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @non_region_erase(i32 %32, i32 %27, i32 %26, i32 %28)
br label %49
47: ; preds = %42
%48 = call i32 @region_erase(i32 %32, i32 %27, i32 %26, i32 %28, i32 %43)
br label %49
49: ; preds = %37, %47, %45, %34, %6
%50 = phi i32 [ 16, %6 ], [ 8, %34 ], [ %46, %45 ], [ %48, %47 ], [ 0, %37 ]
ret i32 %50
}