#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 */
typedef int /*<<< orphan*/ time_t;
typedef char *caddr_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int ftruncate(int, long);
long getpagesize();
scalar_t__ isalpha(char);
char *malloc(long);
char *mmap(char *, long, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, long);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
char toupper(char);
int main() {
  int fd;
  long page_size;
  char *mem;
  page_size = getpagesize();
  fd = open("mem_mapped_file.txt", O_CREAT | O_RDWR,
            S_IRWXU | S_IRWXG | S_IRWXO);
  if (fd == -1) {
    perror("open");
    exit(1);
  }
  if (ftruncate(fd, page_size) == -1) {
    perror("ftruncate");
    exit(1);
  }
  mem = mmap((caddr_t)0, page_size, PROT_WRITE, MAP_SHARED, fd, 0);
  if (mem == (caddr_t)-1) {
    perror("mmap");
    exit(1);
  }
  char *buffer = malloc(page_size);
  time_t timestamp;
  time(&timestamp);
  sprintf(buffer, "This is just some studpid text.\nTimestamp: %ld", timestamp);
  strcpy(mem, buffer);
  int i;
  for (i = 0; i < page_size; i++) {
    if (isalpha(mem[i])) {
      mem[i] = toupper(mem[i]);
    }
    i++;
  }
  printf("Text written to the file (%d characters):\n%s\n", (int)strlen(mem),
         mem);
  munmap(mem, page_size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1100 <getpagesize@plt>
mov    %rax,-0x10(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2d9e(%rip),%rax
or     (%rax),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
mov    0x2d84(%rip),%rax
or     (%rax),%edx
mov    0x2da3(%rip),%rax
or     (%rax),%edx
lea    0xdb2(%rip),%rdi
call   10e0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1276 <main+0x76>
lea    0xdad(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12a1 <main+0xa1>
lea    0xd87(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x10(%rbp),%rsi
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
mov    0x2d2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    12f2 <main+0xf2>
lea    0xd40(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   10a0 <time@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xd12(%rip),%rsi
call   1110 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    138d <main+0x18d>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   10c0 <isalpha@plt>
cmp    $0x0,%rax
je     1376 <main+0x176>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <toupper@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    132f <main+0x12f>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xcb4(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10d0 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1100 <getpagesize@plt>
mov    0x2dde(%rip),%rcx
mov    0x2da7(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2da4(%rip),%rcx
mov    0x2d8d(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %rax,%r12
mov    0x2da7(%rip),%rax
or     (%rax),%edx
lea    0xdb6(%rip),%rdi
call   10e0 <open@plt>
cmp    $0xffffffff,%eax
je     1343 <main+0x143>
mov    %eax,%r14d
mov    %eax,%edi
mov    %r12,%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     134c <main+0x14c>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
mov    0x2d62(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1355 <main+0x155>
mov    %rax,%rbp
mov    %r12,%rdi
call   10b0 <malloc@plt>
mov    %rax,%r15
lea    0xc(%rsp),%rdi
call   10a0 <time@plt>
mov    0xc(%rsp),%edx
lea    0xd6b(%rip),%rsi
mov    %r15,%rdi
call   1110 <sprintf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
test   %r12,%r12
jle    1308 <main+0x108>
xor    %ebx,%ebx
jmp    12e9 <main+0xe9>
nopl   0x0(%rax)
add    $0x2,%rbx
cmp    %rbx,%r12
jle    1308 <main+0x108>
movsbl 0x0(%rbp,%rbx,1),%edi
call   10c0 <isalpha@plt>
test   %rax,%rax
je     12e0 <main+0xe0>
movsbl 0x0(%rbp,%rbx,1),%edi
call   1040 <toupper@plt>
mov    %al,0x0(%rbp,%rbx,1)
jmp    12e0 <main+0xe0>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0xd40(%rip),%rdi
mov    %eax,%esi
mov    %rbp,%rdx
call   1070 <printf@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10d0 <munmap@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcca(%rip),%rdi
jmp    135c <main+0x15c>
lea    0xcc6(%rip),%rdi
jmp    135c <main+0x15c>
lea    0xcc7(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"mem_mapped_file.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [47 x i8] c"This is just some studpid text.\0ATimestamp: %ld\00"
@.str.5 = [46 x i8] c"Text written to the file (%d characters):\0A%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i64 (...) @getpagesize()
store i64 %8, i64* %3
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRWXU
%13 = load i32, i32* @S_IRWXG
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRWXO
%16 = or i32 %14, %15
%17 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %0
%24 = load i32, i32* %2
%25 = load i64, i64* %3
%26 = call i32 @ftruncate(i32 %24, i64 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = load i64, i64* %3
%33 = load i32, i32* @PROT_WRITE
%34 = load i32, i32* @MAP_SHARED
%35 = load i32, i32* %2
%36 = call i8* @mmap(i8* null, i64 %32, i32 %33, i32 %34, i32 %35, i32 0)
store i8* %36, i8** %4
%37 = load i8*, i8** %4
%38 = icmp eq i8* %37, inttoptr (i64 -1 to i8*)
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %31
%43 = load i64, i64* %3
%44 = call i8* @malloc(i64 %43)
store i8* %44, i8** %5
%45 = call i32 @time(i32* %6)
%46 = load i8*, i8** %5
%47 = load i32, i32* %6
%48 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i8*, i8** %4
%50 = load i8*, i8** %5
%51 = call i32 @strcpy(i8* %49, i8* %50)
store i32 0, i32* %7
br label %52
52: ; preds = %79, %42
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = load i64, i64* %3
%56 = icmp slt i64 %54, %55
br i1 %56, label %57, label %82
57: ; preds = %52
%58 = load i8*, i8** %4
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i8, i8* %61
%63 = call i64 @isalpha(i8 signext %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %76
65: ; preds = %57
%66 = load i8*, i8** %4
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = load i8, i8* %69
%71 = call signext i8 @toupper(i8 signext %70)
%72 = load i8*, i8** %4
%73 = load i32, i32* %7
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %72, i64 %74
store i8 %71, i8* %75
br label %76
76: ; preds = %65, %57
%77 = load i32, i32* %7
%78 = add nsw i32 %77, 1
store i32 %78, i32* %7
br label %79
79: ; preds = %76
%80 = load i32, i32* %7
%81 = add nsw i32 %80, 1
store i32 %81, i32* %7
br label %52
82: ; preds = %52
%83 = load i8*, i8** %4
%84 = call i64 @strlen(i8* %83)
%85 = trunc i64 %84 to i32
%86 = load i8*, i8** %4
%87 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i32 %85, i8* %86)
%88 = load i8*, i8** %4
%89 = load i64, i64* %3
%90 = call i32 @munmap(i8* %88, i64 %89)
%91 = load i32, i32* %2
%92 = call i32 @close(i32 %91)
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"mem_mapped_file.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [47 x i8] c"This is just some studpid text.\0ATimestamp: %ld\00"
@.str.5 = [46 x i8] c"Text written to the file (%d characters):\0A%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i64 (...) @getpagesize()
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRWXU
%7 = load i32, i32* @S_IRWXG
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRWXO
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = tail call i32 @ftruncate(i32 %11, i64 %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = load i32, i32* @PROT_WRITE
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i8* @mmap(i8* null, i64 %2, i32 %23, i32 %24, i32 %11, i32 0)
%26 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = tail call i8* @malloc(i64 %2)
%32 = bitcast i32* %1 to i8*
%33 = call i32 @time(i32* %1)
%34 = load i32, i32* %1
%35 = call i32 @sprintf(i8* dereferenceable(1) %31, i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = call i32 @strcpy(i8* %25, i8* %31)
%37 = icmp sgt i64 %2, 0
br i1 %37, label %38, label %50
38: ; preds = %30, %47
%39 = phi i64 [ %48, %47 ], [ 0, %30 ]
%40 = getelementptr inbounds i8, i8* %25, i64 %39
%41 = load i8, i8* %40
%42 = call i64 @isalpha(i8 signext %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %38
%45 = load i8, i8* %40
%46 = call signext i8 @toupper(i8 signext %45)
store i8 %46, i8* %40
br label %47
47: ; preds = %44, %38
%48 = add nuw i64 %39, 2
%49 = icmp sgt i64 %2, %48
br i1 %49, label %38, label %50
50: ; preds = %47, %30
%51 = call i64 @strlen(i8* dereferenceable(1) %25)
%52 = trunc i64 %51 to i32
%53 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i32 %52, i8* %25)
%54 = call i32 @munmap(i8* %25, i64 %2)
%55 = call i32 @close(i32 %11)
ret i32 0
}