#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;
  int /*<<< orphan*/ 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*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
void *mapfile(char const *filename, 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 (utimbuf) {
    utimbuf->actime = stat.st_atime;
    utimbuf->modtime = stat.st_mtime;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<mapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11b1 <mapfile+0x51>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11e9 <mapfile+0x89>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
mov    -0x14(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%eax
jne    120a <mapfile+0xaa>
mov    -0x8(%rbp),%rdi
lea    0xdfb(%rip),%rsi
call   1060 <bail@plt>
mov    -0x18(%rbp),%esi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
mov    0x2da3(%rip),%rax
or     (%rax),%edx
mov    0x2dc2(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
jne    1272 <mapfile+0x112>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
cmpq   $0x0,-0x10(%rbp)
je     1290 <mapfile+0x130>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    119b <mapfile+0x3b>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
test   %rax,%rax
je     11c3 <mapfile+0x63>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
mov    0xc(%rsp),%edi
call   1040 <S_ISREG@plt>
test   %eax,%eax
jne    11df <mapfile+0x7f>
lea    0xe29(%rip),%rsi
mov    %rbx,%rdi
call   1060 <bail@plt>
mov    0x8(%rsp),%esi
mov    0x2e0e(%rip),%rax
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dec(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2da3(%rip),%rax
cmp    (%rax),%rbp
je     1239 <mapfile+0xd9>
test   %r14,%r14
je     122d <mapfile+0xcd>
mov    0x4(%rsp),%eax
mov    %eax,0x4(%r14)
mov    (%rsp),%eax
mov    %eax,(%r14)
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <bail@plt>
test   %r14,%r14
jne    121f <mapfile+0xbf>
jmp    122d <mapfile+0xcd>

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

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