#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 */

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
size_t getpagesize();
int /*<<< orphan*/ memcpy(void *, void *, unsigned int);
void *mmap(int /*<<< orphan*/ *, unsigned int, int, int /*<<< orphan*/, int,
           int);
int /*<<< orphan*/ perror(char *);
void copy_flash_img(int mem_fd, int flash_fd, unsigned int size) {
  static void *memimg, *fwimg;
  size_t pagesize = getpagesize();
  memimg = mmap(NULL, ((size / pagesize) + 1) * pagesize,
                PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, (0x5c000000));
  if (memimg == MAP_FAILED) {
    perror("Unable to map image destination memory");
    exit(1);
  }
  fwimg = mmap(NULL, size, PROT_READ, MAP_SHARED, flash_fd, 0);
  if (fwimg == MAP_FAILED) {
    perror("Unable to open image source memory");
    exit(1);
  }
  memcpy(memimg, fwimg, size);
}


x86 assembly (clang, -O0):
<copy_flash_img>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
add    $0x1,%rax
imul   -0x18(%rbp),%rax
mov    %eax,%esi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    0x2e3e(%rip),%rax
or     (%rax),%edx
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x5c000000,%r9d
call   1030 <mmap@plt>
mov    %rax,0x2e99(%rip)
mov    0x2e92(%rip),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
jne    11e4 <copy_flash_img+0x94>
lea    0xe2b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,0x2e48(%rip)
mov    0x2e41(%rip),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    123d <copy_flash_img+0xed>
lea    0xdf9(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e0c(%rip),%rdi
mov    0x2e0d(%rip),%rsi
mov    -0xc(%rbp),%edx
call   1040 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_flash_img>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
mov    %edi,%r15d
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %rax,%rsi
mov    %ebx,%eax
cmp    %rsi,%rax
jb     117c <copy_flash_img+0x2c>
xor    %edx,%edx
div    %esi
mov    %eax,%ebp
add    $0x1,%ebp
imul   %ebp,%esi
mov    0x2e6f(%rip),%r12
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
or     (%r12),%edx
mov    0x2e4b(%rip),%r13
mov    0x0(%r13),%ecx
xor    %edi,%edi
mov    %r15d,%r8d
mov    $0x5c000000,%r9d
call   1030 <mmap@plt>
mov    %rax,0x2e98(%rip)
mov    0x2e09(%rip),%rbp
cmp    0x0(%rbp),%rax
je     1208 <copy_flash_img+0xb8>
mov    (%r12),%edx
mov    0x0(%r13),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,0x2e75(%rip)
cmp    0x0(%rbp),%rax
je     1211 <copy_flash_img+0xc1>
mov    0x2e60(%rip),%rdi
mov    %rax,%rsi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <memcpy@plt>
lea    0xdf1(%rip),%rdi
jmp    1218 <copy_flash_img+0xc8>
lea    0xe0f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@copy_flash_img.memimg = internal global i8* null
@copy_flash_img.fwimg = internal global i8* null
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [39 x i8] c"Unable to map image destination memory\00"
@.str.1 = [35 x i8] c"Unable to open image source memory\00"
define void @copy_flash_img(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = call i64 (...) @getpagesize()
store i64 %8, i64* %7
%9 = load i32, i32* %6
%10 = zext i32 %9 to i64
%11 = load i64, i64* %7
%12 = udiv i64 %10, %11
%13 = add i64 %12, 1
%14 = load i64, i64* %7
%15 = mul i64 %13, %14
%16 = trunc i64 %15 to i32
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* %4
%22 = call i8* @mmap(i32* null, i32 %16, i32 %19, i32 %20, i32 %21, i32 1543503872)
store i8* %22, i8** @copy_flash_img.memimg
%23 = load i8*, i8** @copy_flash_img.memimg
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %29
26: ; preds = %3
%27 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %3
%30 = load i32, i32* %6
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @MAP_SHARED
%33 = load i32, i32* %5
%34 = call i8* @mmap(i32* null, i32 %30, i32 %31, i32 %32, i32 %33, i32 0)
store i8* %34, i8** @copy_flash_img.fwimg
%35 = load i8*, i8** @copy_flash_img.fwimg
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %29
%42 = load i8*, i8** @copy_flash_img.memimg
%43 = load i8*, i8** @copy_flash_img.fwimg
%44 = load i32, i32* %6
%45 = call i32 @memcpy(i8* %42, i8* %43, i32 %44)
ret void
}

LLVM IR (-O2):
@copy_flash_img.memimg = internal unnamed_addr global i8* null
@copy_flash_img.fwimg = internal unnamed_addr global i8* null
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [39 x i8] c"Unable to map image destination memory\00"
@.str.1 = [35 x i8] c"Unable to open image source memory\00"
define void @copy_flash_img(i32 %0, i32 %1, i32 %2) {
%4 = tail call i64 (...) @getpagesize()
%5 = zext i32 %2 to i64
%6 = udiv i64 %5, %4
%7 = add nuw nsw i64 %6, 1
%8 = mul i64 %7, %4
%9 = trunc i64 %8 to i32
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i8* @mmap(i32* null, i32 %9, i32 %12, i32 %13, i32 %0, i32 1543503872)
store i8* %14, i8** @copy_flash_img.memimg
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp eq i8* %14, %15
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @MAP_SHARED
%23 = tail call i8* @mmap(i32* null, i32 %2, i32 %21, i32 %22, i32 %1, i32 0)
store i8* %23, i8** @copy_flash_img.fwimg
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = load i8*, i8** @copy_flash_img.memimg
%31 = tail call i32 @memcpy(i8* %30, i8* %23, i32 %2)
ret void
}