#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;
  scalar_t__ erasesize;
  scalar_t__ length;
};
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 0x%llx at offset 0x%llx",
             erase.length, erase.start);
      fflush(stdout);
      if (unlock != 0) {
        printf("\rPerforming Flash unlock at offset 0x%llx", 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    $0x50,%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    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    1286 <non_region_erase+0x136>
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1278 <non_region_erase+0x128>
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%edx
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
cmpl   $0x0,-0x14(%rbp)
je     121d <non_region_erase+0xcd>
mov    -0x48(%rbp),%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1218 <non_region_erase+0xc8>
lea    0xe65(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    128d <non_region_erase+0x13d>
jmp    121d <non_region_erase+0xcd>
mov    -0x8(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     125c <non_region_erase+0x10c>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    128d <non_region_erase+0x13d>
mov    -0x28(%rbp),%rcx
movslq -0x48(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1191 <non_region_erase+0x41>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<non_region_erase>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
call   1050 <ioctl@plt>
xor    %ecx,%ecx
test   %rax,%rax
jne    124d <non_region_erase+0xed>
mov    %r14d,0x8(%rsp)
mov    0x28(%rsp),%rsi
mov    %rsi,0x18(%rsp)
test   %ebp,%ebp
jle    123f <non_region_erase+0xdf>
mov    0x2e2c(%rip),%r12
lea    0x8(%rsp),%r13
nopl   0x0(%rax)
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    0xe4b(%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    1267 <non_region_erase+0x107>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    125e <non_region_erase+0xfe>
mov    0x8(%rsp),%r14d
add    0x28(%rsp),%r14d
mov    %r14d,0x8(%rsp)
cmp    $0x2,%ebp
jl     123f <non_region_erase+0xdf>
add    $0xffffffff,%ebp
mov    0x18(%rsp),%rsi
jmp    11c0 <non_region_erase+0x60>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe13(%rip),%rdi
jmp    126e <non_region_erase+0x10e>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x8,%ecx
jmp    124d <non_region_erase+0xed>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i64, i64 }
@MEMGETINFO = global i32 0
@.str = [58 x i8] c"\0DPerforming Flash Erase of length 0x%llx at offset 0x%llx\00"
@stdout = global i32 0
@.str.1 = [42 x i8] c"\0DPerforming Flash unlock at offset 0x%llx\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 %70
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 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 2
store i64 %20, i64* %21
br label %22
22: ; preds = %65, %16
%23 = load i32, i32* %8
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %68
25: ; preds = %22
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 2
%27 = load i64, i64* %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 ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i64 %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 ([42 x i8], [42 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 %71
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 %71
57: ; preds = %48
%58 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %10, i32 0, i32 1
%59 = load i64, i64* %58
%60 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
%61 = load i32, i32* %60
%62 = sext i32 %61 to i64
%63 = add nsw i64 %62, %59
%64 = trunc i64 %63 to i32
store i32 %64, i32* %60
br label %65
65: ; preds = %57
%66 = load i32, i32* %8
%67 = add nsw i32 %66, -1
store i32 %67, i32* %8
br label %22
68: ; preds = %22
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %70
70: ; preds = %68, %4
store i32 0, i32* %5
br label %71
71: ; preds = %70, %53, %43
%72 = load i32, i32* %5
ret i32 %72
}

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