#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;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ errno;
int filemapIsElf(char const *, void *, size_t);
int filemapIsScript(void *, size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
scalar_t__ munmap(void *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int fileIsExec(const char *filename, int fd, const struct stat *st) {
  size_t size = st->st_size;
  void *addr = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED) {
    fprintf(stderr, "%s: mmap error %s\n", filename, strerror(errno));
    return -1;
  }
  int ret = filemapIsScript(addr, size);
  if (ret == 0) {
    ret = filemapIsElf(filename, addr, size);
  }
  if (munmap(addr, size) != 0) {
    fprintf(stderr, "%s: munmap error %s, keep going\n", filename,
            strerror(errno));
  }
  return ret;
}


x86 assembly (clang, -O0):
<fileIsExec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    0x2e36(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jne    1200 <fileIsExec+0xa0>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe11(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <fileIsExec+0x123>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <filemapIsScript@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    122e <fileIsExec+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <filemapIsElf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <munmap@plt>
cmp    $0x0,%rax
je     127d <fileIsExec+0x11d>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9b(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileIsExec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r8d
mov    %rdi,%r14
mov    (%rdx),%rbx
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
mov    0x2e27(%rip),%rax
cmp    (%rax),%rbp
je     11da <fileIsExec+0x7a>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <filemapIsScript@plt>
mov    %eax,%r15d
test   %eax,%eax
jne    11c1 <fileIsExec+0x61>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <filemapIsElf@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <munmap@plt>
test   %rax,%rax
je     120e <fileIsExec+0xae>
lea    0xe3b(%rip),%rbx
jmp    11e7 <fileIsExec+0x87>
mov    $0xffffffff,%r15d
lea    0xe19(%rip),%rbx
mov    0x2dea(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str = [19 x i8] c"%s: mmap error %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"%s: munmap error %s, keep going\0A\00"
define i32 @fileIsExec(i8* %0, i32 %1, %struct.stat* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.stat* %2, %struct.stat** %7
%11 = load %struct.stat*, %struct.stat** %7
%12 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%13 = load i64, i64* %12
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_PRIVATE
%17 = load i32, i32* %6
%18 = call i8* @mmap(i32* null, i64 %14, i32 %15, i32 %16, i32 %17, i32 0)
store i8* %18, i8** %9
%19 = load i8*, i8** %9
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %28
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %5
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %26)
store i32 -1, i32* %4
br label %52
28: ; preds = %3
%29 = load i8*, i8** %9
%30 = load i64, i64* %8
%31 = call i32 @filemapIsScript(i8* %29, i64 %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %28
%35 = load i8*, i8** %5
%36 = load i8*, i8** %9
%37 = load i64, i64* %8
%38 = call i32 @filemapIsElf(i8* %35, i8* %36, i64 %37)
store i32 %38, i32* %10
br label %39
39: ; preds = %34, %28
%40 = load i8*, i8** %9
%41 = load i64, i64* %8
%42 = call i64 @munmap(i8* %40, i64 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %5
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %46, i8* %48)
br label %50
50: ; preds = %44, %39
%51 = load i32, i32* %10
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %22
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str = [19 x i8] c"%s: mmap error %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"%s: munmap error %s, keep going\0A\00"
define i32 @fileIsExec(i8* %0, i32 %1, %struct.stat* nocapture %2) {
%4 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%5 = load i64, i64* %4
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @MAP_PRIVATE
%8 = tail call i8* @mmap(i32* null, i64 %5, i32 %6, i32 %7, i32 %1, i32 0)
%9 = load i8*, i8** @MAP_FAILED
%10 = icmp eq i8* %8, %9
br i1 %10, label %20, label %11
11: ; preds = %3
%12 = tail call i32 @filemapIsScript(i8* %8, i64 %5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @filemapIsElf(i8* %0, i8* %8, i64 %5)
br label %16
16: ; preds = %14, %11
%17 = phi i32 [ %15, %14 ], [ %12, %11 ]
%18 = tail call i64 @munmap(i8* %8, i64 %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %16, %3
%21 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), %16 ]
%22 = phi i32 [ -1, %3 ], [ %17, %16 ]
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 @fprintf(i32 %23, i8* %21, i8* %0, i8* %25)
br label %27
27: ; preds = %20, %16
%28 = phi i32 [ %17, %16 ], [ %22, %20 ]
ret i32 %28
}