#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*/ EXIT_SUCCESS;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ STDOUT_FILENO;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpagesize();
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, *nodename = "/dev/mycdrv";
  char c[2] = "CX";
  if (argc > 1)
    nodename = argv[1];
  size = getpagesize();
  if (argc > 2)
    size = atoi(argv[2]);
  printf(" Memory Mapping Node: %s, of size %d bytes\n", nodename, size);
  if ((fd = open(nodename, O_RDWR)) < 0) {
    perror("problems opening the node ");
    exit(errno);
  };
  area = mmap(NULL, size, PROT_READ | PROT_WRITE, 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(EXIT_SUCCESS);
}


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)
lea    0xe43(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xeaa(%rip),%ax
mov    %ax,-0x3a(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11e2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10b0 <getpagesize@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    1206 <main+0x66>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xdf8(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1251 <main+0xb1>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
mov    0x2d54(%rip),%rax
or     (%rax),%edx
mov    0x2d73(%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    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
jne    12b1 <main+0x111>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   10c0 <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    1305 <main+0x165>
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    12c8 <main+0x128>
mov    0x2cdc(%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     1343 <main+0x1a3>
lea    0xd31(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movw   $0x5843,0xe(%rsp)
cmp    $0x1,%edi
jle    11db <main+0x3b>
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x8(%rsi),%rbx
xor    %eax,%eax
call   10b0 <getpagesize@plt>
cmp    $0x2,%ebp
je     11d6 <main+0x36>
mov    0x10(%r15),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
jmp    11ec <main+0x4c>
xor    %eax,%eax
call   10b0 <getpagesize@plt>
mov    %eax,%r14d
lea    0xe14(%rip),%rbx
lea    0xe19(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <printf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
jns    121f <main+0x7f>
lea    0xe1e(%rip),%rdi
jmp    12c7 <main+0x127>
mov    %eax,%ebp
mov    0x2dd0(%rip),%rax
mov    0x2d89(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da6(%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    0x2d52(%rip),%rax
cmp    (%rax),%r12
jne    1264 <main+0xc4>
lea    0xdf1(%rip),%rdi
jmp    12c7 <main+0x127>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0x2,%r14d
jl     1299 <main+0xf9>
lea    -0x1(%r14),%ebp
xor    %ebx,%ebx
lea    0xe(%rsp),%r15
nopl   0x0(%rax)
lea    (%r12,%rbx,1),%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
add    $0x2,%rbx
cmp    %ebp,%ebx
jl     1280 <main+0xe0>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
jne    12c0 <main+0x120>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xd9a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

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

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