#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;
  if (nbytes == 0) {
    static const char c = '\0';
    processor(&c, 0, name, arg);
    close(fd);
    return 1;
  }
  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    1305 <filemap+0x185>
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    1305 <filemap+0x185>
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    1305 <filemap+0x185>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1277 <filemap+0xf7>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xdb9(%rip),%rdi
xor    %esi,%esi
call   *%rax
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <filemap+0x185>
mov    -0x30(%rbp),%rsi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    0x2d3d(%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    12d1 <filemap+0x151>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1305 <filemap+0x185>
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,%r12
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     127d <filemap+0xfd>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
js     1289 <filemap+0x109>
mov    0x10(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     122c <filemap+0xac>
mov    0x8(%rsp),%r13
test   %r13,%r13
je     124f <filemap+0xcf>
mov    %rbp,(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    0x2dd9(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r13,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     129c <filemap+0x11c>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
call   *(%rsp)
mov    %r15d,%edi
mov    %r13,%rsi
call   1070 <munmap@plt>
jmp    1260 <filemap+0xe0>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %ebp,%ebp
jmp    126c <filemap+0xec>
lea    0xdc2(%rip),%rdi
xor    %esi,%esi
mov    %r14,%rdx
mov    %r12,%rcx
call   *%rbp
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1090 <perror@plt>
xor    %ebp,%ebp
jmp    126c <filemap+0xec>
mov    %r14,%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
jmp    126c <filemap+0xec>
mov    %r14,%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    126c <filemap+0xec>

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"
@filemap.c = internal constant i8 0
@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 %78
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 %78
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 %78
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 = icmp eq i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %40
%46 = load void (i8*, i64, i8*, i8*)*, void (i8*, i64, i8*, i8*)** %6
%47 = load i8*, i8** %5
%48 = load i8*, i8** %7
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
store i32 1, i32* %4
br label %78
51: ; preds = %40
%52 = load i64, i64* %9
%53 = load i32, i32* @PROT_READ
%54 = load i32, i32* @MAP_PRIVATE
%55 = or i32 0, %54
%56 = load i32, i32* %8
%57 = call i64 @mmap(i32 0, i64 %52, i32 %53, i32 %55, i32 %56, i32 0)
%58 = inttoptr i64 %57 to i8*
store i8* %58, i8** %11
%59 = load i8*, i8** %11
%60 = icmp eq i8* %59, inttoptr (i64 -1 to i8*)
br i1 %60, label %61, label %66
61: ; preds = %51
%62 = load i8*, i8** %5
%63 = call i32 @perror(i8* %62)
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
store i32 0, i32* %4
br label %78
66: ; preds = %51
%67 = load void (i8*, i64, i8*, i8*)*, void (i8*, i64, i8*, i8*)** %6
%68 = load i8*, i8** %11
%69 = load i64, i64* %9
%70 = load i8*, i8** %5
%71 = load i8*, i8** %7
%72 = load i8*, i8** %11
%73 = ptrtoint i8* %72 to i32
%74 = load i64, i64* %9
%75 = call i32 @munmap(i32 %73, i64 %74)
%76 = load i32, i32* %8
%77 = call i32 @close(i32 %76)
store i32 1, i32* %4
br label %78
78: ; preds = %66, %61, %45, %34, %24, %17
%79 = load i32, i32* %4
ret i32 %79
}

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"
@filemap.c = internal constant i8 0
@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 %45
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 %45
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 %45
26: ; preds = %17
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%28 = load i64, i64* %27
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
tail call void %1(i8* @filemap.c, i64 0, i8* %0, i8* %2)
%31 = tail call i32 @close(i32 %7)
br label %45
32: ; preds = %26
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @MAP_PRIVATE
%35 = tail call i64 @mmap(i32 0, i64 %28, i32 %33, i32 %34, i32 %7, i32 0)
%36 = inttoptr i64 %35 to i8*
%37 = icmp eq i8* %36, inttoptr (i64 -1 to i8*)
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = tail call i32 @perror(i8* %0)
%40 = tail call i32 @close(i32 %7)
br label %45
41: ; preds = %32
tail call void %1(i8* %36, i64 %28, i8* %0, i8* %2)
%42 = trunc i64 %35 to i32
%43 = tail call i32 @munmap(i32 %42, i64 %28)
%44 = tail call i32 @close(i32 %7)
br label %45
45: ; preds = %41, %38, %30, %22, %14, %9
%46 = phi i32 [ 0, %9 ], [ 0, %14 ], [ 1, %30 ], [ 0, %38 ], [ 1, %41 ], [ 0, %22 ]
ret i32 %46
}