#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*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int lseek(int, int, int /*<<< orphan*/);
int *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int munmap(int *, int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int i;
  int fd;
  int result;
  int *map;
  fd = open("/home/george/Documents/C/mmapped.bin", O_RDWR | O_CREAT | O_TRUNC,
            (mode_t)0600);
  if (fd == -1) {
    perror("Error opening file for writing");
    exit(EXIT_FAILURE);
  }
  result = lseek(fd, ((10) * sizeof(int)) - 1, SEEK_SET);
  if (result == -1) {
    close(fd);
    perror("Error calling lseek() to 'stretch' the file");
    exit(EXIT_FAILURE);
  }
  result = write(fd, "", 1);
  if (result != 1) {
    close(fd);
    perror("Error writing last byte of the file");
    exit(EXIT_FAILURE);
  }
  map =
      mmap(0, ((10) * sizeof(int)), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED) {
    close(fd);
    perror("Error mmapping the file");
    exit(EXIT_FAILURE);
  }
  for (i = 1; i <= (10); ++i) {
    map[i] = 2 * i;
  }
  if (munmap(map, ((10) * sizeof(int))) == -1) {
    perror("Error un-mmapping the file");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e9 <main+0x69>
lea    0xe4f(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    $0x27,%esi
call   1050 <lseek@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    122e <main+0xae>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    0xe37(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
je     1271 <main+0xf1>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xe12(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
mov    0x2d27(%rip),%rax
or     (%rax),%edx
mov    0x2d4e(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x28,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%rax
jne    12da <main+0x15a>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xdcd(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jg     130a <main+0x18a>
mov    -0x14(%rbp),%edx
shl    $0x1,%edx
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12e1 <main+0x161>
mov    -0x28(%rbp),%rdi
mov    $0x28,%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    132d <main+0x1ad>
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e40(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
lea    0xe7d(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     127a <main+0xfa>
mov    %eax,%ebx
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x27,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0x103>
lea    0xeb2(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    1293 <main+0x113>
mov    0x2def(%rip),%rax
mov    0x2da8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dcd(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x28,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rax
je     12a3 <main+0x123>
movaps 0xdce(%rip),%xmm0
movups %xmm0,0x4(%rax)
movaps 0xdd3(%rip),%xmm0
movups %xmm0,0x14(%rax)
movabs $0x1400000012,%rcx
mov    %rcx,0x24(%rax)
mov    %rax,%rdi
mov    $0x28,%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
je     126c <main+0xec>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe59(%rip),%rdi
call   1090 <perror@plt>
jmp    1261 <main+0xe1>
lea    0xdc4(%rip),%rdi
jmp    12b1 <main+0x131>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xdd3(%rip),%rdi
jmp    12b1 <main+0x131>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xdef(%rip),%rdi
jmp    12b1 <main+0x131>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe03(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"/home/george/Documents/C/mmapped.bin\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [31 x i8] c"Error opening file for writing\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [44 x i8] c"Error calling lseek() to 'stretch' the file\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [36 x i8] c"Error writing last byte of the file\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.5 = [24 x i8] c"Error mmapping the file\00"
@.str.6 = [27 x i8] c"Error un-mmapping the file\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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %14, i32 384)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i32, i32* %7
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %23, i32 39, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
%31 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %22
%35 = load i32, i32* %7
%36 = call i32 @write(i32 %35, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i32 1)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %45
39: ; preds = %34
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
%42 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %34
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @PROT_WRITE
%48 = or i32 %46, %47
%49 = load i32, i32* @MAP_SHARED
%50 = load i32, i32* %7
%51 = call i32* @mmap(i32 0, i32 40, i32 %48, i32 %49, i32 %50, i32 0)
store i32* %51, i32** %9
%52 = load i32*, i32** %9
%53 = load i32*, i32** @MAP_FAILED
%54 = icmp eq i32* %52, %53
br i1 %54, label %55, label %61
55: ; preds = %45
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
%58 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %45
store i32 1, i32* %6
br label %62
62: ; preds = %72, %61
%63 = load i32, i32* %6
%64 = icmp sle i32 %63, 10
br i1 %64, label %65, label %75
65: ; preds = %62
%66 = load i32, i32* %6
%67 = mul nsw i32 2, %66
%68 = load i32*, i32** %9
%69 = load i32, i32* %6
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32, i32* %68, i64 %70
store i32 %67, i32* %71
br label %72
72: ; preds = %65
%73 = load i32, i32* %6
%74 = add nsw i32 %73, 1
store i32 %74, i32* %6
br label %62
75: ; preds = %62
%76 = load i32*, i32** %9
%77 = call i32 @munmap(i32* %76, i32 40)
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %81
79: ; preds = %75
%80 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %81
81: ; preds = %79, %75
%82 = load i32, i32* %7
%83 = call i32 @close(i32 %82)
ret i32 0
}

LLVM IR (-O2):
@.str = [37 x i8] c"/home/george/Documents/C/mmapped.bin\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [31 x i8] c"Error opening file for writing\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [44 x i8] c"Error calling lseek() to 'stretch' the file\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [36 x i8] c"Error writing last byte of the file\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.5 = [24 x i8] c"Error mmapping the file\00"
@.str.6 = [27 x i8] c"Error un-mmapping the file\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %7, i32 384)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %8, i32 39, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = tail call i32 @close(i32 %8)
%20 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i32 1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = tail call i32 @close(i32 %8)
%28 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %23
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %33, %32
%35 = load i32, i32* @MAP_SHARED
%36 = tail call i32* @mmap(i32 0, i32 40, i32 %34, i32 %35, i32 %8, i32 0)
%37 = load i32*, i32** @MAP_FAILED
%38 = icmp eq i32* %36, %37
br i1 %38, label %48, label %39
39: ; preds = %31
%40 = getelementptr inbounds i32, i32* %36, i64 1
%41 = bitcast i32* %40 to <4 x i32>*
store <4 x i32> <i32 2, i32 4, i32 6, i32 8>, <4 x i32>* %41
%42 = getelementptr inbounds i32, i32* %36, i64 5
%43 = bitcast i32* %42 to <4 x i32>*
store <4 x i32> <i32 10, i32 12, i32 14, i32 16>, <4 x i32>* %43
%44 = getelementptr inbounds i32, i32* %36, i64 9
store i32 18, i32* %44
%45 = getelementptr inbounds i32, i32* %36, i64 10
store i32 20, i32* %45
%46 = tail call i32 @munmap(i32* %36, i32 40)
%47 = icmp eq i32 %46, -1
br i1 %47, label %53, label %55
48: ; preds = %31
%49 = tail call i32 @close(i32 %8)
%50 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = tail call i32 @exit(i32 %51)
unreachable
53: ; preds = %39
%54 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %39
%56 = tail call i32 @close(i32 %8)
ret i32 0
}