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

/* Variables and functions */
scalar_t__ EPERM;
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*/ printf(char *, ...);
scalar_t__ write(int, char *, int);
void mfd_fail_write(int fd) {
  ssize_t l;
  void *p;
  int r;
  l = write(fd, "data", 4);
  if (l != -EPERM) {
    printf("expected EPERM on write(), but got %d: %m\n", (int)l);
    abort();
  }
  p = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (p != MAP_FAILED) {
    printf("mmap() didn't fail as expected\n");
    abort();
  }
  p = mmap(NULL, 8192, PROT_WRITE, MAP_SHARED, fd, 0);
  if (p != MAP_FAILED) {
    printf("mmap() didn't fail as expected\n");
    abort();
  }
  p = mmap(NULL, 8192, PROT_READ, MAP_SHARED, fd, 0);
  if (p != MAP_FAILED) {
    r = mprotect(p, 8192, PROT_READ | PROT_WRITE);
    if (r >= 0) {
      printf("mmap()+mprotect() didn't fail as expected\n");
      abort();
    }
  }
  r = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 8192);
  if (r >= 0) {
    printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
    abort();
  }
}


x86 assembly (clang, -O0):
<mfd_fail_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    0xe8b(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    0x2e18(%rip),%rdx
sub    (%rdx),%rcx
cmp    %rcx,%rax
je     11b7 <mfd_fail_write+0x57>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2df9(%rip),%rax
or     (%rax),%edx
mov    0x2e18(%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,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
je     1214 <mfd_fail_write+0xb4>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
mov    0x2dc4(%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,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d6e(%rip),%rcx
cmp    (%rcx),%rax
je     1268 <mfd_fail_write+0x108>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
mov    0x2d70(%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,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
je     12ee <mfd_fail_write+0x18e>
mov    -0x18(%rbp),%rdi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
mov    0x2d05(%rip),%rax
or     (%rax),%edx
mov    $0x2000,%esi
call   1080 <mprotect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     12e9 <mfd_fail_write+0x189>
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    12ee <mfd_fail_write+0x18e>
mov    -0x4(%rbp),%edi
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
mov    0x2cc7(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
mov    $0x2000,%ecx
call   1070 <fallocate@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1331 <mfd_fail_write+0x1d1>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfd_fail_write>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edi,%ebx
lea    0xe7e(%rip),%rsi
mov    $0x4,%edx
call   1050 <write@plt>
mov    0x2e15(%rip),%rcx
mov    (%rcx),%rcx
add    %rax,%rcx
jne    126d <mfd_fail_write+0xfd>
mov    0x2e52(%rip),%r14
mov    0x2e13(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    0x2e2e(%rip),%r12
mov    (%r12),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    0x2de1(%rip),%r13
cmp    0x0(%r13),%rax
jne    1264 <mfd_fail_write+0xf4>
mov    (%r15),%edx
mov    (%r12),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
cmp    0x0(%r13),%rax
jne    1264 <mfd_fail_write+0xf4>
mov    (%r14),%edx
mov    (%r12),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
cmp    0x0(%r13),%rax
je     1236 <mfd_fail_write+0xc6>
mov    (%r15),%edx
or     (%r14),%edx
mov    %rax,%rdi
mov    $0x2000,%esi
call   1090 <mprotect@plt>
test   %eax,%eax
jns    128d <mfd_fail_write+0x11d>
mov    0x2d9b(%rip),%rax
mov    0x2d84(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
mov    $0x2000,%ecx
call   1080 <fallocate@plt>
test   %eax,%eax
jns    1284 <mfd_fail_write+0x114>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe1d(%rip),%rdi
jmp    1294 <mfd_fail_write+0x124>
lea    0xd91(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xda5(%rip),%rdi
jmp    1294 <mfd_fail_write+0x124>
lea    0xdca(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"data\00"
@EPERM = global i64 0
@.str.1 = [43 x i8] c"expected EPERM on write(), but got %d: %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 = [32 x i8] c"mmap() didn't fail as expected\0A\00"
@.str.3 = [43 x i8] c"mmap()+mprotect() didn't fail as expected\0A\00"
@FALLOC_FL_PUNCH_HOLE = global i32 0
@FALLOC_FL_KEEP_SIZE = global i32 0
@.str.4 = [47 x i8] c"fallocate(PUNCH_HOLE) didn't fail as expected\0A\00"
define void @mfd_fail_write(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i64 @write(i32 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = load i64, i64* @EPERM
%10 = sub nsw i64 0, %9
%11 = icmp ne i64 %8, %10
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i64, i64* %3
%14 = trunc i64 %13 to i32
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %1
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_SHARED
%22 = load i32, i32* %2
%23 = call i8* @mmap(i32* null, i32 8192, i32 %20, i32 %21, i32 %22, i32 0)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp ne i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %17
%31 = load i32, i32* @PROT_WRITE
%32 = load i32, i32* @MAP_SHARED
%33 = load i32, i32* %2
%34 = call i8* @mmap(i32* null, i32 8192, i32 %31, i32 %32, i32 %33, i32 0)
store i8* %34, i8** %4
%35 = load i8*, i8** %4
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp ne i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 (...) @abort()
unreachable
41: ; preds = %30
%42 = load i32, i32* @PROT_READ
%43 = load i32, i32* @MAP_SHARED
%44 = load i32, i32* %2
%45 = call i8* @mmap(i32* null, i32 8192, i32 %42, i32 %43, i32 %44, i32 0)
store i8* %45, i8** %4
%46 = load i8*, i8** %4
%47 = load i8*, i8** @MAP_FAILED
%48 = icmp ne i8* %46, %47
br i1 %48, label %49, label %61
49: ; preds = %41
%50 = load i8*, i8** %4
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @PROT_WRITE
%53 = or i32 %51, %52
%54 = call i32 @mprotect(i8* %50, i32 8192, i32 %53)
store i32 %54, i32* %5
%55 = load i32, i32* %5
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %49
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 (...) @abort()
unreachable
60: ; preds = %49
br label %61
61: ; preds = %60, %41
%62 = load i32, i32* %2
%63 = load i32, i32* @FALLOC_FL_PUNCH_HOLE
%64 = load i32, i32* @FALLOC_FL_KEEP_SIZE
%65 = or i32 %63, %64
%66 = call i32 @fallocate(i32 %62, i32 %65, i32 0, i32 8192)
store i32 %66, i32* %5
%67 = load i32, i32* %5
%68 = icmp sge i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %61
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
%71 = call i32 (...) @abort()
unreachable
72: ; preds = %61
ret void
}

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