#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;
};
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;
    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    $0x30,%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    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    127e <non_region_erase+0x12e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1270 <non_region_erase+0x120>
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
cmpl   $0x0,-0x14(%rbp)
je     121a <non_region_erase+0xca>
mov    -0x30(%rbp),%esi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1215 <non_region_erase+0xc5>
lea    0xe60(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    1285 <non_region_erase+0x135>
jmp    121a <non_region_erase+0xca>
mov    -0x8(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1259 <non_region_erase+0x109>
lea    0xe30(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    1285 <non_region_erase+0x135>
mov    -0x18(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    118f <non_region_erase+0x3f>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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,%r14d
mov    %edi,%ebx
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
call   1050 <ioctl@plt>
xor    %ecx,%ecx
test   %rax,%rax
jne    124c <non_region_erase+0xec>
mov    %r14d,0x8(%rsp)
mov    0x20(%rsp),%esi
mov    %esi,0xc(%rsp)
test   %ebp,%ebp
jle    123e <non_region_erase+0xde>
mov    0x2e2e(%rip),%r12
lea    0x8(%rsp),%r13
nopw   0x0(%rax,%rax,1)
lea    0xe39(%rip),%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
test   %r15d,%r15d
je     120e <non_region_erase+0xae>
mov    0x8(%rsp),%esi
lea    0xe45(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    1266 <non_region_erase+0x106>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    125d <non_region_erase+0xfd>
mov    0x8(%rsp),%r14d
add    0x20(%rsp),%r14d
mov    %r14d,0x8(%rsp)
cmp    $0x2,%ebp
jl     123e <non_region_erase+0xde>
add    $0xffffffff,%ebp
mov    0xc(%rsp),%esi
jmp    11c0 <non_region_erase+0x60>
lea    0xe3e(%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    0xe0c(%rip),%rdi
jmp    126d <non_region_erase+0x10d>
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x8,%ecx
jmp    124c <non_region_erase+0xec>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32, i32 }
@MEMGETINFO = global i32 0
@.str = [52 x i8] c"\0DPerforming Flash Erase of length %u at offset 0x%x\00"
@stdout = global i32 0
@.str.1 = [40 x i8] c"\0DPerforming Flash unlock at offset 0x%x\00"
@MEMUNLOCK = global i32 0
@.str.2 = [20 x i8] c"\0AMTD Unlock failure\00"
@MEMERASE = global i32 0
@.str.3 = [19 x i8] c"\0AMTD Erase failure\00"
@.str.4 = [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 %68
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
br label %22
22: ; preds = %63, %16
%23 = load i32, i32* %8
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %66
25: ; preds = %22
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %29)
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %25
%36 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %6
%40 = load i32, i32* @MEMUNLOCK
%41 = call i64 @ioctl(i32 %39, i32 %40, %struct.TYPE_4__* %11)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 8, i32* %5
br label %69
47: ; preds = %35
br label %48
48: ; preds = %47, %25
%49 = load i32, i32* %6
%50 = load i32, i32* @MEMERASE
%51 = call i64 @ioctl(i32 %49, i32 %50, %struct.TYPE_4__* %11)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
store i32 8, i32* %5
br label %69
57: ; preds = %48
%58 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %10, i32 0, i32 2
%59 = load i32, i32* %58
%60 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
%61 = load i32, i32* %60
%62 = add nsw i32 %61, %59
store i32 %62, i32* %60
br label %63
63: ; preds = %57
%64 = load i32, i32* %8
%65 = add nsw i32 %64, -1
store i32 %65, i32* %8
br label %22
66: ; preds = %22
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %68
68: ; preds = %66, %4
store i32 0, i32* %5
br label %69
69: ; preds = %68, %53, %43
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32, i32 }
@MEMGETINFO = global i32 0
@.str = [52 x i8] c"\0DPerforming Flash Erase of length %u at offset 0x%x\00"
@stdout = global i32 0
@.str.1 = [40 x i8] c"\0DPerforming Flash unlock at offset 0x%x\00"
@MEMUNLOCK = global i32 0
@.str.2 = [20 x i8] c"\0AMTD Unlock failure\00"
@MEMERASE = global i32 0
@.str.3 = [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 %51
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 sgt i32 %2, 0
br i1 %17, label %18, label %45
18: ; preds = %11
%19 = icmp eq i32 %3, 0
br label %20
20: ; preds = %42, %18
%21 = phi i32 [ %1, %18 ], [ %40, %42 ]
%22 = phi i32 [ %15, %18 ], [ %44, %42 ]
%23 = phi i32 [ %2, %18 ], [ %43, %42 ]
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %21)
%25 = load i32, i32* @stdout
%26 = call i32 @fflush(i32 %25)
br i1 %19, label %33, label %27
27: ; preds = %20
%28 = load i32, i32* %13
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @MEMUNLOCK
%31 = call i64 @ioctl(i32 %0, i32 %30, %struct.TYPE_4__* %6)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %27, %20
%34 = load i32, i32* @MEMERASE
%35 = call i64 @ioctl(i32 %0, i32 %34, %struct.TYPE_4__* %6)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %33
%38 = load i32, i32* %14
%39 = load i32, i32* %13
%40 = add nsw i32 %39, %38
store i32 %40, i32* %13
%41 = icmp sgt i32 %23, 1
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = add nsw i32 %23, -1
%44 = load i32, i32* %16
br label %20
45: ; preds = %37, %11
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %51
47: ; preds = %33, %27
%48 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %27 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), %33 ]
%49 = call i32 @perror(i8* %48)
%50 = call i32 @close(i32 %0)
br label %51
51: ; preds = %4, %45, %47
%52 = phi i32 [ 8, %47 ], [ 0, %45 ], [ 0, %4 ]
ret i32 %52
}