#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*/ rd_buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int i, fd = 1, len;
  char ch, wr_buf[100], rd_buf[100];
  if (argc != 2 && argc != 3) {
    printf("Usage %s <read|write> [data]\n", argv[0]);
    exit(0);
  }
  fd = open("/dev/interface", O_RDWR);
  if (fd == -1) {
    printf("Error opening file :(\nError: %d\n", errno);
    exit(-1);
  }
  if (argc == 2)
    len = 0;
  else if (argc == 3)
    len = strlen(argv[2]);
  if (!strcmp("read", argv[1])) {
    read(fd, rd_buf, sizeof(rd_buf));
    printf("Read device data: %s\n", rd_buf);
  } else if (!strcmp("write", argv[1])) {
    strncpy(wr_buf, argv[2], len);
    write(fd, wr_buf, len);
    wr_buf[len] = '\0';
    printf("Wrote to device: %s\n", wr_buf);
  }
  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)
movl   $0x1,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e0 <main+0x50>
cmpl   $0x3,-0x8(%rbp)
je     11e0 <main+0x50>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0xe2e(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1223 <main+0x93>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    1239 <main+0xa9>
movl   $0x0,-0x1c(%rbp)
jmp    1258 <main+0xc8>
cmpl   $0x3,-0x8(%rbp)
jne    1253 <main+0xc3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
jmp    1258 <main+0xc8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xde7(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    12a3 <main+0x113>
mov    -0x18(%rbp),%edi
lea    -0x100(%rbp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
lea    -0x100(%rbp),%rsi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    130f <main+0x17f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdb7(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    130a <main+0x17a>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rsi
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    130f <main+0x17f>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    1275 <main+0xe5>
mov    %edi,%ebp
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     128d <main+0xfd>
mov    %eax,%r15d
xor    %r14d,%r14d
cmp    $0x3,%ebp
jne    11e2 <main+0x52>
mov    0x10(%rbx),%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    0x8(%rbx),%rbp
lea    0xe61(%rip),%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     123c <main+0xac>
lea    0xe69(%rip),%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
jne    1260 <main+0xd0>
mov    0x10(%rbx),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <write@plt>
movslq %r14d,%rax
movb   $0x0,(%rsp,%rax,1)
lea    0xe35(%rip),%rdi
jmp    1256 <main+0xc6>
mov    %rsp,%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
lea    0xdfd(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd81(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
lea    0xd90(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"Usage %s <read|write> [data]\0A\00"
@.str.1 = [15 x i8] c"/dev/interface\00"
@O_RDWR = global i32 0
@.str.2 = [33 x i8] c"Error opening file :(\0AError: %d\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [22 x i8] c"Read device data: %s\0A\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [21 x i8] c"Wrote to device: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca [100 x i8]
%11 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %14, %2
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i32, i32* @errno
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %23
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 2
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 0, i32* %8
br label %45
36: ; preds = %32
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 3
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32 @strlen(i8* %42)
store i32 %43, i32* %8
br label %44
44: ; preds = %39, %36
br label %45
45: ; preds = %44, %35
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %45
%52 = load i32, i32* %7
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 100)
%55 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %55)
br label %80
57: ; preds = %45
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = call i32 @strcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %79, label %63
63: ; preds = %57
%64 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 2
%67 = load i8*, i8** %66
%68 = load i32, i32* %8
%69 = call i32 @strncpy(i8* %64, i8* %67, i32 %68)
%70 = load i32, i32* %7
%71 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%72 = load i32, i32* %8
%73 = call i32 @write(i32 %70, i8* %71, i32 %72)
%74 = load i32, i32* %8
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 %75
store i8 0, i8* %76
%77 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %77)
br label %79
79: ; preds = %63, %57
br label %80
80: ; preds = %79, %51
%81 = load i32, i32* %7
%82 = call i32 @close(i32 %81)
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"Usage %s <read|write> [data]\0A\00"
@.str.1 = [15 x i8] c"/dev/interface\00"
@O_RDWR = global i32 0
@.str.2 = [33 x i8] c"Error opening file :(\0AError: %d\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [22 x i8] c"Read device data: %s\0A\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [21 x i8] c"Wrote to device: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = and i32 %0, -2
%8 = icmp eq i32 %7, 2
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %13
%22 = icmp eq i32 %0, 3
br i1 %22, label %23, label %27
23: ; preds = %21
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = tail call i32 @strlen(i8* %25)
br label %27
27: ; preds = %21, %23
%28 = phi i32 [ %26, %23 ], [ 0, %21 ]
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = tail call i32 @strcmp(i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* dereferenceable(1) %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @read(i32 %15, i8* %6, i32 100)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %47
36: ; preds = %27
%37 = tail call i32 @strcmp(i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* dereferenceable(1) %30)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = getelementptr inbounds i8*, i8** %1, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @strncpy(i8* %5, i8* %41, i32 %28)
%43 = call i32 @write(i32 %15, i8* %5, i32 %28)
%44 = sext i32 %28 to i64
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %44
store i8 0, i8* %45
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %5)
br label %47
47: ; preds = %36, %39, %33
%48 = call i32 @close(i32 %15)
ret i32 0
}