#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ mmap(char *, size_t, int, int /*<<< orphan*/, int, long);
int /*<<< orphan*/ munmap(char *, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int /*<<< orphan*/, char *, int);
int main(int argc, char *args[]) {
  int fd = open("./1.txt", O_RDWR);
  char *buffer = NULL;
  size_t len = 10;
  buffer =
      (char *)mmap(buffer, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
  if (buffer == MAP_FAILED) {
    fprintf(stderr, "map error: %s\n", strerror(errno));
    exit(1);
  }
  int i = 0;
  for (; i < len; i++) {
    if (write(STDOUT_FILENO, &buffer[i], 1) != 1) {
      fprintf(stderr, "write error: %s\n", strerror(errno));
      exit(1);
    }
  }
  buffer[2] = 'a';
  munmap(buffer, len);
  printf("\n");
  return 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    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0xa,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2dda(%rip),%rax
or     (%rax),%edx
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    123d <main+0xbd>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xdda(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    12c2 <main+0x142>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     12af <main+0x12f>
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xd77(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12b4 <main+0x134>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1244 <main+0xc4>
mov    -0x20(%rbp),%rax
movb   $0x61,0x2(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <munmap@plt>
lea    0xd48(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1080 <open@plt>
mov    0x2e58(%rip),%rcx
mov    0x2e09(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%ecx
mov    $0xa,%esi
xor    %edi,%edi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2dd8(%rip),%rax
cmp    %rbx,(%rax)
je     132d <main+0x1ad>
mov    0x2e08(%rip),%r14
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
lea    0x1(%rbx),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
lea    0x2(%rbx),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
lea    0x3(%rbx),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
lea    0x4(%rbx),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
lea    0x5(%rbx),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
lea    0x6(%rbx),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
lea    0x7(%rbx),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
lea    0x8(%rbx),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
mov    (%r14),%edi
mov    %rbx,%rsi
add    $0x9,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12fb <main+0x17b>
movb   $0x61,0x2(%rbx)
mov    $0xa,%esi
mov    %rbx,%rdi
call   1070 <munmap@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2cbe(%rip),%rax
mov    (%rax),%ebx
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xcfe(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%ebx
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xcbd(%rip),%rsi
jmp    1319 <main+0x199>

LLVM IR (-O0):
@.str = [8 x i8] c"./1.txt\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str.1 = [15 x i8] c"map error: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [17 x i8] c"write error: %s\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%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 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
store i8* null, i8** %7
store i64 10, i64* %8
%12 = load i8*, i8** %7
%13 = load i64, i64* %8
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_SHARED
%18 = load i32, i32* %6
%19 = call i64 @mmap(i8* %12, i64 %13, i32 %16, i32 %17, i32 %18, i64 0)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %2
store i32 0, i32* %9
br label %31
31: ; preds = %51, %30
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = load i64, i64* %8
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %54
36: ; preds = %31
%37 = load i32, i32* @STDOUT_FILENO
%38 = load i8*, i8** %7
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = call i32 @write(i32 %37, i8* %41, i32 1)
%43 = icmp ne i32 %42, 1
br i1 %43, label %44, label %50
44: ; preds = %36
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %36
br label %51
51: ; preds = %50
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
store i32 %53, i32* %9
br label %31
54: ; preds = %31
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i64 2
store i8 97, i8* %56
%57 = load i8*, i8** %7
%58 = load i64, i64* %8
%59 = call i32 @munmap(i8* %57, i64 %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"./1.txt\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str.1 = [15 x i8] c"map error: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [17 x i8] c"write error: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_SHARED
%9 = tail call i64 @mmap(i8* null, i64 10, i32 %7, i32 %8, i32 %4, i64 0)
%10 = inttoptr i64 %9 to i8*
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %11, %10
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @write(i32 %14, i8* %10, i32 1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %23, label %71
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %13
%24 = load i32, i32* @STDOUT_FILENO
%25 = getelementptr inbounds i8, i8* %10, i64 1
%26 = tail call i32 @write(i32 %24, i8* %25, i32 1)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %71
28: ; preds = %23
%29 = load i32, i32* @STDOUT_FILENO
%30 = getelementptr inbounds i8, i8* %10, i64 2
%31 = tail call i32 @write(i32 %29, i8* %30, i32 1)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %71
33: ; preds = %28
%34 = load i32, i32* @STDOUT_FILENO
%35 = getelementptr inbounds i8, i8* %10, i64 3
%36 = tail call i32 @write(i32 %34, i8* %35, i32 1)
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %71
38: ; preds = %33
%39 = load i32, i32* @STDOUT_FILENO
%40 = getelementptr inbounds i8, i8* %10, i64 4
%41 = tail call i32 @write(i32 %39, i8* %40, i32 1)
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %71
43: ; preds = %38
%44 = load i32, i32* @STDOUT_FILENO
%45 = getelementptr inbounds i8, i8* %10, i64 5
%46 = tail call i32 @write(i32 %44, i8* %45, i32 1)
%47 = icmp eq i32 %46, 1
br i1 %47, label %48, label %71
48: ; preds = %43
%49 = load i32, i32* @STDOUT_FILENO
%50 = getelementptr inbounds i8, i8* %10, i64 6
%51 = tail call i32 @write(i32 %49, i8* %50, i32 1)
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %71
53: ; preds = %48
%54 = load i32, i32* @STDOUT_FILENO
%55 = getelementptr inbounds i8, i8* %10, i64 7
%56 = tail call i32 @write(i32 %54, i8* %55, i32 1)
%57 = icmp eq i32 %56, 1
br i1 %57, label %58, label %71
58: ; preds = %53
%59 = load i32, i32* @STDOUT_FILENO
%60 = getelementptr inbounds i8, i8* %10, i64 8
%61 = tail call i32 @write(i32 %59, i8* %60, i32 1)
%62 = icmp eq i32 %61, 1
br i1 %62, label %63, label %71
63: ; preds = %58
%64 = load i32, i32* @STDOUT_FILENO
%65 = getelementptr inbounds i8, i8* %10, i64 9
%66 = tail call i32 @write(i32 %64, i8* %65, i32 1)
%67 = icmp eq i32 %66, 1
br i1 %67, label %68, label %71
68: ; preds = %63
store i8 97, i8* %30
%69 = tail call i32 @munmap(i8* %10, i64 10)
%70 = tail call i32 @putchar(i32 10)
ret i32 0
71: ; preds = %63, %58, %53, %48, %43, %38, %33, %28, %23, %13
%72 = load i32, i32* @stderr
%73 = load i32, i32* @errno
%74 = tail call i8* @strerror(i32 %73)
%75 = tail call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %74)
%76 = tail call i32 @exit(i32 1)
unreachable
}