#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*/ uint32_t;
typedef int off_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
size_t map_mem(int, void **, int, size_t);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ swap(int /*<<< orphan*/ *, size_t);
int main(int argc, char *argv[]) {
  int i;
  int fd;
  void *va;
  size_t filesize;
  off_t pa = 0xc0000000;
  size_t length;
  uint32_t *mem;
  FILE *binfile;
  char *filename;
  if (argc < 2) {
    printf("Usage: %s <binary file>\n", argv[0]);
    return 1;
  }
  filename = argv[1];
  binfile = fopen(filename, "rb");
  if (!binfile) {
    printf("Error opening %s\n", filename);
    return 1;
  }
  fseek(binfile, 0, SEEK_END);
  filesize = ftell(binfile);
  rewind(binfile);
  if ((fd = open("/dev/mem", O_RDWR, 0)) < 0) {
    printf("Error opening /dev/mem\n");
    return 1;
  }
  length = map_mem(fd, &va, pa, filesize);
  if (!va) {
    printf("mmap of 0x%08x failed\n", (unsigned int)va);
    return 1;
  }
  mem = (uint32_t *)(va + length - filesize);
  if (filesize % sizeof(uint32_t))
    printf("Error: file size is not ending on a 32-bit boundary\n");
  filesize = filesize / sizeof(uint32_t);
  if (fread(mem, sizeof(uint32_t), filesize, binfile) != filesize) {
    printf("Error reading %s\n", filename);
  }
  swap(mem, filesize);
  fclose(binfile);
  munmap(va, length);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xc0000000,-0x2c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1208 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139f <main+0x1df>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1251 <main+0x91>
mov    -0x50(%rbp),%rsi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139f <main+0x1df>
mov    -0x48(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x48(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x48(%rbp),%rdi
call   1070 <rewind@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
lea    0xda3(%rip),%rdi
xor    %edx,%edx
call   10c0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12b8 <main+0xf8>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139f <main+0x1df>
mov    -0x18(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rcx
lea    -0x20(%rbp),%rsi
call   1030 <map_mem@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12fa <main+0x13a>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139f <main+0x1df>
mov    -0x20(%rbp),%rax
add    -0x38(%rbp),%rax
xor    %ecx,%ecx
sub    -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
and    $0x3,%rax
cmp    $0x0,%rax
je     132f <main+0x16f>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rax
shr    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    $0x4,%esi
call   1040 <fread@plt>
cmp    -0x28(%rbp),%rax
je     136d <main+0x1ad>
mov    -0x50(%rbp),%rsi
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10e0 <swap@plt>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10b0 <munmap@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11ef <main+0x1f>
mov    (%rsi),%rsi
lea    0xe16(%rip),%rdi
jmp    12f2 <main+0x122>
mov    0x8(%rsi),%r14
lea    0xe1f(%rip),%rsi
mov    %r14,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     12e8 <main+0x118>
mov    %rax,%rbx
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1080 <rewind@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
xor    %edx,%edx
call   10d0 <open@plt>
test   %eax,%eax
js     130d <main+0x13d>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc0000000,%edx
mov    %r13,%rcx
call   1030 <map_mem@plt>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     131b <main+0x14b>
mov    %rax,%r12
add    %rax,%rbp
sub    %r13,%rbp
test   $0x3,%r13b
je     1290 <main+0xc0>
lea    0xdcc(%rip),%rdi
call   1040 <puts@plt>
shr    $0x2,%r13
mov    %rbp,%rdi
mov    $0x4,%esi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    %r13,%rax
je     12bd <main+0xed>
lea    0xd92(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   10f0 <swap@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    (%rsp),%rdi
mov    %r12,%rsi
call   10c0 <munmap@plt>
mov    %r15d,%edi
call   1090 <close@plt>
xor    %eax,%eax
jmp    12fe <main+0x12e>
lea    0xd2d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd77(%rip),%rdi
call   1040 <puts@plt>
jmp    12f9 <main+0x129>
lea    0xd0c(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12f9 <main+0x129>

LLVM IR (-O0):
@.str = [25 x i8] c"Usage: %s <binary file>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [18 x i8] c"Error opening %s\0A\00"
@SEEK_END = global i32 0
@.str.3 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.4 = [24 x i8] c"Error opening /dev/mem\0A\00"
@.str.5 = [23 x i8] c"mmap of 0x%08x failed\0A\00"
@.str.6 = [53 x i8] c"Error: file size is not ending on a 32-bit boundary\0A\00"
@.str.7 = [18 x i8] c"Error reading %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1073741824, i32* %10
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %93
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %14
%26 = load i8*, i8** %14
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %13
%28 = load i32*, i32** %13
%29 = icmp ne i32* %28, null
br i1 %29, label %33, label %30
30: ; preds = %22
%31 = load i8*, i8** %14
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store i32 1, i32* %3
br label %93
33: ; preds = %22
%34 = load i32*, i32** %13
%35 = load i32, i32* @SEEK_END
%36 = call i32 @fseek(i32* %34, i32 0, i32 %35)
%37 = load i32*, i32** %13
%38 = call i64 @ftell(i32* %37)
store i64 %38, i64* %9
%39 = load i32*, i32** %13
%40 = call i32 @rewind(i32* %39)
%41 = load i32, i32* @O_RDWR
%42 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 0)
store i32 %42, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %93
46: ; preds = %33
%47 = load i32, i32* %7
%48 = load i32, i32* %10
%49 = load i64, i64* %9
%50 = call i64 @map_mem(i32 %47, i8** %8, i32 %48, i64 %49)
store i64 %50, i64* %11
%51 = load i8*, i8** %8
%52 = icmp ne i8* %51, null
br i1 %52, label %57, label %53
53: ; preds = %46
%54 = load i8*, i8** %8
%55 = ptrtoint i8* %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %55)
store i32 1, i32* %3
br label %93
57: ; preds = %46
%58 = load i8*, i8** %8
%59 = load i64, i64* %11
%60 = getelementptr i8, i8* %58, i64 %59
%61 = load i64, i64* %9
%62 = sub i64 0, %61
%63 = getelementptr i8, i8* %60, i64 %62
%64 = bitcast i8* %63 to i32*
store i32* %64, i32** %12
%65 = load i64, i64* %9
%66 = urem i64 %65, 4
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %57
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.6, i64 0, i64 0))
br label %70
70: ; preds = %68, %57
%71 = load i64, i64* %9
%72 = udiv i64 %71, 4
store i64 %72, i64* %9
%73 = load i32*, i32** %12
%74 = load i64, i64* %9
%75 = load i32*, i32** %13
%76 = call i64 @fread(i32* %73, i32 4, i64 %74, i32* %75)
%77 = load i64, i64* %9
%78 = icmp ne i64 %76, %77
br i1 %78, label %79, label %82
79: ; preds = %70
%80 = load i8*, i8** %14
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %79, %70
%83 = load i32*, i32** %12
%84 = load i64, i64* %9
%85 = call i32 @swap(i32* %83, i64 %84)
%86 = load i32*, i32** %13
%87 = call i32 @fclose(i32* %86)
%88 = load i8*, i8** %8
%89 = load i64, i64* %11
%90 = call i32 @munmap(i8* %88, i64 %89)
%91 = load i32, i32* %7
%92 = call i32 @close(i32 %91)
store i32 0, i32* %3
br label %93
93: ; preds = %82, %53, %44, %30, %17
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@.str = [25 x i8] c"Usage: %s <binary file>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [18 x i8] c"Error opening %s\0A\00"
@SEEK_END = global i32 0
@.str.3 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.5 = [23 x i8] c"mmap of 0x%08x failed\0A\00"
@.str.7 = [18 x i8] c"Error reading %s\0A\00"
@str = [52 x i8] c"Error: file size is not ending on a 32-bit boundary\00"
@str.8 = [23 x i8] c"Error opening /dev/mem\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %53
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %53
16: ; preds = %9
%17 = load i32, i32* @SEEK_END
%18 = tail call i32 @fseek(i32* %12, i32 0, i32 %17)
%19 = tail call i64 @ftell(i32* %12)
%20 = tail call i32 @rewind(i32* %12)
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
br label %53
26: ; preds = %16
%27 = call i64 @map_mem(i32 %22, i8** %3, i32 -1073741824, i64 %19)
%28 = load i8*, i8** %3
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 0)
br label %53
32: ; preds = %26
%33 = getelementptr i8, i8* %28, i64 %27
%34 = sub i64 0, %19
%35 = getelementptr i8, i8* %33, i64 %34
%36 = bitcast i8* %35 to i32*
%37 = and i64 %19, 3
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %32
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
br label %41
41: ; preds = %39, %32
%42 = lshr i64 %19, 2
%43 = call i64 @fread(i32* %36, i32 4, i64 %42, i32* %12)
%44 = icmp eq i64 %43, %42
br i1 %44, label %47, label %45
45: ; preds = %41
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %11)
br label %47
47: ; preds = %45, %41
%48 = call i32 @swap(i32* %36, i64 %42)
%49 = call i32 @fclose(i32* %12)
%50 = load i8*, i8** %3
%51 = call i32 @munmap(i8* %50, i64 %27)
%52 = call i32 @close(i32 %22)
br label %53
53: ; preds = %47, %30, %24, %14, %6
%54 = phi i32 [ 1, %6 ], [ 1, %24 ], [ 0, %47 ], [ 1, %30 ], [ 1, %14 ]
ret i32 %54
}