#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*/ jiffies;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_kmem(unsigned long, unsigned long *, int);
int /*<<< orphan*/ read_mem(unsigned long, unsigned long *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  FILE *fp;
  char addr_str[11] = "0x";
  char var[51];
  unsigned long addr;
  unsigned long jiffies;
  char ch;
  int r;
  if (argc != 2) {
    fprintf(stderr, "usage: %s System.map\n", argv[0]);
    exit(-1);
  }
  if ((fp = fopen(argv[1], "r")) == NULL) {
    perror("fopen");
    exit(-1);
  }
  do {
    r = fscanf(fp, "%8s %c %50s\n", &addr_str[2], &ch, var);
    if (strcmp(var, "jiffies") == 0)
      break;
  } while (r > 0);
  if (r < 0) {
    printf("could not find jiffies\n");
    exit(-1);
  }
  addr = strtoul(addr_str, NULL, 16);
  printf("found jiffies at (%s) %08lx\n", addr_str, addr);
  read_kmem(addr, &jiffies, sizeof(jiffies));
  printf("jiffies=%ld (read from virtual memory)\n\n", jiffies);
  jiffies = 0;
  read_mem(addr - 0xC0000000, &jiffies, sizeof(jiffies));
  printf("jiffies=%ld (read from physical memory)\n", jiffies);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe40(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe3d(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xe35(%rip),%al
mov    %al,-0x19(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1208 <main+0x68>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe12(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1240 <main+0xa0>
lea    0xdf2(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    1245 <main+0xa5>
mov    -0x18(%rbp),%rdi
lea    -0x23(%rbp),%rdx
add    $0x2,%rdx
lea    -0x60(%rbp),%r8
lea    0xdcd(%rip),%rsi
lea    -0x71(%rbp),%rcx
call   1050 <fscanf@plt>
mov    %eax,-0x78(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1287 <main+0xe7>
jmp    1296 <main+0xf6>
jmp    128c <main+0xec>
cmpl   $0x0,-0x78(%rbp)
jg     1245 <main+0xa5>
cmpl   $0x0,-0x78(%rbp)
jge    12b8 <main+0x118>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    -0x23(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
mov    %rax,-0x68(%rbp)
lea    -0x23(%rbp),%rsi
mov    -0x68(%rbp),%rdx
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x68(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    $0x8,%edx
call   1040 <read_kmem@plt>
mov    -0x70(%rbp),%rsi
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x70(%rbp)
mov    -0x68(%rbp),%rdi
movabs $0xc0000000,%rax
sub    %rax,%rdi
lea    -0x70(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <read_mem@plt>
mov    -0x70(%rbp),%rsi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
movl   $0x0,0x17(%rsp)
movq   $0x7830,0x10(%rsp)
cmp    $0x2,%edi
jne    12e1 <main+0x131>
mov    0x8(%rsi),%rdi
lea    0xe3e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1303 <main+0x153>
mov    %rax,%r13
lea    0xe2e(%rip),%r15
lea    0x7(%rsp),%r12
lea    0x20(%rsp),%rbx
lea    0xea8(%rip),%rbp
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r15,%rsi
lea    0x12(%rsp),%rdx
mov    %r12,%rcx
mov    %rbx,%r8
call   1060 <fscanf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %r14d,%r14d
jle    123e <main+0x8e>
test   %rax,%rax
jne    1210 <main+0x60>
test   %r14d,%r14d
js     1319 <main+0x169>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    %rax,%rbp
lea    0xdd1(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x8(%rsp),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1050 <read_kmem@plt>
mov    0x8(%rsp),%rsi
lea    0xdc0(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x0,0x8(%rsp)
movabs $0xffffffff40000000,%rdi
ff ff ff
add    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   10c0 <read_mem@plt>
mov    0x8(%rsp),%rsi
lea    0xdb3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd17(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xd19(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xd85(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@__const.main.addr_str = [11 x i8] c"0x\00\00\00\00\00\00\00\00\00"
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s System.map\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [13 x i8] c"%8s %c %50s\0A\00"
@.str.4 = [8 x i8] c"jiffies\00"
@.str.5 = [24 x i8] c"could not find jiffies\0A\00"
@.str.6 = [29 x i8] c"found jiffies at (%s) %08lx\0A\00"
@.str.7 = [41 x i8] c"jiffies=%ld (read from virtual memory)\0A\0A\00"
@.str.8 = [41 x i8] c"jiffies=%ld (read from physical memory)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [11 x i8]
%8 = alloca [51 x i8]
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = bitcast [11 x i8]* %7 to i8*
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %6
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %23
br label %33
33: ; preds = %43, %32
%34 = load i32*, i32** %6
%35 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 2
%36 = getelementptr inbounds [51 x i8], [51 x i8]* %8, i64 0, i64 0
%37 = call i32 @fscanf(i32* %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* %11, i8* %36)
store i32 %37, i32* %12
%38 = getelementptr inbounds [51 x i8], [51 x i8]* %8, i64 0, i64 0
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %33
br label %46
42: ; preds = %33
br label %43
43: ; preds = %42
%44 = load i32, i32* %12
%45 = icmp sgt i32 %44, 0
br i1 %45, label %33, label %46
46: ; preds = %43, %41
%47 = load i32, i32* %12
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %46
%53 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 0
%54 = call i64 @strtoul(i8* %53, i32* null, i32 16)
store i64 %54, i64* %9
%55 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 0
%56 = load i64, i64* %9
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %55, i64 %56)
%58 = load i64, i64* %9
%59 = call i32 @read_kmem(i64 %58, i64* %10, i32 8)
%60 = load i64, i64* %10
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i64 %60)
store i64 0, i64* %10
%62 = load i64, i64* %9
%63 = sub i64 %62, 3221225472
%64 = call i32 @read_mem(i64 %63, i64* %10, i32 8)
%65 = load i64, i64* %10
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i64 %65)
ret i32 0
}

LLVM IR (-O2):
@__const.main.addr_str = [11 x i8] c"0x\00\00\00\00\00\00\00\00\00"
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s System.map\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [13 x i8] c"%8s %c %50s\0A\00"
@.str.4 = [8 x i8] c"jiffies\00"
@.str.6 = [29 x i8] c"found jiffies at (%s) %08lx\0A\00"
@.str.7 = [41 x i8] c"jiffies=%ld (read from virtual memory)\0A\0A\00"
@.str.8 = [41 x i8] c"jiffies=%ld (read from physical memory)\0A\00"
@str = [23 x i8] c"could not find jiffies\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [11 x i8]
%4 = alloca [51 x i8]
%5 = alloca i64
%6 = alloca i8
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [51 x i8], [51 x i8]* %4, i64 0, i64 0
%9 = bitcast i64* %5 to i8*
%10 = icmp eq i32 %0, 2
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 2
br label %26
23: ; preds = %16
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %21, %26
%27 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %22, i8* %6, i8* %8)
%28 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp ne i64 %28, 0
%30 = icmp sgt i32 %27, 0
%31 = and i1 %30, %29
br i1 %31, label %26, label %32
32: ; preds = %26
%33 = icmp slt i32 %27, 0
br i1 %33, label %34, label %37
34: ; preds = %32
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %32
%38 = call i64 @strtoul(i8* nocapture %7, i32* null, i32 16)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %7, i64 %38)
%40 = call i32 @read_kmem(i64 %38, i64* %5, i32 8)
%41 = load i64, i64* %5
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i64 %41)
store i64 0, i64* %5
%43 = add i64 %38, -3221225472
%44 = call i32 @read_mem(i64 %43, i64* %5, i32 8)
%45 = load i64, i64* %5
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0), i64 %45)
ret i32 0
}