#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 /*<<< orphan*/ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, char const *, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/, int /*<<< orphan*/, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_mem_segment(void *, int /*<<< orphan*/);
int /*<<< orphan*/ readfile(int);
int /*<<< orphan*/ strlen(char const *);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("%s file\n", argv[0]);
    exit(127);
  }
  const char *filename = argv[1];
  int fd = open(filename, O_RDWR);
  if (fd < 0) {
    perror("open");
    exit(127);
  }
  struct stat st;
  if (fstat(fd, &st) < 0) {
    perror("stat");
    exit(127);
  }
  void *fmap = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (fmap == MAP_FAILED) {
    perror("mmap");
    exit(127);
  }
  printf("Reading memory area before modification\n");
  read_mem_segment(fmap, st.st_size);
  const char *txt = "DATA IS OVERWRITTEN HERE!";
  if (ftruncate(fd, strlen(txt)) < 0) {
    perror("ftruncate");
    exit(127);
  }
  memcpy(fmap, txt, strlen(txt));
  printf("\n\nReading memory area after modification\n");
  read_mem_segment(fmap, strlen(txt));
  printf("\n\nRead file after modification\n");
  readfile(fd);
  munmap(fmap, st.st_size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ff <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1240 <main+0x80>
lea    0xdd8(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10e0 <fstat@plt>
cmp    $0x0,%rax
jge    126c <main+0xac>
lea    0xdb1(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%esi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
mov    0x2d49(%rip),%rax
or     (%rax),%edx
mov    0x2d60(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d18(%rip),%rcx
cmp    (%rcx),%rax
jne    12c7 <main+0x107>
lea    0xd5b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   10a0 <read_mem_segment@plt>
lea    0xd59(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
jge    1325 <main+0x165>
lea    0xd45(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
call   10a0 <read_mem_segment@plt>
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   10d0 <readfile@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1080 <munmap@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    12eb <main+0x11b>
mov    0x8(%rsi),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     1306 <main+0x136>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10f0 <fstat@plt>
test   %rax,%rax
js     130f <main+0x13f>
mov    (%rsp),%r14d
mov    0x2dde(%rip),%rax
mov    0x2da7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
mov    0x2d78(%rip),%rax
cmp    (%rax),%rbp
je     1318 <main+0x148>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   10b0 <read_mem_segment@plt>
lea    0xda9(%rip),%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1070 <ftruncate@plt>
test   %rax,%rax
js     1321 <main+0x151>
lea    0xd8b(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1080 <memcpy@plt>
lea    0xdbb(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   10b0 <read_mem_segment@plt>
lea    0xdc6(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   10e0 <readfile@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1090 <munmap@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsi),%rsi
lea    0xd0b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x7f,%edi
call   10d0 <exit@plt>
lea    0xcfc(%rip),%rdi
jmp    1328 <main+0x158>
lea    0xcf8(%rip),%rdi
jmp    1328 <main+0x158>
lea    0xcf4(%rip),%rdi
jmp    1328 <main+0x158>
lea    0xd0a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x7f,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"%s file\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"stat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [41 x i8] c"Reading memory area before modification\0A\00"
@.str.5 = [26 x i8] c"DATA IS OVERWRITTEN HERE!\00"
@.str.6 = [10 x i8] c"ftruncate\00"
@.str.7 = [42 x i8] c"\0A\0AReading memory area after modification\0A\00"
@.str.8 = [32 x i8] c"\0A\0ARead file after modification\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 127)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 127)
unreachable
31: ; preds = %19
%32 = load i32, i32* %7
%33 = call i64 @fstat(i32 %32, %struct.stat* %8)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 127)
unreachable
38: ; preds = %31
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_WRITE
%43 = or i32 %41, %42
%44 = load i32, i32* @MAP_SHARED
%45 = load i32, i32* %7
%46 = call i8* @mmap(i32 0, i32 %40, i32 %43, i32 %44, i32 %45, i32 0)
store i8* %46, i8** %9
%47 = load i8*, i8** %9
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp eq i8* %47, %48
br i1 %49, label %50, label %53
50: ; preds = %38
%51 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 127)
unreachable
53: ; preds = %38
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%55 = load i8*, i8** %9
%56 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @read_mem_segment(i8* %55, i32 %57)
store i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8** %10
%59 = load i32, i32* %7
%60 = load i8*, i8** %10
%61 = call i32 @strlen(i8* %60)
%62 = call i64 @ftruncate(i32 %59, i32 %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %53
%65 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%66 = call i32 @exit(i32 127)
unreachable
67: ; preds = %53
%68 = load i8*, i8** %9
%69 = load i8*, i8** %10
%70 = load i8*, i8** %10
%71 = call i32 @strlen(i8* %70)
%72 = call i32 @memcpy(i8* %68, i8* %69, i32 %71)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0))
%74 = load i8*, i8** %9
%75 = load i8*, i8** %10
%76 = call i32 @strlen(i8* %75)
%77 = call i32 @read_mem_segment(i8* %74, i32 %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
%79 = load i32, i32* %7
%80 = call i32 @readfile(i32 %79)
%81 = load i8*, i8** %9
%82 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%83 = load i32, i32* %82
%84 = call i32 @munmap(i8* %81, i32 %83)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"%s file\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"stat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.5 = [26 x i8] c"DATA IS OVERWRITTEN HERE!\00"
@.str.6 = [10 x i8] c"ftruncate\00"
@str = [40 x i8] c"Reading memory area before modification\00"
@str.9 = [41 x i8] c"\0A\0AReading memory area after modification\00"
@str.10 = [31 x i8] c"\0A\0ARead file after modification\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 127)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 127)
unreachable
18: ; preds = %9
%19 = bitcast %struct.stat* %3 to i8*
%20 = call i64 @fstat(i32 %13, %struct.stat* %3)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 127)
unreachable
25: ; preds = %18
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @PROT_WRITE
%30 = or i32 %29, %28
%31 = load i32, i32* @MAP_SHARED
%32 = tail call i8* @mmap(i32 0, i32 %27, i32 %30, i32 %31, i32 %13, i32 0)
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i32 @exit(i32 127)
unreachable
38: ; preds = %25
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%40 = tail call i32 @read_mem_segment(i8* %32, i32 %27)
%41 = tail call i32 @strlen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%42 = tail call i64 @ftruncate(i32 %13, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%46 = tail call i32 @exit(i32 127)
unreachable
47: ; preds = %38
%48 = tail call i32 @strlen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%49 = tail call i32 @memcpy(i8* %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %48)
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.9, i64 0, i64 0))
%51 = tail call i32 @strlen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%52 = tail call i32 @read_mem_segment(i8* %32, i32 %51)
%53 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
%54 = tail call i32 @readfile(i32 %13)
%55 = tail call i32 @munmap(i8* %32, i32 %27)
ret i32 0
}