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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IFMT;
int S_IFREG;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
int ftruncate(int, size_t);
void *mmap(int /*<<< orphan*/, size_t, int, int, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void *open_and_map_file(char *filename, size_t length, int flags, int prot,
                        int *fdp) {
  int fd;
  void *status;
  struct stat sbuf;
  if ((fd = open(filename, O_RDWR | O_CREAT, 0777)) < 0) {
    fprintf(stderr, "could not open file %s: %s\n", filename, strerror(errno));
    exit(-1);
  }
  fstat(fd, &sbuf);
  if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
    perror("bad filetype");
    exit(-1);
  }
  if (ftruncate(fd, length) == -1) {
    perror("ftruncate failed");
    exit(-1);
  }
  status = mmap(0, length, prot, flags, fd, 0);
  if (status == (void *)-1) {
    perror("mmap failed");
    exit(-1);
  }
  *fdp = fd;
  return status;
}


x86 assembly (clang, -O0):
<open_and_map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1208 <open_and_map_file+0x88>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe07(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10a0 <fstat@plt>
mov    -0x38(%rbp),%eax
mov    0x2daa(%rip),%rcx
and    (%rcx),%eax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%eax
je     1245 <open_and_map_file+0xc5>
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    1270 <open_and_map_file+0xf0>
lea    0xdc8(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x24(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x30(%rbp)
jne    12b4 <open_and_map_file+0x134>
lea    0xd95(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_and_map_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r13
mov    0x2e3f(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1ff,%edx
call   1060 <open@plt>
test   %eax,%eax
js     121a <open_and_map_file+0x9a>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
and    (%rsp),%eax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%eax
jne    124f <open_and_map_file+0xcf>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1258 <open_and_map_file+0xd8>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r12d,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1261 <open_and_map_file+0xe1>
mov    %ebp,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d9f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc8(%rip),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xdc6(%rip),%rdi
jmp    1268 <open_and_map_file+0xe8>
lea    0xdca(%rip),%rdi
jmp    1268 <open_and_map_file+0xe8>
lea    0xdd2(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"could not open file %s: %s\0A\00"
@errno = global i32 0
@S_IFMT = global i32 0
@S_IFREG = global i32 0
@.str.1 = [13 x i8] c"bad filetype\00"
@.str.2 = [17 x i8] c"ftruncate failed\00"
@.str.3 = [12 x i8] c"mmap failed\00"
define i8* @open_and_map_file(i8* %0, i64 %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca %struct.stat
store i8* %0, i8** %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32* %4, i32** %10
%14 = load i8*, i8** %6
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17, i32 511)
store i32 %18, i32* %11
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %5
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %6
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %24)
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %5
%28 = load i32, i32* %11
%29 = call i32 @fstat(i32 %28, %struct.stat* %13)
%30 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @S_IFMT
%33 = and i32 %31, %32
%34 = load i32, i32* @S_IFREG
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %27
%40 = load i32, i32* %11
%41 = load i64, i64* %7
%42 = call i32 @ftruncate(i32 %40, i64 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %39
%48 = load i64, i64* %7
%49 = load i32, i32* %9
%50 = load i32, i32* %8
%51 = load i32, i32* %11
%52 = call i8* @mmap(i32 0, i64 %48, i32 %49, i32 %50, i32 %51, i32 0)
store i8* %52, i8** %12
%53 = load i8*, i8** %12
%54 = icmp eq i8* %53, inttoptr (i64 -1 to i8*)
br i1 %54, label %55, label %58
55: ; preds = %47
%56 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %47
%59 = load i32, i32* %11
%60 = load i32*, i32** %10
store i32 %59, i32* %60
%61 = load i8*, i8** %12
ret i8* %61
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"could not open file %s: %s\0A\00"
@errno = global i32 0
@S_IFMT = global i32 0
@S_IFREG = global i32 0
@.str.1 = [13 x i8] c"bad filetype\00"
@.str.2 = [17 x i8] c"ftruncate failed\00"
@.str.3 = [12 x i8] c"mmap failed\00"
define i8* @open_and_map_file(i8* %0, i64 %1, i32 %2, i32 %3, i32* nocapture writeonly %4) {
%6 = alloca %struct.stat
%7 = bitcast %struct.stat* %6 to i8*
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %0, i32 %10, i32 511)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %5
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %16)
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %5
%20 = call i32 @fstat(i32 %11, %struct.stat* %6)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @S_IFMT
%24 = and i32 %23, %22
%25 = load i32, i32* @S_IFREG
%26 = icmp eq i32 %24, %25
br i1 %26, label %30, label %27
27: ; preds = %19
%28 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %19
%31 = tail call i32 @ftruncate(i32 %11, i64 %1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%35 = tail call i32 @exit(i32 -1)
unreachable
36: ; preds = %30
%37 = tail call i8* @mmap(i32 0, i64 %1, i32 %3, i32 %2, i32 %11, i32 0)
%38 = icmp eq i8* %37, inttoptr (i64 -1 to i8*)
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%41 = tail call i32 @exit(i32 -1)
unreachable
42: ; preds = %36
store i32 %11, i32* %4
ret i8* %37
}