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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ boot_firmware_image(int, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getpagesize();
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int readl(void *);
int /*<<< orphan*/ writel(int, void *);
int main(int argc, char *argv[]) {
  int mem_fd;
  void *lpcreg;
  uint32_t lpc_hicr7_val = ((0x30000000) | 0xe00);
  if (argc > 2) {
    printf("Usage: %s <flash image>\n", argv[0]);
    exit(1);
  }
  mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (mem_fd < 0) {
    perror("Unable to open /dev/mem");
    exit(1);
  }
  lpcreg = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd,
                0x1e789000);
  if (lpcreg == MAP_FAILED) {
    perror("Unable to map LPC register memory");
    exit(1);
  }
  if (argc == 2) {
    boot_firmware_image(mem_fd, argv[1]);
    lpc_hicr7_val = ((0x5c000000) | 0xe00);
    printf("Booting from memory after power cycle\n");
  }
  if (readl(lpcreg + 0x88) != lpc_hicr7_val) {
    printf("Resetting LPC_HICR7 to 0x%x\n", lpc_hicr7_val);
    writel(lpc_hicr7_val, lpcreg + 0x88);
  }
  printf("LPC_HICR7 = 0x%x\n", lpc_hicr7_val);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x30000e00,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11d6 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
lea    0xe39(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1217 <main+0x87>
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %eax,%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    0x2d90(%rip),%rax
or     (%rax),%edx
mov    0x2daf(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1e789000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <main+0xec>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    12ab <main+0x11b>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1050 <boot_firmware_image@plt>
movl   $0x5c000e00,-0x24(%rbp)
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
add    $0x88,%rdi
call   1060 <readl@plt>
cmp    -0x24(%rbp),%eax
je     12e8 <main+0x158>
mov    -0x24(%rbp),%esi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    $0x88,%rsi
call   1090 <writel@plt>
mov    -0x24(%rbp),%esi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
cmp    $0x3,%edi
jge    1297 <main+0xf7>
mov    %edi,%r15d
mov    0x2e1a(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe58(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12b2 <main+0x112>
mov    %eax,%r12d
xor    %eax,%eax
call   10b0 <getpagesize@plt>
mov    0x2e0a(%rip),%rcx
mov    0x2dcb(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2de8(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %r12d,%r8d
mov    $0x1e789000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2d9a(%rip),%rax
cmp    (%rax),%rbx
je     12bb <main+0x11b>
mov    $0x30000e00,%ebp
cmp    $0x2,%r15d
jne    124f <main+0xaf>
mov    0x8(%r14),%rsi
mov    %r12d,%edi
call   1060 <boot_firmware_image@plt>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5c000e00,%ebp
add    $0x88,%rbx
mov    %rbx,%rdi
call   1070 <readl@plt>
cmp    %ebp,%eax
je     127c <main+0xdc>
lea    0xdea(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10a0 <writel@plt>
lea    0xded(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd5f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd60(%rip),%rdi
jmp    12c2 <main+0x122>
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Usage: %s <flash image>\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [24 x i8] c"Unable to open /dev/mem\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [34 x i8] c"Unable to map LPC register memory\00"
@.str.4 = [39 x i8] c"Booting from memory after power cycle\0A\00"
@.str.5 = [29 x i8] c"Resetting LPC_HICR7 to 0x%x\0A\00"
@.str.6 = [18 x i8] c"LPC_HICR7 = 0x%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 805309952, i32* %8
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_SYNC
%20 = or i32 %18, %19
%21 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = call i32 (...) @getpagesize()
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_SHARED
%33 = load i32, i32* %6
%34 = call i8* @mmap(i32* null, i32 %28, i32 %31, i32 %32, i32 %33, i32 511217664)
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %27
%39 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %27
%42 = load i32, i32* %4
%43 = icmp eq i32 %42, 2
br i1 %43, label %44, label %51
44: ; preds = %41
%45 = load i32, i32* %6
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @boot_firmware_image(i32 %45, i8* %48)
store i32 1543507456, i32* %8
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %44, %41
%52 = load i8*, i8** %7
%53 = getelementptr i8, i8* %52, i64 136
%54 = call i32 @readl(i8* %53)
%55 = load i32, i32* %8
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %64
57: ; preds = %51
%58 = load i32, i32* %8
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %8
%61 = load i8*, i8** %7
%62 = getelementptr i8, i8* %61, i64 136
%63 = call i32 @writel(i32 %60, i8* %62)
br label %64
64: ; preds = %57, %51
%65 = load i32, i32* %8
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %65)
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"Usage: %s <flash image>\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [24 x i8] c"Unable to open /dev/mem\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [34 x i8] c"Unable to map LPC register memory\00"
@.str.5 = [29 x i8] c"Resetting LPC_HICR7 to 0x%x\0A\00"
@.str.6 = [18 x i8] c"LPC_HICR7 = 0x%x\0A\00"
@str = [38 x i8] c"Booting from memory after power cycle\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_SYNC
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = tail call i32 (...) @getpagesize()
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_SHARED
%23 = tail call i8* @mmap(i32* null, i32 %18, i32 %21, i32 %22, i32 %12, i32 511217664)
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = icmp eq i32 %0, 2
br i1 %30, label %31, label %36
31: ; preds = %29
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = tail call i32 @boot_firmware_image(i32 %12, i8* %33)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %31, %29
%37 = phi i32 [ 1543507456, %31 ], [ 805309952, %29 ]
%38 = getelementptr i8, i8* %23, i64 136
%39 = tail call i32 @readl(i8* %38)
%40 = icmp eq i32 %39, %37
br i1 %40, label %44, label %41
41: ; preds = %36
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%43 = tail call i32 @writel(i32 %37, i8* %38)
br label %44
44: ; preds = %41, %36
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %37)
ret i32 0
}