#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, unsigned long *, int);
unsigned long strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ write(int, unsigned long *, int);
int main(int argc, char **argv) {
  int fd;
  unsigned long val[2];
  if (argc < 3 || argc > 4) {
    printf("Usage : %s r addr \n", argv[0]);
    printf("Usage : %s w addr value\n", argv[0]);
    return -1;
  }
  fd = open("/dev/regopt", O_RDWR);
  if (fd < 0) {
    printf("can't open!\n");
    return -1;
  }
  val[0] = strtol(argv[2], NULL, 16);
  if (*(argv[1]) == 'w') {
    val[1] = strtol(argv[3], NULL, 16);
    write(fd, val, 8);
  } else if (*(argv[1]) == 'r') {
    read(fd, val, 8);
    printf("add:0x%x = 0x%x\n", val[0], val[1]);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     118a <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    11c0 <main+0x60>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <main+0x149>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    0xe5d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fc <main+0x9c>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <main+0x149>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    125a <main+0xfa>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
jmp    129a <main+0x13a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    1295 <main+0x135>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129a <main+0x13a>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
add    $0xfffffffb,%edi
cmp    $0xfffffffd,%edi
ja     11ad <main+0x3d>
mov    (%rbx),%rsi
lea    0xe75(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%rsi
lea    0xe78(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
jmp    1252 <main+0xe2>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1210 <main+0xa0>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %rax,(%rsp)
mov    0x8(%rbx),%rax
mov    (%rax),%al
cmp    $0x72,%al
je     1223 <main+0xb3>
cmp    $0x77,%al
jne    1249 <main+0xd9>
mov    0x18(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %rax,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1040 <write@plt>
jmp    1249 <main+0xd9>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1252 <main+0xe2>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1070 <read@plt>
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
lea    0xdf7(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Usage : %s r addr \0A\00"
@.str.1 = [25 x i8] c"Usage : %s w addr value\0A\00"
@.str.2 = [12 x i8] c"/dev/regopt\00"
@O_RDWR = global i32 0
@.str.3 = [13 x i8] c"can't open!\0A\00"
@.str.4 = [17 x i8] c"add:0x%x = 0x%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i64]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 3
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 4
br i1 %12, label %13, label %22
13: ; preds = %10, %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
%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 ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %70
22: ; preds = %10
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %70
29: ; preds = %22
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i64 @strtol(i8* %32, i32* null, i32 16)
%34 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 0
store i64 %33, i64* %34
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 119
br i1 %40, label %41, label %50
41: ; preds = %29
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 3
%44 = load i8*, i8** %43
%45 = call i64 @strtol(i8* %44, i32* null, i32 16)
%46 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 1
store i64 %45, i64* %46
%47 = load i32, i32* %6
%48 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 0
%49 = call i32 @write(i32 %47, i64* %48, i32 8)
br label %67
50: ; preds = %29
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 114
br i1 %56, label %57, label %66
57: ; preds = %50
%58 = load i32, i32* %6
%59 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 0
%60 = call i32 @read(i32 %58, i64* %59, i32 8)
%61 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 0
%62 = load i64, i64* %61
%63 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 1
%64 = load i64, i64* %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 %62, i64 %64)
br label %66
66: ; preds = %57, %50
br label %67
67: ; preds = %66, %41
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
store i32 0, i32* %3
br label %70
70: ; preds = %67, %27, %13
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str = [20 x i8] c"Usage : %s r addr \0A\00"
@.str.1 = [25 x i8] c"Usage : %s w addr value\0A\00"
@.str.2 = [12 x i8] c"/dev/regopt\00"
@O_RDWR = global i32 0
@.str.4 = [17 x i8] c"add:0x%x = 0x%x\0A\00"
@str = [12 x i8] c"can't open!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i64]
%4 = bitcast [2 x i64]* %3 to i8*
%5 = add i32 %0, -5
%6 = icmp ult i32 %5, -2
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %40
12: ; preds = %2
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %40
18: ; preds = %12
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i64 @strtol(i8* nocapture %20, i32* null, i32 16)
%22 = getelementptr inbounds [2 x i64], [2 x i64]* %3, i64 0, i64 0
store i64 %21, i64* %22
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = load i8, i8* %24
switch i8 %25, label %38 [
i8 119, label %26
i8 114, label %32
]
26: ; preds = %18
%27 = getelementptr inbounds i8*, i8** %1, i64 3
%28 = load i8*, i8** %27
%29 = tail call i64 @strtol(i8* nocapture %28, i32* null, i32 16)
%30 = getelementptr inbounds [2 x i64], [2 x i64]* %3, i64 0, i64 1
store i64 %29, i64* %30
%31 = call i32 @write(i32 %14, i64* %22, i32 8)
br label %38
32: ; preds = %18
%33 = call i32 @read(i32 %14, i64* %22, i32 8)
%34 = load i64, i64* %22
%35 = getelementptr inbounds [2 x i64], [2 x i64]* %3, i64 0, i64 1
%36 = load i64, i64* %35
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 %34, i64 %36)
br label %38
38: ; preds = %18, %32, %26
%39 = tail call i32 @close(i32 %14)
br label %40
40: ; preds = %38, %16, %7
%41 = phi i32 [ -1, %7 ], [ -1, %16 ], [ 0, %38 ]
ret i32 %41
}