#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*/ 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*/ 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, len;
  char ch, write_buf[100], read_buf[100];
  if (argc != 3 && argc != 2) {
    printf("Usage: %s <read|write> [data]\n", argv[0]);
    exit(0);
  }
  fd = open("/dev/interface", O_RDWR);
  if (fd == -1) {
    printf("Error in opening file\n");
    exit(-1);
  }
  if (argc == 3)
    len = strlen(argv[2]);
  else
    len = 0;
  if (!strcmp("read", argv[1])) {
    read(fd, read_buf, sizeof(read_buf));
    printf("tryit: read device data: %s\n", read_buf);
  } else if (!strcmp("write", argv[1])) {
    strncpy(write_buf, argv[2], len);
    write(fd, write_buf, len);
    printf("tryit: wrote device data: %s\n", write_buf);
  }
  close(fd);
}


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)
cmpl   $0x3,-0x8(%rbp)
je     11d9 <main+0x49>
cmpl   $0x2,-0x8(%rbp)
je     11d9 <main+0x49>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1213 <main+0x83>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jne    1232 <main+0xa2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
jmp    1239 <main+0xa9>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdfd(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1284 <main+0xf4>
mov    -0x18(%rbp),%edi
lea    -0x100(%rbp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
lea    -0x100(%rbp),%rsi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12e4 <main+0x154>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdd4(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    12df <main+0x14f>
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>
lea    -0x90(%rbp),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12e4 <main+0x154>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%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,%r14
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    127a <main+0xda>
mov    %edi,%ebp
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1292 <main+0xf2>
mov    %eax,%ebx
xor    %r15d,%r15d
cmp    $0x3,%ebp
jne    11f1 <main+0x51>
mov    0x10(%r14),%rdi
call   1060 <strlen@plt>
mov    %eax,%r15d
mov    0x8(%r14),%rbp
lea    0xe32(%rip),%rdi
mov    %rbp,%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1243 <main+0xa3>
lea    0xe41(%rip),%rdi
mov    %rbp,%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
jne    1266 <main+0xc6>
mov    0x10(%r14),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1050 <write@plt>
lea    0xe15(%rip),%rdi
jmp    125c <main+0xbc>
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x64,%edx
call   1090 <read@plt>
lea    0xdd7(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd7c(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xddb(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [31 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 = [23 x i8] c"Error in opening file\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [29 x i8] c"tryit: read device data: %s\0A\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [30 x i8] c"tryit: wrote device data: %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
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
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 ([31 x i8], [31 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 %31
28: ; preds = %23
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %23
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, 3
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32 @strlen(i8* %37)
store i32 %38, i32* %8
br label %40
39: ; preds = %31
store i32 0, i32* %8
br label %40
40: ; preds = %39, %34
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %52, label %46
46: ; preds = %40
%47 = load i32, i32* %7
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 100)
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %72
52: ; preds = %40
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @strcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %71, label %58
58: ; preds = %52
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 2
%62 = load i8*, i8** %61
%63 = load i32, i32* %8
%64 = call i32 @strncpy(i8* %59, i8* %62, i32 %63)
%65 = load i32, i32* %7
%66 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%67 = load i32, i32* %8
%68 = call i32 @write(i32 %65, i8* %66, i32 %67)
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %58, %52
br label %72
72: ; preds = %71, %46
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [31 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.3 = [5 x i8] c"read\00"
@.str.4 = [29 x i8] c"tryit: read device data: %s\0A\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [30 x i8] c"tryit: wrote device data: %s\0A\00"
@str = [22 x i8] c"Error in opening file\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 ([31 x i8], [31 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 %20
17: ; preds = %13
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
%21 = icmp eq i32 %0, 3
br i1 %21, label %22, label %26
22: ; preds = %20
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = tail call i32 @strlen(i8* %24)
br label %26
26: ; preds = %20, %22
%27 = phi i32 [ %25, %22 ], [ 0, %20 ]
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = tail call i32 @strcmp(i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* dereferenceable(1) %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @read(i32 %15, i8* %6, i32 100)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %44
35: ; preds = %26
%36 = tail call i32 @strcmp(i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* dereferenceable(1) %29)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %35
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = call i32 @strncpy(i8* %5, i8* %40, i32 %27)
%42 = call i32 @write(i32 %15, i8* %5, i32 %27)
%43 = 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 %44
44: ; preds = %35, %38, %32
%45 = call i32 @close(i32 %15)
ret i32 0
}