#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 */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ MEMGETREGIONCOUNT;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int non_region_erase(int, int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int region_erase(int, int, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int erase_fis_block() {
  int fd = open("/dev/mtd0", O_RDWR);
  if (fd == -1) {
    fprintf(stderr, "Can't open %s for writing!\n", "/dev/mtd0");
    return -1;
  }
  int regcount, res;
  if (ioctl(fd, MEMGETREGIONCOUNT, &regcount) == 0) {
    if (regcount == 0) {
      res = non_region_erase(fd, 0x80000, 1, 0);
    } else {
      res = region_erase(fd, 0x80000, 1, 0, regcount);
    }
  }
  close(fd);
  return res;
}


x86 assembly (clang, -O0):
<erase_fis_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b2 <erase_fis_block+0x52>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
lea    0xe5f(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <erase_fis_block+0xc5>
mov    -0x8(%rbp),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    1217 <erase_fis_block+0xb7>
cmpl   $0x0,-0xc(%rbp)
jne    11f7 <erase_fis_block+0x97>
mov    -0x8(%rbp),%edi
mov    $0x80000,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <non_region_erase@plt>
mov    %eax,-0x10(%rbp)
jmp    1212 <erase_fis_block+0xb2>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%r8d
mov    $0x80000,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1070 <region_erase@plt>
mov    %eax,-0x10(%rbp)
jmp    1217 <erase_fis_block+0xb7>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<erase_fis_block>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11b8 <erase_fis_block+0x58>
mov    %eax,%ebx
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    11e2 <erase_fis_block+0x82>
mov    0x4(%rsp),%r8d
mov    %ebx,%edi
mov    $0x80000,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
test   %r8d,%r8d
je     11db <erase_fis_block+0x7b>
call   1070 <region_erase@plt>
jmp    11e0 <erase_fis_block+0x80>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
lea    0xe31(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    11e9 <erase_fis_block+0x89>
call   1030 <non_region_erase@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/mtd0\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"Can't open %s for writing!\0A\00"
@MEMGETREGIONCOUNT = global i32 0
define i32 @erase_fis_block() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %32
12: ; preds = %0
%13 = load i32, i32* %2
%14 = load i32, i32* @MEMGETREGIONCOUNT
%15 = call i64 @ioctl(i32 %13, i32 %14, i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %2
%22 = call i32 @non_region_erase(i32 %21, i32 524288, i32 1, i32 0)
store i32 %22, i32* %4
br label %27
23: ; preds = %17
%24 = load i32, i32* %2
%25 = load i32, i32* %3
%26 = call i32 @region_erase(i32 %24, i32 524288, i32 1, i32 0, i32 %25)
store i32 %26, i32* %4
br label %27
27: ; preds = %23, %20
br label %28
28: ; preds = %27, %12
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %4
store i32 %31, i32* %1
br label %32
32: ; preds = %28, %9
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/mtd0\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"Can't open %s for writing!\0A\00"
@MEMGETREGIONCOUNT = global i32 0
define i32 @erase_fis_block() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %23
8: ; preds = %0
%9 = bitcast i32* %1 to i8*
%10 = load i32, i32* @MEMGETREGIONCOUNT
%11 = call i64 @ioctl(i32 %3, i32 %10, i32* %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = load i32, i32* %1
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @non_region_erase(i32 %3, i32 524288, i32 1, i32 0)
br label %20
18: ; preds = %13
%19 = call i32 @region_erase(i32 %3, i32 524288, i32 1, i32 0, i32 %14)
br label %20
20: ; preds = %16, %18, %8
%21 = phi i32 [ %17, %16 ], [ %19, %18 ], [ undef, %8 ]
%22 = call i32 @close(i32 %3)
br label %23
23: ; preds = %20, %5
%24 = phi i32 [ -1, %5 ], [ %21, %20 ]
ret i32 %24
}