#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_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct flashinfo {
  int dummy;
};
struct TYPE_5__ {
  int erasesize;
};
typedef TYPE_1__ region_info_t;
struct TYPE_6__ {
  unsigned long start;
  int length;
};
typedef TYPE_2__ erase_info_t;

/* Variables and functions */
int /*<<< orphan*/ MEMERASE;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
TYPE_1__ *find_region(unsigned long, struct flashinfo *);
scalar_t__ ioctl(int, int /*<<< orphan*/, TYPE_2__ *);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
void flash_erase(int fd, struct flashinfo *fi, unsigned long off,
                 unsigned long len) {
  region_info_t *ri;
  erase_info_t ei;
  while (len != 0) {
    ri = find_region(off, fi);
    if (ri == NULL)
      panic("this should never happen :-(\n");
    ei.start = off;
    ei.length = ri->erasesize;
    printf("erasing  : %08lX .. %08lX\r", (unsigned long)(ei.start),
           (unsigned long)(ei.start + ei.length - 1));
    fflush(stdout);
    if (ioctl(fd, MEMERASE, &ei) != 0)
      panic("\nerror erasing flash\n");
    off += ri->erasesize;
    len -= ri->erasesize;
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<flash_erase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     122b <flash_erase+0xdb>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <find_region@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119a <flash_erase+0x4a>
lea    0xe6b(%rip),%rdi
call   1040 <panic@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x38(%rbp),%rdx
movslq -0x30(%rbp),%rax
add    %rax,%rdx
sub    $0x1,%rdx
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     1205 <flash_erase+0xb5>
lea    0xe39(%rip),%rdi
call   1040 <panic@plt>
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
movslq (%rax),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1167 <flash_erase+0x17>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_erase>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rcx,%rcx
je     1218 <flash_erase+0xb8>
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
lea    0x8(%rsp),%r12
jmp    119c <flash_erase+0x3c>
nopl   0x0(%rax)
movslq 0x0(%r13),%rax
add    %rax,%rbx
sub    %rax,%r14
je     1218 <flash_erase+0xb8>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <find_region@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11bb <flash_erase+0x5b>
lea    0xe4a(%rip),%rdi
call   1050 <panic@plt>
mov    %rbx,0x8(%rsp)
movslq 0x0(%r13),%rax
mov    %eax,0x10(%rsp)
lea    (%rbx,%rax,1),%rdx
add    $0xffffffffffffffff,%rdx
lea    0xe47(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
je     1190 <flash_erase+0x30>
lea    0xe2b(%rip),%rdi
call   1050 <panic@plt>
jmp    1190 <flash_erase+0x30>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flashinfo = type { i32 }
%struct.TYPE_5__ = type { i32 }
%struct.TYPE_6__ = type { i64, i32 }
@.str = [30 x i8] c"this should never happen :-(\0A\00"
@.str.1 = [27 x i8] c"erasing : %08lX .. %08lX\0D\00"
@stdout = global i32 0
@MEMERASE = global i32 0
@.str.2 = [22 x i8] c"\0Aerror erasing flash\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @flash_erase(i32 %0, %struct.flashinfo* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca %struct.flashinfo*
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.TYPE_5__*
%10 = alloca %struct.TYPE_6__
store i32 %0, i32* %5
store %struct.flashinfo* %1, %struct.flashinfo** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
br label %11
11: ; preds = %47, %4
%12 = load i64, i64* %8
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %60
14: ; preds = %11
%15 = load i64, i64* %7
%16 = load %struct.flashinfo*, %struct.flashinfo** %6
%17 = call %struct.TYPE_5__* @find_region(i64 %15, %struct.flashinfo* %16)
store %struct.TYPE_5__* %17, %struct.TYPE_5__** %9
%18 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%19 = icmp eq %struct.TYPE_5__* %18, null
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @panic(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %14
%23 = load i64, i64* %7
%24 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %10, i32 0, i32 0
store i64 %23, i64* %24
%25 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%26 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %10, i32 0, i32 1
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %10, i32 0, i32 0
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %10, i32 0, i32 0
%32 = load i64, i64* %31
%33 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %10, i32 0, i32 1
%34 = load i32, i32* %33
%35 = sext i32 %34 to i64
%36 = add i64 %32, %35
%37 = sub i64 %36, 1
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %30, i64 %37)
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
%41 = load i32, i32* %5
%42 = load i32, i32* @MEMERASE
%43 = call i64 @ioctl(i32 %41, i32 %42, %struct.TYPE_6__* %10)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %22
%46 = call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %45, %22
%48 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%49 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = load i64, i64* %7
%53 = add i64 %52, %51
store i64 %53, i64* %7
%54 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%55 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = sext i32 %56 to i64
%58 = load i64, i64* %8
%59 = sub i64 %58, %57
store i64 %59, i64* %8
br label %11
60: ; preds = %11
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.flashinfo = type { i32 }
%struct.TYPE_6__ = type { i64, i32 }
%struct.TYPE_5__ = type { i32 }
@.str = [30 x i8] c"this should never happen :-(\0A\00"
@.str.1 = [27 x i8] c"erasing : %08lX .. %08lX\0D\00"
@stdout = global i32 0
@MEMERASE = global i32 0
@.str.2 = [22 x i8] c"\0Aerror erasing flash\0A\00"
define void @flash_erase(i32 %0, %struct.flashinfo* %1, i64 %2, i64 %3) {
%5 = alloca %struct.TYPE_6__
%6 = bitcast %struct.TYPE_6__* %5 to i8*
%7 = icmp eq i64 %3, 0
br i1 %7, label %38, label %8
8: ; preds = %4
%9 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %5, i64 0, i32 0
%10 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %5, i64 0, i32 1
br label %11
11: ; preds = %8, %32
%12 = phi i64 [ %2, %8 ], [ %35, %32 ]
%13 = phi i64 [ %3, %8 ], [ %36, %32 ]
%14 = call %struct.TYPE_5__* @find_region(i64 %12, %struct.flashinfo* %1)
%15 = icmp eq %struct.TYPE_5__* %14, null
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @panic(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
store i64 %12, i64* %9
%19 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %14, i64 0, i32 0
%20 = load i32, i32* %19
store i32 %20, i32* %10
%21 = sext i32 %20 to i64
%22 = add i64 %12, -1
%23 = add i64 %22, %21
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %12, i64 %23)
%25 = load i32, i32* @stdout
%26 = call i32 @fflush(i32 %25)
%27 = load i32, i32* @MEMERASE
%28 = call i64 @ioctl(i32 %0, i32 %27, %struct.TYPE_6__* %5)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %18
%31 = call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %18
%33 = load i32, i32* %19
%34 = sext i32 %33 to i64
%35 = add i64 %12, %34
%36 = sub i64 %13, %34
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %11
38: ; preds = %32, %4
%39 = call i32 @putchar(i32 10)
ret void
}