#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_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int S_IRGRP;
int S_IRUSR;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int S_IWGRP;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fstat(int, struct stat *);
int /*<<< orphan*/ ftruncate(int, int);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int, ...);
int /*<<< orphan*/ stderr;
int mapfile(const char *filename, int size, void **filedata) {
  struct stat sb;
  int fd;
  if (size == 0) {
    fd = open(filename, O_RDONLY);
    if (fstat(fd, &sb) == -1) {
      fprintf(stderr, "Can't find file %s\n", filename);
      return -1;
    }
    if (!S_ISREG(sb.st_mode)) {
      fprintf(stderr, "%s is not a regular file\n", filename);
      return -1;
    }
    *filedata = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  } else {
    fd = open(filename, O_CREAT | O_RDWR | O_TRUNC,
              S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
    ftruncate(fd, size);
    *filedata = mmap(0, size, PROT_WRITE, MAP_SHARED, fd, 0);
  }
  if (*filedata == MAP_FAILED) {
    fprintf(stderr, "Can't map %s to memory\n", filename);
    return -1;
  }
  if (close(fd) == -1) {
    fprintf(stderr, "Can't close file %s\n", filename);
    return -1;
  }
  return size > 0 ? size : sb.st_size;
}


x86 assembly (clang, -O0):
<mapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1247 <mapfile+0xd7>
mov    -0x10(%rbp),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11de <mapfile+0x6e>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe33(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <mapfile+0x1f0>
mov    -0x24(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1214 <mapfile+0xa4>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <mapfile+0x1f0>
mov    -0x28(%rbp),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    12cd <mapfile+0x15d>
mov    -0x10(%rbp),%rdi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    0x2d6d(%rip),%rax
or     (%rax),%esi
mov    0x2d8c(%rip),%rax
or     (%rax),%esi
mov    0x2d1b(%rip),%rax
mov    (%rax),%edx
mov    0x2d3a(%rip),%rax
or     (%rax),%edx
mov    0x2d19(%rip),%rax
or     (%rax),%edx
mov    0x2d30(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <ftruncate@plt>
mov    -0x14(%rbp),%esi
mov    0x2ce7(%rip),%rax
mov    (%rax),%edx
mov    0x2d26(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2ca5(%rip),%rcx
cmp    (%rcx),%rax
jne    1309 <mapfile+0x199>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd36(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <mapfile+0x1f0>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    133f <mapfile+0x1cf>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd18(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <mapfile+0x1f0>
cmpl   $0x0,-0x14(%rbp)
jle    1354 <mapfile+0x1e4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    135a <mapfile+0x1ea>
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%r15
test   %esi,%esi
je     122b <mapfile+0xbb>
mov    0x2e64(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    0x2dfa(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
mov    0x2dee(%rip),%rax
or     (%rax),%edx
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r12d,%esi
call   1040 <ftruncate@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
mov    0x2de6(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%rax
je     1288 <mapfile+0x118>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     129a <mapfile+0x12a>
test   %r12d,%r12d
cmovg  %r12d,%ebp
jmp    12db <mapfile+0x16b>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     12ac <mapfile+0x13c>
mov    0xc(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     12be <mapfile+0x14e>
mov    0x8(%rsp),%ebp
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    0x2d66(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
jmp    1204 <mapfile+0x94>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
jmp    12ce <mapfile+0x15e>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
jmp    12ce <mapfile+0x15e>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd44(%rip),%rsi
jmp    12ce <mapfile+0x15e>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
lea    0xd46(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Can't find file %s\0A\00"
@.str.1 = [26 x i8] c"%s is not a regular file\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [24 x i8] c"Can't map %s to memory\0A\00"
@.str.3 = [21 x i8] c"Can't close file %s\0A\00"
define i32 @mapfile(i8* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %40
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 (i8*, i32, ...) @open(i8* %13, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = call i32 @fstat(i32 %16, %struct.stat* %8)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %4
br label %91
23: ; preds = %12
%24 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 @S_ISREG(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %5
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 -1, i32* %4
br label %91
32: ; preds = %23
%33 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @MAP_SHARED
%37 = load i32, i32* %9
%38 = call i8* @mmap(i32 0, i32 %34, i32 %35, i32 %36, i32 %37, i32 0)
%39 = load i8**, i8*** %7
store i8* %38, i8** %39
br label %64
40: ; preds = %3
%41 = load i8*, i8** %5
%42 = load i32, i32* @O_CREAT
%43 = load i32, i32* @O_RDWR
%44 = or i32 %42, %43
%45 = load i32, i32* @O_TRUNC
%46 = or i32 %44, %45
%47 = load i32, i32* @S_IRUSR
%48 = load i32, i32* @S_IWUSR
%49 = or i32 %47, %48
%50 = load i32, i32* @S_IRGRP
%51 = or i32 %49, %50
%52 = load i32, i32* @S_IWGRP
%53 = or i32 %51, %52
%54 = call i32 (i8*, i32, ...) @open(i8* %41, i32 %46, i32 %53)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = load i32, i32* %6
%57 = call i32 @ftruncate(i32 %55, i32 %56)
%58 = load i32, i32* %6
%59 = load i32, i32* @PROT_WRITE
%60 = load i32, i32* @MAP_SHARED
%61 = load i32, i32* %9
%62 = call i8* @mmap(i32 0, i32 %58, i32 %59, i32 %60, i32 %61, i32 0)
%63 = load i8**, i8*** %7
store i8* %62, i8** %63
br label %64
64: ; preds = %40, %32
%65 = load i8**, i8*** %7
%66 = load i8*, i8** %65
%67 = load i8*, i8** @MAP_FAILED
%68 = icmp eq i8* %66, %67
br i1 %68, label %69, label %73
69: ; preds = %64
%70 = load i32, i32* @stderr
%71 = load i8*, i8** %5
%72 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %71)
store i32 -1, i32* %4
br label %91
73: ; preds = %64
%74 = load i32, i32* %9
%75 = call i32 @close(i32 %74)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %81
77: ; preds = %73
%78 = load i32, i32* @stderr
%79 = load i8*, i8** %5
%80 = call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %79)
store i32 -1, i32* %4
br label %91
81: ; preds = %73
%82 = load i32, i32* %6
%83 = icmp sgt i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %81
%85 = load i32, i32* %6
br label %89
86: ; preds = %81
%87 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%88 = load i32, i32* %87
br label %89
89: ; preds = %86, %84
%90 = phi i32 [ %85, %84 ], [ %88, %86 ]
store i32 %90, i32* %4
br label %91
91: ; preds = %89, %77, %69, %28, %19
%92 = load i32, i32* %4
ret i32 %92
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Can't find file %s\0A\00"
@.str.1 = [26 x i8] c"%s is not a regular file\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [24 x i8] c"Can't map %s to memory\0A\00"
@.str.3 = [21 x i8] c"Can't close file %s\0A\00"
define i32 @mapfile(i8* %0, i32 %1, i8** nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = icmp eq i32 %1, 0
br i1 %6, label %7, label %29
7: ; preds = %3
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %8)
%10 = call i32 @fstat(i32 %9, %struct.stat* %4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %65
15: ; preds = %7
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%17 = load i32, i32* %16
%18 = tail call i32 @S_ISREG(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %65
23: ; preds = %15
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_SHARED
%28 = tail call i8* @mmap(i32 0, i32 %25, i32 %26, i32 %27, i32 %9, i32 0)
br label %47
29: ; preds = %3
%30 = load i32, i32* @O_CREAT
%31 = load i32, i32* @O_RDWR
%32 = or i32 %31, %30
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %36, %35
%38 = load i32, i32* @S_IRGRP
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IWGRP
%41 = or i32 %39, %40
%42 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %34, i32 %41)
%43 = tail call i32 @ftruncate(i32 %42, i32 %1)
%44 = load i32, i32* @PROT_WRITE
%45 = load i32, i32* @MAP_SHARED
%46 = tail call i8* @mmap(i32 0, i32 %1, i32 %44, i32 %45, i32 %42, i32 0)
br label %47
47: ; preds = %29, %23
%48 = phi i32 [ undef, %29 ], [ %25, %23 ]
%49 = phi i8* [ %46, %29 ], [ %28, %23 ]
%50 = phi i32 [ %42, %29 ], [ %9, %23 ]
store i8* %49, i8** %2
%51 = load i8*, i8** @MAP_FAILED
%52 = icmp eq i8* %49, %51
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = tail call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %65
56: ; preds = %47
%57 = tail call i32 @close(i32 %50)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = tail call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %65
62: ; preds = %56
%63 = icmp sgt i32 %1, 0
%64 = select i1 %63, i32 %1, i32 %48
br label %65
65: ; preds = %62, %59, %53, %20, %12
%66 = phi i32 [ -1, %12 ], [ -1, %53 ], [ -1, %59 ], [ %64, %62 ], [ -1, %20 ]
ret i32 %66
}