#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 scalar_t__ __off64_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ lseek64(int, scalar_t__, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
void *utl_str2int(char *);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("args invalid\n");
    return 0;
  }
  unsigned long addr = utl_str2int(argv[1]);
  int size = utl_str2int(argv[2]);
  printf("read from 0x%lX, size %d\n", addr, size);
  int fd = open("/dev/phy_mem", O_RDONLY);
  if (fd < 0) {
    printf("phy_mem open fail\n");
    return 0;
  }
  addr = addr & 0xFFFFFFFFFFFFFFF8L;
  if (size > 256) {
    size = 256;
  }
  if (size == 0) {
    size = 8;
  }
  size = (size + 7) & 0xFFFFFFF8;
  __off64_t off = lseek64(fd, (__off64_t)addr, SEEK_SET);
  if (off < 0) {
    printf("lseek fail\n");
    return 0;
  }
  unsigned int buf[256 / 4];
  int read_size = read(fd, (char *)buf, size);
  if (read_size < 0) {
    printf("read failed\n");
    return 0;
  }
  int i;
  for (i = 0; i < size / 8; i++) {
    printf("%#lX:\t\t %08x  %08x \n", addr + i * 8, buf[2 * i], buf[2 * i + 1]);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     119d <main+0x3d>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1364 <main+0x204>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <utl_str2int@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <utl_str2int@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0xe45(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    120f <main+0xaf>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1364 <main+0x204>
mov    -0x18(%rbp),%rax
and    $0xfffffffffffffff8,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x100,-0x1c(%rbp)
jle    122f <main+0xcf>
movl   $0x100,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1240 <main+0xe0>
movl   $0x8,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x7,%eax
and    $0xfffffff8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
call   1080 <lseek64@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    128a <main+0x12a>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1364 <main+0x204>
mov    -0x20(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jge    12c9 <main+0x169>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1364 <main+0x204>
movl   $0x0,-0x138(%rbp)
mov    -0x138(%rbp),%eax
mov    %eax,-0x13c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x13c(%rbp),%eax
cmp    %ecx,%eax
jge    1355 <main+0x1f5>
mov    -0x18(%rbp),%rsi
mov    -0x138(%rbp),%eax
shl    $0x3,%eax
cltq
add    %rax,%rsi
mov    -0x138(%rbp),%eax
shl    $0x1,%eax
cltq
mov    -0x130(%rbp,%rax,4),%edx
mov    -0x138(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
mov    -0x130(%rbp,%rax,4),%ecx
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x138(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x138(%rbp)
jmp    12d3 <main+0x173>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
cmp    $0x3,%edi
jne    1268 <main+0xf8>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1060 <utl_str2int@plt>
mov    %rax,%r12
mov    0x10(%rbp),%rdi
call   1060 <utl_str2int@plt>
mov    %rax,%rbp
lea    0xe56(%rip),%rdi
mov    %r12,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1271 <main+0x101>
mov    %eax,%r14d
and    $0xfffffffffffffff8,%r12
cmp    $0x100,%ebp
mov    $0x100,%eax
cmovl  %ebp,%eax
lea    0x7(%rax),%ecx
and    $0xfffffff8,%ecx
test   %eax,%eax
mov    $0x8,%ebp
cmovne %ecx,%ebp
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %r12,%rsi
call   1090 <lseek64@plt>
test   %rax,%rax
js     127a <main+0x10a>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %ebp,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1283 <main+0x113>
cmp    $0x8,%ebp
jl     125e <main+0xee>
mov    %ebp,%ebp
lea    0xdf7(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r12,%rbx,1),%rsi
mov    (%rsp,%rbx,1),%edx
mov    0x4(%rsp,%rbx,1),%ecx
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x8,%rbx
cmp    %rbx,%rbp
jne    1240 <main+0xd0>
mov    %r14d,%edi
call   1050 <close@plt>
jmp    128f <main+0x11f>
lea    0xdf6(%rip),%rdi
jmp    128a <main+0x11a>
lea    0xddb(%rip),%rdi
jmp    128a <main+0x11a>
lea    0xdc7(%rip),%rdi
jmp    128a <main+0x11a>
lea    0xdb2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"args invalid\0A\00"
@.str.1 = [26 x i8] c"read from 0x%lX, size %d\0A\00"
@.str.2 = [13 x i8] c"/dev/phy_mem\00"
@O_RDONLY = global i32 0
@.str.3 = [19 x i8] c"phy_mem open fail\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [12 x i8] c"lseek fail\0A\00"
@.str.5 = [13 x i8] c"read failed\0A\00"
@.str.6 = [21 x i8] c"%#lX:\09\09 %08x %08x \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca [64 x i32]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %99
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i8* @utl_str2int(i8* %20)
%22 = ptrtoint i8* %21 to i64
store i64 %22, i64* %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i8* @utl_str2int(i8* %25)
%27 = ptrtoint i8* %26 to i32
store i32 %27, i32* %7
%28 = load i64, i64* %6
%29 = load i32, i32* %7
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %28, i32 %29)
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %17
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %99
37: ; preds = %17
%38 = load i64, i64* %6
%39 = and i64 %38, -8
store i64 %39, i64* %6
%40 = load i32, i32* %7
%41 = icmp sgt i32 %40, 256
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 256, i32* %7
br label %43
43: ; preds = %42, %37
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 8, i32* %7
br label %47
47: ; preds = %46, %43
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 7
%50 = and i32 %49, -8
store i32 %50, i32* %7
%51 = load i32, i32* %8
%52 = load i64, i64* %6
%53 = load i32, i32* @SEEK_SET
%54 = call i64 @lseek64(i32 %51, i64 %52, i32 %53)
store i64 %54, i64* %9
%55 = load i64, i64* %9
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %47
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %99
59: ; preds = %47
%60 = load i32, i32* %8
%61 = getelementptr inbounds [64 x i32], [64 x i32]* %10, i64 0, i64 0
%62 = bitcast i32* %61 to i8*
%63 = load i32, i32* %7
%64 = call i32 @read(i32 %60, i8* %62, i32 %63)
store i32 %64, i32* %11
%65 = load i32, i32* %11
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %59
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %99
69: ; preds = %59
store i32 0, i32* %12
br label %70
70: ; preds = %93, %69
%71 = load i32, i32* %12
%72 = load i32, i32* %7
%73 = sdiv i32 %72, 8
%74 = icmp slt i32 %71, %73
br i1 %74, label %75, label %96
75: ; preds = %70
%76 = load i64, i64* %6
%77 = load i32, i32* %12
%78 = mul nsw i32 %77, 8
%79 = sext i32 %78 to i64
%80 = add i64 %76, %79
%81 = load i32, i32* %12
%82 = mul nsw i32 2, %81
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [64 x i32], [64 x i32]* %10, i64 0, i64 %83
%85 = load i32, i32* %84
%86 = load i32, i32* %12
%87 = mul nsw i32 2, %86
%88 = add nsw i32 %87, 1
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [64 x i32], [64 x i32]* %10, i64 0, i64 %89
%91 = load i32, i32* %90
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %80, i32 %85, i32 %91)
br label %93
93: ; preds = %75
%94 = load i32, i32* %12
%95 = add nsw i32 %94, 1
store i32 %95, i32* %12
br label %70
96: ; preds = %70
%97 = load i32, i32* %8
%98 = call i32 @close(i32 %97)
store i32 0, i32* %3
br label %99
99: ; preds = %96, %67, %57, %35, %15
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
@.str.1 = [26 x i8] c"read from 0x%lX, size %d\0A\00"
@.str.2 = [13 x i8] c"/dev/phy_mem\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str.6 = [21 x i8] c"%#lX:\09\09 %08x %08x \0A\00"
@str = [12 x i8] c"read failed\00"
@str.7 = [11 x i8] c"lseek fail\00"
@str.8 = [18 x i8] c"phy_mem open fail\00"
@str.9 = [13 x i8] c"args invalid\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i32]
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
br label %63
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i8* @utl_str2int(i8* %9)
%11 = ptrtoint i8* %10 to i64
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i8* @utl_str2int(i8* %13)
%15 = ptrtoint i8* %14 to i64
%16 = trunc i64 %15 to i32
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %11, i32 %16)
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %7
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %63
23: ; preds = %7
%24 = and i64 %11, -8
%25 = icmp slt i32 %16, 256
%26 = select i1 %25, i32 %16, i32 256
%27 = icmp eq i32 %26, 0
%28 = add nsw i32 %26, 7
%29 = and i32 %28, -8
%30 = select i1 %27, i32 8, i32 %29
%31 = load i32, i32* @SEEK_SET
%32 = tail call i64 @lseek64(i32 %19, i64 %24, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %23
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.7, i64 0, i64 0))
br label %63
36: ; preds = %23
%37 = bitcast [64 x i32]* %3 to i8*
%38 = call i32 @read(i32 %19, i8* %37, i32 %30)
%39 = icmp slt i32 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %36
%41 = icmp sgt i32 %30, 7
br i1 %41, label %42, label %60
42: ; preds = %40
%43 = lshr exact i32 %30, 3
%44 = zext i32 %43 to i64
br label %47
45: ; preds = %36
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %62
47: ; preds = %42, %47
%48 = phi i64 [ 0, %42 ], [ %58, %47 ]
%49 = shl nsw i64 %48, 3
%50 = add i64 %24, %49
%51 = shl nuw nsw i64 %48, 1
%52 = getelementptr inbounds [64 x i32], [64 x i32]* %3, i64 0, i64 %51
%53 = load i32, i32* %52
%54 = or i64 %51, 1
%55 = getelementptr inbounds [64 x i32], [64 x i32]* %3, i64 0, i64 %54
%56 = load i32, i32* %55
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %50, i32 %53, i32 %56)
%58 = add nuw nsw i64 %48, 1
%59 = icmp eq i64 %58, %44
br i1 %59, label %60, label %47
60: ; preds = %47, %40
%61 = tail call i32 @close(i32 %19)
br label %62
62: ; preds = %60, %45
br label %63
63: ; preds = %21, %62, %34, %5
ret i32 0
}