#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int start;
  int length;
  int erasesize;
  int size;
};
typedef TYPE_1__ mtd_info_t;
typedef TYPE_1__ erase_info_t;

/* Variables and functions */
int /*<<< orphan*/ MEMERASE;
int /*<<< orphan*/ MEMGETINFO;
int /*<<< orphan*/ MEMUNLOCK;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, TYPE_1__ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int non_region_erase(int Fd, int start, int count, int unlock) {
  mtd_info_t meminfo;
  if (ioctl(Fd, MEMGETINFO, &meminfo) == 0) {
    erase_info_t erase;
    erase.start = start;
    erase.length = meminfo.erasesize;
    if (count == 0) {
      count = (meminfo.size - start) / meminfo.erasesize;
      printf("Erase Total %d Units\n", count);
    }
    for (; count > 0; count--) {
      printf("\rPerforming Flash Erase of length %u at offset 0x%x",
             erase.length, erase.start);
      fflush(stdout);
      if (unlock != 0) {
        printf("\rPerforming Flash unlock at offset 0x%x", erase.start);
        if (ioctl(Fd, MEMUNLOCK, &erase) != 0) {
          perror("\nMTD Unlock failure");
          close(Fd);
          return 8;
        }
      }
      if (ioctl(Fd, MEMERASE, &erase) != 0) {
        perror("\nMTD Erase failure");
        close(Fd);
        return 8;
      }
      erase.start += meminfo.erasesize;
    }
    printf(" done\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<non_region_erase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    12ab <non_region_erase+0x15b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    11b7 <non_region_erase+0x67>
mov    -0x1c(%rbp),%eax
sub    -0xc(%rbp),%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11bc <non_region_erase+0x6c>
cmpl   $0x0,-0x10(%rbp)
jle    129d <non_region_erase+0x14d>
mov    -0x34(%rbp),%esi
mov    -0x38(%rbp),%edx
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
cmpl   $0x0,-0x14(%rbp)
je     1247 <non_region_erase+0xf7>
mov    -0x38(%rbp),%esi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1242 <non_region_erase+0xf2>
lea    0xe49(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    12b2 <non_region_erase+0x162>
jmp    1247 <non_region_erase+0xf7>
mov    -0x8(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1286 <non_region_erase+0x136>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    12b2 <non_region_erase+0x162>
mov    -0x20(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11bc <non_region_erase+0x6c>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<non_region_erase>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r12d
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
call   1050 <ioctl@plt>
xor    %ecx,%ecx
test   %rax,%rax
jne    1276 <non_region_erase+0x116>
mov    %ebx,0x8(%rsp)
mov    0x20(%rsp),%r13d
mov    %r13d,0xc(%rsp)
test   %ebp,%ebp
je     11b1 <non_region_erase+0x51>
test   %ebp,%ebp
jg     11d5 <non_region_erase+0x75>
jmp    1268 <non_region_erase+0x108>
mov    0x24(%rsp),%eax
sub    %ebx,%eax
cltd
idiv   %r13d
mov    %eax,%ebp
lea    0xe3c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jle    1268 <non_region_erase+0x108>
lea    0x8(%rsp),%r14
nopw   0x0(%rax,%rax,1)
lea    0xe2f(%rip),%rdi
mov    %r13d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
test   %r15d,%r15d
je     1231 <non_region_erase+0xd1>
mov    0x8(%rsp),%esi
lea    0xe39(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    1290 <non_region_erase+0x130>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    1287 <non_region_erase+0x127>
mov    0x8(%rsp),%ebx
add    0x20(%rsp),%ebx
mov    %ebx,0x8(%rsp)
cmp    $0x2,%ebp
jl     1268 <non_region_erase+0x108>
add    $0xffffffff,%ebp
mov    0xc(%rsp),%r13d
jmp    11e0 <non_region_erase+0x80>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
jmp    1297 <non_region_erase+0x137>
lea    0xddb(%rip),%rdi
call   1080 <perror@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    $0x8,%ecx
jmp    1276 <non_region_erase+0x116>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32, i32, i32 }
@MEMGETINFO = global i32 0
@.str = [22 x i8] c"Erase Total %d Units\0A\00"
@.str.1 = [52 x i8] c"\0DPerforming Flash Erase of length %u at offset 0x%x\00"
@stdout = global i32 0
@.str.2 = [40 x i8] c"\0DPerforming Flash unlock at offset 0x%x\00"
@MEMUNLOCK = global i32 0
@.str.3 = [20 x i8] c"\0AMTD Unlock failure\00"
@MEMERASE = global i32 0
@.str.4 = [19 x i8] c"\0AMTD Erase failure\00"
@.str.5 = [7 x i8] c" done\0A\00"
define i32 @non_region_erase(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.TYPE_4__
%11 = alloca %struct.TYPE_4__
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %6
%13 = load i32, i32* @MEMGETINFO
%14 = call i64 @ioctl(i32 %12, i32 %13, %struct.TYPE_4__* %10)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %81
16: ; preds = %4
%17 = load i32, i32* %7
%18 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %10, i32 0, i32 2
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %16
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %10, i32 0, i32 3
%26 = load i32, i32* %25
%27 = load i32, i32* %7
%28 = sub nsw i32 %26, %27
%29 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %10, i32 0, i32 2
%30 = load i32, i32* %29
%31 = sdiv i32 %28, %30
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %24, %16
br label %35
35: ; preds = %76, %34
%36 = load i32, i32* %8
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %79
38: ; preds = %35
%39 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %42)
%44 = load i32, i32* @stdout
%45 = call i32 @fflush(i32 %44)
%46 = load i32, i32* %9
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %61
48: ; preds = %38
%49 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %6
%53 = load i32, i32* @MEMUNLOCK
%54 = call i64 @ioctl(i32 %52, i32 %53, %struct.TYPE_4__* %11)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %48
%57 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
store i32 8, i32* %5
br label %82
60: ; preds = %48
br label %61
61: ; preds = %60, %38
%62 = load i32, i32* %6
%63 = load i32, i32* @MEMERASE
%64 = call i64 @ioctl(i32 %62, i32 %63, %struct.TYPE_4__* %11)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
store i32 8, i32* %5
br label %82
70: ; preds = %61
%71 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %10, i32 0, i32 2
%72 = load i32, i32* %71
%73 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
%74 = load i32, i32* %73
%75 = add nsw i32 %74, %72
store i32 %75, i32* %73
br label %76
76: ; preds = %70
%77 = load i32, i32* %8
%78 = add nsw i32 %77, -1
store i32 %78, i32* %8
br label %35
79: ; preds = %35
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %81
81: ; preds = %79, %4
store i32 0, i32* %5
br label %82
82: ; preds = %81, %66, %56
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32, i32, i32 }
@MEMGETINFO = global i32 0
@.str = [22 x i8] c"Erase Total %d Units\0A\00"
@.str.1 = [52 x i8] c"\0DPerforming Flash Erase of length %u at offset 0x%x\00"
@stdout = global i32 0
@.str.2 = [40 x i8] c"\0DPerforming Flash unlock at offset 0x%x\00"
@MEMUNLOCK = global i32 0
@.str.3 = [20 x i8] c"\0AMTD Unlock failure\00"
@MEMERASE = global i32 0
@.str.4 = [19 x i8] c"\0AMTD Erase failure\00"
@str = [6 x i8] c" done\00"
define i32 @non_region_erase(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.TYPE_4__
%6 = alloca %struct.TYPE_4__
%7 = bitcast %struct.TYPE_4__* %5 to i8*
%8 = load i32, i32* @MEMGETINFO
%9 = call i64 @ioctl(i32 %0, i32 %8, %struct.TYPE_4__* %5)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %60
11: ; preds = %4
%12 = bitcast %struct.TYPE_4__* %6 to i8*
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 0
store i32 %1, i32* %13
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %5, i64 0, i32 2
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 1
store i32 %15, i32* %16
%17 = icmp eq i32 %2, 0
br i1 %17, label %18, label %24
18: ; preds = %11
%19 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %5, i64 0, i32 3
%20 = load i32, i32* %19
%21 = sub nsw i32 %20, %1
%22 = sdiv i32 %21, %15
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %18, %11
%25 = phi i32 [ %22, %18 ], [ %2, %11 ]
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %54
27: ; preds = %24
%28 = icmp eq i32 %3, 0
br label %29
29: ; preds = %51, %27
%30 = phi i32 [ %1, %27 ], [ %49, %51 ]
%31 = phi i32 [ %15, %27 ], [ %53, %51 ]
%32 = phi i32 [ %25, %27 ], [ %52, %51 ]
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %30)
%34 = load i32, i32* @stdout
%35 = call i32 @fflush(i32 %34)
br i1 %28, label %42, label %36
36: ; preds = %29
%37 = load i32, i32* %13
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @MEMUNLOCK
%40 = call i64 @ioctl(i32 %0, i32 %39, %struct.TYPE_4__* %6)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %56
42: ; preds = %36, %29
%43 = load i32, i32* @MEMERASE
%44 = call i64 @ioctl(i32 %0, i32 %43, %struct.TYPE_4__* %6)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %42
%47 = load i32, i32* %14
%48 = load i32, i32* %13
%49 = add nsw i32 %48, %47
store i32 %49, i32* %13
%50 = icmp sgt i32 %32, 1
br i1 %50, label %51, label %54
51: ; preds = %46
%52 = add nsw i32 %32, -1
%53 = load i32, i32* %16
br label %29
54: ; preds = %46, %24
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %60
56: ; preds = %42, %36
%57 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %36 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), %42 ]
%58 = call i32 @perror(i8* %57)
%59 = call i32 @close(i32 %0)
br label %60
60: ; preds = %4, %54, %56
%61 = phi i32 [ 8, %56 ], [ 0, %54 ], [ 0, %4 ]
ret i32 %61
}