#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 /*<<< 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 argc, const char *argv[]) {
  int fd;
  char ch, write_buf[100], read_buf[100];
  fd = open("/dev/Gil_device", O_RDWR);
  if (fd == -1) {
    printf("\nfile %s either does not exist or has been locked by another "
           "process \n",
           "/dev/Gil_device");
    exit(-1);
  }
  printf("\nr = read from device\nw = write into device\n");
  scanf("%c", &ch);
  switch (ch) {
  case 'w':
    printf("\nEnter data: \n");
    scanf("%s", write_buf);
    write(fd, write_buf, sizeof(write_buf));
  case 'r':
    read(fd, read_buf, sizeof(read_buf));
    printf("device %s\n", read_buf);
    break;
  default:
    printf("\nCommand not recognized\n");
    break;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ca <main+0x5a>
lea    0xe5e(%rip),%rdi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe86(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea5(%rip),%rdi
lea    -0x15(%rbp),%rsi
call   1070 <scanf@plt>
movsbl -0x15(%rbp),%eax
mov    %eax,-0xf4(%rbp)
sub    $0x72,%eax
je     1243 <main+0xd3>
jmp    1200 <main+0x90>
mov    -0xf4(%rbp),%eax
sub    $0x77,%eax
jne    1271 <main+0x101>
jmp    1214 <main+0xa4>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xe69(%rip),%rdi
call   1070 <scanf@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0xf0(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
lea    -0xf0(%rbp),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127f <main+0x10f>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xe8,%rsp
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0xe66(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     124f <main+0xcf>
mov    %eax,%ebx
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9a(%rip),%rdi
lea    0xf(%rsp),%rsi
call   1080 <scanf@plt>
movsbl 0xf(%rsp),%eax
cmp    $0x72,%eax
je     1208 <main+0x88>
cmp    $0x77,%eax
jne    122f <main+0xaf>
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe71(%rip),%rdi
lea    0x80(%rsp),%r14
mov    %r14,%rsi
call   1080 <scanf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1040 <write@plt>
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
lea    0xe3a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    123b <main+0xbb>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdba(%rip),%rdi
lea    0xda3(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/Gil_device\00"
@O_RDWR = global i32 0
@.str.1 = [71 x i8] c"\0Afile %s either does not exist or has been locked by another process \0A\00"
@.str.2 = [45 x i8] c"\0Ar = read from device\0Aw = write into device\0A\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [15 x i8] c"\0AEnter data: \0A\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [11 x i8] c"device %s\0A\00"
@.str.7 = [25 x i8] c"\0ACommand not recognized\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%8 = alloca [100 x i8]
%9 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%20 = load i8, i8* %7
%21 = sext i8 %20 to i32
switch i32 %21, label %35 [
i32 119, label %22
i32 114, label %29
]
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %6
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%28 = call i32 @write(i32 %26, i8* %27, i32 100)
br label %29
29: ; preds = %17, %22
%30 = load i32, i32* %6
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 100)
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %33)
br label %37
35: ; preds = %17
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/Gil_device\00"
@O_RDWR = global i32 0
@.str.1 = [71 x i8] c"\0Afile %s either does not exist or has been locked by another process \0A\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [11 x i8] c"device %s\0A\00"
@str = [44 x i8] c"\0Ar = read from device\0Aw = write into device\00"
@str.8 = [14 x i8] c"\0AEnter data: \00"
@str.9 = [24 x i8] c"\0ACommand not recognized\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
switch i32 %18, label %26 [
i32 119, label %19
i32 114, label %23
]
19: ; preds = %14
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%22 = call i32 @write(i32 %9, i8* %6, i32 100)
br label %23
23: ; preds = %14, %19
%24 = call i32 @read(i32 %9, i8* %7, i32 100)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %7)
br label %28
26: ; preds = %14
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.9, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = call i32 @close(i32 %9)
ret i32 0
}