#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 ssize_t;

/* Variables and functions */
int FALLOC_FL_KEEP_SIZE;
int FALLOC_FL_PUNCH_HOLE;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ abort();
int fallocate(int, int, int /*<<< orphan*/, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int mprotect(void *, int, int);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ printf(char *);
int write(int, char *, int);
void mfd_assert_write(int fd) {
  ssize_t l;
  void *p;
  int r;
  l = write(fd, "\0\0\0\0", 4);
  if (l != 4) {
    printf("write() failed: %m\n");
    abort();
  }
  p = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (p == MAP_FAILED) {
    printf("mmap() failed: %m\n");
    abort();
  }
  *(char *)p = 0;
  munmap(p, 8192);
  p = mmap(NULL, 8192, PROT_WRITE, MAP_SHARED, fd, 0);
  if (p == MAP_FAILED) {
    printf("mmap() failed: %m\n");
    abort();
  }
  *(char *)p = 0;
  munmap(p, 8192);
  p = mmap(NULL, 8192, PROT_READ, MAP_SHARED, fd, 0);
  if (p == MAP_FAILED) {
    printf("mmap() failed: %m\n");
    abort();
  }
  r = mprotect(p, 8192, PROT_READ | PROT_WRITE);
  if (r < 0) {
    printf("mprotect() failed: %m\n");
    abort();
  }
  *(char *)p = 0;
  munmap(p, 8192);
  r = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 8192);
  if (r < 0) {
    printf("fallocate(PUNCH_HOLE) failed: %m\n");
    abort();
  }
}


x86 assembly (clang, -O0):
<mfd_assert_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    0xe7b(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11af <mfd_assert_write+0x3f>
lea    0xe62(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    0x2e01(%rip),%rax
or     (%rax),%edx
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <mfd_assert_write+0x9a>
lea    0xe1b(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x2000,%esi
call   1080 <munmap@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
mov    0x2db9(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
jne    1271 <mfd_assert_write+0x101>
lea    0xdb4(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x2000,%esi
call   1080 <munmap@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edx
mov    0x2d52(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2cfc(%rip),%rcx
cmp    (%rcx),%rax
jne    12d8 <mfd_assert_write+0x168>
lea    0xd4d(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d15(%rip),%rax
mov    (%rax),%edx
mov    0x2cd4(%rip),%rax
or     (%rax),%edx
mov    $0x2000,%esi
call   1090 <mprotect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1318 <mfd_assert_write+0x1a8>
lea    0xd20(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x2000,%esi
call   1080 <munmap@plt>
mov    -0x4(%rbp),%edi
mov    0x2ca1(%rip),%rax
mov    (%rax),%esi
mov    0x2c88(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
mov    $0x2000,%ecx
call   1070 <fallocate@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    136e <mfd_assert_write+0x1fe>
lea    0xce1(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfd_assert_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebp
lea    0xe7f(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0x4,%eax
jne    1292 <mfd_assert_write+0x122>
mov    0x2e5d(%rip),%r14
mov    0x2e1e(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    0x2e39(%rip),%rbx
mov    (%rbx),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2dee(%rip),%r12
cmp    (%r12),%rax
je     1289 <mfd_assert_write+0x119>
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    $0x2000,%esi
call   1080 <munmap@plt>
mov    (%r15),%edx
mov    (%rbx),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    (%r12),%rax
je     1289 <mfd_assert_write+0x119>
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    $0x2000,%esi
call   1080 <munmap@plt>
mov    (%r14),%edx
mov    (%rbx),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    (%r12),%rax
je     1289 <mfd_assert_write+0x119>
mov    %rax,%rbx
mov    (%r15),%edx
or     (%r14),%edx
mov    %rax,%rdi
mov    $0x2000,%esi
call   1090 <mprotect@plt>
test   %eax,%eax
js     129b <mfd_assert_write+0x12b>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
mov    $0x2000,%esi
call   1080 <munmap@plt>
mov    0x2d75(%rip),%rax
mov    0x2d5e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
mov    $0x2000,%ecx
call   1070 <fallocate@plt>
test   %eax,%eax
js     12a4 <mfd_assert_write+0x134>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd89(%rip),%rdi
jmp    12ab <mfd_assert_write+0x13b>
lea    0xd6c(%rip),%rdi
jmp    12ab <mfd_assert_write+0x13b>
lea    0xd8a(%rip),%rdi
jmp    12ab <mfd_assert_write+0x13b>
lea    0xd98(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [5 x i8] zeroinitializer
@.str.1 = [20 x i8] c"write() failed: %m\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [19 x i8] c"mmap() failed: %m\0A\00"
@.str.3 = [23 x i8] c"mprotect() failed: %m\0A\00"
@FALLOC_FL_PUNCH_HOLE = global i32 0
@FALLOC_FL_KEEP_SIZE = global i32 0
@.str.4 = [34 x i8] c"fallocate(PUNCH_HOLE) failed: %m\0A\00"
define void @mfd_assert_write(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @write(i32 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 4
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (...) @abort()
unreachable
13: ; preds = %1
%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* %2
%19 = call i8* @mmap(i32* null, i32 8192, i32 %16, i32 %17, i32 %18, i32 0)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 (...) @abort()
unreachable
26: ; preds = %13
%27 = load i8*, i8** %4
store i8 0, i8* %27
%28 = load i8*, i8** %4
%29 = call i32 @munmap(i8* %28, i32 8192)
%30 = load i32, i32* @PROT_WRITE
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* %2
%33 = call i8* @mmap(i32* null, i32 8192, i32 %30, i32 %31, i32 %32, i32 0)
store i8* %33, i8** %4
%34 = load i8*, i8** %4
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 (...) @abort()
unreachable
40: ; preds = %26
%41 = load i8*, i8** %4
store i8 0, i8* %41
%42 = load i8*, i8** %4
%43 = call i32 @munmap(i8* %42, i32 8192)
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @MAP_SHARED
%46 = load i32, i32* %2
%47 = call i8* @mmap(i32* null, i32 8192, i32 %44, i32 %45, i32 %46, i32 0)
store i8* %47, i8** %4
%48 = load i8*, i8** %4
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp eq i8* %48, %49
br i1 %50, label %51, label %54
51: ; preds = %40
%52 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%53 = call i32 (...) @abort()
unreachable
54: ; preds = %40
%55 = load i8*, i8** %4
%56 = load i32, i32* @PROT_READ
%57 = load i32, i32* @PROT_WRITE
%58 = or i32 %56, %57
%59 = call i32 @mprotect(i8* %55, i32 8192, i32 %58)
store i32 %59, i32* %5
%60 = load i32, i32* %5
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %54
%63 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 (...) @abort()
unreachable
65: ; preds = %54
%66 = load i8*, i8** %4
store i8 0, i8* %66
%67 = load i8*, i8** %4
%68 = call i32 @munmap(i8* %67, i32 8192)
%69 = load i32, i32* %2
%70 = load i32, i32* @FALLOC_FL_PUNCH_HOLE
%71 = load i32, i32* @FALLOC_FL_KEEP_SIZE
%72 = or i32 %70, %71
%73 = call i32 @fallocate(i32 %69, i32 %72, i32 0, i32 8192)
store i32 %73, i32* %5
%74 = load i32, i32* %5
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %65
%77 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%78 = call i32 (...) @abort()
unreachable
79: ; preds = %65
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] zeroinitializer
@.str.1 = [20 x i8] c"write() failed: %m\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [19 x i8] c"mmap() failed: %m\0A\00"
@.str.3 = [23 x i8] c"mprotect() failed: %m\0A\00"
@FALLOC_FL_PUNCH_HOLE = global i32 0
@FALLOC_FL_KEEP_SIZE = global i32 0
@.str.4 = [34 x i8] c"fallocate(PUNCH_HOLE) failed: %m\0A\00"
define void @mfd_assert_write(i32 %0) {
%2 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%3 = icmp eq i32 %2, 4
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %1
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i8* @mmap(i32* null, i32 8192, i32 %10, i32 %11, i32 %0, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 (...) @abort()
unreachable
18: ; preds = %7
store i8 0, i8* %12
%19 = tail call i32 @munmap(i8* %12, i32 8192)
%20 = load i32, i32* @PROT_WRITE
%21 = load i32, i32* @MAP_SHARED
%22 = tail call i8* @mmap(i32* null, i32 8192, i32 %20, i32 %21, i32 %0, i32 0)
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 (...) @abort()
unreachable
28: ; preds = %18
store i8 0, i8* %22
%29 = tail call i32 @munmap(i8* %22, i32 8192)
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @MAP_SHARED
%32 = tail call i8* @mmap(i32* null, i32 8192, i32 %30, i32 %31, i32 %0, i32 0)
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%37 = tail call i32 (...) @abort()
unreachable
38: ; preds = %28
%39 = load i32, i32* @PROT_READ
%40 = load i32, i32* @PROT_WRITE
%41 = or i32 %40, %39
%42 = tail call i32 @mprotect(i8* %32, i32 8192, i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%46 = tail call i32 (...) @abort()
unreachable
47: ; preds = %38
store i8 0, i8* %32
%48 = tail call i32 @munmap(i8* %32, i32 8192)
%49 = load i32, i32* @FALLOC_FL_PUNCH_HOLE
%50 = load i32, i32* @FALLOC_FL_KEEP_SIZE
%51 = or i32 %50, %49
%52 = tail call i32 @fallocate(i32 %0, i32 %51, i32 0, i32 8192)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%56 = tail call i32 (...) @abort()
unreachable
57: ; preds = %47
ret void
}