#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_scr0sio_val;
  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);
  }
  lpc_scr0sio_val = readl(lpcreg + 0x170);
  lpc_scr0sio_val &= ~0xff;
  lpc_scr0sio_val |= 0x42;
  lpc_scr0sio_val &= ~(0x10 << 8);
  if (argc == 2) {
    boot_firmware_image(mem_fd, argv[1]);
    lpc_hicr7_val = ((0x5c000000) | 0xe00);
    lpc_scr0sio_val |= (0x10 << 8);
    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);
  }
  writel(0x42, lpcreg + 0x170);
  writel(lpc_scr0sio_val, lpcreg + 0x170);
  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,-0x28(%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>
mov    -0x20(%rbp),%rdi
add    $0x170,%rdi
call   1060 <readl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0xffffff00,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
or     $0x42,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0xffffefff,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    12e8 <main+0x158>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1050 <boot_firmware_image@plt>
movl   $0x5c000e00,-0x28(%rbp)
mov    -0x24(%rbp),%eax
or     $0x1000,%eax
mov    %eax,-0x24(%rbp)
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
add    $0x88,%rdi
call   1060 <readl@plt>
cmp    -0x28(%rbp),%eax
je     1325 <main+0x195>
mov    -0x28(%rbp),%esi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    $0x88,%rsi
call   1090 <writel@plt>
mov    -0x20(%rbp),%rsi
add    $0x170,%rsi
mov    $0x42,%edi
call   1090 <writel@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    $0x170,%rsi
call   1090 <writel@plt>
mov    -0x28(%rbp),%esi
lea    0xd40(%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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x3,%edi
jge    12df <main+0x13f>
mov    %edi,%r12d
mov    0x2e17(%rip),%rax
mov    0x2e08(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12fa <main+0x15a>
mov    %eax,%r13d
xor    %eax,%eax
call   10b0 <getpagesize@plt>
mov    0x2e07(%rip),%rcx
mov    0x2dc8(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2de5(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %r13d,%r8d
mov    $0x1e789000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2d97(%rip),%rax
cmp    (%rax),%rbx
je     1303 <main+0x163>
lea    0x170(%rbx),%r15
mov    %r15,%rdi
call   1070 <readl@plt>
mov    %eax,%ebp
and    $0xffffef00,%ebp
cmp    $0x2,%r12d
jne    126d <main+0xcd>
mov    0x8(%r14),%rsi
mov    %r13d,%edi
call   1060 <boot_firmware_image@plt>
or     $0x1042,%ebp
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5c000e00,%r14d
jmp    1276 <main+0xd6>
or     $0x42,%ebp
mov    $0x30000e00,%r14d
add    $0x88,%rbx
mov    %rbx,%rdi
call   1070 <readl@plt>
cmp    %r14d,%eax
je     12a6 <main+0x106>
lea    0xdc2(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   10a0 <writel@plt>
mov    $0x42,%edi
mov    %r15,%rsi
call   10a0 <writel@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   10a0 <writel@plt>
lea    0xdac(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd17(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd18(%rip),%rdi
jmp    130a <main+0x16a>
lea    0xd27(%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
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 805309952, i32* %9
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_SYNC
%21 = or i32 %19, %20
%22 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = call i32 (...) @getpagesize()
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_SHARED
%34 = load i32, i32* %6
%35 = call i8* @mmap(i32* null, i32 %29, i32 %32, i32 %33, i32 %34, i32 511217664)
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %42
39: ; preds = %28
%40 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %28
%43 = load i8*, i8** %7
%44 = getelementptr i8, i8* %43, i64 368
%45 = call i32 @readl(i8* %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = and i32 %46, -256
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = or i32 %48, 66
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = and i32 %50, -4097
store i32 %51, i32* %8
%52 = load i32, i32* %4
%53 = icmp eq i32 %52, 2
br i1 %53, label %54, label %63
54: ; preds = %42
%55 = load i32, i32* %6
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 1
%58 = load i8*, i8** %57
%59 = call i32 @boot_firmware_image(i32 %55, i8* %58)
store i32 1543507456, i32* %9
%60 = load i32, i32* %8
%61 = or i32 %60, 4096
store i32 %61, i32* %8
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %54, %42
%64 = load i8*, i8** %7
%65 = getelementptr i8, i8* %64, i64 136
%66 = call i32 @readl(i8* %65)
%67 = load i32, i32* %9
%68 = icmp ne i32 %66, %67
br i1 %68, label %69, label %76
69: ; preds = %63
%70 = load i32, i32* %9
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %70)
%72 = load i32, i32* %9
%73 = load i8*, i8** %7
%74 = getelementptr i8, i8* %73, i64 136
%75 = call i32 @writel(i32 %72, i8* %74)
br label %76
76: ; preds = %69, %63
%77 = load i8*, i8** %7
%78 = getelementptr i8, i8* %77, i64 368
%79 = call i32 @writel(i32 66, i8* %78)
%80 = load i32, i32* %8
%81 = load i8*, i8** %7
%82 = getelementptr i8, i8* %81, i64 368
%83 = call i32 @writel(i32 %80, i8* %82)
%84 = load i32, i32* %9
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %84)
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 = getelementptr i8, i8* %23, i64 368
%31 = tail call i32 @readl(i8* %30)
%32 = and i32 %31, -4352
%33 = or i32 %32, 66
%34 = icmp eq i32 %0, 2
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = getelementptr inbounds i8*, i8** %1, i64 1
%37 = load i8*, i8** %36
%38 = tail call i32 @boot_firmware_image(i32 %12, i8* %37)
%39 = or i32 %32, 4162
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %41
41: ; preds = %35, %29
%42 = phi i32 [ %39, %35 ], [ %33, %29 ]
%43 = phi i32 [ 1543507456, %35 ], [ 805309952, %29 ]
%44 = getelementptr i8, i8* %23, i64 136
%45 = tail call i32 @readl(i8* %44)
%46 = icmp eq i32 %45, %43
br i1 %46, label %50, label %47
47: ; preds = %41
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %43)
%49 = tail call i32 @writel(i32 %43, i8* %44)
br label %50
50: ; preds = %47, %41
%51 = tail call i32 @writel(i32 66, i8* %30)
%52 = tail call i32 @writel(i32 %42, i8* %30)
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %43)
ret i32 0
}