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

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fstat(int, struct stat *);
scalar_t__ ftruncate(int, size_t);
scalar_t__ lseek(int, size_t, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ report_error(char *, char *);
int /*<<< orphan*/ unlock_and_exit(int);
scalar_t__ write(int, char *, int);
void create_rw_mapping(char *filename, size_t len, int *out_fd,
                       char **out_data) {
  int fd;
  char *data;
  struct stat sb;
  fd = open(filename, O_RDWR | O_CREAT, 0600);
  if (fd < 0) {
    report_error("open", filename);
    unlock_and_exit(2);
  }
  if (fstat(fd, &sb) < 0) {
    report_error("stat", filename);
    unlock_and_exit(2);
  }
  if (sb.st_size < len) {
    if (lseek(fd, len - 1, SEEK_SET) < 0) {
      report_error("lseek", filename);
      unlock_and_exit(2);
    }
    if (write(fd, "\000", 1) < 0) {
      report_error("write", filename);
      unlock_and_exit(2);
    }
  } else if (sb.st_size > len) {
    if (ftruncate(fd, len) < 0) {
      report_error("ftruncate", filename);
      unlock_and_exit(2);
    }
  } else {
  }
  data = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (data == MAP_FAILED) {
    report_error("writer:mmap", filename);
    unlock_and_exit(2);
  }
  *out_data = data;
  *out_fd = fd;
}


x86 assembly (clang, -O0):
<create_rw_mapping>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11df <create_rw_mapping+0x5f>
mov    -0x8(%rbp),%rsi
lea    0xe30(%rip),%rdi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
jge    120f <create_rw_mapping+0x8f>
mov    -0x8(%rbp),%rsi
lea    0xe05(%rip),%rdi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1297 <create_rw_mapping+0x117>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    125a <create_rw_mapping+0xda>
mov    -0x8(%rbp),%rsi
lea    0xdbf(%rip),%rdi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
mov    -0x24(%rbp),%edi
lea    0xdc8(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1292 <create_rw_mapping+0x112>
mov    -0x8(%rbp),%rsi
lea    0xd8d(%rip),%rdi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
jmp    12e4 <create_rw_mapping+0x164>
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    12da <create_rw_mapping+0x15a>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
jge    12d5 <create_rw_mapping+0x155>
mov    -0x8(%rbp),%rsi
lea    0xd50(%rip),%rdi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
jmp    12df <create_rw_mapping+0x15f>
jmp    12df <create_rw_mapping+0x15f>
jmp    12e4 <create_rw_mapping+0x164>
mov    -0x10(%rbp),%rsi
mov    0x2d01(%rip),%rax
mov    (%rax),%edx
mov    0x2cc0(%rip),%rax
or     (%rax),%edx
mov    0x2cdf(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2c8f(%rip),%rcx
cmp    (%rcx),%rax
jne    1344 <create_rw_mapping+0x1c4>
mov    -0x8(%rbp),%rsi
lea    0xceb(%rip),%rdi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_rw_mapping>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e31(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11d3 <create_rw_mapping+0x53>
lea    0xe3f(%rip),%rdi
mov    %r12,%rsi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
js     123e <create_rw_mapping+0xbe>
cmp    %rbx,0x8(%rsp)
jae    125e <create_rw_mapping+0xde>
lea    -0x1(%rbx),%rsi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1060 <lseek@plt>
test   %rax,%rax
jns    121d <create_rw_mapping+0x9d>
lea    0xdff(%rip),%rdi
mov    %r12,%rsi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
lea    0xe08(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1288 <create_rw_mapping+0x108>
lea    0xdd4(%rip),%rdi
jmp    1276 <create_rw_mapping+0xf6>
lea    0xdc0(%rip),%rdi
mov    %r12,%rsi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
cmp    %rbx,0x8(%rsp)
jb     11eb <create_rw_mapping+0x6b>
jbe    1288 <create_rw_mapping+0x108>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <ftruncate@plt>
test   %rax,%rax
jns    1288 <create_rw_mapping+0x108>
lea    0xda0(%rip),%rdi
mov    %r12,%rsi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
mov    0x2d61(%rip),%rax
mov    0x2d22(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d3f(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2cf3(%rip),%rax
cmp    (%rax),%rbx
jne    12db <create_rw_mapping+0x15b>
lea    0xd57(%rip),%rdi
mov    %r12,%rsi
call   1080 <report_error@plt>
mov    $0x2,%edi
call   1070 <unlock_and_exit@plt>
mov    %rbx,(%r14)
mov    %ebp,(%r15)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"stat\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@.str.3 = [2 x i8] zeroinitializer
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.6 = [12 x i8] c"writer:mmap\00"
define void @create_rw_mapping(i8* %0, i64 %1, i32* %2, i8** %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.stat
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
store i8** %3, i8*** %8
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15, i32 384)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %4
%20 = load i8*, i8** %5
%21 = call i32 @report_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @unlock_and_exit(i32 2)
br label %23
23: ; preds = %19, %4
%24 = load i32, i32* %9
%25 = call i64 @fstat(i32 %24, %struct.stat* %11)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i8*, i8** %5
%29 = call i32 @report_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @unlock_and_exit(i32 2)
br label %31
31: ; preds = %27, %23
%32 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* %6
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %56
36: ; preds = %31
%37 = load i32, i32* %9
%38 = load i64, i64* %6
%39 = sub i64 %38, 1
%40 = load i32, i32* @SEEK_SET
%41 = call i64 @lseek(i32 %37, i64 %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = load i8*, i8** %5
%45 = call i32 @report_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = call i32 @unlock_and_exit(i32 2)
br label %47
47: ; preds = %43, %36
%48 = load i32, i32* %9
%49 = call i64 @write(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i8*, i8** %5
%53 = call i32 @report_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %52)
%54 = call i32 @unlock_and_exit(i32 2)
br label %55
55: ; preds = %51, %47
br label %73
56: ; preds = %31
%57 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%58 = load i64, i64* %57
%59 = load i64, i64* %6
%60 = icmp ugt i64 %58, %59
br i1 %60, label %61, label %71
61: ; preds = %56
%62 = load i32, i32* %9
%63 = load i64, i64* %6
%64 = call i64 @ftruncate(i32 %62, i64 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %61
%67 = load i8*, i8** %5
%68 = call i32 @report_error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %67)
%69 = call i32 @unlock_and_exit(i32 2)
br label %70
70: ; preds = %66, %61
br label %72
71: ; preds = %56
br label %72
72: ; preds = %71, %70
br label %73
73: ; preds = %72, %55
%74 = load i64, i64* %6
%75 = load i32, i32* @PROT_READ
%76 = load i32, i32* @PROT_WRITE
%77 = or i32 %75, %76
%78 = load i32, i32* @MAP_SHARED
%79 = load i32, i32* %9
%80 = call i8* @mmap(i32 0, i64 %74, i32 %77, i32 %78, i32 %79, i32 0)
store i8* %80, i8** %10
%81 = load i8*, i8** %10
%82 = load i8*, i8** @MAP_FAILED
%83 = icmp eq i8* %81, %82
br i1 %83, label %84, label %88
84: ; preds = %73
%85 = load i8*, i8** %5
%86 = call i32 @report_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %85)
%87 = call i32 @unlock_and_exit(i32 2)
br label %88
88: ; preds = %84, %73
%89 = load i8*, i8** %10
%90 = load i8**, i8*** %8
store i8* %89, i8** %90
%91 = load i32, i32* %9
%92 = load i32*, i32** %7
store i32 %91, i32* %92
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"stat\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@.str.3 = [2 x i8] zeroinitializer
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.6 = [12 x i8] c"writer:mmap\00"
define void @create_rw_mapping(i8* %0, i64 %1, i32* nocapture writeonly %2, i8** nocapture writeonly %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %9, i32 384)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = tail call i32 @report_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = tail call i32 @unlock_and_exit(i32 2)
br label %15
15: ; preds = %12, %4
%16 = call i64 @fstat(i32 %10, %struct.stat* %5)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @report_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%20 = tail call i32 @unlock_and_exit(i32 2)
br label %21
21: ; preds = %18, %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp ult i64 %23, %1
br i1 %24, label %25, label %36
25: ; preds = %21
%26 = add i64 %1, -1
%27 = load i32, i32* @SEEK_SET
%28 = tail call i64 @lseek(i32 %10, i64 %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = tail call i32 @report_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%32 = tail call i32 @unlock_and_exit(i32 2)
br label %33
33: ; preds = %30, %25
%34 = tail call i64 @write(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%35 = icmp slt i64 %34, 0
br i1 %35, label %41, label %45
36: ; preds = %21
%37 = icmp ugt i64 %23, %1
br i1 %37, label %38, label %45
38: ; preds = %36
%39 = tail call i64 @ftruncate(i32 %10, i64 %1)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %38, %33
%42 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), %33 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %38 ]
%43 = tail call i32 @report_error(i8* %42, i8* %0)
%44 = tail call i32 @unlock_and_exit(i32 2)
br label %45
45: ; preds = %41, %38, %36, %33
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @PROT_WRITE
%48 = or i32 %47, %46
%49 = load i32, i32* @MAP_SHARED
%50 = tail call i8* @mmap(i32 0, i64 %1, i32 %48, i32 %49, i32 %10, i32 0)
%51 = load i8*, i8** @MAP_FAILED
%52 = icmp eq i8* %50, %51
br i1 %52, label %53, label %56
53: ; preds = %45
%54 = tail call i32 @report_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %0)
%55 = tail call i32 @unlock_and_exit(i32 2)
br label %56
56: ; preds = %53, %45
store i8* %50, i8** %3
store i32 %10, i32* %2
ret void
}