#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*/ u_int;
typedef int /*<<< orphan*/ u_char;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ open_file(char *, int /*<<< orphan*/, int *);
scalar_t__ read_buf(int, int /*<<< orphan*/, int /*<<< orphan*/,
                    int /*<<< orphan*/ *);
int ReadPhysMem(u_int offset, u_int size, u_char *buffer) {
  int fd;
  int status = 0;
  if (open_file("/dev/mem", O_RDONLY, &fd) == 0) {
    if (read_buf(fd, offset, size, buffer) == 0)
      status = 1;
    close(fd);
  }
  return status;
}


x86 assembly (clang, -O0):
<ReadPhysMem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
lea    -0x14(%rbp),%rdx
call   1040 <open_file@plt>
cmp    $0x0,%rax
jne    1197 <ReadPhysMem+0x67>
mov    -0x14(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0x10(%rbp),%rcx
call   1030 <read_buf@plt>
cmp    $0x0,%rax
jne    118f <ReadPhysMem+0x5f>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadPhysMem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
lea    0x4(%rsp),%rdx
call   1040 <open_file@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1183 <ReadPhysMem+0x53>
mov    0x4(%rsp),%edi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1030 <read_buf@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
define i32 @ReadPhysMem(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %8
%9 = load i32, i32* @O_RDONLY
%10 = call i64 @open_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %3
%13 = load i32, i32* %7
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = load i32*, i32** %6
%17 = call i64 @read_buf(i32 %13, i32 %14, i32 %15, i32* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %12
store i32 1, i32* %8
br label %20
20: ; preds = %19, %12
%21 = load i32, i32* %7
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %20, %3
%24 = load i32, i32* %8
ret i32 %24
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
define i32 @ReadPhysMem(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = call i64 @open_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %3
%10 = load i32, i32* %4
%11 = call i64 @read_buf(i32 %10, i32 %0, i32 %1, i32* %2)
%12 = icmp eq i64 %11, 0
%13 = zext i1 %12 to i32
%14 = load i32, i32* %4
%15 = call i32 @close(i32 %14)
br label %16
16: ; preds = %9, %3
%17 = phi i32 [ %13, %9 ], [ 0, %3 ]
ret i32 %17
}