#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*/ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, unsigned int *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ usage(char *);
int /*<<< orphan*/ write(int, unsigned int *, int);
int main(int argc, char *argv[]) {
  int fd_osif, i;
  unsigned int buf[3];
  ssize_t retval;
  char mode;
  if (argc < 2) {
    usage(argv[0]);
    return -1;
  }
  mode = argv[1][0];
  if (mode == 'w' && argc < 3 + 2) {
    usage(argv[0]);
    return -1;
  }
  fd_osif = open("/dev/osif0", O_RDWR);
  if (fd_osif < 0) {
    perror("error while opening /dev/osif0");
    return -1;
  }
  switch (mode) {
  case 'r':
    retval = read(fd_osif, buf, sizeof(buf));
    break;
  case 'w':
    for (i = 0; i < 3; i++) {
      if (argv[i + 2][1] == 'x')
        sscanf(argv[i + 2], "0x%08x", &buf[i]);
      else
        sscanf(argv[i + 2], "%08x", &buf[i]);
    }
    retval = write(fd_osif, buf, sizeof(buf));
    break;
  default:
    usage(argv[0]);
    return -1;
  }
  close(fd_osif);
  switch (mode) {
  case 'r':
    printf("Values read:\n");
    break;
  case 'w':
    printf("Values written:\n");
    break;
  }
  for (i = 0; i < 3; i++)
    printf("\tRegister %d: 0x%08X\n", i, buf[i]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cb <main+0x24b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0x77,%eax
jne    11f4 <main+0x74>
cmpl   $0x5,-0x8(%rbp)
jge    11f4 <main+0x74>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cb <main+0x24b>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xe10(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    122e <main+0xae>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cb <main+0x24b>
movsbl -0x29(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x72,%eax
je     1254 <main+0xd4>
jmp    1243 <main+0xc3>
mov    -0x30(%rbp),%eax
sub    $0x77,%eax
je     126d <main+0xed>
jmp    1325 <main+0x1a5>
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0xc,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
jmp    133d <main+0x1bd>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3,-0x18(%rbp)
jge    130c <main+0x18c>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
movsbl 0x1(%rax),%eax
cmp    $0x78,%eax
jne    12cd <main+0x14d>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
movslq -0x18(%rbp),%rax
lea    -0x24(%rbp),%rdx
shl    $0x2,%rax
add    %rax,%rdx
lea    0xd5c(%rip),%rsi
call   1070 <sscanf@plt>
jmp    12f9 <main+0x179>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
movslq -0x18(%rbp),%rax
lea    -0x24(%rbp),%rdx
shl    $0x2,%rax
add    %rax,%rdx
lea    0xd2d(%rip),%rsi
call   1070 <sscanf@plt>
jmp    12fe <main+0x17e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1274 <main+0xf4>
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
jmp    133d <main+0x1bd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cb <main+0x24b>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movsbl -0x29(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x72,%eax
je     136b <main+0x1eb>
jmp    135a <main+0x1da>
mov    -0x34(%rbp),%eax
sub    $0x77,%eax
je     137e <main+0x1fe>
jmp    138c <main+0x20c>
lea    0xcb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    138c <main+0x20c>
lea    0xcaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3,-0x18(%rbp)
jge    13c4 <main+0x244>
mov    -0x18(%rbp),%esi
movslq -0x18(%rbp),%rax
mov    -0x24(%rbp,%rax,4),%edx
lea    0xc96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1393 <main+0x213>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jle    126d <main+0xdd>
mov    0x8(%rbx),%rax
mov    (%rax),%bpl
cmp    $0x4,%edi
ja     11be <main+0x2e>
cmp    $0x77,%bpl
je     126d <main+0xdd>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12f0 <main+0x160>
mov    %eax,%r15d
movsbl %bpl,%eax
cmp    $0x72,%eax
je     1289 <main+0xf9>
cmp    $0x77,%eax
jne    126d <main+0xdd>
mov    0x10(%rbx),%rdi
cmpb   $0x78,0x1(%rdi)
lea    0xe20(%rip),%r12
lea    0xe1b(%rip),%rbp
mov    %rbp,%rsi
cmove  %r12,%rsi
lea    0x4(%rsp),%r14
mov    %r14,%rdx
call   1080 <sscanf@plt>
mov    0x18(%rbx),%rdi
cmpb   $0x78,0x1(%rdi)
mov    %rbp,%rsi
cmove  %r12,%rsi
lea    0x8(%rsp),%rdx
call   1080 <sscanf@plt>
mov    0x20(%rbx),%rdi
cmpb   $0x78,0x1(%rdi)
cmove  %r12,%rbp
lea    0xc(%rsp),%rdx
mov    %rbp,%rsi
call   1080 <sscanf@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <write@plt>
mov    %r15d,%edi
call   1060 <close@plt>
lea    0xdd1(%rip),%rdi
jmp    12aa <main+0x11a>
mov    (%rbx),%rdi
call   1090 <usage@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x4(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   1070 <read@plt>
mov    %r15d,%edi
call   1060 <close@plt>
lea    0xda2(%rip),%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%edx
lea    0xd6c(%rip),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edx
mov    %rbp,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edx
mov    %rbp,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    127a <main+0xea>
lea    0xd09(%rip),%rdi
call   10b0 <perror@plt>
jmp    1275 <main+0xe5>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/osif0\00"
@O_RDWR = global i32 0
@.str.1 = [31 x i8] c"error while opening /dev/osif0\00"
@.str.2 = [7 x i8] c"0x%08x\00"
@.str.3 = [5 x i8] c"%08x\00"
@.str.4 = [14 x i8] c"Values read:\0A\00"
@.str.5 = [17 x i8] c"Values written:\0A\00"
@.str.6 = [22 x i8] c"\09Register %d: 0x%08X\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 [3 x i32]
%9 = alloca i32
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @usage(i8* %16)
store i32 -1, i32* %3
br label %123
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
store i8 %23, i8* %10
%24 = load i8, i8* %10
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 119
br i1 %26, label %27, label %35
27: ; preds = %18
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 5
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = call i32 @usage(i8* %33)
store i32 -1, i32* %3
br label %123
35: ; preds = %27, %18
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %123
42: ; preds = %35
%43 = load i8, i8* %10
%44 = sext i8 %43 to i32
switch i32 %44, label %94 [
i32 114, label %45
i32 119, label %49
]
45: ; preds = %42
%46 = load i32, i32* %6
%47 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 0
%48 = call i32 @read(i32 %46, i32* %47, i32 12)
store i32 %48, i32* %9
br label %99
49: ; preds = %42
store i32 0, i32* %7
br label %50
50: ; preds = %87, %49
%51 = load i32, i32* %7
%52 = icmp slt i32 %51, 3
br i1 %52, label %53, label %90
53: ; preds = %50
%54 = load i8**, i8*** %5
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 2
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8*, i8** %54, i64 %57
%59 = load i8*, i8** %58
%60 = getelementptr inbounds i8, i8* %59, i64 1
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 120
br i1 %63, label %64, label %75
64: ; preds = %53
%65 = load i8**, i8*** %5
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 2
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8*, i8** %65, i64 %68
%70 = load i8*, i8** %69
%71 = load i32, i32* %7
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 %72
%74 = call i32 @sscanf(i8* %70, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %73)
br label %86
75: ; preds = %53
%76 = load i8**, i8*** %5
%77 = load i32, i32* %7
%78 = add nsw i32 %77, 2
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8*, i8** %76, i64 %79
%81 = load i8*, i8** %80
%82 = load i32, i32* %7
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 %83
%85 = call i32 @sscanf(i8* %81, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* %84)
br label %86
86: ; preds = %75, %64
br label %87
87: ; preds = %86
%88 = load i32, i32* %7
%89 = add nsw i32 %88, 1
store i32 %89, i32* %7
br label %50
90: ; preds = %50
%91 = load i32, i32* %6
%92 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 0
%93 = call i32 @write(i32 %91, i32* %92, i32 12)
store i32 %93, i32* %9
br label %99
94: ; preds = %42
%95 = load i8**, i8*** %5
%96 = getelementptr inbounds i8*, i8** %95, i64 0
%97 = load i8*, i8** %96
%98 = call i32 @usage(i8* %97)
store i32 -1, i32* %3
br label %123
99: ; preds = %90, %45
%100 = load i32, i32* %6
%101 = call i32 @close(i32 %100)
%102 = load i8, i8* %10
%103 = sext i8 %102 to i32
switch i32 %103, label %108 [
i32 114, label %104
i32 119, label %106
]
104: ; preds = %99
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %108
106: ; preds = %99
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %108
108: ; preds = %99, %106, %104
store i32 0, i32* %7
br label %109
109: ; preds = %119, %108
%110 = load i32, i32* %7
%111 = icmp slt i32 %110, 3
br i1 %111, label %112, label %122
112: ; preds = %109
%113 = load i32, i32* %7
%114 = load i32, i32* %7
%115 = sext i32 %114 to i64
%116 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 %115
%117 = load i32, i32* %116
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %113, i32 %117)
br label %119
119: ; preds = %112
%120 = load i32, i32* %7
%121 = add nsw i32 %120, 1
store i32 %121, i32* %7
br label %109
122: ; preds = %109
store i32 0, i32* %3
br label %123
123: ; preds = %122, %94, %40, %30, %13
%124 = load i32, i32* %3
ret i32 %124
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/osif0\00"
@O_RDWR = global i32 0
@.str.1 = [31 x i8] c"error while opening /dev/osif0\00"
@.str.2 = [7 x i8] c"0x%08x\00"
@.str.3 = [5 x i8] c"%08x\00"
@.str.6 = [22 x i8] c"\09Register %d: 0x%08X\0A\00"
@str = [16 x i8] c"Values written:\00"
@str.7 = [13 x i8] c"Values read:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i32]
%4 = bitcast [3 x i32]* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @usage(i8* %7)
br label %74
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i8 %12, 119
%15 = icmp ult i32 %0, 5
%16 = and i1 %15, %14
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i8*, i8** %1
%19 = tail call i32 @usage(i8* %18)
br label %74
20: ; preds = %9
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %74
26: ; preds = %20
switch i32 %13, label %55 [
i32 114, label %58
i32 119, label %27
]
27: ; preds = %26
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 120
%33 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%34 = select i1 %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
%35 = call i32 @sscanf(i8* %29, i8* %34, i32* %33)
%36 = getelementptr inbounds i8*, i8** %1, i64 3
%37 = load i8*, i8** %36
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 120
%41 = select i1 %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
%42 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
%43 = call i32 @sscanf(i8* %37, i8* %41, i32* %42)
%44 = getelementptr inbounds i8*, i8** %1, i64 4
%45 = load i8*, i8** %44
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = load i8, i8* %46
%48 = icmp eq i8 %47, 120
%49 = select i1 %48, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
%50 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
%51 = call i32 @sscanf(i8* %45, i8* %49, i32* %50)
%52 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%53 = call i32 @write(i32 %22, i32* %52, i32 12)
%54 = call i32 @close(i32 %22)
br label %62
55: ; preds = %26
%56 = load i8*, i8** %1
%57 = tail call i32 @usage(i8* %56)
br label %74
58: ; preds = %26
%59 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%60 = call i32 @read(i32 %22, i32* %59, i32 12)
%61 = tail call i32 @close(i32 %22)
br label %62
62: ; preds = %27, %58
%63 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0), %27 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0), %58 ]
%64 = call i32 @puts(i8* dereferenceable(1) %63)
%65 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%66 = load i32, i32* %65
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 0, i32 %66)
%68 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
%69 = load i32, i32* %68
%70 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 %69)
%71 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
%72 = load i32, i32* %71
%73 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 2, i32 %72)
br label %74
74: ; preds = %62, %55, %24, %17, %6
%75 = phi i32 [ -1, %6 ], [ -1, %17 ], [ -1, %24 ], [ -1, %55 ], [ 0, %62 ]
ret i32 %75
}