#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_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, int *, int);
int peek(int addr) {
  int ram;
  int i;
  ram = open("/dev/mem", O_RDONLY);
  if (ram == -1) {
    printf("Can't open RAM. Use root privileges!\n");
    return 0;
  }
  if (lseek(ram, addr, SEEK_SET) == -1) {
    printf("Can'r seek RAM\n");
    return 0;
  }
  i = 0;
  if (read(ram, &i, 2) != 2) {
    printf("Can't read RAM\n");
    return 0;
  }
  close(ram);
  return i;
}


x86 assembly (clang, -O0):
<peek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1195 <peek+0x45>
lea    0xe85(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1211 <peek+0xc1>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11ca <peek+0x7a>
lea    0xe76(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1211 <peek+0xc1>
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
cmp    $0x2,%eax
je     1203 <peek+0xb3>
lea    0xe4d(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1211 <peek+0xc1>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peek>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11b3 <peek+0x63>
mov    %eax,%ebx
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11bc <peek+0x6c>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1060 <read@plt>
cmp    $0x2,%eax
jne    11c5 <peek+0x75>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x4(%rsp),%eax
jmp    11d3 <peek+0x83>
lea    0xe6d(%rip),%rdi
jmp    11cc <peek+0x7c>
lea    0xe55(%rip),%rdi
jmp    11cc <peek+0x7c>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"Can't open RAM. Use root privileges!\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [16 x i8] c"Can'r seek RAM\0A\00"
@.str.3 = [16 x i8] c"Can't read RAM\0A\00"
define i32 @peek(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %30
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* %3
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @lseek(i32 %13, i32 %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %30
20: ; preds = %12
store i32 0, i32* %5
%21 = load i32, i32* %4
%22 = call i32 @read(i32 %21, i32* %5, i32 2)
%23 = icmp ne i32 %22, 2
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %30
26: ; preds = %20
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %5
store i32 %29, i32* %2
br label %30
30: ; preds = %26, %24, %18, %10
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@str = [15 x i8] c"Can't read RAM\00"
@str.4 = [15 x i8] c"Can'r seek RAM\00"
@str.5 = [37 x i8] c"Can't open RAM. Use root privileges!\00"
define i32 @peek(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.5, i64 0, i64 0))
br label %23
9: ; preds = %1
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %5, i32 %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.4, i64 0, i64 0))
br label %23
15: ; preds = %9
store i32 0, i32* %2
%16 = call i32 @read(i32 %5, i32* %2, i32 2)
%17 = icmp eq i32 %16, 2
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %23
20: ; preds = %15
%21 = tail call i32 @close(i32 %5)
%22 = load i32, i32* %2
br label %23
23: ; preds = %20, %18, %13, %7
%24 = phi i32 [ 0, %7 ], [ 0, %13 ], [ 0, %18 ], [ %22, %20 ]
ret i32 %24
}