#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_memory(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, long);
int main(int argc, char **argv) {
  void *grp_regs = map_memory("/dev/hw3d", 0, 1024 * 1024);
  printf("GPU base mapped at %p\n", grp_regs);
  int state_offset = 0x10140;
  printf("GPU state = %08lx\n", *((long *)((char *)grp_regs + state_offset)));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xec3(%rip),%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1040 <map_memory@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xeb2(%rip),%rdi
call   1030 <printf@plt>
movl   $0x10140,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%rsi
lea    0xeaa(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xed8(%rip),%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1040 <map_memory@plt>
mov    %rax,%rbx
lea    0xecc(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    0x10140(%rbx),%rsi
lea    0xecd(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/hw3d\00"
@.str.1 = [23 x i8] c"GPU base mapped at %p\0A\00"
@.str.2 = [19 x i8] c"GPU state = %08lx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i8* @map_memory(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 1048576)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = ptrtoint i8* %9 to i64
%11 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %10)
store i32 65856, i32* %7
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = bitcast i8* %15 to i64*
%17 = load i64, i64* %16
%18 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 %17)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/hw3d\00"
@.str.1 = [23 x i8] c"GPU base mapped at %p\0A\00"
@.str.2 = [19 x i8] c"GPU state = %08lx\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @map_memory(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 1048576)
%4 = ptrtoint i8* %3 to i64
%5 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %4)
%6 = getelementptr inbounds i8, i8* %3, i64 65856
%7 = bitcast i8* %6 to i64*
%8 = load i64, i64* %7
%9 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 %8)
ret i32 0
}