#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
long long int gettimer(void) {
  long long int t, prev, *timer;
  int fd;
  void *st_base;
  if (-1 == (fd = open("/dev/mem", O_RDONLY))) {
    fprintf(stderr, "open() failed.\n");
    return 255;
  }
  if (MAP_FAILED ==
      (st_base = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, (0x20003000)))) {
    fprintf(stderr, "mmap() failed.\n");
    return 254;
  }
  timer = (long long int *)((char *)st_base + (4));
  t = *timer;
  return t;
}


x86 assembly (clang, -O0):
<gettimer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1181 <gettimer+0x51>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe9a(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0xff,-0x8(%rbp)
jmp    120e <gettimer+0xde>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0x20003000,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,-0x30(%rbp)
cmp    %rcx,%rax
jne    11ef <gettimer+0xbf>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0xfe,-0x8(%rbp)
jmp    120e <gettimer+0xde>
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettimer>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xebf(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1187 <gettimer+0x57>
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%rbx
mov    0x2e9c(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e83(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
mov    $0x20003000,%r9d
call   1030 <mmap@plt>
cmp    %rax,%rbx
je     11a3 <gettimer+0x73>
mov    0x4(%rax),%rax
pop    %rbx
ret
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xff,%eax
pop    %rbx
ret
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xfe,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"open() failed.\0A\00"
@MAP_FAILED = global i8* null
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [16 x i8] c"mmap() failed.\0A\00"
define i64 @gettimer() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i8*
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %5
%9 = icmp eq i32 -1, %8
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i64 255, i64* %1
br label %30
13: ; preds = %0
%14 = load i8*, i8** @MAP_FAILED
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* %5
%18 = call i8* @mmap(i32* null, i32 4096, i32 %15, i32 %16, i32 %17, i32 536883200)
store i8* %18, i8** %6
%19 = icmp eq i8* %14, %18
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i64 254, i64* %1
br label %30
23: ; preds = %13
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 4
%26 = bitcast i8* %25 to i64*
store i64* %26, i64** %4
%27 = load i64*, i64** %4
%28 = load i64, i64* %27
store i64 %28, i64* %2
%29 = load i64, i64* %2
store i64 %29, i64* %1
br label %30
30: ; preds = %23, %20, %10
%31 = load i64, i64* %1
ret i64 %31
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"open() failed.\0A\00"
@MAP_FAILED = global i8* null
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [16 x i8] c"mmap() failed.\0A\00"
define i64 @gettimer() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %20
7: ; preds = %0
%8 = load i8*, i8** @MAP_FAILED
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @MAP_SHARED
%11 = tail call i8* @mmap(i32* null, i32 4096, i32 %9, i32 %10, i32 %2, i32 536883200)
%12 = icmp eq i8* %8, %11
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %20
16: ; preds = %7
%17 = getelementptr inbounds i8, i8* %11, i64 4
%18 = bitcast i8* %17 to i64*
%19 = load i64, i64* %18
br label %20
20: ; preds = %16, %13, %4
%21 = phi i64 [ 255, %4 ], [ 254, %13 ], [ %19, %16 ]
ret i64 %21
}