#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ memcpy(char *, char **, int);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int fd, size, rc, j;
  char *area, *tmp;
  struct stat filestat;
  char c[2] = "CX";
  if ((fd = open(argv[1], O_RDWR)) < 0) {
    perror("problems opening the file");
    exit(errno);
  };
  if (fstat(fd, &filestat)) {
    perror(" problems fstating the file:");
    exit(errno);
  };
  size = filestat.st_size;
  printf(" Memory Mapping File: %s, of size %d bytes\n", argv[1], size);
  area = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
  if (area == MAP_FAILED) {
    perror("error mmaping");
    exit(errno);
  };
  close(fd);
  tmp = area;
  for (j = 0; j < size - 1; j += 2, tmp += 2)
    memcpy(tmp, &c, 2);
  rc = write(STDOUT_FILENO, area, size);
  if (rc != size) {
    perror("problems writing");
    exit(errno);
  };
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe53(%rip),%ax
mov    %ax,-0x3a(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ed <main+0x5d>
lea    0xe28(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
je     121d <main+0x8d>
lea    0xe12(%rip),%rdi
call   1090 <perror@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x18(%rbp),%edx
lea    0xe04(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%esi
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
mov    0x2dab(%rip),%rax
or     (%rax),%edx
mov    0x2d8a(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d39(%rip),%rcx
cmp    (%rcx),%rax
jne    129a <main+0x10a>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12ee <main+0x15e>
mov    -0x30(%rbp),%rdi
lea    -0x3a(%rbp),%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
jmp    12b1 <main+0x121>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     132c <main+0x19c>
lea    0xd5a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movw   $0x5843,0x6(%rsp)
mov    0x8(%rsi),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
jns    11c8 <main+0x38>
lea    0xe3d(%rip),%rdi
jmp    12a0 <main+0x110>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
jne    1236 <main+0xa6>
mov    0x8(%rsp),%r14d
mov    0x8(%rbx),%rsi
lea    0xe4c(%rip),%rdi
mov    %r14d,%edx
call   1050 <printf@plt>
mov    0x2dbe(%rip),%rax
mov    0x2df7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd4(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
mov    0x2d88(%rip),%rax
cmp    (%rax),%r12
jne    123f <main+0xaf>
lea    0xe2f(%rip),%rdi
jmp    12a0 <main+0x110>
lea    0xddd(%rip),%rdi
jmp    12a0 <main+0x110>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0x2,%r14d
jl     1279 <main+0xe9>
lea    -0x1(%r14),%ebp
xor    %ebx,%ebx
lea    0x6(%rsp),%r15
nopw   0x0(%rax,%rax,1)
lea    (%r12,%rbx,1),%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
add    $0x2,%rbx
cmp    %ebp,%ebx
jl     1260 <main+0xd0>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
jne    1299 <main+0x109>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdd1(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.main.c = [2 x i8] c"CX"
@O_RDWR = global i32 0
@.str = [26 x i8] c"problems opening the file\00"
@errno = global i32 0
@.str.1 = [29 x i8] c" problems fstating the file:\00"
@.str.2 = [44 x i8] c" Memory Mapping File: %s, of size %d bytes\0A\00"
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [14 x i8] c"error mmaping\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [17 x i8] c"problems writing\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca %struct.stat
%13 = alloca [2 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = bitcast [2 x i8]* %13 to i8*
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @errno
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = load i32, i32* %6
%27 = call i64 @fstat(i32 %26, %struct.stat* %12)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @errno
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %25
%34 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%35 = load i32, i32* %34
store i32 %35, i32* %7
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = load i32, i32* %7
%40 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %38, i32 %39)
%41 = load i32, i32* %7
%42 = load i32, i32* @PROT_WRITE
%43 = load i32, i32* @PROT_READ
%44 = or i32 %42, %43
%45 = load i32, i32* @MAP_SHARED
%46 = load i32, i32* %6
%47 = call i8* @mmap(i32* null, i32 %41, i32 %44, i32 %45, i32 %46, i32 0)
store i8* %47, i8** %10
%48 = load i8*, i8** %10
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp eq i8* %48, %49
br i1 %50, label %51, label %55
51: ; preds = %33
%52 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @errno
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %33
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
%58 = load i8*, i8** %10
store i8* %58, i8** %11
store i32 0, i32* %9
br label %59
59: ; preds = %68, %55
%60 = load i32, i32* %9
%61 = load i32, i32* %7
%62 = sub nsw i32 %61, 1
%63 = icmp slt i32 %60, %62
br i1 %63, label %64, label %73
64: ; preds = %59
%65 = load i8*, i8** %11
%66 = bitcast [2 x i8]* %13 to i8**
%67 = call i32 @memcpy(i8* %65, i8** %66, i32 2)
br label %68
68: ; preds = %64
%69 = load i32, i32* %9
%70 = add nsw i32 %69, 2
store i32 %70, i32* %9
%71 = load i8*, i8** %11
%72 = getelementptr inbounds i8, i8* %71, i64 2
store i8* %72, i8** %11
br label %59
73: ; preds = %59
%74 = load i32, i32* @STDOUT_FILENO
%75 = load i8*, i8** %10
%76 = load i32, i32* %7
%77 = call i32 @write(i32 %74, i8* %75, i32 %76)
store i32 %77, i32* %8
%78 = load i32, i32* %8
%79 = load i32, i32* %7
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %85
81: ; preds = %73
%82 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%83 = load i32, i32* @errno
%84 = call i32 @exit(i32 %83)
unreachable
85: ; preds = %73
%86 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@.str = [26 x i8] c"problems opening the file\00"
@errno = global i32 0
@.str.1 = [29 x i8] c" problems fstating the file:\00"
@.str.2 = [44 x i8] c" Memory Mapping File: %s, of size %d bytes\0A\00"
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [14 x i8] c"error mmaping\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [17 x i8] c"problems writing\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i16
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast i16* %4 to i8*
store i16 22595, i16* %4
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @errno
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = call i64 @fstat(i32 %10, %struct.stat* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @errno
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = load i8*, i8** %7
%27 = tail call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %26, i32 %25)
%28 = load i32, i32* @PROT_WRITE
%29 = load i32, i32* @PROT_READ
%30 = or i32 %29, %28
%31 = load i32, i32* @MAP_SHARED
%32 = tail call i8* @mmap(i32* null, i32 %25, i32 %30, i32 %31, i32 %10, i32 0)
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %39
35: ; preds = %23
%36 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* @errno
%38 = tail call i32 @exit(i32 %37)
unreachable
39: ; preds = %23
%40 = tail call i32 @close(i32 %10)
%41 = add nsw i32 %25, -1
%42 = icmp sgt i32 %25, 1
br i1 %42, label %43, label %52
43: ; preds = %39
%44 = bitcast i16* %4 to i8**
br label %45
45: ; preds = %43, %45
%46 = phi i8* [ %32, %43 ], [ %50, %45 ]
%47 = phi i32 [ 0, %43 ], [ %49, %45 ]
%48 = call i32 @memcpy(i8* %46, i8** %44, i32 2)
%49 = add nuw nsw i32 %47, 2
%50 = getelementptr inbounds i8, i8* %46, i64 2
%51 = icmp slt i32 %49, %41
br i1 %51, label %45, label %52
52: ; preds = %45, %39
%53 = load i32, i32* @STDOUT_FILENO
%54 = call i32 @write(i32 %53, i8* %32, i32 %25)
%55 = icmp eq i32 %54, %25
br i1 %55, label %60, label %56
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%58 = load i32, i32* @errno
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %52
%61 = call i32 @exit(i32 0)
unreachable
}