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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ MS_SYNC;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ ftruncate(int, int);
int /*<<< orphan*/ memcpy(void *, char *, int);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ msync(void *, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  void *paddr;
  char buf[] = "Hello, World!\n";
  int fd;
  struct stat statbuf;
  fd = open("map.foo", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
  if (fd < 0) {
    perror("open map.foo failed.");
    return -1;
  }
  if (fstat(fd, &statbuf) < 0) {
    perror("fstat error");
    return -1;
  }
  printf("file length is %d\n", statbuf.st_size);
  ftruncate(fd, sizeof buf);
  paddr = mmap(0, sizeof buf, PROT_WRITE, MAP_SHARED, fd, 0);
  memcpy(paddr, buf, sizeof buf + 1);
  msync(paddr, sizeof buf, MS_SYNC);
  munmap(paddr, sizeof buf);
  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    0xe43(%rip),%rax
mov    %rax,-0x27(%rbp)
mov    0xe41(%rip),%eax
mov    %eax,-0x1f(%rbp)
mov    0xe3b(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xe33(%rip),%al
mov    %al,-0x19(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    0x2dc0(%rip),%rax
or     (%rax),%edx
mov    0x2ddf(%rip),%rax
or     (%rax),%edx
lea    0xdfd(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    123c <main+0x9c>
lea    0xdec(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f6 <main+0x156>
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
jge    126a <main+0xca>
lea    0xdd3(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f6 <main+0x156>
mov    -0x30(%rbp),%esi
lea    0xdc4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    $0xf,%esi
call   1060 <ftruncate@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edx
mov    0x2d4a(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    $0xf,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x27(%rbp),%rsi
mov    $0x10,%edx
call   1080 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    0x2ce3(%rip),%rax
mov    (%rax),%edx
mov    $0xf,%esi
call   1030 <msync@plt>
mov    -0x18(%rbp),%rdi
mov    $0xf,%esi
call   1090 <munmap@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movabs $0xa21646c726f57,%rax
mov    %rax,0x17(%rsp)
mov    0x2e0a(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e00(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
movabs $0x57202c6f6c6c6548,%rax
2c 20 57
mov    %rax,0x10(%rsp)
mov    0x2df7(%rip),%rax
or     (%rax),%edx
lea    0xe15(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12ac <main+0x10c>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
test   %rax,%rax
js     12b5 <main+0x115>
mov    0x8(%rsp),%esi
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    $0xf,%esi
call   1060 <ftruncate@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    0x2d96(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0xf,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
mov    $0x10,%edx
call   1080 <memcpy@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0xf,%esi
call   1030 <msync@plt>
mov    %rbp,%rdi
mov    $0xf,%esi
call   1090 <munmap@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd64(%rip),%rdi
jmp    12bc <main+0x11c>
lea    0xd70(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    12a0 <main+0x100>

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.main.buf = [15 x i8] c"Hello, World!\0A\00"
@.str = [8 x i8] c"map.foo\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.1 = [21 x i8] c"open map.foo failed.\00"
@.str.2 = [12 x i8] c"fstat error\00"
@.str.3 = [19 x i8] c"file length is %d\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MS_SYNC = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [15 x i8]
%8 = alloca i32
%9 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [15 x i8]* %7 to i8*
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRWXU
%15 = load i32, i32* @S_IRWXG
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRWXO
%18 = or i32 %16, %17
%19 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
24: ; preds = %2
%25 = load i32, i32* %8
%26 = call i64 @fstat(i32 %25, %struct.stat* %9)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
30: ; preds = %24
%31 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %8
%35 = call i32 @ftruncate(i32 %34, i32 15)
%36 = load i32, i32* @PROT_WRITE
%37 = load i32, i32* @MAP_SHARED
%38 = load i32, i32* %8
%39 = call i8* @mmap(i32 0, i32 15, i32 %36, i32 %37, i32 %38, i32 0)
store i8* %39, i8** %6
%40 = load i8*, i8** %6
%41 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%42 = call i32 @memcpy(i8* %40, i8* %41, i32 16)
%43 = load i8*, i8** %6
%44 = load i32, i32* @MS_SYNC
%45 = call i32 @msync(i8* %43, i32 15, i32 %44)
%46 = load i8*, i8** %6
%47 = call i32 @munmap(i8* %46, i32 15)
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
store i32 0, i32* %3
br label %50
50: ; preds = %30, %28, %22
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.main.buf = [15 x i8] c"Hello, World!\0A\00"
@.str = [8 x i8] c"map.foo\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.1 = [21 x i8] c"open map.foo failed.\00"
@.str.2 = [12 x i8] c"fstat error\00"
@.str.3 = [19 x i8] c"file length is %d\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MS_SYNC = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [15 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRWXU
%11 = load i32, i32* @S_IRWXG
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRWXO
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %37
19: ; preds = %2
%20 = call i64 @fstat(i32 %15, %struct.stat* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %37
24: ; preds = %19
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = tail call i32 @ftruncate(i32 %15, i32 15)
%29 = load i32, i32* @PROT_WRITE
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i8* @mmap(i32 0, i32 15, i32 %29, i32 %30, i32 %15, i32 0)
%32 = call i32 @memcpy(i8* %31, i8* %5, i32 16)
%33 = load i32, i32* @MS_SYNC
%34 = call i32 @msync(i8* %31, i32 15, i32 %33)
%35 = call i32 @munmap(i8* %31, i32 15)
%36 = call i32 @close(i32 %15)
br label %37
37: ; preds = %24, %22, %17
%38 = phi i32 [ -1, %17 ], [ -1, %22 ], [ 0, %24 ]
ret i32 %38
}