#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;
    printf("Erase Unit Size 0x%lx, ", meminfo.erasesize);
    for (; count > 0; count--) {
      printf("\rPerforming Flash Erase of length %lu at offset 0x%lx",
             erase.length, erase.start);
      fflush(stdout);
      if (unlock != 0) {
        printf("\rPerforming Flash unlock at offset 0x%lx", 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    1298 <non_region_erase+0x148>
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jle    128a <non_region_erase+0x13a>
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%edx
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
cmpl   $0x0,-0x14(%rbp)
je     122f <non_region_erase+0xdf>
mov    -0x48(%rbp),%esi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     122a <non_region_erase+0xda>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    129f <non_region_erase+0x14f>
jmp    122f <non_region_erase+0xdf>
mov    -0x8(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     126e <non_region_erase+0x11e>
lea    0xe36(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    129f <non_region_erase+0x14f>
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    11a3 <non_region_erase+0x53>
lea    0xe0d(%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,%r14d
mov    %edx,%ebp
mov    %esi,%r15d
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    1268 <non_region_erase+0x108>
mov    %r15d,0x8(%rsp)
mov    0x28(%rsp),%r12
mov    %r12,0x18(%rsp)
lea    0xe54(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jle    125a <non_region_erase+0xfa>
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xe41(%rip),%rdi
mov    %r12,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
test   %r14d,%r14d
je     1221 <non_region_erase+0xc1>
mov    0x8(%rsp),%esi
lea    0xe4c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    1282 <non_region_erase+0x122>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    1279 <non_region_erase+0x119>
mov    0x8(%rsp),%r15d
add    0x28(%rsp),%r15d
mov    %r15d,0x8(%rsp)
cmp    $0x2,%ebp
jl     125a <non_region_erase+0xfa>
add    $0xffffffff,%ebp
mov    0x18(%rsp),%r12
jmp    11d0 <non_region_erase+0x70>
lea    0xe3d(%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    0xe0b(%rip),%rdi
jmp    1289 <non_region_erase+0x129>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x8,%ecx
jmp    1268 <non_region_erase+0x108>

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

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