#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_5__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  int regionindex;
  int offset;
  int numblocks;
  int erasesize;
  int start;
  int length;
};
typedef TYPE_1__ region_info_t;
typedef TYPE_1__ erase_info_t;

/* Variables and functions */
int /*<<< orphan*/ MEMERASE;
int /*<<< orphan*/ MEMGETREGIONINFO;
int /*<<< orphan*/ MEMUNLOCK;
TYPE_1__ *calloc(int, int);
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 region_erase(int Fd, int start, int count, int unlock, int regcount) {
  int i, j;
  region_info_t *reginfo;
  reginfo = calloc(regcount, sizeof(region_info_t));
  for (i = 0; i < regcount; i++) {
    reginfo[i].regionindex = i;
    if (ioctl(Fd, MEMGETREGIONINFO, &(reginfo[i])) != 0)
      return 8;
    else
      printf("Region %d is at %d of %d sector and with sector "
             "size %x\n",
             i, reginfo[i].offset, reginfo[i].numblocks, reginfo[i].erasesize);
  }
  for (i = 0; i < regcount; i++) {
    region_info_t *r = &(reginfo[i]);
    if ((start >= reginfo[i].offset) &&
        (start < (r->offset + r->numblocks * r->erasesize)))
      break;
  }
  if (i >= regcount) {
    printf("Starting offset %x not within chip.\n", start);
    return 8;
  }
  for (j = 0; (j < count) && (i < regcount); j++) {
    erase_info_t erase;
    region_info_t *r = &(reginfo[i]);
    erase.start = start;
    erase.length = r->erasesize;
    if (unlock != 0) {
      if (ioctl(Fd, MEMUNLOCK, &erase) != 0) {
        perror("\nMTD Unlock failure");
        close(Fd);
        return 8;
      }
    }
    printf("\rPerforming Flash Erase of length %lu at offset 0x%lx",
           erase.length, erase.start);
    fflush(stdout);
    if (ioctl(Fd, MEMERASE, &erase) != 0) {
      perror("\nMTD Erase failure");
      close(Fd);
      return 8;
    }
    start += erase.length;
    if (start >= (r->offset + r->numblocks * r->erasesize)) {
      printf("\nMoving to region %d\n", i + 1);
      i++;
    }
  }
  printf(" done\n");
  return 0;
}


x86 assembly (clang, -O0):
<region_erase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x18,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1241 <region_erase+0xe1>
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rdx
imul   $0x18,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
movslq -0x1c(%rbp),%rax
imul   $0x18,%rax,%rax
add    %rax,%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     11e6 <region_erase+0x86>
movl   $0x8,-0x4(%rbp)
jmp    145d <region_erase+0x2fd>
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
mov    0x4(%rax),%edx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
mov    0x8(%rax),%ecx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rdi
imul   $0x18,%rdi,%rdi
add    %rdi,%rax
mov    0xc(%rax),%r8d
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1233 <region_erase+0xd3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1190 <region_erase+0x30>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12bd <region_erase+0x15d>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x28(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
imul   $0x18,%rdx,%rdx
add    %rdx,%rcx
cmp    0x4(%rcx),%eax
jl     12aa <region_erase+0x14a>
mov    -0xc(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    0x4(%rcx),%ecx
mov    -0x30(%rbp),%rdx
mov    0x8(%rdx),%edx
mov    -0x30(%rbp),%rsi
imul   0xc(%rsi),%edx
add    %edx,%ecx
cmp    %ecx,%eax
jge    12aa <region_erase+0x14a>
jmp    12bd <region_erase+0x15d>
jmp    12af <region_erase+0x14f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1248 <region_erase+0xe8>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     12e6 <region_erase+0x186>
mov    -0xc(%rbp),%esi
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x8,-0x4(%rbp)
jmp    145d <region_erase+0x2fd>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
cmp    -0x10(%rbp),%ecx
mov    %al,-0x51(%rbp)
jge    130a <region_erase+0x1aa>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
setl   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    131a <region_erase+0x1ba>
jmp    1448 <region_erase+0x2e8>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     138b <region_erase+0x22b>
mov    -0x8(%rbp),%edi
mov    0x2c7f(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1386 <region_erase+0x226>
lea    0xcf1(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    145d <region_erase+0x2fd>
jmp    138b <region_erase+0x22b>
mov    -0x34(%rbp),%esi
mov    -0x38(%rbp),%edx
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x8(%rbp),%edi
mov    0x2c29(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     13ec <region_erase+0x28c>
lea    0xcd5(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x8,-0x4(%rbp)
jmp    145d <region_erase+0x2fd>
mov    -0x34(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x50(%rbp),%rcx
mov    0x4(%rcx),%ecx
mov    -0x50(%rbp),%rdx
mov    0x8(%rdx),%edx
mov    -0x50(%rbp),%rsi
imul   0xc(%rsi),%edx
add    %edx,%ecx
cmp    %ecx,%eax
jl     1435 <region_erase+0x2d5>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
lea    0xc96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    143a <region_erase+0x2da>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12ed <region_erase+0x18d>
lea    0xc82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<region_erase>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%ebx
mov    %ecx,0x1c(%rsp)
mov    %edx,0x4(%rsp)
mov    %esi,%ebp
mov    %edi,(%rsp)
mov    %r8d,%edi
mov    $0x18,%esi
call   1070 <calloc@plt>
mov    %rax,0x8(%rsp)
test   %ebx,%ebx
jle    135f <region_erase+0x1ef>
mov    %ebp,0x18(%rsp)
mov    %ebx,0x14(%rsp)
mov    %ebx,%r13d
mov    0x2e2e(%rip),%rbp
lea    0xe3f(%rip),%r15
mov    0x8(%rsp),%r12
xor    %r14d,%r14d
mov    $0x8,%ebx
xchg   %ax,%ax
mov    %r14d,(%r12)
mov    0x0(%rbp),%esi
mov    (%rsp),%edi
mov    %r12,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    1374 <region_erase+0x204>
mov    0x4(%r12),%edx
mov    0x8(%r12),%ecx
mov    0xc(%r12),%r8d
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r14
add    $0x18,%r12
cmp    %r14,%r13
jne    11d0 <region_erase+0x60>
cmpl   $0x0,0x14(%rsp)
mov    0x18(%rsp),%ebp
jle    135f <region_erase+0x1ef>
mov    0x8(%rsp),%rax
add    $0xc,%rax
xor    %r14d,%r14d
jmp    1251 <region_erase+0xe1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
add    $0x18,%rax
cmp    %r14,%r13
je     135f <region_erase+0x1ef>
mov    -0x8(%rax),%ecx
cmp    %ebp,%ecx
jg     1240 <region_erase+0xd0>
mov    (%rax),%edx
imul   -0x4(%rax),%edx
add    %ecx,%edx
cmp    %ebp,%edx
jle    1240 <region_erase+0xd0>
cmpl   $0x0,0x4(%rsp)
jle    134f <region_erase+0x1df>
cmp    %r13d,%r14d
jge    134f <region_erase+0x1df>
mov    $0x1,%r15d
xchg   %ax,%ax
movslq %r14d,%rax
mov    %ebp,0x30(%rsp)
lea    (%rax,%rax,2),%r12
mov    0x8(%rsp),%rax
mov    0xc(%rax,%r12,8),%esi
mov    %esi,0x34(%rsp)
mov    %ebp,%edx
cmpl   $0x0,0x1c(%rsp)
je     12c9 <region_erase+0x159>
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    (%rsp),%edi
lea    0x20(%rsp),%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    138e <region_erase+0x21e>
mov    0x30(%rsp),%edx
mov    0x34(%rsp),%esi
lea    0xda2(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
mov    (%rsp),%edi
lea    0x20(%rsp),%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    1385 <region_erase+0x215>
add    0x34(%rsp),%ebp
mov    0x8(%rsp),%rcx
lea    (%rcx,%r12,8),%rax
add    $0xc,%rax
mov    (%rax),%eax
imul   0x8(%rcx,%r12,8),%eax
add    0x4(%rcx,%r12,8),%eax
cmp    %eax,%ebp
jl     133b <region_erase+0x1cb>
add    $0x1,%r14d
lea    0xd8a(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    0x4(%rsp),%r15d
jge    134f <region_erase+0x1df>
add    $0x1,%r15d
cmp    %r13d,%r14d
jl     1280 <region_erase+0x110>
lea    0xd7b(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1374 <region_erase+0x204>
lea    0xcd3(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x8,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1c(%rip),%rdi
jmp    1395 <region_erase+0x225>
lea    0xcc9(%rip),%rdi
call   1090 <perror@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
jmp    1374 <region_erase+0x204>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32, i32, i32, i32, i32, i32 }
@MEMGETREGIONINFO = global i32 0
@.str = [57 x i8] c"Region %d is at %d of %d sector and with sector size %x\0A\00"
@.str.1 = [37 x i8] c"Starting offset %x not within chip.\0A\00"
@MEMUNLOCK = global i32 0
@.str.2 = [20 x i8] c"\0AMTD Unlock failure\00"
@.str.3 = [54 x i8] c"\0DPerforming Flash Erase of length %lu at offset 0x%lx\00"
@stdout = global i32 0
@MEMERASE = global i32 0
@.str.4 = [19 x i8] c"\0AMTD Erase failure\00"
@.str.5 = [22 x i8] c"\0AMoving to region %d\0A\00"
@.str.6 = [7 x i8] c" done\0A\00"
define i32 @region_erase(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.TYPE_5__*
%15 = alloca %struct.TYPE_5__*
%16 = alloca %struct.TYPE_5__
%17 = alloca %struct.TYPE_5__*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%18 = load i32, i32* %11
%19 = call %struct.TYPE_5__* @calloc(i32 %18, i32 24)
store %struct.TYPE_5__* %19, %struct.TYPE_5__** %14
store i32 0, i32* %12
br label %20
20: ; preds = %62, %5
%21 = load i32, i32* %12
%22 = load i32, i32* %11
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %65
24: ; preds = %20
%25 = load i32, i32* %12
%26 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%27 = load i32, i32* %12
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %26, i64 %28
%30 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %29, i32 0, i32 0
store i32 %25, i32* %30
%31 = load i32, i32* %7
%32 = load i32, i32* @MEMGETREGIONINFO
%33 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%34 = load i32, i32* %12
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %33, i64 %35
%37 = call i64 @ioctl(i32 %31, i32 %32, %struct.TYPE_5__* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %24
store i32 8, i32* %6
br label %189
40: ; preds = %24
%41 = load i32, i32* %12
%42 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%43 = load i32, i32* %12
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %42, i64 %44
%46 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %48, i64 %50
%52 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %51, i32 0, i32 2
%53 = load i32, i32* %52
%54 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%55 = load i32, i32* %12
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %54, i64 %56
%58 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %57, i32 0, i32 3
%59 = load i32, i32* %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %41, i32 %47, i32 %53, i32 %59)
br label %61
61: ; preds = %40
br label %62
62: ; preds = %61
%63 = load i32, i32* %12
%64 = add nsw i32 %63, 1
store i32 %64, i32* %12
br label %20
65: ; preds = %20
store i32 0, i32* %12
br label %66
66: ; preds = %99, %65
%67 = load i32, i32* %12
%68 = load i32, i32* %11
%69 = icmp slt i32 %67, %68
br i1 %69, label %70, label %102
70: ; preds = %66
%71 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%72 = load i32, i32* %12
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %71, i64 %73
store %struct.TYPE_5__* %74, %struct.TYPE_5__** %15
%75 = load i32, i32* %8
%76 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%77 = load i32, i32* %12
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %76, i64 %78
%80 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %79, i32 0, i32 1
%81 = load i32, i32* %80
%82 = icmp sge i32 %75, %81
br i1 %82, label %83, label %98
83: ; preds = %70
%84 = load i32, i32* %8
%85 = load %struct.TYPE_5__*, %struct.TYPE_5__** %15
%86 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %85, i32 0, i32 1
%87 = load i32, i32* %86
%88 = load %struct.TYPE_5__*, %struct.TYPE_5__** %15
%89 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %88, i32 0, i32 2
%90 = load i32, i32* %89
%91 = load %struct.TYPE_5__*, %struct.TYPE_5__** %15
%92 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %91, i32 0, i32 3
%93 = load i32, i32* %92
%94 = mul nsw i32 %90, %93
%95 = add nsw i32 %87, %94
%96 = icmp slt i32 %84, %95
br i1 %96, label %97, label %98
97: ; preds = %83
br label %102
98: ; preds = %83, %70
br label %99
99: ; preds = %98
%100 = load i32, i32* %12
%101 = add nsw i32 %100, 1
store i32 %101, i32* %12
br label %66
102: ; preds = %97, %66
%103 = load i32, i32* %12
%104 = load i32, i32* %11
%105 = icmp sge i32 %103, %104
br i1 %105, label %106, label %109
106: ; preds = %102
%107 = load i32, i32* %8
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %107)
store i32 8, i32* %6
br label %189
109: ; preds = %102
store i32 0, i32* %13
br label %110
110: ; preds = %184, %109
%111 = load i32, i32* %13
%112 = load i32, i32* %9
%113 = icmp slt i32 %111, %112
br i1 %113, label %114, label %118
114: ; preds = %110
%115 = load i32, i32* %12
%116 = load i32, i32* %11
%117 = icmp slt i32 %115, %116
br label %118
118: ; preds = %114, %110
%119 = phi i1 [ false, %110 ], [ %117, %114 ]
br i1 %119, label %120, label %187
120: ; preds = %118
%121 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
%122 = load i32, i32* %12
%123 = sext i32 %122 to i64
%124 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %121, i64 %123
store %struct.TYPE_5__* %124, %struct.TYPE_5__** %17
%125 = load i32, i32* %8
%126 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %16, i32 0, i32 4
store i32 %125, i32* %126
%127 = load %struct.TYPE_5__*, %struct.TYPE_5__** %17
%128 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %127, i32 0, i32 3
%129 = load i32, i32* %128
%130 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %16, i32 0, i32 5
store i32 %129, i32* %130
%131 = load i32, i32* %10
%132 = icmp ne i32 %131, 0
br i1 %132, label %133, label %143
133: ; preds = %120
%134 = load i32, i32* %7
%135 = load i32, i32* @MEMUNLOCK
%136 = call i64 @ioctl(i32 %134, i32 %135, %struct.TYPE_5__* %16)
%137 = icmp ne i64 %136, 0
br i1 %137, label %138, label %142
138: ; preds = %133
%139 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%140 = load i32, i32* %7
%141 = call i32 @close(i32 %140)
store i32 8, i32* %6
br label %189
142: ; preds = %133
br label %143
143: ; preds = %142, %120
%144 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %16, i32 0, i32 5
%145 = load i32, i32* %144
%146 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %16, i32 0, i32 4
%147 = load i32, i32* %146
%148 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i32 %145, i32 %147)
%149 = load i32, i32* @stdout
%150 = call i32 @fflush(i32 %149)
%151 = load i32, i32* %7
%152 = load i32, i32* @MEMERASE
%153 = call i64 @ioctl(i32 %151, i32 %152, %struct.TYPE_5__* %16)
%154 = icmp ne i64 %153, 0
br i1 %154, label %155, label %159
155: ; preds = %143
%156 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%157 = load i32, i32* %7
%158 = call i32 @close(i32 %157)
store i32 8, i32* %6
br label %189
159: ; preds = %143
%160 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %16, i32 0, i32 5
%161 = load i32, i32* %160
%162 = load i32, i32* %8
%163 = add nsw i32 %162, %161
store i32 %163, i32* %8
%164 = load i32, i32* %8
%165 = load %struct.TYPE_5__*, %struct.TYPE_5__** %17
%166 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %165, i32 0, i32 1
%167 = load i32, i32* %166
%168 = load %struct.TYPE_5__*, %struct.TYPE_5__** %17
%169 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %168, i32 0, i32 2
%170 = load i32, i32* %169
%171 = load %struct.TYPE_5__*, %struct.TYPE_5__** %17
%172 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %171, i32 0, i32 3
%173 = load i32, i32* %172
%174 = mul nsw i32 %170, %173
%175 = add nsw i32 %167, %174
%176 = icmp sge i32 %164, %175
br i1 %176, label %177, label %183
177: ; preds = %159
%178 = load i32, i32* %12
%179 = add nsw i32 %178, 1
%180 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %179)
%181 = load i32, i32* %12
%182 = add nsw i32 %181, 1
store i32 %182, i32* %12
br label %183
183: ; preds = %177, %159
br label %184
184: ; preds = %183
%185 = load i32, i32* %13
%186 = add nsw i32 %185, 1
store i32 %186, i32* %13
br label %110
187: ; preds = %118
%188 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %6
br label %189
189: ; preds = %187, %155, %138, %106, %39
%190 = load i32, i32* %6
ret i32 %190
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32, i32, i32, i32, i32, i32 }
@MEMGETREGIONINFO = global i32 0
@.str = [57 x i8] c"Region %d is at %d of %d sector and with sector size %x\0A\00"
@.str.1 = [37 x i8] c"Starting offset %x not within chip.\0A\00"
@MEMUNLOCK = global i32 0
@.str.2 = [20 x i8] c"\0AMTD Unlock failure\00"
@.str.3 = [54 x i8] c"\0DPerforming Flash Erase of length %lu at offset 0x%lx\00"
@stdout = global i32 0
@MEMERASE = global i32 0
@.str.4 = [19 x i8] c"\0AMTD Erase failure\00"
@.str.5 = [22 x i8] c"\0AMoving to region %d\0A\00"
@str = [6 x i8] c" done\00"
define i32 @region_erase(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.TYPE_5__
%7 = tail call %struct.TYPE_5__* @calloc(i32 %4, i32 24)
%8 = icmp sgt i32 %4, 0
br i1 %8, label %9, label %58
9: ; preds = %5
%10 = zext i32 %4 to i64
br label %14
11: ; preds = %22
br i1 %8, label %12, label %58
12: ; preds = %11
%13 = zext i32 %4 to i64
br label %32
14: ; preds = %9, %22
%15 = phi i64 [ 0, %9 ], [ %30, %22 ]
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %15
%17 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %16, i64 0, i32 0
%18 = trunc i64 %15 to i32
store i32 %18, i32* %17
%19 = load i32, i32* @MEMGETREGIONINFO
%20 = tail call i64 @ioctl(i32 %0, i32 %19, %struct.TYPE_5__* %16)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %109
22: ; preds = %14
%23 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %15, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %15, i32 2
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %15, i32 3
%28 = load i32, i32* %27
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %24, i32 %26, i32 %28)
%30 = add nuw nsw i64 %15, 1
%31 = icmp eq i64 %30, %10
br i1 %31, label %11, label %14
32: ; preds = %12, %55
%33 = phi i64 [ 0, %12 ], [ %56, %55 ]
%34 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %33, i32 1
%35 = load i32, i32* %34
%36 = icmp sgt i32 %35, %1
br i1 %36, label %55, label %37
37: ; preds = %32
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %33, i32 2
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %33, i32 3
%41 = load i32, i32* %40
%42 = mul nsw i32 %41, %39
%43 = add nsw i32 %42, %35
%44 = icmp sgt i32 %43, %1
br i1 %44, label %45, label %55
45: ; preds = %37
%46 = trunc i64 %33 to i32
%47 = icmp sgt i32 %2, 0
%48 = icmp slt i32 %46, %4
%49 = select i1 %47, i1 %48, i1 false
br i1 %49, label %50, label %107
50: ; preds = %45
%51 = bitcast %struct.TYPE_5__* %6 to i8*
%52 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %6, i64 0, i32 4
%53 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %6, i64 0, i32 5
%54 = icmp eq i32 %3, 0
br label %60
55: ; preds = %32, %37
%56 = add nuw nsw i64 %33, 1
%57 = icmp eq i64 %56, %13
br i1 %57, label %58, label %32
58: ; preds = %55, %5, %11
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %109
60: ; preds = %50, %101
%61 = phi i32 [ %1, %50 ], [ %85, %101 ]
%62 = phi i32 [ %46, %50 ], [ %102, %101 ]
%63 = phi i32 [ 0, %50 ], [ %103, %101 ]
%64 = sext i32 %62 to i64
store i32 %61, i32* %52
%65 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %64, i32 3
%66 = load i32, i32* %65
store i32 %66, i32* %53
br i1 %54, label %74, label %67
67: ; preds = %60
%68 = load i32, i32* @MEMUNLOCK
%69 = call i64 @ioctl(i32 %0, i32 %68, %struct.TYPE_5__* %6)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %97
71: ; preds = %67
%72 = load i32, i32* %53
%73 = load i32, i32* %52
br label %74
74: ; preds = %71, %60
%75 = phi i32 [ %73, %71 ], [ %61, %60 ]
%76 = phi i32 [ %72, %71 ], [ %66, %60 ]
%77 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i32 %76, i32 %75)
%78 = load i32, i32* @stdout
%79 = call i32 @fflush(i32 %78)
%80 = load i32, i32* @MEMERASE
%81 = call i64 @ioctl(i32 %0, i32 %80, %struct.TYPE_5__* %6)
%82 = icmp eq i64 %81, 0
br i1 %82, label %83, label %97
83: ; preds = %74
%84 = load i32, i32* %53
%85 = add nsw i32 %84, %61
%86 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %64, i32 1
%87 = load i32, i32* %86
%88 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i64 %64, i32 2
%89 = load i32, i32* %88
%90 = load i32, i32* %65
%91 = mul nsw i32 %90, %89
%92 = add nsw i32 %91, %87
%93 = icmp slt i32 %85, %92
br i1 %93, label %101, label %94
94: ; preds = %83
%95 = add nsw i32 %62, 1
%96 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %95)
br label %101
97: ; preds = %74, %67
%98 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %67 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), %74 ]
%99 = call i32 @perror(i8* %98)
%100 = call i32 @close(i32 %0)
br label %109
101: ; preds = %94, %83
%102 = phi i32 [ %62, %83 ], [ %95, %94 ]
%103 = add nuw nsw i32 %63, 1
%104 = icmp slt i32 %103, %2
%105 = icmp slt i32 %102, %4
%106 = select i1 %104, i1 %105, i1 false
br i1 %106, label %60, label %107
107: ; preds = %101, %45
%108 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %109
109: ; preds = %14, %97, %107, %58
%110 = phi i32 [ 8, %58 ], [ 8, %97 ], [ 0, %107 ], [ 8, %14 ]
ret i32 %110
}