#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 */
typedef scalar_t__ uint8_t;
typedef scalar_t__ uint64_t;
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
scalar_t__ *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
scalar_t__ *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
                 int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(scalar_t__ *, scalar_t__);
scalar_t__ offset2val(scalar_t__);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ quietprint(char *, ...);
int kf_check(const char *filename, uint64_t size, uint64_t offset) {
  int fd = -1;
  uint8_t *ptr = MAP_FAILED;
  uint64_t cur;
  int ret = 1;
  struct stat sb;
  fd = open(filename, O_RDONLY);
  quietprint("Checking %s / size:%lu / offset:%lu", filename, size, offset);
  if (fd == -1) {
    quietprint("open of \"%s\" for reading failed", filename);
    goto end;
  }
  if (fstat(fd, &sb) == -1) {
    quietprint("fstat failed on \"%s\"", filename);
    goto end;
  }
  if (sb.st_size != size) {
    quietprint("size check failed on \"%s\". Expected %lu, got %lu", filename,
               size, sb.st_size);
    goto end;
  }
  ptr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (ptr == MAP_FAILED) {
    quietprint("mmap failed on \"%s\"", filename);
    goto end;
  }
  for (cur = 0; cur < sb.st_size; cur++) {
    if (ptr[cur] != offset2val(offset + cur)) {
      quietprint("Content check failed at offset %lu in  \"%s\", offset        "
                 "         %lu in knownfile",
                 cur, filename, offset + cur);
      goto end;
    }
  }
  ret = 0;
  quietprint("Success");
end:
  if (ptr != MAP_FAILED) {
    munmap(ptr, sb.st_size);
  }
  if (fd >= 0) {
    close(fd);
  }
  return ret;
}


x86 assembly (clang, -O0):
<kf_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1050 <quietprint@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f0 <kf_check+0x80>
mov    -0x8(%rbp),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <quietprint@plt>
jmp    132f <kf_check+0x1bf>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    121c <kf_check+0xac>
mov    -0x8(%rbp),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <quietprint@plt>
jmp    132f <kf_check+0x1bf>
mov    -0x40(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1249 <kf_check+0xd9>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x40(%rbp),%rcx
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <quietprint@plt>
jmp    132f <kf_check+0x1bf>
mov    -0x40(%rbp),%rsi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    0x2d73(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%rax
jne    129e <kf_check+0x12e>
mov    -0x8(%rbp),%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <quietprint@plt>
jmp    132f <kf_check+0x1bf>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x40(%rbp),%rax
jge    131a <kf_check+0x1aa>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x30(%rbp),%rdi
call   1080 <offset2val@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
je     1304 <kf_check+0x194>
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
add    -0x30(%rbp),%rcx
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1050 <quietprint@plt>
jmp    132f <kf_check+0x1bf>
jmp    1309 <kf_check+0x199>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12a6 <kf_check+0x136>
movl   $0x0,-0x34(%rbp)
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <quietprint@plt>
mov    -0x28(%rbp),%rax
mov    0x2c8e(%rip),%rcx
cmp    (%rcx),%rax
je     1350 <kf_check+0x1e0>
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <munmap@plt>
cmpl   $0x0,-0x1c(%rbp)
jl     1362 <kf_check+0x1f2>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kf_check>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2e3a(%rip),%r14
mov    (%r14),%r15
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xe58(%rip),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <quietprint@plt>
cmp    $0xffffffff,%ebp
je     127d <kf_check+0x10d>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     1286 <kf_check+0x116>
mov    0x8(%rsp),%rcx
cmp    %r12,%rcx
jne    128f <kf_check+0x11f>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    0x2ddc(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
cmp    (%r14),%rax
je     12a5 <kf_check+0x135>
mov    %rbx,0x10(%rsp)
mov    %ebp,0x4(%rsp)
test   %r12,%r12
jle    125a <kf_check+0xea>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rbx
mov    (%r15,%rbp,8),%r15
mov    %r13,%r14
add    %rbp,%r14
mov    %r14,%rdi
call   1080 <offset2val@plt>
cmp    %rax,%r15
jne    12eb <kf_check+0x17b>
add    $0x1,%rbp
cmp    %rbp,%r12
mov    %rbx,%r15
jne    1230 <kf_check+0xc0>
lea    0xe92(%rip),%rdi
xor    %r12d,%r12d
xor    %eax,%eax
call   1050 <quietprint@plt>
mov    0x4(%rsp),%ebp
mov    0x2d52(%rip),%r14
cmp    (%r14),%r15
jne    12c1 <kf_check+0x151>
jmp    12ce <kf_check+0x15e>
lea    0xda0(%rip),%rdi
jmp    12ac <kf_check+0x13c>
lea    0xdb7(%rip),%rdi
jmp    12ac <kf_check+0x13c>
lea    0xdc3(%rip),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <quietprint@plt>
jmp    12b6 <kf_check+0x146>
lea    0xdde(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <quietprint@plt>
mov    $0x1,%r12d
cmp    (%r14),%r15
je     12ce <kf_check+0x15e>
mov    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1060 <munmap@plt>
test   %ebp,%ebp
js     12d9 <kf_check+0x169>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdac(%rip),%rdi
mov    %rbp,%rsi
mov    0x10(%rsp),%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <quietprint@plt>
mov    $0x1,%r12d
mov    0x4(%rsp),%ebp
mov    0x2cb3(%rip),%r14
mov    %rbx,%r15
cmp    (%r14),%r15
jne    12c1 <kf_check+0x151>
jmp    12ce <kf_check+0x15e>

LLVM IR (-O0):
%struct.stat = type { i64 }
@MAP_FAILED = global i64* null
@O_RDONLY = global i32 0
@.str = [36 x i8] c"Checking %s / size:%lu / offset:%lu\00"
@.str.1 = [32 x i8] c"open of \22%s\22 for reading failed\00"
@.str.2 = [21 x i8] c"fstat failed on \22%s\22\00"
@.str.3 = [49 x i8] c"size check failed on \22%s\22. Expected %lu, got %lu\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.4 = [20 x i8] c"mmap failed on \22%s\22\00"
@.str.5 = [85 x i8] c"Content check failed at offset %lu in \22%s\22, offset %lu in knownfile\00"
@.str.6 = [8 x i8] c"Success\00"
define i32 @kf_check(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.stat
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
store i32 -1, i32* %7
%12 = load i64*, i64** @MAP_FAILED
store i64* %12, i64** %8
store i32 1, i32* %10
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = load i64, i64* %6
%19 = call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %16, i64 %17, i64 %18)
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %3
%23 = load i8*, i8** %4
%24 = call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %85
25: ; preds = %3
%26 = load i32, i32* %7
%27 = call i32 @fstat(i32 %26, %struct.stat* %11)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %85
32: ; preds = %25
%33 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load i64, i64* %5
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %43
37: ; preds = %32
%38 = load i8*, i8** %4
%39 = load i64, i64* %5
%40 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%41 = load i64, i64* %40
%42 = call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %38, i64 %39, i64 %41)
br label %85
43: ; preds = %32
%44 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%45 = load i64, i64* %44
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @MAP_PRIVATE
%48 = load i32, i32* %7
%49 = call i64* @mmap(i32* null, i64 %45, i32 %46, i32 %47, i32 %48, i32 0)
store i64* %49, i64** %8
%50 = load i64*, i64** %8
%51 = load i64*, i64** @MAP_FAILED
%52 = icmp eq i64* %50, %51
br i1 %52, label %53, label %56
53: ; preds = %43
%54 = load i8*, i8** %4
%55 = call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %54)
br label %85
56: ; preds = %43
store i64 0, i64* %9
br label %57
57: ; preds = %80, %56
%58 = load i64, i64* %9
%59 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%60 = load i64, i64* %59
%61 = icmp slt i64 %58, %60
br i1 %61, label %62, label %83
62: ; preds = %57
%63 = load i64*, i64** %8
%64 = load i64, i64* %9
%65 = getelementptr inbounds i64, i64* %63, i64 %64
%66 = load i64, i64* %65
%67 = load i64, i64* %6
%68 = load i64, i64* %9
%69 = add nsw i64 %67, %68
%70 = call i64 @offset2val(i64 %69)
%71 = icmp ne i64 %66, %70
br i1 %71, label %72, label %79
72: ; preds = %62
%73 = load i64, i64* %9
%74 = load i8*, i8** %4
%75 = load i64, i64* %6
%76 = load i64, i64* %9
%77 = add nsw i64 %75, %76
%78 = call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.5, i64 0, i64 0), i64 %73, i8* %74, i64 %77)
br label %85
79: ; preds = %62
br label %80
80: ; preds = %79
%81 = load i64, i64* %9
%82 = add nsw i64 %81, 1
store i64 %82, i64* %9
br label %57
83: ; preds = %57
store i32 0, i32* %10
%84 = call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %85
85: ; preds = %83, %72, %53, %37, %29, %22
%86 = load i64*, i64** %8
%87 = load i64*, i64** @MAP_FAILED
%88 = icmp ne i64* %86, %87
br i1 %88, label %89, label %94
89: ; preds = %85
%90 = load i64*, i64** %8
%91 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%92 = load i64, i64* %91
%93 = call i32 @munmap(i64* %90, i64 %92)
br label %94
94: ; preds = %89, %85
%95 = load i32, i32* %7
%96 = icmp sge i32 %95, 0
br i1 %96, label %97, label %100
97: ; preds = %94
%98 = load i32, i32* %7
%99 = call i32 @close(i32 %98)
br label %100
100: ; preds = %97, %94
%101 = load i32, i32* %10
ret i32 %101
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@MAP_FAILED = global i64* null
@O_RDONLY = global i32 0
@.str = [36 x i8] c"Checking %s / size:%lu / offset:%lu\00"
@.str.1 = [32 x i8] c"open of \22%s\22 for reading failed\00"
@.str.2 = [21 x i8] c"fstat failed on \22%s\22\00"
@.str.3 = [49 x i8] c"size check failed on \22%s\22. Expected %lu, got %lu\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.4 = [20 x i8] c"mmap failed on \22%s\22\00"
@.str.5 = [85 x i8] c"Content check failed at offset %lu in \22%s\22, offset %lu in knownfile\00"
@.str.6 = [8 x i8] c"Success\00"
define i32 @kf_check(i8* %0, i64 %1, i64 %2) {
%4 = alloca %struct.stat
%5 = load i64*, i64** @MAP_FAILED
%6 = bitcast %struct.stat* %4 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = tail call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %1, i64 %2)
%10 = icmp eq i32 %8, -1
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %48
13: ; preds = %3
%14 = call i32 @fstat(i32 %8, %struct.stat* %4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %48
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = load i64, i64* %19
%21 = icmp eq i64 %20, %1
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %0, i64 %1, i64 %20)
br label %48
24: ; preds = %18
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_PRIVATE
%27 = tail call i64* @mmap(i32* null, i64 %1, i32 %25, i32 %26, i32 %8, i32 0)
%28 = load i64*, i64** @MAP_FAILED
%29 = icmp eq i64* %27, %28
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = icmp sgt i64 %1, 0
br i1 %31, label %34, label %46
32: ; preds = %24
%33 = tail call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %48
34: ; preds = %30, %43
%35 = phi i64 [ %44, %43 ], [ 0, %30 ]
%36 = getelementptr inbounds i64, i64* %27, i64 %35
%37 = load i64, i64* %36
%38 = add nsw i64 %35, %2
%39 = tail call i64 @offset2val(i64 %38)
%40 = icmp eq i64 %37, %39
br i1 %40, label %43, label %41
41: ; preds = %34
%42 = tail call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.5, i64 0, i64 0), i64 %35, i8* %0, i64 %38)
br label %48
43: ; preds = %34
%44 = add nuw nsw i64 %35, 1
%45 = icmp eq i64 %44, %1
br i1 %45, label %46, label %34
46: ; preds = %43, %30
%47 = tail call i32 (i8*, ...) @quietprint(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %46, %41, %32, %22, %16, %11
%49 = phi i64* [ %5, %11 ], [ %5, %16 ], [ %5, %22 ], [ %27, %32 ], [ %27, %41 ], [ %27, %46 ]
%50 = phi i32 [ 1, %11 ], [ 1, %16 ], [ 1, %22 ], [ 1, %32 ], [ 1, %41 ], [ 0, %46 ]
%51 = load i64*, i64** @MAP_FAILED
%52 = icmp eq i64* %49, %51
br i1 %52, label %57, label %53
53: ; preds = %48
%54 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%55 = load i64, i64* %54
%56 = tail call i32 @munmap(i64* %49, i64 %55)
br label %57
57: ; preds = %53, %48
%58 = icmp sgt i32 %8, -1
br i1 %58, label %59, label %61
59: ; preds = %57
%60 = tail call i32 @close(i32 %8)
br label %61
61: ; preds = %59, %57
ret i32 %50
}