#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 atoi(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char **, int);
scalar_t__ strcmp(char *, char *);
void read_driver(char *string) {
  int LENGTH = -1, num = -1;
  LENGTH = strcmp(string, "") == 0 ? 256 : atoi(string);
  if (LENGTH > 256 || LENGTH < 0) {
    printf("SECURITY VIOLATION...ATTEMPT TO ACCESS ILLEGAL MEMORY LOCATIONS\n");
    return;
  }
  char buf[256];
  memset(buf, 0, 256);
  int fp = open("/dev/gmem", 0);
  num = read(fp, &buf, LENGTH);
  if (num == 0) {
    printf("READ FAILED\n");
    return;
  } else if (num < LENGTH && LENGTH != 256)
    printf("Sorry only %d bytes could be read\nPERHAPS YOU ARE TRYING TO READ "
           "AN UNFILLED POSITION OR THE DEVICE IS BUSY",
           num);
  printf("Driver contents: %s\n", buf);
}


x86 assembly (clang, -O0):
<read_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xecf(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11a7 <read_driver+0x47>
mov    $0x100,%eax
mov    %eax,-0x118(%rbp)
jmp    11b6 <read_driver+0x56>
mov    -0x8(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x118(%rbp)
mov    -0x118(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x100,-0xc(%rbp)
jg     11d6 <read_driver+0x76>
cmpl   $0x0,-0xc(%rbp)
jge    11e9 <read_driver+0x89>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1289 <read_driver+0x129>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    0xe3e(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1245 <read_driver+0xe5>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1289 <read_driver+0x129>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    126f <read_driver+0x10f>
cmpl   $0x100,-0xc(%rbp)
je     126f <read_driver+0x10f>
mov    -0x10(%rbp),%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1274 <read_driver+0x114>
lea    -0x110(%rbp),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_driver>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%rbx
lea    0xf06(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11b5 <read_driver+0x45>
mov    %rbx,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
cmp    $0x101,%eax
jb     11ba <read_driver+0x4a>
lea    0xef1(%rip),%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>
mov    $0x100,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xe2d(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1219 <read_driver+0xa9>
cmp    $0x100,%ebx
je     1206 <read_driver+0x96>
cmp    %ebx,%eax
jge    1206 <read_driver+0x96>
lea    0xe0d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe69(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1225 <read_driver+0xb5>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [65 x i8] c"SECURITY VIOLATION...ATTEMPT TO ACCESS ILLEGAL MEMORY LOCATIONS\0A\00"
@.str.2 = [10 x i8] c"/dev/gmem\00"
@.str.3 = [13 x i8] c"READ FAILED\0A\00"
@.str.4 = [108 x i8] c"Sorry only %d bytes could be read\0APERHAPS YOU ARE TRYING TO READ AN UNFILLED POSITION OR THE DEVICE IS BUSY\00"
@.str.5 = [21 x i8] c"Driver contents: %s\0A\00"
define void @read_driver(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
store i32 -1, i32* %4
%7 = load i8*, i8** %2
%8 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
br label %14
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 @atoi(i8* %12)
br label %14
14: ; preds = %11, %10
%15 = phi i32 [ 256, %10 ], [ %13, %11 ]
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp sgt i32 %16, 256
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18, %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
br label %49
23: ; preds = %18
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%25 = call i32 @memset(i8* %24, i32 0, i32 256)
%26 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 0)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = bitcast [256 x i8]* %5 to i8**
%29 = load i32, i32* %3
%30 = call i32 @read(i32 %27, i8** %28, i32 %29)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %49
35: ; preds = %23
%36 = load i32, i32* %4
%37 = load i32, i32* %3
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i32, i32* %3
%41 = icmp ne i32 %40, 256
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %4
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([108 x i8], [108 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %42, %39, %35
br label %46
46: ; preds = %45
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %46, %33, %21
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.2 = [10 x i8] c"/dev/gmem\00"
@.str.4 = [108 x i8] c"Sorry only %d bytes could be read\0APERHAPS YOU ARE TRYING TO READ AN UNFILLED POSITION OR THE DEVICE IS BUSY\00"
@.str.5 = [21 x i8] c"Driver contents: %s\0A\00"
@str = [12 x i8] c"READ FAILED\00"
@str.6 = [64 x i8] c"SECURITY VIOLATION...ATTEMPT TO ACCESS ILLEGAL MEMORY LOCATIONS\00"
define void @read_driver(i8* %0) {
%2 = alloca [256 x i8]
%3 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %1
%6 = tail call i32 @atoi(i8* %0)
%7 = icmp ugt i32 %6, 256
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str.6, i64 0, i64 0))
br label %29
10: ; preds = %1, %5
%11 = phi i32 [ %6, %5 ], [ 256, %1 ]
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 256)
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 0)
%15 = bitcast [256 x i8]* %2 to i8**
%16 = call i32 @read(i32 %14, i8** %15, i32 %11)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %28
20: ; preds = %10
%21 = icmp slt i32 %16, %11
%22 = icmp ne i32 %11, 256
%23 = and i1 %22, %21
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([108 x i8], [108 x i8]* @.str.4, i64 0, i64 0), i32 %16)
br label %26
26: ; preds = %20, %24
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %12)
br label %28
28: ; preds = %26, %18
br label %29
29: ; preds = %28, %8
ret void
}