#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 utimbuf {
  int /*<<< orphan*/ modtime;
  int /*<<< orphan*/ actime;
};
struct stat {
  int /*<<< orphan*/ st_mtime;
  int /*<<< orphan*/ st_atime;
  scalar_t__ st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ bail(char const *, char *);
int /*<<< orphan*/ errno;
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, scalar_t__, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
void *mapfile(char const *filename, size_t *psize, struct utimbuf *utimbuf) {
  struct stat stat;
  void *ptr;
  int fd;
  fd = open(filename, O_RDWR);
  if (fd < 0)
    bail(filename, strerror(errno));
  if (fstat(fd, &stat))
    bail(filename, strerror(errno));
  if (!S_ISREG(stat.st_mode))
    bail(filename, "not an ordinary file.");
  ptr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (ptr == MAP_FAILED)
    bail(filename, strerror(errno));
  if (psize)
    *psize = (size_t)stat.st_size;
  if (utimbuf) {
    utimbuf->actime = stat.st_atime;
    utimbuf->modtime = stat.st_mtime;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<mapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11b5 <mapfile+0x55>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11ed <mapfile+0x8d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
mov    -0x20(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%eax
jne    120e <mapfile+0xae>
mov    -0x8(%rbp),%rdi
lea    0xdf7(%rip),%rsi
call   1060 <bail@plt>
mov    -0x28(%rbp),%rsi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    0x2d9e(%rip),%rax
or     (%rax),%edx
mov    0x2dbd(%rip),%rax
mov    (%rax),%ecx
mov    -0x3c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d6c(%rip),%rcx
cmp    (%rcx),%rax
jne    1277 <mapfile+0x117>
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
cmpq   $0x0,-0x10(%rbp)
je     128d <mapfile+0x12d>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     12ab <mapfile+0x14b>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11a2 <mapfile+0x42>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
test   %rax,%rax
je     11cc <mapfile+0x6c>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
mov    0x18(%rsp),%edi
call   1040 <S_ISREG@plt>
test   %eax,%eax
jne    11e8 <mapfile+0x88>
lea    0xe20(%rip),%rsi
mov    %r12,%rdi
call   1060 <bail@plt>
mov    0x10(%rsp),%rbx
mov    0x2e04(%rip),%rax
mov    0x2dc5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de2(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2d96(%rip),%rax
cmp    (%rax),%rbp
je     1253 <mapfile+0xf3>
test   %r14,%r14
je     122f <mapfile+0xcf>
mov    %rbx,(%r14)
test   %r15,%r15
je     1243 <mapfile+0xe3>
mov    0xc(%rsp),%eax
mov    %eax,0x4(%r15)
mov    0x8(%rsp),%eax
mov    %eax,(%r15)
mov    %rbp,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
test   %r14,%r14
jne    122c <mapfile+0xcc>
jmp    122f <mapfile+0xcf>

LLVM IR (-O0):
%struct.utimbuf = type { i32, i32 }
%struct.stat = type { i32, i32, i64, i32 }
@O_RDWR = global i32 0
@errno = global i32 0
@.str = [22 x i8] c"not an ordinary file.\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i8* @mapfile(i8* %0, i64* %1, %struct.utimbuf* %2) {
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.utimbuf*
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
store %struct.utimbuf* %2, %struct.utimbuf** %6
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @bail(i8* %16, i8* %18)
br label %20
20: ; preds = %15, %3
%21 = load i32, i32* %9
%22 = call i64 @fstat(i32 %21, %struct.stat* %7)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @bail(i8* %25, i8* %27)
br label %29
29: ; preds = %24, %20
%30 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 3
%31 = load i32, i32* %30
%32 = call i32 @S_ISREG(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i8*, i8** %4
%36 = call i32 @bail(i8* %35, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %34, %29
%38 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 2
%39 = load i64, i64* %38
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @PROT_WRITE
%42 = or i32 %40, %41
%43 = load i32, i32* @MAP_SHARED
%44 = load i32, i32* %9
%45 = call i8* @mmap(i32* null, i64 %39, i32 %42, i32 %43, i32 %44, i32 0)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = load i8*, i8** @MAP_FAILED
%48 = icmp eq i8* %46, %47
br i1 %48, label %49, label %54
49: ; preds = %37
%50 = load i8*, i8** %4
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 @bail(i8* %50, i8* %52)
br label %54
54: ; preds = %49, %37
%55 = load i64*, i64** %5
%56 = icmp ne i64* %55, null
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 2
%59 = load i64, i64* %58
%60 = load i64*, i64** %5
store i64 %59, i64* %60
br label %61
61: ; preds = %57, %54
%62 = load %struct.utimbuf*, %struct.utimbuf** %6
%63 = icmp ne %struct.utimbuf* %62, null
br i1 %63, label %64, label %73
64: ; preds = %61
%65 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%66 = load i32, i32* %65
%67 = load %struct.utimbuf*, %struct.utimbuf** %6
%68 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %67, i32 0, i32 1
store i32 %66, i32* %68
%69 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%70 = load i32, i32* %69
%71 = load %struct.utimbuf*, %struct.utimbuf** %6
%72 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %71, i32 0, i32 0
store i32 %70, i32* %72
br label %73
73: ; preds = %64, %61
%74 = load i8*, i8** %8
ret i8* %74
}

LLVM IR (-O2):
%struct.utimbuf = type { i32, i32 }
%struct.stat = type { i32, i32, i64, i32 }
@O_RDWR = global i32 0
@errno = global i32 0
@.str = [22 x i8] c"not an ordinary file.\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i8* @mapfile(i8* %0, i64* writeonly %1, %struct.utimbuf* writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @bail(i8* %0, i8* %11)
br label %13
13: ; preds = %9, %3
%14 = call i64 @fstat(i32 %7, %struct.stat* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @bail(i8* %0, i8* %18)
br label %20
20: ; preds = %16, %13
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 3
%22 = load i32, i32* %21
%23 = tail call i32 @S_ISREG(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @bail(i8* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 2
%29 = load i64, i64* %28
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %31, %30
%33 = load i32, i32* @MAP_SHARED
%34 = tail call i8* @mmap(i32* null, i64 %29, i32 %32, i32 %33, i32 %7, i32 0)
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %41
37: ; preds = %27
%38 = load i32, i32* @errno
%39 = tail call i8* @strerror(i32 %38)
%40 = tail call i32 @bail(i8* %0, i8* %39)
br label %41
41: ; preds = %37, %27
%42 = icmp eq i64* %1, null
br i1 %42, label %44, label %43
43: ; preds = %41
store i64 %29, i64* %1
br label %44
44: ; preds = %43, %41
%45 = icmp eq %struct.utimbuf* %2, null
br i1 %45, label %53, label %46
46: ; preds = %44
%47 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %2, i64 0, i32 1
store i32 %48, i32* %49
%50 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %2, i64 0, i32 0
store i32 %51, i32* %52
br label %53
53: ; preds = %46, %44
ret i8* %34
}