#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 close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int write(int, int /*<<< orphan*/ *, size_t);
int main() {
  int ret, fd = 0;
  int choice = 0;
  size_t len = 1;
  char str[256];
  while (choice != 5) {
    printf("\nPlease choose operation:\n\t1: read\n\t2: write\n\t3: open\n\t4: "
           "close\n\t5: exit this program\nYour choice(1~5): ");
    scanf("%[^\n]%*c", str);
    sscanf(str, "%d", &choice);
    printf("choice: %s -> %d\n", str, choice);
    switch (choice) {
    case 1:
      if (fd <= 0) {
        printf("Device not open. Please first open the device\n");
        break;
      }
      ret = read(fd, NULL, len);
      if (ret < 0) {
        perror("Fail to read.\n");
        return errno;
      }
      break;
    case 2:
      if (fd <= 0) {
        printf("Device not open. Please first open the device\n");
        break;
      }
      ret = write(fd, NULL, len);
      if (ret < 0) {
        perror("Fail to write.\n");
        return errno;
      }
      break;
    case 3:
      if (fd > 0) {
        printf("Device already open.\n");
        break;
      }
      fd = open("/dev/mydevname", O_RDWR);
      if (fd < 0) {
        perror("Fail to open the device...");
        return errno;
      }
      break;
    case 4:
      if (fd == 0) {
        printf("Device already closed.\n");
        break;
      }
      ret = close(fd);
      if (ret < 0) {
        perror("Device fail to close.\n");
        return errno;
      }
      fd = 0;
      break;
    case 5:
      return 0;
    default:
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
cmpl   $0x5,-0x10(%rbp)
je     13cc <main+0x24c>
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x129(%rbp)
call   1040 <printf@plt>
lea    0xeb0(%rip),%rdi
lea    -0x120(%rbp),%rsi
mov    %rsi,-0x138(%rbp)
call   1080 <scanf@plt>
mov    -0x138(%rbp),%rdi
lea    0xe98(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x138(%rbp),%rsi
mov    -0x129(%rbp),%al
mov    -0x10(%rbp),%edx
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x128(%rbp)
sub    $0x4,%eax
ja     13c2 <main+0x242>
mov    -0x128(%rbp),%rax
lea    0xdc7(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpl   $0x0,-0xc(%rbp)
jg     125f <main+0xdf>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13c7 <main+0x247>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1299 <main+0x119>
lea    0xe47(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13d3 <main+0x253>
jmp    13c7 <main+0x247>
cmpl   $0x0,-0xc(%rbp)
jg     12bb <main+0x13b>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13c7 <main+0x247>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    12f5 <main+0x175>
lea    0xdfa(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13d3 <main+0x253>
jmp    13c7 <main+0x247>
cmpl   $0x0,-0xc(%rbp)
jle    1317 <main+0x197>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13c7 <main+0x247>
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1356 <main+0x1d6>
lea    0xdce(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13d3 <main+0x253>
jmp    13c7 <main+0x247>
cmpl   $0x0,-0xc(%rbp)
jne    1378 <main+0x1f8>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13c7 <main+0x247>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    13aa <main+0x22a>
lea    0xdad(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13d3 <main+0x253>
movl   $0x0,-0xc(%rbp)
jmp    13c7 <main+0x247>
movl   $0x0,-0x4(%rbp)
jmp    13d3 <main+0x253>
jmp    13c7 <main+0x247>
jmp    11a8 <main+0x28>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
movl   $0x0,0x4(%rsp)
xor    %eax,%eax
mov    %rax,0x8(%rsp)
lea    0x10(%rsp),%rbx
lea    0x4(%rsp),%rbp
lea    0xec8(%rip),%r14
lea    0xe38(%rip),%r12
xor    %r13d,%r13d
jmp    11ec <main+0x5c>
xor    %r15d,%r15d
lea    0xf50(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%r13d
mov    0x4(%rsp),%eax
cmp    $0x5,%eax
je     1310 <main+0x180>
lea    0xe21(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7c(%rip),%rdi
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    %rbx,%rdi
lea    0xe73(%rip),%rsi
mov    %rbp,%rdx
call   1080 <sscanf@plt>
mov    0x4(%rsp),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%eax
lea    -0x1(%rax),%ecx
cmp    $0x4,%ecx
ja     11e3 <main+0x53>
movslq (%r12,%rcx,4),%rax
add    %r12,%rax
jmp    *%rax
test   %r13d,%r13d
jle    11cd <main+0x3d>
mov    $0x1,%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1070 <read@plt>
test   %eax,%eax
jns    11df <main+0x4f>
jmp    12e4 <main+0x154>
test   %r13d,%r13d
jle    11cd <main+0x3d>
mov    $0x1,%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1040 <write@plt>
test   %eax,%eax
jns    11df <main+0x4f>
jmp    12ed <main+0x15d>
lea    0xe8b(%rip),%rdi
mov    %r13d,%r15d
test   %r13d,%r13d
jg     11d7 <main+0x47>
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    11df <main+0x4f>
jmp    12f6 <main+0x166>
xor    %r15d,%r15d
lea    0xe3c(%rip),%rdi
test   %r13d,%r13d
je     11d7 <main+0x47>
mov    %r13d,%edi
call   1060 <close@plt>
xor    %r13d,%r13d
test   %eax,%eax
jns    11df <main+0x4f>
lea    0xe02(%rip),%rdi
jmp    12fd <main+0x16d>
lea    0xdb0(%rip),%rdi
jmp    12fd <main+0x16d>
lea    0xdb6(%rip),%rdi
jmp    12fd <main+0x16d>
lea    0xdcc(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%eax
mov    %rax,0x8(%rsp)
mov    0x8(%rsp),%rax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [105 x i8] c"\0APlease choose operation:\0A\091: read\0A\092: write\0A\093: open\0A\094: close\0A\095: exit this program\0AYour choice(1~5): \00"
@.str.1 = [9 x i8] c"%[^\0A]%*c\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [18 x i8] c"choice: %s -> %d\0A\00"
@.str.4 = [47 x i8] c"Device not open. Please first open the device\0A\00"
@.str.5 = [15 x i8] c"Fail to read.\0A\00"
@errno = global i32 0
@.str.6 = [16 x i8] c"Fail to write.\0A\00"
@.str.7 = [22 x i8] c"Device already open.\0A\00"
@.str.8 = [15 x i8] c"/dev/mydevname\00"
@O_RDWR = global i32 0
@.str.9 = [27 x i8] c"Fail to open the device...\00"
@.str.10 = [24 x i8] c"Device already closed.\0A\00"
@.str.11 = [23 x i8] c"Device fail to close.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca [256 x i8]
store i32 0, i32* %1
store i32 0, i32* %3
store i32 0, i32* %4
store i64 1, i64* %5
br label %7
7: ; preds = %80, %0
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 5
br i1 %9, label %10, label %81
10: ; preds = %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([105 x i8], [105 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = call i32 @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%17 = load i32, i32* %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %16, i32 %17)
%19 = load i32, i32* %4
switch i32 %19, label %79 [
i32 1, label %20
i32 2, label %35
i32 3, label %50
i32 4, label %64
i32 5, label %78
]
20: ; preds = %10
%21 = load i32, i32* %3
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
br label %80
25: ; preds = %20
%26 = load i32, i32* %3
%27 = load i64, i64* %5
%28 = call i32 @read(i32 %26, i32* null, i64 %27)
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%33 = load i32, i32* @errno
store i32 %33, i32* %1
br label %82
34: ; preds = %25
br label %80
35: ; preds = %10
%36 = load i32, i32* %3
%37 = icmp sle i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
br label %80
40: ; preds = %35
%41 = load i32, i32* %3
%42 = load i64, i64* %5
%43 = call i32 @write(i32 %41, i32* null, i64 %42)
store i32 %43, i32* %2
%44 = load i32, i32* %2
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* @errno
store i32 %48, i32* %1
br label %82
49: ; preds = %40
br label %80
50: ; preds = %10
%51 = load i32, i32* %3
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %80
55: ; preds = %50
%56 = load i32, i32* @O_RDWR
%57 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i32 %56)
store i32 %57, i32* %3
%58 = load i32, i32* %3
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
%62 = load i32, i32* @errno
store i32 %62, i32* %1
br label %82
63: ; preds = %55
br label %80
64: ; preds = %10
%65 = load i32, i32* %3
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %80
69: ; preds = %64
%70 = load i32, i32* %3
%71 = call i32 @close(i32 %70)
store i32 %71, i32* %2
%72 = load i32, i32* %2
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %69
%75 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0))
%76 = load i32, i32* @errno
store i32 %76, i32* %1
br label %82
77: ; preds = %69
store i32 0, i32* %3
br label %80
78: ; preds = %10
store i32 0, i32* %1
br label %82
79: ; preds = %10
br label %80
80: ; preds = %79, %77, %67, %63, %53, %49, %38, %34, %23
br label %7
81: ; preds = %7
store i32 0, i32* %1
br label %82
82: ; preds = %81, %78, %74, %60, %46, %31
%83 = load i32, i32* %1
ret i32 %83
}

LLVM IR (-O2):
@.str = [105 x i8] c"\0APlease choose operation:\0A\091: read\0A\092: write\0A\093: open\0A\094: close\0A\095: exit this program\0AYour choice(1~5): \00"
@.str.1 = [9 x i8] c"%[^\0A]%*c\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [18 x i8] c"choice: %s -> %d\0A\00"
@.str.5 = [15 x i8] c"Fail to read.\0A\00"
@errno = global i32 0
@.str.6 = [16 x i8] c"Fail to write.\0A\00"
@.str.8 = [15 x i8] c"/dev/mydevname\00"
@O_RDWR = global i32 0
@.str.9 = [27 x i8] c"Fail to open the device...\00"
@.str.11 = [23 x i8] c"Device fail to close.\0A\00"
@str = [23 x i8] c"Device already closed.\00"
@str.12 = [21 x i8] c"Device already open.\00"
@str.14 = [46 x i8] c"Device not open. Please first open the device\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
br label %5
5: ; preds = %0, %41
%6 = phi i32 [ 0, %0 ], [ %43, %41 ]
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([105 x i8], [105 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%9 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%10 = load i32, i32* %1
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 %10)
%12 = load i32, i32* %1
switch i32 %12, label %41 [
i32 1, label %13
i32 2, label %18
i32 3, label %23
i32 4, label %29
i32 5, label %49
]
13: ; preds = %5
%14 = icmp slt i32 %6, 1
br i1 %14, label %34, label %15
15: ; preds = %13
%16 = call i32 @read(i32 %6, i32* null, i64 1)
%17 = icmp slt i32 %16, 0
br i1 %17, label %45, label %38
18: ; preds = %5
%19 = icmp slt i32 %6, 1
br i1 %19, label %34, label %20
20: ; preds = %18
%21 = call i32 @write(i32 %6, i32* null, i64 1)
%22 = icmp slt i32 %21, 0
br i1 %22, label %45, label %38
23: ; preds = %5
%24 = icmp sgt i32 %6, 0
br i1 %24, label %34, label %25
25: ; preds = %23
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %45, label %38
29: ; preds = %5
%30 = icmp eq i32 %6, 0
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = call i32 @close(i32 %6)
%33 = icmp slt i32 %32, 0
br i1 %33, label %45, label %38
34: ; preds = %29, %23, %18, %13
%35 = phi i8* [ getelementptr inbounds ([46 x i8], [46 x i8]* @str.14, i64 0, i64 0), %13 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @str.14, i64 0, i64 0), %18 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.12, i64 0, i64 0), %23 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0), %29 ]
%36 = phi i32 [ 0, %13 ], [ 0, %18 ], [ %6, %23 ], [ 0, %29 ]
%37 = call i32 @puts(i8* dereferenceable(1) %35)
br label %38
38: ; preds = %34, %15, %20, %25, %31
%39 = phi i32 [ 0, %31 ], [ %6, %15 ], [ %6, %20 ], [ %27, %25 ], [ %36, %34 ]
%40 = load i32, i32* %1
br label %41
41: ; preds = %38, %5
%42 = phi i32 [ %40, %38 ], [ %12, %5 ]
%43 = phi i32 [ %39, %38 ], [ %6, %5 ]
%44 = icmp eq i32 %42, 5
br i1 %44, label %49, label %5
45: ; preds = %31, %25, %20, %15
%46 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), %15 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), %20 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), %25 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0), %31 ]
%47 = call i32 @perror(i8* %46)
%48 = load i32, i32* @errno
br label %49
49: ; preds = %41, %5, %45
%50 = phi i32 [ %48, %45 ], [ 0, %5 ], [ 0, %41 ]
ret i32 %50
}