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

/* Variables and functions */
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(int /*<<< orphan*/, size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stderr;
int filemap(const char *name,
            void (*processor)(const void *, size_t, const char *, void *arg),
            void *arg) {
  int fd;
  size_t nbytes;
  struct stat sb;
  void *p;
  fd = open(name, O_RDONLY);
  if (fd < 0) {
    perror(name);
    return 0;
  }
  if (fstat(fd, &sb) < 0) {
    perror(name);
    close(fd);
    return 0;
  }
  if (!S_ISREG(sb.st_mode)) {
    close(fd);
    fprintf(stderr, "%s: not a regular file\n", name);
    return 0;
  }
  nbytes = sb.st_size;
  p = (void *)mmap((caddr_t)0, (size_t)nbytes, PROT_READ, 0 | MAP_PRIVATE, fd,
                   (off_t)0);
  if (p == (void *)-1) {
    perror(name);
    close(fd);
    return 0;
  }
  processor(p, nbytes, name, arg);
  munmap((caddr_t)p, nbytes);
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<filemap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c8 <filemap+0x48>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cf <filemap+0x14f>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
jge    11fb <filemap+0x7b>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cf <filemap+0x14f>
mov    -0x38(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1239 <filemap+0xb9>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd8(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cf <filemap+0x14f>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
mov    0x2d73(%rip),%rax
mov    (%rax),%ecx
or     $0x0,%ecx
mov    -0x24(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x48(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x48(%rbp)
jne    129b <filemap+0x11b>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cf <filemap+0x14f>
mov    -0x18(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   *%rax
mov    -0x48(%rbp),%rax
mov    %eax,%edi
mov    -0x30(%rbp),%rsi
call   1070 <munmap@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filemap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1261 <filemap+0xe1>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
js     126b <filemap+0xeb>
mov    0x10(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     122f <filemap+0xaf>
mov    %rbx,(%rsp)
mov    0x8(%rsp),%r13
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    0x2dde(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r13,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     127c <filemap+0xfc>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   *(%rsp)
mov    %r14d,%edi
mov    %r13,%rsi
call   1070 <munmap@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x1,%ebx
jmp    1250 <filemap+0xd0>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    %r12,%rdx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1090 <perror@plt>
jmp    124e <filemap+0xce>
mov    %r12,%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    124e <filemap+0xce>
mov    %r12,%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1250 <filemap+0xd0>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"%s: not a regular file\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i32 @filemap(i8* %0, void (i8*, i64, i8*, i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca void (i8*, i64, i8*, i8*)*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca %struct.stat
%11 = alloca i8*
store i8* %0, i8** %5
store void (i8*, i64, i8*, i8*)* %1, void (i8*, i64, i8*, i8*)** %6
store i8* %2, i8** %7
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i32 @perror(i8* %18)
store i32 0, i32* %4
br label %69
20: ; preds = %3
%21 = load i32, i32* %8
%22 = call i64 @fstat(i32 %21, %struct.stat* %10)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = call i32 @perror(i8* %25)
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
store i32 0, i32* %4
br label %69
29: ; preds = %20
%30 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @S_ISREG(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %29
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %5
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %38)
store i32 0, i32* %4
br label %69
40: ; preds = %29
%41 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%42 = load i64, i64* %41
store i64 %42, i64* %9
%43 = load i64, i64* %9
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @MAP_PRIVATE
%46 = or i32 0, %45
%47 = load i32, i32* %8
%48 = call i64 @mmap(i32 0, i64 %43, i32 %44, i32 %46, i32 %47, i32 0)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %11
%50 = load i8*, i8** %11
%51 = icmp eq i8* %50, inttoptr (i64 -1 to i8*)
br i1 %51, label %52, label %57
52: ; preds = %40
%53 = load i8*, i8** %5
%54 = call i32 @perror(i8* %53)
%55 = load i32, i32* %8
%56 = call i32 @close(i32 %55)
store i32 0, i32* %4
br label %69
57: ; preds = %40
%58 = load void (i8*, i64, i8*, i8*)*, void (i8*, i64, i8*, i8*)** %6
%59 = load i8*, i8** %11
%60 = load i64, i64* %9
%61 = load i8*, i8** %5
%62 = load i8*, i8** %7
%63 = load i8*, i8** %11
%64 = ptrtoint i8* %63 to i32
%65 = load i64, i64* %9
%66 = call i32 @munmap(i32 %64, i64 %65)
%67 = load i32, i32* %8
%68 = call i32 @close(i32 %67)
store i32 1, i32* %4
br label %69
69: ; preds = %57, %52, %34, %24, %17
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"%s: not a regular file\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i32 @filemap(i8* %0, void (i8*, i64, i8*, i8*)* nocapture %1, i8* %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @perror(i8* %0)
br label %41
11: ; preds = %3
%12 = call i64 @fstat(i32 %7, %struct.stat* %4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @perror(i8* %0)
%16 = tail call i32 @close(i32 %7)
br label %41
17: ; preds = %11
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%19 = load i32, i32* %18
%20 = tail call i32 @S_ISREG(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = tail call i32 @close(i32 %7)
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %41
26: ; preds = %17
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%28 = load i64, i64* %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_PRIVATE
%31 = tail call i64 @mmap(i32 0, i64 %28, i32 %29, i32 %30, i32 %7, i32 0)
%32 = inttoptr i64 %31 to i8*
%33 = icmp eq i8* %32, inttoptr (i64 -1 to i8*)
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = tail call i32 @perror(i8* %0)
%36 = tail call i32 @close(i32 %7)
br label %41
37: ; preds = %26
tail call void %1(i8* %32, i64 %28, i8* %0, i8* %2)
%38 = trunc i64 %31 to i32
%39 = tail call i32 @munmap(i32 %38, i64 %28)
%40 = tail call i32 @close(i32 %7)
br label %41
41: ; preds = %37, %34, %22, %14, %9
%42 = phi i32 [ 0, %9 ], [ 0, %14 ], [ 0, %34 ], [ 1, %37 ], [ 0, %22 ]
ret i32 %42
}