#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
char getchar();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/);
int /*<<< orphan*/ m_read(int);
int /*<<< orphan*/ m_seek(int);
int /*<<< orphan*/ m_write(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  int fd = -1;
  char cmd;
  if (2 != argc) {
    printf("check your input pls!\n");
    return 0;
  }
  do {
    printf("[o-open|w-write|r-read|c-clear|s-seek|q-quit]:");
    cmd = getchar();
    getchar();
    switch (cmd) {
    case 'o':
      if (-1 == fd)
        fd = open(argv[1], O_RDWR);
      break;
    case 'w':
      if (-1 == fd) {
        printf("you should run command o first!\n");
        break;
      }
      m_write(fd);
      break;
    case 'r':
      if (-1 == fd) {
        printf("you should run command o first!\n");
        break;
      }
      m_read(fd);
      break;
    case 'q':
      if (-1 != fd)
        close(fd);
      break;
    case 'c':
      if (-1 == fd) {
        printf("you should run command o first!\n");
        break;
      }
      ioctl(fd, 0);
      break;
    case 's':
      if (-1 == fd) {
        printf("you should run command o first!\n");
        break;
      }
      m_seek(fd);
      break;
    default:;
    }
  } while ('q' != cmd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11c3 <main+0x43>
lea    0xea2(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x1b0>
jmp    11c8 <main+0x48>
lea    0xe9c(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
mov    %al,-0x21(%rbp)
call   1080 <getchar@plt>
mov    %al,%cl
mov    -0x21(%rbp),%al
mov    %cl,-0x15(%rbp)
call   1080 <getchar@plt>
movsbl -0x15(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x14,%eax
ja     130e <main+0x18e>
mov    -0x20(%rbp),%rax
lea    0xdf4(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    123c <main+0xbc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
jmp    1313 <main+0x193>
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1260 <main+0xe0>
lea    0xe44(%rip),%rdi
call   1050 <printf@plt>
jmp    1313 <main+0x193>
mov    -0x14(%rbp),%edi
call   10a0 <m_write@plt>
jmp    1313 <main+0x193>
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    128c <main+0x10c>
lea    0xe18(%rip),%rdi
call   1050 <printf@plt>
jmp    1313 <main+0x193>
mov    -0x14(%rbp),%edi
call   1030 <m_read@plt>
jmp    1313 <main+0x193>
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
je     12af <main+0x12f>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
jmp    1313 <main+0x193>
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    12d3 <main+0x153>
lea    0xdd1(%rip),%rdi
call   1050 <printf@plt>
jmp    1313 <main+0x193>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <ioctl@plt>
jmp    1313 <main+0x193>
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1301 <main+0x181>
lea    0xda3(%rip),%rdi
call   1050 <printf@plt>
jmp    1313 <main+0x193>
mov    -0x14(%rbp),%edi
call   1040 <m_seek@plt>
jmp    1313 <main+0x193>
jmp    1313 <main+0x193>
jmp    1318 <main+0x198>
movsbl -0x15(%rbp),%ecx
mov    $0x71,%eax
cmp    %ecx,%eax
jne    11c8 <main+0x48>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    128b <main+0xfb>
mov    %rsi,%r14
mov    $0xffffffff,%ebp
lea    0xea1(%rip),%r12
lea    0xe46(%rip),%r13
lea    0xec2(%rip),%r15
jmp    11e6 <main+0x56>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebp
cmp    $0x71,%bl
je     1297 <main+0x107>
mov    %r12,%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getchar@plt>
movsbl %bl,%eax
add    $0xffffff9d,%eax
cmp    $0x14,%eax
ja     11dd <main+0x4d>
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
cmp    $0xffffffff,%ebp
je     11d0 <main+0x40>
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <ioctl@plt>
jmp    11dd <main+0x4d>
cmp    $0xffffffff,%ebp
jne    11dd <main+0x4d>
mov    0x8(%r14),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%ebp
jmp    11dd <main+0x4d>
cmp    $0xffffffff,%ebp
je     11d8 <main+0x48>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    11dd <main+0x4d>
cmp    $0xffffffff,%ebp
je     11d0 <main+0x40>
mov    %ebp,%edi
call   1030 <m_read@plt>
jmp    11dd <main+0x4d>
cmp    $0xffffffff,%ebp
je     11d0 <main+0x40>
mov    %ebp,%edi
call   1040 <m_seek@plt>
jmp    11dd <main+0x4d>
cmp    $0xffffffff,%ebp
je     11d0 <main+0x40>
mov    %ebp,%edi
call   10b0 <m_write@plt>
jmp    11dd <main+0x4d>
lea    0xe11(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"check your input pls!\0A\00"
@.str.1 = [47 x i8] c"[o-open|w-write|r-read|c-clear|s-seek|q-quit]:\00"
@O_RDWR = global i32 0
@.str.2 = [33 x i8] c"you should run command o first!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%8 = load i32, i32* %4
%9 = icmp ne i32 2, %8
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %75
12: ; preds = %2
br label %13
13: ; preds = %70, %12
%14 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%15 = call signext i8 (...) @getchar()
store i8 %15, i8* %7
%16 = call signext i8 (...) @getchar()
%17 = load i8, i8* %7
%18 = sext i8 %17 to i32
switch i32 %18, label %68 [
i32 111, label %19
i32 119, label %29
i32 114, label %37
i32 113, label %45
i32 99, label %52
i32 115, label %60
]
19: ; preds = %13
%20 = load i32, i32* %6
%21 = icmp eq i32 -1, %20
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %6
br label %28
28: ; preds = %22, %19
br label %69
29: ; preds = %13
%30 = load i32, i32* %6
%31 = icmp eq i32 -1, %30
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %69
34: ; preds = %29
%35 = load i32, i32* %6
%36 = call i32 @m_write(i32 %35)
br label %69
37: ; preds = %13
%38 = load i32, i32* %6
%39 = icmp eq i32 -1, %38
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %69
42: ; preds = %37
%43 = load i32, i32* %6
%44 = call i32 @m_read(i32 %43)
br label %69
45: ; preds = %13
%46 = load i32, i32* %6
%47 = icmp ne i32 -1, %46
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %45
br label %69
52: ; preds = %13
%53 = load i32, i32* %6
%54 = icmp eq i32 -1, %53
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %69
57: ; preds = %52
%58 = load i32, i32* %6
%59 = call i32 @ioctl(i32 %58, i32 0)
br label %69
60: ; preds = %13
%61 = load i32, i32* %6
%62 = icmp eq i32 -1, %61
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %69
65: ; preds = %60
%66 = load i32, i32* %6
%67 = call i32 @m_seek(i32 %66)
br label %69
68: ; preds = %13
br label %69
69: ; preds = %68, %65, %63, %57, %55, %51, %42, %40, %34, %32, %28
br label %70
70: ; preds = %69
%71 = load i8, i8* %7
%72 = sext i8 %71 to i32
%73 = icmp ne i32 113, %72
br i1 %73, label %13, label %74
74: ; preds = %70
store i32 0, i32* %3
br label %75
75: ; preds = %74, %10
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str.1 = [47 x i8] c"[o-open|w-write|r-read|c-clear|s-seek|q-quit]:\00"
@O_RDWR = global i32 0
@str.5 = [32 x i8] c"you should run command o first!\00"
@str.6 = [22 x i8] c"check your input pls!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
br label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
br label %51
8: ; preds = %4, %48
%9 = phi i32 [ %49, %48 ], [ -1, %4 ]
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call signext i8 (...) @getchar()
%12 = tail call signext i8 (...) @getchar()
%13 = sext i8 %11 to i32
switch i32 %13, label %48 [
i32 111, label %14
i32 119, label %20
i32 114, label %26
i32 113, label %32
i32 99, label %36
i32 115, label %42
]
14: ; preds = %8
%15 = icmp eq i32 %9, -1
br i1 %15, label %16, label %48
16: ; preds = %14
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* %17, i32 %18)
br label %48
20: ; preds = %8
%21 = icmp eq i32 %9, -1
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.5, i64 0, i64 0))
br label %48
24: ; preds = %20
%25 = tail call i32 @m_write(i32 %9)
br label %48
26: ; preds = %8
%27 = icmp eq i32 %9, -1
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.5, i64 0, i64 0))
br label %48
30: ; preds = %26
%31 = tail call i32 @m_read(i32 %9)
br label %48
32: ; preds = %8
%33 = icmp eq i32 %9, -1
br i1 %33, label %48, label %34
34: ; preds = %32
%35 = tail call i32 @close(i32 %9)
br label %48
36: ; preds = %8
%37 = icmp eq i32 %9, -1
br i1 %37, label %38, label %40
38: ; preds = %36
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.5, i64 0, i64 0))
br label %48
40: ; preds = %36
%41 = tail call i32 @ioctl(i32 %9, i32 0)
br label %48
42: ; preds = %8
%43 = icmp eq i32 %9, -1
br i1 %43, label %44, label %46
44: ; preds = %42
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.5, i64 0, i64 0))
br label %48
46: ; preds = %42
%47 = tail call i32 @m_seek(i32 %9)
br label %48
48: ; preds = %22, %24, %28, %30, %38, %40, %44, %46, %16, %14, %34, %32, %8
%49 = phi i32 [ %9, %8 ], [ -1, %44 ], [ %9, %46 ], [ -1, %38 ], [ %9, %40 ], [ %9, %34 ], [ -1, %32 ], [ -1, %28 ], [ %9, %30 ], [ -1, %22 ], [ %9, %24 ], [ %19, %16 ], [ %9, %14 ]
%50 = icmp eq i8 %11, 113
br i1 %50, label %51, label %8
51: ; preds = %48, %6
ret i32 0
}