#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 */
struct mtd_info_user {
  int length;
  int erasesize;
  int size;
  void *start;
};
struct erase_info_user {
  int length;
  int erasesize;
  int size;
  void *start;
};

/* Variables and functions */
int /*<<< orphan*/ MEMGETINFO;
int /*<<< orphan*/ MEMUNLOCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct mtd_info_user *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(char *, char *, int);
void *strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  struct mtd_info_user mtdInfo;
  struct erase_info_user mtdLockInfo;
  int count;
  if (argc < 2) {
    fprintf(stderr,
            "USAGE: %s <mtd device> <offset in hex> <block count in decimal "
            "number>\n",
            "flash_unlock");
    exit(1);
  } else if (strncmp(argv[1], "/dev/mtd", 8) != 0) {
    fprintf(stderr,
            "'%s' is not a MTD device.  Must specify mtd device: /dev/mtd?\n",
            argv[1]);
    exit(1);
  }
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Could not open mtd device: %s\n", argv[1]);
    exit(1);
  }
  if (ioctl(fd, MEMGETINFO, &mtdInfo)) {
    fprintf(stderr, "Could not get MTD device info from %s\n", argv[1]);
    close(fd);
    exit(1);
  }
  if (argc > 2)
    mtdLockInfo.start = strtol(argv[2], NULL, 0);
  else
    mtdLockInfo.start = 0;
  if (argc > 3) {
    count = strtol(argv[3], NULL, 0);
    mtdLockInfo.length = mtdInfo.erasesize * count;
  } else {
    mtdLockInfo.length = mtdInfo.size - mtdInfo.erasesize;
  }
  if (ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
    fprintf(stderr, "Could not unlock MTD device: %s\n", argv[1]);
    close(fd);
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b6 <main+0x46>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
lea    0xea1(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe90(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1200 <main+0x90>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe6d(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1205 <main+0x95>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124f <main+0xdf>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe5d(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     129d <main+0x12d>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe36(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jle    12c3 <main+0x153>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %rax,-0x38(%rbp)
jmp    12cb <main+0x15b>
movq   $0x0,-0x38(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    12fa <main+0x18a>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x2c(%rbp),%eax
imul   -0x4c(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    1303 <main+0x193>
mov    -0x28(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cbb(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1351 <main+0x1e1>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xda9(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
cmp    $0x1,%edi
jle    1248 <main+0xd8>
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
lea    0xec4(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1261 <main+0xf1>
mov    0x8(%r14),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1277 <main+0x107>
mov    %eax,%ebp
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    129a <main+0x12a>
cmp    $0x2,%ebx
jbe    1211 <main+0xa1>
mov    0x10(%r14),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %rax,0x10(%rsp)
cmp    $0x3,%ebx
je     121a <main+0xaa>
mov    0x18(%r14),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
imul   0x1c(%rsp),%eax
mov    %eax,(%rsp)
jmp    1225 <main+0xb5>
movq   $0x0,0x10(%rsp)
mov    0x20(%rsp),%eax
sub    0x1c(%rsp),%eax
mov    %eax,(%rsp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    12b0 <main+0x140>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
lea    0xde9(%rip),%rdx
jmp    128b <main+0x11b>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xde9(%rip),%rsi
jmp    128b <main+0x11b>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe12(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe0e(%rip),%rsi
jmp    12c4 <main+0x154>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe1f(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.mtd_info_user = type { i32, i32, i32, i8* }
%struct.erase_info_user = type { i32, i32, i32, i8* }
@stderr = global i32 0
@.str = [72 x i8] c"USAGE: %s <mtd device> <offset in hex> <block count in decimal number>\0A\00"
@.str.1 = [13 x i8] c"flash_unlock\00"
@.str.2 = [9 x i8] c"/dev/mtd\00"
@.str.3 = [63 x i8] c"'%s' is not a MTD device. Must specify mtd device: /dev/mtd?\0A\00"
@O_RDWR = global i32 0
@.str.4 = [31 x i8] c"Could not open mtd device: %s\0A\00"
@MEMGETINFO = global i32 0
@.str.5 = [39 x i8] c"Could not get MTD device info from %s\0A\00"
@MEMUNLOCK = global i32 0
@.str.6 = [33 x i8] c"Could not unlock MTD device: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.mtd_info_user
%8 = alloca %struct.erase_info_user
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 8)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %16
br label %30
30: ; preds = %29
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30
%46 = load i32, i32* %6
%47 = load i32, i32* @MEMGETINFO
%48 = call i64 @ioctl(i32 %46, i32 %47, %struct.mtd_info_user* %7)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 1
%54 = load i8*, i8** %53
%55 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %45
%60 = load i32, i32* %4
%61 = icmp sgt i32 %60, 2
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 2
%65 = load i8*, i8** %64
%66 = call i8* @strtol(i8* %65, i32* null, i32 0)
%67 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %8, i32 0, i32 3
store i8* %66, i8** %67
br label %70
68: ; preds = %59
%69 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %8, i32 0, i32 3
store i8* null, i8** %69
br label %70
70: ; preds = %68, %62
%71 = load i32, i32* %4
%72 = icmp sgt i32 %71, 3
br i1 %72, label %73, label %84
73: ; preds = %70
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 3
%76 = load i8*, i8** %75
%77 = call i8* @strtol(i8* %76, i32* null, i32 0)
%78 = ptrtoint i8* %77 to i32
store i32 %78, i32* %9
%79 = getelementptr inbounds %struct.mtd_info_user, %struct.mtd_info_user* %7, i32 0, i32 1
%80 = load i32, i32* %79
%81 = load i32, i32* %9
%82 = mul nsw i32 %80, %81
%83 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %8, i32 0, i32 0
store i32 %82, i32* %83
br label %91
84: ; preds = %70
%85 = getelementptr inbounds %struct.mtd_info_user, %struct.mtd_info_user* %7, i32 0, i32 2
%86 = load i32, i32* %85
%87 = getelementptr inbounds %struct.mtd_info_user, %struct.mtd_info_user* %7, i32 0, i32 1
%88 = load i32, i32* %87
%89 = sub nsw i32 %86, %88
%90 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %8, i32 0, i32 0
store i32 %89, i32* %90
br label %91
91: ; preds = %84, %73
%92 = load i32, i32* %6
%93 = load i32, i32* @MEMUNLOCK
%94 = bitcast %struct.erase_info_user* %8 to %struct.mtd_info_user*
%95 = call i64 @ioctl(i32 %92, i32 %93, %struct.mtd_info_user* %94)
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %106
97: ; preds = %91
%98 = load i32, i32* @stderr
%99 = load i8**, i8*** %5
%100 = getelementptr inbounds i8*, i8** %99, i64 1
%101 = load i8*, i8** %100
%102 = call i32 @fprintf(i32 %98, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %101)
%103 = load i32, i32* %6
%104 = call i32 @close(i32 %103)
%105 = call i32 @exit(i32 1)
unreachable
106: ; preds = %91
ret i32 0
}

LLVM IR (-O2):
%struct.mtd_info_user = type { i32, i32, i32, i8* }
%struct.erase_info_user = type { i32, i32, i32, i8* }
@stderr = global i32 0
@.str = [72 x i8] c"USAGE: %s <mtd device> <offset in hex> <block count in decimal number>\0A\00"
@.str.1 = [13 x i8] c"flash_unlock\00"
@.str.2 = [9 x i8] c"/dev/mtd\00"
@.str.3 = [63 x i8] c"'%s' is not a MTD device. Must specify mtd device: /dev/mtd?\0A\00"
@O_RDWR = global i32 0
@.str.4 = [31 x i8] c"Could not open mtd device: %s\0A\00"
@MEMGETINFO = global i32 0
@.str.5 = [39 x i8] c"Could not get MTD device info from %s\0A\00"
@MEMUNLOCK = global i32 0
@.str.6 = [33 x i8] c"Could not unlock MTD device: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.mtd_info_user
%4 = alloca %struct.erase_info_user
%5 = bitcast %struct.mtd_info_user* %3 to i8*
%6 = bitcast %struct.erase_info_user* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i64 @strncmp(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %13
%20 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = load i8*, i8** %13
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* %23, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %13
%30 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %22
%33 = load i32, i32* @MEMGETINFO
%34 = call i64 @ioctl(i32 %25, i32 %33, %struct.mtd_info_user* %3)
%35 = icmp eq i64 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %13
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i8* %38)
%40 = call i32 @close(i32 %25)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %32
%43 = icmp ugt i32 %0, 2
br i1 %43, label %46, label %44
44: ; preds = %42
%45 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %4, i64 0, i32 3
store i8* null, i8** %45
br label %62
46: ; preds = %42
%47 = getelementptr inbounds i8*, i8** %1, i64 2
%48 = load i8*, i8** %47
%49 = call i8* @strtol(i8* nocapture %48, i32* null, i32 0)
%50 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %4, i64 0, i32 3
store i8* %49, i8** %50
%51 = icmp eq i32 %0, 3
br i1 %51, label %62, label %52
52: ; preds = %46
%53 = getelementptr inbounds i8*, i8** %1, i64 3
%54 = load i8*, i8** %53
%55 = call i8* @strtol(i8* nocapture %54, i32* null, i32 0)
%56 = ptrtoint i8* %55 to i64
%57 = trunc i64 %56 to i32
%58 = getelementptr inbounds %struct.mtd_info_user, %struct.mtd_info_user* %3, i64 0, i32 1
%59 = load i32, i32* %58
%60 = mul nsw i32 %59, %57
%61 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %4, i64 0, i32 0
store i32 %60, i32* %61
br label %69
62: ; preds = %44, %46
%63 = getelementptr inbounds %struct.mtd_info_user, %struct.mtd_info_user* %3, i64 0, i32 2
%64 = load i32, i32* %63
%65 = getelementptr inbounds %struct.mtd_info_user, %struct.mtd_info_user* %3, i64 0, i32 1
%66 = load i32, i32* %65
%67 = sub nsw i32 %64, %66
%68 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %4, i64 0, i32 0
store i32 %67, i32* %68
br label %69
69: ; preds = %62, %52
%70 = load i32, i32* @MEMUNLOCK
%71 = bitcast %struct.erase_info_user* %4 to %struct.mtd_info_user*
%72 = call i64 @ioctl(i32 %25, i32 %70, %struct.mtd_info_user* %71)
%73 = icmp eq i64 %72, 0
br i1 %73, label %80, label %74
74: ; preds = %69
%75 = load i32, i32* @stderr
%76 = load i8*, i8** %13
%77 = call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %76)
%78 = call i32 @close(i32 %25)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %69
ret i32 0
}