#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(void) {
  int fd = -1;
  char cmd;
  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("./memdev_mutex", 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    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
lea    0xeb7(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
mov    %al,-0x19(%rbp)
call   1080 <getchar@plt>
mov    %al,%cl
mov    -0x19(%rbp),%al
mov    %cl,-0x9(%rbp)
call   1080 <getchar@plt>
movsbl -0x9(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x14,%eax
ja     12db <main+0x15b>
mov    -0x18(%rbp),%rax
lea    0xe26(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1209 <main+0x89>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
jmp    12e0 <main+0x160>
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    122d <main+0xad>
lea    0xe6f(%rip),%rdi
call   1050 <printf@plt>
jmp    12e0 <main+0x160>
mov    -0x8(%rbp),%edi
call   10a0 <m_write@plt>
jmp    12e0 <main+0x160>
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1259 <main+0xd9>
lea    0xe43(%rip),%rdi
call   1050 <printf@plt>
jmp    12e0 <main+0x160>
mov    -0x8(%rbp),%edi
call   1030 <m_read@plt>
jmp    12e0 <main+0x160>
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
je     127c <main+0xfc>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    12e0 <main+0x160>
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    12a0 <main+0x120>
lea    0xdfc(%rip),%rdi
call   1050 <printf@plt>
jmp    12e0 <main+0x160>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <ioctl@plt>
jmp    12e0 <main+0x160>
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    12ce <main+0x14e>
lea    0xdce(%rip),%rdi
call   1050 <printf@plt>
jmp    12e0 <main+0x160>
mov    -0x8(%rbp),%edi
call   1040 <m_seek@plt>
jmp    12e0 <main+0x160>
jmp    12e0 <main+0x160>
jmp    12e5 <main+0x165>
movsbl -0x9(%rbp),%ecx
mov    $0x71,%eax
cmp    %ecx,%eax
jne    1196 <main+0x16>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
lea    0xead(%rip),%r12
lea    0xe52(%rip),%r13
lea    0xedd(%rip),%r14
lea    0xec7(%rip),%r15
jmp    11d6 <main+0x46>
xchg   %ax,%ax
mov    %r14,%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebp
cmp    $0x71,%bl
je     1273 <main+0xe3>
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     11cd <main+0x3d>
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
cmp    $0xffffffff,%ebp
je     11c0 <main+0x30>
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <ioctl@plt>
jmp    11cd <main+0x3d>
cmp    $0xffffffff,%ebp
jne    11cd <main+0x3d>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
jmp    11cd <main+0x3d>
cmp    $0xffffffff,%ebp
je     11c8 <main+0x38>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    11cd <main+0x3d>
cmp    $0xffffffff,%ebp
je     11c0 <main+0x30>
mov    %ebp,%edi
call   1030 <m_read@plt>
jmp    11cd <main+0x3d>
cmp    $0xffffffff,%ebp
je     11c0 <main+0x30>
mov    %ebp,%edi
call   1040 <m_seek@plt>
jmp    11cd <main+0x3d>
cmp    $0xffffffff,%ebp
je     11c0 <main+0x30>
mov    %ebp,%edi
call   10b0 <m_write@plt>
jmp    11cd <main+0x3d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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