#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 */
typedef int /*<<< orphan*/ write_buf;
typedef int /*<<< orphan*/ read_buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int i, fd;
  char ch, write_buf[100], read_buf[100];
  fd = open("/dev/mychar0", O_RDWR);
  if (fd == -1) {
    printf("Error in opening file \n");
    return 1;
  }
  printf("Press r to read from device or w to write the device ");
  scanf("%c", &ch);
  switch (ch) {
  case 'w':
    printf(" Enter the data to be written into device");
    scanf(" %[^\n]", write_buf);
    write(fd, write_buf, sizeof(write_buf));
    break;
  case 'r':
    read(fd, read_buf, sizeof(read_buf));
    printf("The data in the device is %s\n", read_buf);
    break;
  default:
    printf("Wrong choice \n");
    break;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    0xe7e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ae <main+0x4e>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1270 <main+0x110>
lea    0xe70(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe98(%rip),%rdi
lea    -0xd(%rbp),%rsi
call   1070 <scanf@plt>
movsbl -0xd(%rbp),%eax
mov    %eax,-0xf4(%rbp)
sub    $0x72,%eax
je     122c <main+0xcc>
jmp    11e4 <main+0x84>
mov    -0xf4(%rbp),%eax
sub    $0x77,%eax
jne    125a <main+0xfa>
jmp    11f8 <main+0x98>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xe77(%rip),%rdi
call   1070 <scanf@plt>
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1030 <write@plt>
jmp    1268 <main+0x108>
mov    -0xc(%rbp),%edi
lea    -0xf0(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
lea    -0xf0(%rbp),%rsi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1268 <main+0x108>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     11f4 <main+0x84>
mov    %eax,%ebx
lea    0xe73(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe9b(%rip),%rdi
lea    0xf(%rsp),%rsi
call   1080 <scanf@plt>
movsbl 0xf(%rsp),%eax
cmp    $0x72,%eax
je     1207 <main+0x97>
cmp    $0x77,%eax
jne    122e <main+0xbe>
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe9a(%rip),%rdi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1080 <scanf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1040 <write@plt>
jmp    123a <main+0xca>
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1243 <main+0xd3>
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
lea    0xe55(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    123a <main+0xca>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/mychar0\00"
@O_RDWR = global i32 0
@.str.1 = [24 x i8] c"Error in opening file \0A\00"
@.str.2 = [54 x i8] c"Press r to read from device or w to write the device \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [42 x i8] c" Enter the data to be written into device\00"
@.str.5 = [7 x i8] c" %[^\0A]\00"
@.str.6 = [30 x i8] c"The data in the device is %s\0A\00"
@.str.7 = [15 x i8] c"Wrong choice \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca [100 x i8]
%6 = alloca [100 x i8]
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %36
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%16 = load i8, i8* %4
%17 = sext i8 %16 to i32
switch i32 %17, label %31 [
i32 119, label %18
i32 114, label %25
]
18: ; preds = %13
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%21 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %3
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%24 = call i32 @write(i32 %22, i8* %23, i32 100)
br label %33
25: ; preds = %13
%26 = load i32, i32* %3
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 100)
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %29)
br label %33
31: ; preds = %13
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %33
33: ; preds = %31, %25, %18
%34 = load i32, i32* %3
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %11
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/mychar0\00"
@O_RDWR = global i32 0
@.str.2 = [54 x i8] c"Press r to read from device or w to write the device \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [42 x i8] c" Enter the data to be written into device\00"
@.str.5 = [7 x i8] c" %[^\0A]\00"
@.str.6 = [30 x i8] c"The data in the device is %s\0A\00"
@str = [14 x i8] c"Wrong choice \00"
@str.8 = [23 x i8] c"Error in opening file \00"
define i32 @main() {
%1 = alloca i8
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
br label %27
11: ; preds = %0
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%14 = load i8, i8* %1
%15 = sext i8 %14 to i32
switch i32 %15, label %23 [
i32 119, label %16
i32 114, label %20
]
16: ; preds = %11
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%19 = call i32 @write(i32 %7, i8* %4, i32 100)
br label %25
20: ; preds = %11
%21 = call i32 @read(i32 %7, i8* %5, i32 100)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %5)
br label %25
23: ; preds = %11
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %20, %16
%26 = call i32 @close(i32 %7)
br label %27
27: ; preds = %25, %9
%28 = phi i32 [ 1, %9 ], [ 0, %25 ]
ret i32 %28
}