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

/* Variables and functions */
scalar_t__ CheckRoot();
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(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;
  ssize_t written, readd;
  char ch, write_buf[100], read_buf[100];
  if (CheckRoot()) {
    fd = open("/dev/fakedevice", O_RDWR);
    if (fd == -1) {
      printf("Failed to open device %s. Device is busy or there is not an "
             "appropriate kernel module loaded?\n",
             "/dev/fakedevice");
      exit(-1);
    }
    printf("r = read from device\nw = write to device\nenter command: ");
    scanf("%c", &ch);
    switch (ch) {
    case 'w':
      printf("enter data: ");
      scanf(" %[^\n]", write_buf);
      written = write(fd, write_buf, sizeof(write_buf));
      printf("bytes written: %zu\n", written);
      break;
    case 'r':
      readd = read(fd, read_buf, sizeof(read_buf));
      printf("device data: %s\n", read_buf);
      printf("bytes read: %lu\n", sizeof(read_buf));
      break;
    default:
      printf("command not recognised\n");
      break;
    }
    close(fd);
  } else {
    printf("You have to be root to run this program!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <CheckRoot@plt>
cmp    $0x0,%rax
je     12d5 <main+0x155>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0xe4d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11e4 <main+0x64>
lea    0xe44(%rip),%rdi
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xe84(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeaf(%rip),%rdi
lea    -0x15(%rbp),%rsi
call   1070 <scanf@plt>
movsbl -0x15(%rbp),%eax
mov    %eax,-0xf4(%rbp)
sub    $0x72,%eax
je     1276 <main+0xf6>
jmp    121a <main+0x9a>
mov    -0xf4(%rbp),%eax
sub    $0x77,%eax
jne    12ba <main+0x13a>
jmp    122e <main+0xae>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1070 <scanf@plt>
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c8 <main+0x148>
mov    -0xc(%rbp),%edi
lea    -0xf0(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
lea    -0xf0(%rbp),%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe3b(%rip),%rdi
mov    $0x64,%esi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c8 <main+0x148>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    12e3 <main+0x163>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
xor    %eax,%eax
call   10a0 <CheckRoot@plt>
test   %rax,%rax
je     123c <main+0xac>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12a1 <main+0x111>
mov    %eax,%ebx
lea    0xea1(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xecc(%rip),%rdi
lea    0xf(%rsp),%rsi
call   1080 <scanf@plt>
movsbl 0xf(%rsp),%eax
cmp    $0x72,%eax
je     124a <main+0xba>
cmp    $0x77,%eax
jne    1284 <main+0xf4>
lea    0xeab(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xeaa(%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>
lea    0xe8e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1290 <main+0x100>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
jmp    1297 <main+0x107>
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
lea    0xe6e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe6e(%rip),%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1290 <main+0x100>
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd68(%rip),%rdi
lea    0xd51(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/fakedevice\00"
@O_RDWR = global i32 0
@.str.1 = [95 x i8] c"Failed to open device %s. Device is busy or there is not an appropriate kernel module loaded?\0A\00"
@.str.2 = [57 x i8] c"r = read from device\0Aw = write to device\0Aenter command: \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [13 x i8] c"enter data: \00"
@.str.5 = [7 x i8] c" %[^\0A]\00"
@.str.6 = [20 x i8] c"bytes written: %zu\0A\00"
@.str.7 = [17 x i8] c"device data: %s\0A\00"
@.str.8 = [17 x i8] c"bytes read: %lu\0A\00"
@.str.9 = [24 x i8] c"command not recognised\0A\00"
@.str.10 = [42 x i8] c"You have to be root to run this program!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca [100 x i8]
%8 = alloca [100 x i8]
store i32 0, i32* %1
%9 = call i64 (...) @CheckRoot()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %45
11: ; preds = %0
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %11
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%22 = load i8, i8* %6
%23 = sext i8 %22 to i32
switch i32 %23, label %40 [
i32 119, label %24
i32 114, label %33
]
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%27 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %3
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%30 = call i32 @write(i32 %28, i8* %29, i32 100)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %31)
br label %42
33: ; preds = %19
%34 = load i32, i32* %3
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 100)
store i32 %36, i32* %5
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i64 100)
br label %42
40: ; preds = %19
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0))
br label %42
42: ; preds = %40, %33, %24
%43 = load i32, i32* %3
%44 = call i32 @close(i32 %43)
br label %47
45: ; preds = %0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.10, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/fakedevice\00"
@O_RDWR = global i32 0
@.str.1 = [95 x i8] c"Failed to open device %s. Device is busy or there is not an appropriate kernel module loaded?\0A\00"
@.str.2 = [57 x i8] c"r = read from device\0Aw = write to device\0Aenter command: \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [13 x i8] c"enter data: \00"
@.str.5 = [7 x i8] c" %[^\0A]\00"
@.str.6 = [20 x i8] c"bytes written: %zu\0A\00"
@.str.7 = [17 x i8] c"device data: %s\0A\00"
@.str.8 = [17 x i8] c"bytes read: %lu\0A\00"
@str = [41 x i8] c"You have to be root to run this program!\00"
@str.11 = [23 x i8] c"command not recognised\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 = tail call i64 (...) @CheckRoot()
%7 = icmp eq i64 %6, 0
br i1 %7, label %33, label %8
8: ; preds = %0
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([95 x i8], [95 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%18 = load i8, i8* %1
%19 = sext i8 %18 to i32
switch i32 %19, label %29 [
i32 119, label %20
i32 114, label %25
]
20: ; preds = %15
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%23 = call i32 @write(i32 %10, i8* %4, i32 100)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %23)
br label %31
25: ; preds = %15
%26 = call i32 @read(i32 %10, i8* %5, i32 100)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %5)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i64 100)
br label %31
29: ; preds = %15
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
br label %31
31: ; preds = %29, %25, %20
%32 = call i32 @close(i32 %10)
br label %35
33: ; preds = %0
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %35
35: ; preds = %33, %31
ret i32 0
}