#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 */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(unsigned long);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, unsigned long);
unsigned char *mmap(int /*<<< orphan*/ *, unsigned long, int,
                    int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(unsigned char *, unsigned long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  unsigned char *data, *blockdata;
  unsigned long size = 1024 * 1024 * 500;
  unsigned long c;
  int fd = open("./mmapdev", O_RDWR);
  if (fd < 0) {
    printf("ERROR : Failed to open mmap char device file.\n");
    printf("Create a device with $mknod ./mmapdev c <major_number> 0\n");
    printf("Major number allocated to the device can be located from the "
           "kernel log using $dmesg\n");
    return 1;
  }
  blockdata = (unsigned char *)malloc(size);
  if (!blockdata) {
    close(fd);
    printf("ERROR : Failed to allocate memory");
    return 1;
  }
  data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  printf("mmap address : %p (%lu)\n", data, size);
  memcpy(blockdata, data, size);
  for (c = 0; c < size; c++) {
    if (*(blockdata + c) != 'A')
      printf("Invalid data at %lu : %d\n", c, *(blockdata + c));
  }
  printf("Verification completed!\n");
  for (c = 0; c < size; c++) {
    *(blockdata + c) = 'B';
  }
  memcpy(data, blockdata, size);
  printf("Copy completed!\n");
  munmap(data, size);
  free(blockdata);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x1f400000,-0x20(%rbp)
1f
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11ef <main+0x6f>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1367 <main+0x1e7>
mov    -0x20(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1229 <main+0xa9>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1367 <main+0x1e7>
mov    -0x20(%rbp),%rsi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    0x2d8b(%rip),%rax
or     (%rax),%edx
mov    0x2da2(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memcpy@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    12e2 <main+0x162>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x41,%eax
je     12cc <main+0x14c>
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d1 <main+0x151>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    128b <main+0x10b>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1323 <main+0x1a3>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x42,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12f8 <main+0x178>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memcpy@plt>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     12c0 <main+0x120>
mov    %eax,%r15d
mov    $0x1f400000,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12e6 <main+0x146>
mov    %rax,%rbx
mov    0x2e13(%rip),%rax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
mov    $0x1f400000,%esi
xor    %edi,%edi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
lea    0xe17(%rip),%rdi
mov    $0x1f400000,%edx
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1f400000,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <memcpy@plt>
lea    0xe0a(%rip),%r12
xor    %ebp,%ebp
jmp    124d <main+0xad>
nop
add    $0x1,%rbp
cmp    $0x1f400000,%rbp
je     1265 <main+0xc5>
movzbl (%rbx,%rbp,1),%edx
cmp    $0x41,%edx
je     1240 <main+0xa0>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1240 <main+0xa0>
lea    0xdf3(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1f400000,%edx
mov    %rbx,%rdi
mov    $0x42,%esi
call   1070 <memset@plt>
mov    $0x1f400000,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <memcpy@plt>
lea    0xddd(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1f400000,%esi
mov    %r14,%rdi
call   10b0 <munmap@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    1301 <main+0x161>
lea    0xdc0(%rip),%rdi
call   1040 <puts@plt>
lea    0xde2(%rip),%rdi
call   1040 <puts@plt>
lea    0xe0f(%rip),%rdi
call   1040 <puts@plt>
jmp    12fc <main+0x15c>
mov    %r15d,%edi
call   1080 <close@plt>
lea    0xd15(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"./mmapdev\00"
@O_RDWR = global i32 0
@.str.1 = [47 x i8] c"ERROR : Failed to open mmap char device file.\0A\00"
@.str.2 = [58 x i8] c"Create a device with $mknod ./mmapdev c <major_number> 0\0A\00"
@.str.3 = [86 x i8] c"Major number allocated to the device can be located from the kernel log using $dmesg\0A\00"
@.str.4 = [34 x i8] c"ERROR : Failed to allocate memory\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [25 x i8] c"mmap address : %p (%lu)\0A\00"
@.str.6 = [26 x i8] c"Invalid data at %lu : %d\0A\00"
@.str.7 = [25 x i8] c"Verification completed!\0A\00"
@.str.8 = [17 x i8] c"Copy completed!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
store i64 524288000, i64* %4
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %89
15: ; preds = %0
%16 = load i64, i64* %4
%17 = call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, null
br i1 %20, label %25, label %21
21: ; preds = %15
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %89
25: ; preds = %15
%26 = load i64, i64* %4
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %27, %28
%30 = load i32, i32* @MAP_SHARED
%31 = load i32, i32* %6
%32 = call i8* @mmap(i32* null, i64 %26, i32 %29, i32 %30, i32 %31, i32 0)
store i8* %32, i8** %2
%33 = load i8*, i8** %2
%34 = load i64, i64* %4
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %33, i64 %34)
%36 = load i8*, i8** %3
%37 = load i8*, i8** %2
%38 = load i64, i64* %4
%39 = call i32 @memcpy(i8* %36, i8* %37, i64 %38)
store i64 0, i64* %5
br label %40
40: ; preds = %60, %25
%41 = load i64, i64* %5
%42 = load i64, i64* %4
%43 = icmp ult i64 %41, %42
br i1 %43, label %44, label %63
44: ; preds = %40
%45 = load i8*, i8** %3
%46 = load i64, i64* %5
%47 = getelementptr inbounds i8, i8* %45, i64 %46
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = icmp ne i32 %49, 65
br i1 %50, label %51, label %59
51: ; preds = %44
%52 = load i64, i64* %5
%53 = load i8*, i8** %3
%54 = load i64, i64* %5
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i64 %52, i32 %57)
br label %59
59: ; preds = %51, %44
br label %60
60: ; preds = %59
%61 = load i64, i64* %5
%62 = add i64 %61, 1
store i64 %62, i64* %5
br label %40
63: ; preds = %40
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
store i64 0, i64* %5
br label %65
65: ; preds = %73, %63
%66 = load i64, i64* %5
%67 = load i64, i64* %4
%68 = icmp ult i64 %66, %67
br i1 %68, label %69, label %76
69: ; preds = %65
%70 = load i8*, i8** %3
%71 = load i64, i64* %5
%72 = getelementptr inbounds i8, i8* %70, i64 %71
store i8 66, i8* %72
br label %73
73: ; preds = %69
%74 = load i64, i64* %5
%75 = add i64 %74, 1
store i64 %75, i64* %5
br label %65
76: ; preds = %65
%77 = load i8*, i8** %2
%78 = load i8*, i8** %3
%79 = load i64, i64* %4
%80 = call i32 @memcpy(i8* %77, i8* %78, i64 %79)
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%82 = load i8*, i8** %2
%83 = load i64, i64* %4
%84 = call i32 @munmap(i8* %82, i64 %83)
%85 = load i8*, i8** %3
%86 = call i32 @free(i8* %85)
%87 = load i32, i32* %6
%88 = call i32 @close(i32 %87)
store i32 0, i32* %1
br label %89
89: ; preds = %76, %21, %11
%90 = load i32, i32* %1
ret i32 %90
}

LLVM IR (-O2):
@.str = [10 x i8] c"./mmapdev\00"
@O_RDWR = global i32 0
@.str.4 = [34 x i8] c"ERROR : Failed to allocate memory\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [25 x i8] c"mmap address : %p (%lu)\0A\00"
@.str.6 = [26 x i8] c"Invalid data at %lu : %d\0A\00"
@str = [24 x i8] c"Verification completed!\00"
@str.9 = [16 x i8] c"Copy completed!\00"
@str.10 = [46 x i8] c"ERROR : Failed to open mmap char device file.\00"
@str.11 = [57 x i8] c"Create a device with $mknod ./mmapdev c <major_number> 0\00"
@str.12 = [85 x i8] c"Major number allocated to the device can be located from the kernel log using $dmesg\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.10, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.11, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([85 x i8], [85 x i8]* @str.12, i64 0, i64 0))
br label %41
8: ; preds = %0
%9 = tail call i64 @malloc(i64 524288000)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @close(i32 %2)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %41
15: ; preds = %8
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %17, %16
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32* null, i64 524288000, i32 %18, i32 %19, i32 %2, i32 0)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %20, i64 524288000)
%22 = tail call i32 @memcpy(i8* %10, i8* %20, i64 524288000)
br label %23
23: ; preds = %15, %31
%24 = phi i64 [ 0, %15 ], [ %32, %31 ]
%25 = getelementptr inbounds i8, i8* %10, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 65
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = zext i8 %26 to i32
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i64 %24, i32 %29)
br label %31
31: ; preds = %23, %28
%32 = add nuw nsw i64 %24, 1
%33 = icmp eq i64 %32, 524288000
br i1 %33, label %34, label %23
34: ; preds = %31
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%36 = tail call i32 @memcpy(i8* %20, i8* %10, i64 524288000)
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
%38 = tail call i32 @munmap(i8* %20, i64 524288000)
%39 = tail call i32 @free(i8* %10)
%40 = tail call i32 @close(i32 %2)
br label %41
41: ; preds = %34, %12, %4
%42 = phi i32 [ 1, %4 ], [ 0, %34 ], [ 1, %12 ]
ret i32 %42
}