#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ abort();
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
void mfd_assert_read(int fd) {
  char buf[16];
  void *p;
  ssize_t l;
  l = read(fd, buf, sizeof(buf));
  if (l != sizeof(buf)) {
    printf("read() failed: %m\n");
    abort();
  }
  p = mmap(NULL, 8192, PROT_READ, MAP_PRIVATE, fd, 0);
  if (p == MAP_FAILED) {
    printf("mmap() failed: %m\n");
    abort();
  }
  munmap(p, 8192);
  p = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (p == MAP_FAILED) {
    printf("mmap() failed: %m\n");
    abort();
  }
  munmap(p, 8192);
}


x86 assembly (clang, -O0):
<mfd_assert_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    $0x10,%rax
je     1190 <mfd_assert_read+0x40>
lea    0xe7c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11e2 <mfd_assert_read+0x92>
lea    0xe3d(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x28(%rbp),%rdi
mov    $0x2000,%esi
call   1070 <munmap@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    0x2dd8(%rip),%rax
or     (%rax),%edx
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
jne    124b <mfd_assert_read+0xfb>
lea    0xdd4(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x28(%rbp),%rdi
mov    $0x2000,%esi
call   1070 <munmap@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfd_assert_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x10,%edx
call   1060 <read@plt>
cmp    $0x10,%eax
jne    11fb <mfd_assert_read+0xab>
mov    0x2e7e(%rip),%r14
mov    (%r14),%edx
mov    0x2e4c(%rip),%r15
mov    (%r15),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2e28(%rip),%r12
cmp    (%r12),%rax
je     11f2 <mfd_assert_read+0xa2>
mov    %rax,%rdi
mov    $0x2000,%esi
call   1070 <munmap@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
or     (%r14),%edx
mov    (%r15),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    (%r12),%rax
je     11f2 <mfd_assert_read+0xa2>
mov    %rax,%rdi
mov    $0x2000,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <munmap@plt>
lea    0xe1a(%rip),%rdi
jmp    1202 <mfd_assert_read+0xb2>
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"read() failed: %m\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [19 x i8] c"mmap() failed: %m\0A\00"
@PROT_WRITE = global i32 0
define void @mfd_assert_read(i32 %0) {
%2 = alloca i32
%3 = alloca [16 x i8]
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%8 = call i32 @read(i32 %6, i8* %7, i32 16)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = icmp ne i64 %10, 16
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (...) @abort()
unreachable
15: ; preds = %1
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @MAP_PRIVATE
%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 = %15
%24 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 (...) @abort()
unreachable
26: ; preds = %15
%27 = load i8*, i8** %4
%28 = call i32 @munmap(i8* %27, i32 8192)
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_PRIVATE
%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 eq i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %26
%39 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 (...) @abort()
unreachable
41: ; preds = %26
%42 = load i8*, i8** %4
%43 = call i32 @munmap(i8* %42, i32 8192)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"read() failed: %m\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [19 x i8] c"mmap() failed: %m\0A\00"
@PROT_WRITE = global i32 0
define void @mfd_assert_read(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 16)
%5 = icmp eq i32 %4, 16
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %1
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @MAP_PRIVATE
%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 = %9
%16 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 (...) @abort()
unreachable
18: ; preds = %9
%19 = tail call i32 @munmap(i8* %12, i32 8192)
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = load i32, i32* @MAP_PRIVATE
%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 %27, label %30
27: ; preds = %18
%28 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 (...) @abort()
unreachable
30: ; preds = %18
%31 = tail call i32 @munmap(i8* %24, i32 8192)
ret void
}