#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 /*<<< orphan*/ st_size;
};

/* Variables and functions */
double DBL_MAX;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
double kentropy(char *, int /*<<< orphan*/, unsigned long);
double kentropy0(char *, int /*<<< orphan*/);
scalar_t__ mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
double kentropy_file(char *filename, unsigned long int k) {
  int fd;
  struct stat s;
  char *m;
  double kent;
  fd = open(filename, O_RDONLY);
  if (fd == -1) {
    perror("open");
    return DBL_MAX;
  }
  fstat(fd, &s);
  m = (char *)mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (m == MAP_FAILED) {
    perror("mmap");
    return DBL_MAX;
  }
  if (k == 0)
    kent = kentropy0(m, s.st_size);
  else
    kent = kentropy(m, s.st_size, k);
  munmap(m, s.st_size);
  close(fd);
  return kent;
}


x86 assembly (clang, -O0):
<kentropy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d0 <kentropy_file+0x50>
lea    0xe4a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dfe(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    128e <kentropy_file+0x10e>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10a0 <fstat@plt>
mov    -0x20(%rbp),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    0x2de1(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <kentropy_file+0xba>
lea    0xde5(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d94(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    128e <kentropy_file+0x10e>
cmpq   $0x0,-0x18(%rbp)
jne    125b <kentropy_file+0xdb>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <kentropy0@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    1270 <kentropy_file+0xf0>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1060 <kentropy@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kentropy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     122b <kentropy_file+0xab>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
mov    0x8(%rsp),%r15d
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2de5(%rip),%rax
cmp    %rbp,(%rax)
je     1234 <kentropy_file+0xb4>
mov    %rbp,%rdi
mov    %r15d,%esi
test   %r14,%r14
je     11fd <kentropy_file+0x7d>
mov    %r14,%rdx
call   1060 <kentropy@plt>
jmp    1202 <kentropy_file+0x82>
call   1030 <kentropy0@plt>
movsd  %xmm0,0x10(%rsp)
mov    %rbp,%rdi
mov    %r15d,%esi
call   1070 <munmap@plt>
mov    %ebx,%edi
call   1050 <close@plt>
movsd  0x10(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdce(%rip),%rdi
jmp    123b <kentropy_file+0xbb>
lea    0xdca(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d79(%rip),%rax
movsd  (%rax),%xmm0
jmp    1220 <kentropy_file+0xa0>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@DBL_MAX = global double 0.000000e+00
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define double @kentropy_file(i8* %0, i64 %1) {
%3 = alloca double
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca double
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = load double, double* @DBL_MAX
store double %17, double* %3
br label %56
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32 @fstat(i32 %19, %struct.stat* %7)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @MAP_PRIVATE
%25 = load i32, i32* %6
%26 = call i64 @mmap(i32* null, i32 %22, i32 %23, i32 %24, i32 %25, i32 0)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %34
31: ; preds = %18
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%33 = load double, double* @DBL_MAX
store double %33, double* %3
br label %56
34: ; preds = %18
%35 = load i64, i64* %5
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i8*, i8** %8
%39 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call double @kentropy0(i8* %38, i32 %40)
store double %41, double* %9
br label %48
42: ; preds = %34
%43 = load i8*, i8** %8
%44 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%45 = load i32, i32* %44
%46 = load i64, i64* %5
%47 = call double @kentropy(i8* %43, i32 %45, i64 %46)
store double %47, double* %9
br label %48
48: ; preds = %42, %37
%49 = load i8*, i8** %8
%50 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @munmap(i8* %49, i32 %51)
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
%55 = load double, double* %9
store double %55, double* %3
br label %56
56: ; preds = %48, %31, %15
%57 = load double, double* %3
ret double %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@DBL_MAX = global double 0.000000e+00
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define double @kentropy_file(i8* nocapture %0, i64 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = load double, double* @DBL_MAX
br label %34
11: ; preds = %2
%12 = call i32 @fstat(i32 %6, %struct.stat* %3)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_PRIVATE
%17 = tail call i64 @mmap(i32* null, i32 %14, i32 %15, i32 %16, i32 %6, i32 0)
%18 = inttoptr i64 %17 to i8*
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %19, %18
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = load double, double* @DBL_MAX
br label %34
24: ; preds = %11
%25 = icmp eq i64 %1, 0
br i1 %25, label %26, label %28
26: ; preds = %24
%27 = tail call double @kentropy0(i8* %18, i32 %14)
br label %30
28: ; preds = %24
%29 = tail call double @kentropy(i8* %18, i32 %14, i64 %1)
br label %30
30: ; preds = %28, %26
%31 = phi double [ %27, %26 ], [ %29, %28 ]
%32 = tail call i32 @munmap(i8* %18, i32 %14)
%33 = tail call i32 @close(i32 %6)
br label %34
34: ; preds = %30, %21, %8
%35 = phi double [ %10, %8 ], [ %23, %21 ], [ %31, %30 ]
ret double %35
}