#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*/ MAP_SHARED;
int /*<<< orphan*/ MS_SYNC;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ msync(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printargs(int, char **);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int strlen(char const *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  printargs(argc, argv);
  int fd = open("test_file", O_RDWR | O_CREAT, (mode_t)0600);
  const char *text = "hello";
  size_t textsize = strlen(text) + 1;
  lseek(fd, textsize - 1, SEEK_SET);
  write(fd, "", 1);
  char *map = mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  printf("sup: mmap at %p\n", map);
  memcpy(map, text, strlen(text));
  msync(map, textsize, MS_SYNC);
  munmap(map, textsize);
  close(fd);
  sleep(300);
  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    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <printargs@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
lea    0xe05(%rip),%rdi
mov    $0x180,%edx
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xdfb(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
call   1090 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    0xdd8(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x28(%rbp),%rsi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    0x2d4a(%rip),%rax
or     (%rax),%edx
mov    0x2d71(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1070 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xd81(%rip),%rdi
call   1080 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   10b0 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2ced(%rip),%rax
mov    (%rax),%edx
call   1050 <msync@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10c0 <munmap@plt>
mov    -0x14(%rbp),%edi
call   10a0 <close@plt>
mov    $0x12c,%edi
call   10e0 <sleep@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
call   1040 <printargs@plt>
mov    0x2dfd(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe1b(%rip),%rdi
mov    $0x180,%edx
call   10d0 <open@plt>
mov    %eax,%ebp
lea    0xe12(%rip),%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
movslq %eax,%rsi
lea    0x1(%rsi),%r14
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1090 <lseek@plt>
lea    0xe02(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x2dbf(%rip),%rax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d9d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%rbx
lea    0xdb1(%rip),%rdi
mov    %rax,%rsi
call   1080 <printf@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   10b0 <memcpy@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <msync@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10c0 <munmap@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    $0x12c,%edi
call   10e0 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"test_file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [6 x i8] c"hello\00"
@SEEK_SET = global i32 0
@.str.2 = [1 x i8] zeroinitializer
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [17 x i8] c"sup: mmap at %p\0A\00"
@MS_SYNC = global i32 0
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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = load i8**, i8*** %5
%12 = call i32 @printargs(i32 %10, i8** %11)
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %15, i32 384)
store i32 %16, i32* %6
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %7
%17 = load i8*, i8** %7
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
store i64 %20, i64* %8
%21 = load i32, i32* %6
%22 = load i64, i64* %8
%23 = sub i64 %22, 1
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %21, i64 %23, i32 %24)
%26 = load i32, i32* %6
%27 = call i32 @write(i32 %26, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i32 1)
%28 = load i64, i64* %8
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_SHARED
%33 = load i32, i32* %6
%34 = call i8* @mmap(i32 0, i64 %28, i32 %31, i32 %32, i32 %33, i32 0)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %9
%38 = load i8*, i8** %7
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
%41 = call i32 @memcpy(i8* %37, i8* %38, i32 %40)
%42 = load i8*, i8** %9
%43 = load i64, i64* %8
%44 = load i32, i32* @MS_SYNC
%45 = call i32 @msync(i8* %42, i64 %43, i32 %44)
%46 = load i8*, i8** %9
%47 = load i64, i64* %8
%48 = call i32 @munmap(i8* %46, i64 %47)
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
%51 = call i32 @sleep(i32 300)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"test_file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [6 x i8] c"hello\00"
@SEEK_SET = global i32 0
@.str.2 = [1 x i8] zeroinitializer
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [17 x i8] c"sup: mmap at %p\0A\00"
@MS_SYNC = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 @printargs(i32 %0, i8** %1)
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6, i32 384)
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = add nsw i64 %10, -1
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @lseek(i32 %7, i64 %11, i32 %12)
%14 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i32 1)
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = tail call i8* @mmap(i32 0, i64 %10, i32 %17, i32 %18, i32 %7, i32 0)
%20 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @memcpy(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @MS_SYNC
%24 = tail call i32 @msync(i8* %19, i64 %10, i32 %23)
%25 = tail call i32 @munmap(i8* %19, i64 %10)
%26 = tail call i32 @close(i32 %7)
%27 = tail call i32 @sleep(i32 300)
ret i32 0
}