#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 __u32;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
unsigned char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                    int /*<<< orphan*/);
int /*<<< orphan*/ munmap(unsigned char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_data(char *, unsigned char *, int);
int strtoul(char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int ifdr;
  __u32 addr = 0;
  __u32 len = 0x01000000;
  unsigned char *memspace;
  __u32 *lmspace;
  if (argc < 2 || argc > 4) {
    printf("Loads tl880 memory\n");
    printf("Usage: %s mem_file [addr [len]]\n", argv[0]);
    return -1;
  }
  if (argc >= 3) {
    addr = strtoul(argv[2], NULL, 16);
  }
  if (argc == 4) {
    len = strtoul(argv[3], NULL, 16);
  }
  if ((ifdr = open("/dev/tl880/mem0", O_RDWR)) < 0) {
    perror("Unable to open /dev/tl880/mem0");
    return -1;
  }
  printf("Mapping memory space\n");
  if (!(memspace =
            mmap(0, 0x01000000, PROT_READ | PROT_WRITE, MAP_SHARED, ifdr, 0))) {
    perror("Failed to mmap card memory");
    close(ifdr);
    return -1;
  }
  lmspace = (__u32 *)memspace;
  addr &= 0xfffffffc;
  len &= 0xfffffffc;
  len = len > 4 ? len : 4;
  if (addr >= 0x01000000) {
    addr = 0;
  }
  if (addr + len > 0x01000000) {
    len = 0x01000000 - addr;
  }
  printf("Loading 0x%x bytes of %s at address 0x%x\n", len, argv[1], addr);
  read_data(argv[1], memspace + addr, len);
  printf("Unmapping memory space\n");
  munmap(memspace, 0x01000000);
  close(ifdr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1000000,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11b8 <main+0x38>
cmpl   $0x4,-0x8(%rbp)
jle    11e7 <main+0x67>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a5 <main+0x225>
cmpl   $0x3,-0x8(%rbp)
jl     120a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    122d <main+0xad>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1266 <main+0xe6>
lea    0xde0(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a5 <main+0x225>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
mov    0x2d44(%rip),%rax
or     (%rax),%edx
mov    0x2d5b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000000,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12d1 <main+0x151>
lea    0xdb2(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a5 <main+0x225>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
and    $0xfffffffc,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xfffffffc,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jle    1300 <main+0x180>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    130d <main+0x18d>
mov    $0x4,%eax
mov    %eax,-0x34(%rbp)
jmp    130d <main+0x18d>
mov    -0x34(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1000000,-0x18(%rbp)
jl     1327 <main+0x1a7>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
cmp    $0x1000000,%eax
jle    1343 <main+0x1c3>
mov    $0x1000000,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x18(%rbp),%ecx
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1030 <read_data@plt>
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1000000,%esi
call   1070 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    -0x5(%rbp),%eax
cmp    $0xfffffffc,%eax
ja     11d0 <main+0x40>
lea    0xf02(%rip),%rdi
call   1040 <puts@plt>
mov    (%r14),%rsi
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%r13d
jmp    12ef <main+0x15f>
mov    $0x1000000,%r15d
xor    %ebx,%ebx
cmp    $0x3,%ebp
jb     120e <main+0x7e>
mov    0x10(%r14),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    %eax,%ebx
and    $0xfffffffc,%ebx
cmp    $0x4,%ebp
jne    120e <main+0x7e>
mov    0x18(%r14),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    %eax,%r15d
and    $0xfffffffc,%r15d
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    0xe12(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1301 <main+0x171>
mov    %eax,%r12d
mov    %r15d,0x4(%rsp)
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2db2(%rip),%rax
mov    0x2d7b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d90(%rip),%rax
mov    (%rax),%ecx
mov    $0x1000000,%ebp
xor    %r13d,%r13d
xor    %edi,%edi
mov    $0x1000000,%esi
mov    %r12d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     1315 <main+0x185>
mov    %rax,%r15
mov    0x4(%rsp),%ecx
cmp    $0x5,%ecx
mov    $0x4,%eax
cmovge %ecx,%eax
cmp    $0x1000000,%ebx
cmovge %r13d,%ebx
lea    (%rax,%rbx,1),%ecx
sub    %ebx,%ebp
cmp    $0x1000001,%ecx
cmovl  %eax,%ebp
mov    0x8(%r14),%rdx
lea    0xda9(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%r14),%rdi
movslq %ebx,%rsi
add    %r15,%rsi
mov    %ebp,%edx
call   1030 <read_data@plt>
lea    0xdc5(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
mov    $0x1000000,%esi
call   1080 <munmap@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd19(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r13d
jmp    12ef <main+0x15f>
lea    0xd24(%rip),%rdi
call   10a0 <perror@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r13d
jmp    12ef <main+0x15f>

LLVM IR (-O0):
@.str = [20 x i8] c"Loads tl880 memory\0A\00"
@.str.1 = [33 x i8] c"Usage: %s mem_file [addr [len]]\0A\00"
@.str.2 = [16 x i8] c"/dev/tl880/mem0\00"
@O_RDWR = global i32 0
@.str.3 = [31 x i8] c"Unable to open /dev/tl880/mem0\00"
@.str.4 = [22 x i8] c"Mapping memory space\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [27 x i8] c"Failed to mmap card memory\00"
@.str.6 = [42 x i8] c"Loading 0x%x bytes of %s at address 0x%x\0A\00"
@.str.7 = [24 x i8] c"Unmapping memory space\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 i32
%9 = alloca i8*
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i32 16777216, i32* %8
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 4
br i1 %15, label %16, label %22
16: ; preds = %13, %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%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 ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %103
22: ; preds = %13
%23 = load i32, i32* %4
%24 = icmp sge i32 %23, 3
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @strtoul(i8* %28, i32* null, i32 16)
store i32 %29, i32* %7
br label %30
30: ; preds = %25, %22
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 4
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 3
%36 = load i8*, i8** %35
%37 = call i32 @strtoul(i8* %36, i32* null, i32 16)
store i32 %37, i32* %8
br label %38
38: ; preds = %33, %30
%39 = load i32, i32* @O_RDWR
%40 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %39)
store i32 %40, i32* %6
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %103
44: ; preds = %38
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @PROT_WRITE
%48 = or i32 %46, %47
%49 = load i32, i32* @MAP_SHARED
%50 = load i32, i32* %6
%51 = call i8* @mmap(i32 0, i32 16777216, i32 %48, i32 %49, i32 %50, i32 0)
store i8* %51, i8** %9
%52 = icmp ne i8* %51, null
br i1 %52, label %57, label %53
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
store i32 -1, i32* %3
br label %103
57: ; preds = %44
%58 = load i8*, i8** %9
%59 = bitcast i8* %58 to i32*
store i32* %59, i32** %10
%60 = load i32, i32* %7
%61 = and i32 %60, -4
store i32 %61, i32* %7
%62 = load i32, i32* %8
%63 = and i32 %62, -4
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = icmp sgt i32 %64, 4
br i1 %65, label %66, label %68
66: ; preds = %57
%67 = load i32, i32* %8
br label %69
68: ; preds = %57
br label %69
69: ; preds = %68, %66
%70 = phi i32 [ %67, %66 ], [ 4, %68 ]
store i32 %70, i32* %8
%71 = load i32, i32* %7
%72 = icmp sge i32 %71, 16777216
br i1 %72, label %73, label %74
73: ; preds = %69
store i32 0, i32* %7
br label %74
74: ; preds = %73, %69
%75 = load i32, i32* %7
%76 = load i32, i32* %8
%77 = add nsw i32 %75, %76
%78 = icmp sgt i32 %77, 16777216
br i1 %78, label %79, label %82
79: ; preds = %74
%80 = load i32, i32* %7
%81 = sub nsw i32 16777216, %80
store i32 %81, i32* %8
br label %82
82: ; preds = %79, %74
%83 = load i32, i32* %8
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = load i32, i32* %7
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 %83, i8* %86, i32 %87)
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 1
%91 = load i8*, i8** %90
%92 = load i8*, i8** %9
%93 = load i32, i32* %7
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds i8, i8* %92, i64 %94
%96 = load i32, i32* %8
%97 = call i32 @read_data(i8* %91, i8* %95, i32 %96)
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%99 = load i8*, i8** %9
%100 = call i32 @munmap(i8* %99, i32 16777216)
%101 = load i32, i32* %6
%102 = call i32 @close(i32 %101)
store i32 0, i32* %3
br label %103
103: ; preds = %82, %53, %42, %16
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
@.str.1 = [33 x i8] c"Usage: %s mem_file [addr [len]]\0A\00"
@.str.2 = [16 x i8] c"/dev/tl880/mem0\00"
@O_RDWR = global i32 0
@.str.3 = [31 x i8] c"Unable to open /dev/tl880/mem0\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [27 x i8] c"Failed to mmap card memory\00"
@.str.6 = [42 x i8] c"Loading 0x%x bytes of %s at address 0x%x\0A\00"
@str = [21 x i8] c"Mapping memory space\00"
@str.8 = [23 x i8] c"Unmapping memory space\00"
@str.9 = [19 x i8] c"Loads tl880 memory\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -3
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %60
9: ; preds = %2
%10 = icmp ugt i32 %0, 2
br i1 %10, label %11, label %22
11: ; preds = %9
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @strtoul(i8* nocapture %13, i32* null, i32 16)
%15 = and i32 %14, -4
%16 = icmp eq i32 %0, 4
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = tail call i32 @strtoul(i8* nocapture %19, i32* null, i32 16)
%21 = and i32 %20, -4
br label %22
22: ; preds = %9, %17, %11
%23 = phi i32 [ %15, %17 ], [ %15, %11 ], [ 0, %9 ]
%24 = phi i32 [ %21, %17 ], [ 16777216, %11 ], [ 16777216, %9 ]
%25 = load i32, i32* @O_RDWR
%26 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %60
30: ; preds = %22
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %33, %32
%35 = load i32, i32* @MAP_SHARED
%36 = tail call i8* @mmap(i32 0, i32 16777216, i32 %34, i32 %35, i32 %26, i32 0)
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%40 = tail call i32 @close(i32 %26)
br label %60
41: ; preds = %30
%42 = icmp sgt i32 %24, 4
%43 = select i1 %42, i32 %24, i32 4
%44 = icmp sgt i32 %23, 16777215
%45 = select i1 %44, i32 0, i32 %23
%46 = add nsw i32 %43, %45
%47 = icmp sgt i32 %46, 16777216
%48 = sub nsw i32 16777216, %45
%49 = select i1 %47, i32 %48, i32 %43
%50 = getelementptr inbounds i8*, i8** %1, i64 1
%51 = load i8*, i8** %50
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 %49, i8* %51, i32 %45)
%53 = load i8*, i8** %50
%54 = sext i32 %45 to i64
%55 = getelementptr inbounds i8, i8* %36, i64 %54
%56 = tail call i32 @read_data(i8* %53, i8* %55, i32 %49)
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
%58 = tail call i32 @munmap(i8* %36, i32 16777216)
%59 = tail call i32 @close(i32 %26)
br label %60
60: ; preds = %41, %38, %28, %5
%61 = phi i32 [ -1, %5 ], [ -1, %28 ], [ 0, %41 ], [ -1, %38 ]
ret i32 %61
}