#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 */
unsigned char *MAP_FAILED;
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 = 4096 * 50;
  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\n");
    return 1;
  }
  data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (data == MAP_FAILED) {
    free(blockdata);
    close(fd);
    printf("ERROR : Failed to mmap memory\n");
    return 1;
  }
  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   $0x32000,-0x20(%rbp)
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    13a6 <main+0x226>
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    13a6 <main+0x226>
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),%rax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
jne    129b <main+0x11b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <main+0x226>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe61(%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    1321 <main+0x1a1>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x41,%eax
je     130b <main+0x18b>
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1310 <main+0x190>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12ca <main+0x14a>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1362 <main+0x1e2>
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    1337 <main+0x1b7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memcpy@plt>
lea    0xddd(%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     1275 <main+0xd5>
mov    %eax,%r15d
mov    $0x32000,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12f4 <main+0x154>
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    $0x32000,%esi
xor    %edi,%edi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
mov    0x2dab(%rip),%rax
cmp    (%rax),%r14
je     1305 <main+0x165>
lea    0xde5(%rip),%rdi
mov    $0x32000,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x32000,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <memcpy@plt>
lea    0xdd8(%rip),%r12
xor    %ebp,%ebp
jmp    125d <main+0xbd>
nop
add    $0x1,%rbp
cmp    $0x32000,%rbp
je     1299 <main+0xf9>
movzbl (%rbx,%rbp,1),%edx
cmp    $0x41,%edx
je     1250 <main+0xb0>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1250 <main+0xb0>
lea    0xe29(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
lea    0xe78(%rip),%rdi
jmp    131c <main+0x17c>
lea    0xdbf(%rip),%rdi
call   1040 <puts@plt>
mov    $0x32000,%edx
mov    %rbx,%rdi
mov    $0x42,%esi
call   1070 <memset@plt>
mov    $0x32000,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <memcpy@plt>
lea    0xda9(%rip),%rdi
call   1040 <puts@plt>
mov    $0x32000,%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    1326 <main+0x186>
mov    %r15d,%edi
call   1080 <close@plt>
lea    0xd3a(%rip),%rdi
jmp    131c <main+0x17c>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1080 <close@plt>
lea    0xd6b(%rip),%rdi
call   1040 <puts@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 = [35 x i8] c"ERROR : Failed to allocate memory\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [31 x i8] c"ERROR : Failed to mmap memory\0A\00"
@.str.6 = [25 x i8] c"mmap address : %p (%lu)\0A\00"
@.str.7 = [26 x i8] c"Invalid data at %lu : %d\0A\00"
@.str.8 = [25 x i8] c"Verification completed!\0A\00"
@.str.9 = [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 204800, 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 %99
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 ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %99
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 i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %42
36: ; preds = %25
%37 = load i8*, i8** %3
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %99
42: ; preds = %25
%43 = load i8*, i8** %2
%44 = load i64, i64* %4
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %43, i64 %44)
%46 = load i8*, i8** %3
%47 = load i8*, i8** %2
%48 = load i64, i64* %4
%49 = call i32 @memcpy(i8* %46, i8* %47, i64 %48)
store i64 0, i64* %5
br label %50
50: ; preds = %70, %42
%51 = load i64, i64* %5
%52 = load i64, i64* %4
%53 = icmp ult i64 %51, %52
br i1 %53, label %54, label %73
54: ; preds = %50
%55 = load i8*, i8** %3
%56 = load i64, i64* %5
%57 = getelementptr inbounds i8, i8* %55, i64 %56
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = icmp ne i32 %59, 65
br i1 %60, label %61, label %69
61: ; preds = %54
%62 = load i64, i64* %5
%63 = load i8*, i8** %3
%64 = load i64, i64* %5
%65 = getelementptr inbounds i8, i8* %63, i64 %64
%66 = load i8, i8* %65
%67 = zext i8 %66 to i32
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i64 %62, i32 %67)
br label %69
69: ; preds = %61, %54
br label %70
70: ; preds = %69
%71 = load i64, i64* %5
%72 = add i64 %71, 1
store i64 %72, i64* %5
br label %50
73: ; preds = %50
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
store i64 0, i64* %5
br label %75
75: ; preds = %83, %73
%76 = load i64, i64* %5
%77 = load i64, i64* %4
%78 = icmp ult i64 %76, %77
br i1 %78, label %79, label %86
79: ; preds = %75
%80 = load i8*, i8** %3
%81 = load i64, i64* %5
%82 = getelementptr inbounds i8, i8* %80, i64 %81
store i8 66, i8* %82
br label %83
83: ; preds = %79
%84 = load i64, i64* %5
%85 = add i64 %84, 1
store i64 %85, i64* %5
br label %75
86: ; preds = %75
%87 = load i8*, i8** %2
%88 = load i8*, i8** %3
%89 = load i64, i64* %4
%90 = call i32 @memcpy(i8* %87, i8* %88, i64 %89)
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
%92 = load i8*, i8** %2
%93 = load i64, i64* %4
%94 = call i32 @munmap(i8* %92, i64 %93)
%95 = load i8*, i8** %3
%96 = call i32 @free(i8* %95)
%97 = load i32, i32* %6
%98 = call i32 @close(i32 %97)
store i32 0, i32* %1
br label %99
99: ; preds = %86, %36, %21, %11
%100 = load i32, i32* %1
ret i32 %100
}

LLVM IR (-O2):
@.str = [10 x i8] c"./mmapdev\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.6 = [25 x i8] c"mmap address : %p (%lu)\0A\00"
@.str.7 = [26 x i8] c"Invalid data at %lu : %d\0A\00"
@str = [34 x i8] c"ERROR : Failed to allocate memory\00"
@str.10 = [24 x i8] c"Verification completed!\00"
@str.11 = [16 x i8] c"Copy completed!\00"
@str.12 = [30 x i8] c"ERROR : Failed to mmap memory\00"
@str.13 = [46 x i8] c"ERROR : Failed to open mmap char device file.\00"
@str.14 = [57 x i8] c"Create a device with $mknod ./mmapdev c <major_number> 0\00"
@str.15 = [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.13, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.14, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([85 x i8], [85 x i8]* @str.15, i64 0, i64 0))
br label %48
8: ; preds = %0
%9 = tail call i64 @malloc(i64 204800)
%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 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %48
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 204800, i32 %18, i32 %19, i32 %2, i32 0)
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %27
23: ; preds = %15
%24 = tail call i32 @free(i8* %10)
%25 = tail call i32 @close(i32 %2)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.12, i64 0, i64 0))
br label %48
27: ; preds = %15
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %20, i64 204800)
%29 = tail call i32 @memcpy(i8* %10, i8* %20, i64 204800)
br label %30
30: ; preds = %27, %38
%31 = phi i64 [ 0, %27 ], [ %39, %38 ]
%32 = getelementptr inbounds i8, i8* %10, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 65
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = zext i8 %33 to i32
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i64 %31, i32 %36)
br label %38
38: ; preds = %30, %35
%39 = add nuw nsw i64 %31, 1
%40 = icmp eq i64 %39, 204800
br i1 %40, label %41, label %30
41: ; preds = %38
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
%43 = tail call i32 @memcpy(i8* %20, i8* %10, i64 204800)
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.11, i64 0, i64 0))
%45 = tail call i32 @munmap(i8* %20, i64 204800)
%46 = tail call i32 @free(i8* %10)
%47 = tail call i32 @close(i32 %2)
br label %48
48: ; preds = %41, %23, %12, %4
%49 = phi i32 [ 1, %4 ], [ 1, %23 ], [ 0, %41 ], [ 1, %12 ]
ret i32 %49
}