#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 /*<<< orphan*/ size;
  int /*<<< orphan*/ length;
  scalar_t__ start;
};
struct erase_info_user {
  int /*<<< orphan*/ size;
  int /*<<< orphan*/ length;
  scalar_t__ 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);
int main(int argc, char *argv[]) {
  int fd;
  struct mtd_info_user mtdInfo;
  struct erase_info_user mtdLockInfo;
  if (argc != 2) {
    fprintf(stderr, "USAGE: %s <mtd device>\n", argv[0]);
    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);
  }
  mtdLockInfo.start = 0;
  mtdLockInfo.length = mtdInfo.size;
  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    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a6 <main+0x46>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe63(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11f0 <main+0x90>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11f5 <main+0x95>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    123f <main+0xdf>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe30(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     128d <main+0x12d>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe09(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     12e9 <main+0x189>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdd4(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11f6 <main+0x96>
mov    0x8(%rbx),%rdi
lea    0xe9b(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    120b <main+0xab>
mov    0x8(%rbx),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1221 <main+0xc1>
mov    %eax,%ebp
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    1244 <main+0xe4>
movq   $0x0,0x10(%rsp)
mov    0x18(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    125a <main+0xfa>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdf7(%rip),%rsi
jmp    1235 <main+0xd5>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe02(%rip),%rsi
jmp    1235 <main+0xd5>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe2b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe27(%rip),%rsi
jmp    126e <main+0x10e>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe38(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.mtd_info_user = type { i32, i32, i64 }
%struct.erase_info_user = type { i32, i32, i64 }
@stderr = global i32 0
@.str = [24 x i8] c"USAGE: %s <mtd device>\0A\00"
@.str.1 = [9 x i8] c"/dev/mtd\00"
@.str.2 = [63 x i8] c"'%s' is not a MTD device. Must specify mtd device: /dev/mtd?\0A\00"
@O_RDWR = global i32 0
@.str.3 = [31 x i8] c"Could not open mtd device: %s\0A\00"
@MEMGETINFO = global i32 0
@.str.4 = [39 x i8] c"Could not get MTD device info from %s\0A\00"
@MEMUNLOCK = global i32 0
@.str.5 = [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 eq i32 %0, 2
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %14
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %13
%24 = load i8*, i8** %14
%25 = load i32, i32* @O_RDWR
%26 = tail call i32 @open(i8* %24, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %14
%31 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %23
%34 = load i32, i32* @MEMGETINFO
%35 = call i64 @ioctl(i32 %26, i32 %34, %struct.mtd_info_user* %3)
%36 = icmp eq i64 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %14
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%41 = call i32 @close(i32 %26)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %33
%44 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %4, i64 0, i32 2
store i64 0, i64* %44
%45 = getelementptr inbounds %struct.mtd_info_user, %struct.mtd_info_user* %3, i64 0, i32 0
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.erase_info_user, %struct.erase_info_user* %4, i64 0, i32 1
store i32 %46, i32* %47
%48 = load i32, i32* @MEMUNLOCK
%49 = bitcast %struct.erase_info_user* %4 to %struct.mtd_info_user*
%50 = call i64 @ioctl(i32 %26, i32 %48, %struct.mtd_info_user* %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %58, label %52
52: ; preds = %43
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %14
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %54)
%56 = call i32 @close(i32 %26)
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %43
ret i32 0
}