#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, short, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, int *, int);
short int peek2(short int seg, short int offset) {
  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, offset, 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):
<peek2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %si,%ax
mov    %di,%cx
mov    %cx,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    119f <peek2+0x4f>
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
movw   $0x0,-0x2(%rbp)
jmp    121e <peek2+0xce>
mov    -0xc(%rbp),%edi
mov    -0x6(%rbp),%ax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%edx
movswl %ax,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11d7 <peek2+0x87>
lea    0xe68(%rip),%rdi
call   1030 <printf@plt>
movw   $0x0,-0x2(%rbp)
jmp    121e <peek2+0xce>
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
cmp    $0x2,%eax
je     120f <peek2+0xbf>
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
movw   $0x0,-0x2(%rbp)
jmp    121e <peek2+0xce>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%eax
mov    %ax,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peek2>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11b5 <peek2+0x65>
mov    %eax,%ebx
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
movswl %bp,%esi
mov    %ebx,%edi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11be <peek2+0x6e>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1060 <read@plt>
cmp    $0x2,%eax
jne    11c7 <peek2+0x77>
mov    %ebx,%edi
call   1050 <close@plt>
movzwl 0x4(%rsp),%eax
jmp    11d5 <peek2+0x85>
lea    0xe6b(%rip),%rdi
jmp    11ce <peek2+0x7e>
lea    0xe53(%rip),%rdi
jmp    11ce <peek2+0x7e>
lea    0xe3b(%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 signext i16 @peek2(i16 signext %0, i16 signext %1) {
%3 = alloca i16
%4 = alloca i16
%5 = alloca i16
%6 = alloca i32
%7 = alloca i32
store i16 %0, i16* %4
store i16 %1, i16* %5
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i16 0, i16* %3
br label %33
14: ; preds = %2
%15 = load i32, i32* %6
%16 = load i16, i16* %5
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @lseek(i32 %15, i16 signext %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i16 0, i16* %3
br label %33
22: ; preds = %14
store i32 0, i32* %7
%23 = load i32, i32* %6
%24 = call i32 @read(i32 %23, i32* %7, i32 2)
%25 = icmp ne i32 %24, 2
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i16 0, i16* %3
br label %33
28: ; preds = %22
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %7
%32 = trunc i32 %31 to i16
store i16 %32, i16* %3
br label %33
33: ; preds = %28, %26, %20, %12
%34 = load i16, i16* %3
ret i16 %34
}

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 signext i16 @peek2(i16 signext %0, i16 signext %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.5, i64 0, i64 0))
br label %25
10: ; preds = %2
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %6, i16 signext %1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.4, i64 0, i64 0))
br label %25
16: ; preds = %10
store i32 0, i32* %3
%17 = call i32 @read(i32 %6, i32* %3, i32 2)
%18 = icmp eq i32 %17, 2
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %25
21: ; preds = %16
%22 = tail call i32 @close(i32 %6)
%23 = load i32, i32* %3
%24 = trunc i32 %23 to i16
br label %25
25: ; preds = %21, %19, %14, %8
%26 = phi i16 [ 0, %8 ], [ 0, %14 ], [ 0, %19 ], [ %24, %21 ]
ret i16 %26
}