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

/* Variables and functions */
int /*<<< orphan*/ *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int calcsum(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/ *, size_t,
            int /*<<< orphan*/ const *, size_t, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/,
                         int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ munmap(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ stderr;
int pgpv_digest_file(uint8_t *data, size_t size, const char *name,
                     const uint8_t *hashed, size_t hashsize, int doarmor) {
  struct stat st;
  uint8_t *mem;
  size_t cc;
  FILE *fp;
  int ret;
  if (hashed == NULL || data == NULL || name == NULL) {
    fprintf(stderr, "no hashed data provided\n");
    return 0;
  }
  ret = 0;
  mem = NULL;
  cc = 0;
  if ((fp = fopen(name, "r")) == NULL) {
    fprintf(stderr, "%s - not found", name);
    return 0;
  }
  if (fstat(fileno(fp), &st) < 0) {
    fprintf(stderr, "%s - can't stat", name);
    goto done;
  }
  cc = (size_t)(st.st_size);
  if ((mem = mmap(NULL, cc, PROT_READ, MAP_SHARED, fileno(fp), 0)) ==
      MAP_FAILED) {
    fprintf(stderr, "%s - can't mmap", name);
    goto done;
  }
  ret = calcsum(data, size, mem, cc, hashed, hashsize, doarmor);
done:
  if (data) {
    munmap(mem, cc);
  }
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<pgpv_digest_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c4 <pgpv_digest_file+0x44>
cmpq   $0x0,-0x10(%rbp)
je     11c4 <pgpv_digest_file+0x44>
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <pgpv_digest_file+0x67>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134c <pgpv_digest_file+0x1cc>
movl   $0x0,-0x5c(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
jne    1243 <pgpv_digest_file+0xc3>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134c <pgpv_digest_file+0x1cc>
mov    -0x58(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x40(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
jge    1281 <pgpv_digest_file+0x101>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1325 <pgpv_digest_file+0x1a5>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x58(%rbp),%rdi
call   1060 <fileno@plt>
mov    -0x70(%rbp),%rsi
mov    -0x64(%rbp),%edx
mov    -0x60(%rbp),%ecx
mov    %eax,%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x48(%rbp)
mov    0x2cf2(%rip),%rcx
cmp    (%rcx),%rax
jne    12ff <pgpv_digest_file+0x17f>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd47(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1325 <pgpv_digest_file+0x1a5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
mov    -0x34(%rbp),%eax
mov    %eax,(%rsp)
call   1080 <calcsum@plt>
mov    %eax,-0x5c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     133d <pgpv_digest_file+0x1bd>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1070 <munmap@plt>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgpv_digest_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
test   %rdx,%rdx
je     1288 <pgpv_digest_file+0x108>
mov    %rdi,%r13
test   %rdi,%rdi
je     1288 <pgpv_digest_file+0x108>
mov    %rcx,%r12
test   %rcx,%rcx
je     1288 <pgpv_digest_file+0x108>
mov    %r9d,%r15d
mov    %r8,%rbp
mov    %rdx,%rbx
mov    %rsi,0x28(%rsp)
lea    0xe55(%rip),%rsi
mov    %rdx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12a4 <pgpv_digest_file+0x124>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <fileno@plt>
lea    0x30(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
js     12c3 <pgpv_digest_file+0x143>
mov    %r15d,0xc(%rsp)
mov    %rbp,0x20(%rsp)
mov    0x30(%rsp),%rbp
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r14,%rdi
call   1060 <fileno@plt>
xor    %r15d,%r15d
xor    %edi,%edi
mov    %rbp,0x18(%rsp)
mov    %rbp,%rsi
mov    0x14(%rsp),%edx
mov    0x10(%rsp),%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2d78(%rip),%rax
cmp    (%rax),%rbp
je     12e6 <pgpv_digest_file+0x166>
mov    0xc(%rsp),%eax
mov    %eax,(%rsp)
mov    %r13,%rdi
mov    0x28(%rsp),%rsi
mov    %rbp,%rdx
mov    0x18(%rsp),%rbx
mov    %rbx,%rcx
mov    %r12,%r8
mov    0x20(%rsp),%r9
call   1080 <calcsum@plt>
mov    %eax,%r15d
mov    %rbx,%rsi
jmp    1305 <pgpv_digest_file+0x185>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1315 <pgpv_digest_file+0x195>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
xor    %r15d,%r15d
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1315 <pgpv_digest_file+0x195>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %esi,%esi
xor    %r15d,%r15d
jmp    1305 <pgpv_digest_file+0x185>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    0xd44(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x18(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <munmap@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [25 x i8] c"no hashed data provided\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"%s - not found\00"
@.str.3 = [16 x i8] c"%s - can't stat\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.4 = [16 x i8] c"%s - can't mmap\00"
define i32 @pgpv_digest_file(i32* %0, i64 %1, i8* %2, i32* %3, i64 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32
%14 = alloca %struct.stat
%15 = alloca i32*
%16 = alloca i64
%17 = alloca i32*
%18 = alloca i32
store i32* %0, i32** %8
store i64 %1, i64* %9
store i8* %2, i8** %10
store i32* %3, i32** %11
store i64 %4, i64* %12
store i32 %5, i32* %13
%19 = load i32*, i32** %11
%20 = icmp eq i32* %19, null
br i1 %20, label %27, label %21
21: ; preds = %6
%22 = load i32*, i32** %8
%23 = icmp eq i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i8*, i8** %10
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24, %21, %6
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %82
30: ; preds = %24
store i32 0, i32* %18
store i32* null, i32** %15
store i64 0, i64* %16
%31 = load i8*, i8** %10
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %32, i32** %17
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %10
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %36)
store i32 0, i32* %7
br label %82
38: ; preds = %30
%39 = load i32*, i32** %17
%40 = call i32 @fileno(i32* %39)
%41 = call i64 @fstat(i32 %40, %struct.stat* %14)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %10
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %45)
br label %71
47: ; preds = %38
%48 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%49 = load i64, i64* %48
store i64 %49, i64* %16
%50 = load i64, i64* %16
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @MAP_SHARED
%53 = load i32*, i32** %17
%54 = call i32 @fileno(i32* %53)
%55 = call i32* @mmap(i32* null, i64 %50, i32 %51, i32 %52, i32 %54, i32 0)
store i32* %55, i32** %15
%56 = load i32*, i32** @MAP_FAILED
%57 = icmp eq i32* %55, %56
br i1 %57, label %58, label %62
58: ; preds = %47
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %10
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %60)
br label %71
62: ; preds = %47
%63 = load i32*, i32** %8
%64 = load i64, i64* %9
%65 = load i32*, i32** %15
%66 = load i64, i64* %16
%67 = load i32*, i32** %11
%68 = load i64, i64* %12
%69 = load i32, i32* %13
%70 = call i32 @calcsum(i32* %63, i64 %64, i32* %65, i64 %66, i32* %67, i64 %68, i32 %69)
store i32 %70, i32* %18
br label %71
71: ; preds = %62, %58, %43
%72 = load i32*, i32** %8
%73 = icmp ne i32* %72, null
br i1 %73, label %74, label %78
74: ; preds = %71
%75 = load i32*, i32** %15
%76 = load i64, i64* %16
%77 = call i32 @munmap(i32* %75, i64 %76)
br label %78
78: ; preds = %74, %71
%79 = load i32*, i32** %17
%80 = call i32 @fclose(i32* %79)
%81 = load i32, i32* %18
store i32 %81, i32* %7
br label %82
82: ; preds = %78, %34, %27
%83 = load i32, i32* %7
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [25 x i8] c"no hashed data provided\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"%s - not found\00"
@.str.3 = [16 x i8] c"%s - can't stat\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.4 = [16 x i8] c"%s - can't mmap\00"
define i32 @pgpv_digest_file(i32* %0, i64 %1, i8* %2, i32* %3, i64 %4, i32 %5) {
%7 = alloca %struct.stat
%8 = bitcast %struct.stat* %7 to i8*
%9 = icmp eq i32* %3, null
%10 = icmp eq i32* %0, null
%11 = or i1 %10, %9
%12 = icmp eq i8* %2, null
%13 = or i1 %12, %11
br i1 %13, label %14, label %17
14: ; preds = %6
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %50
17: ; preds = %6
%18 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %50
23: ; preds = %17
%24 = tail call i32 @fileno(i32* %18)
%25 = call i64 @fstat(i32 %24, %struct.stat* %7)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %2)
br label %44
30: ; preds = %23
%31 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
%32 = load i64, i64* %31
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @MAP_SHARED
%35 = tail call i32 @fileno(i32* %18)
%36 = tail call i32* @mmap(i32* null, i64 %32, i32 %33, i32 %34, i32 %35, i32 0)
%37 = load i32*, i32** @MAP_FAILED
%38 = icmp eq i32* %36, %37
br i1 %38, label %39, label %42
39: ; preds = %30
%40 = load i32, i32* @stderr
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %44
42: ; preds = %30
%43 = tail call i32 @calcsum(i32* %0, i64 %1, i32* %36, i64 %32, i32* %3, i64 %4, i32 %5)
br label %44
44: ; preds = %42, %39, %27
%45 = phi i32* [ null, %27 ], [ %36, %39 ], [ %36, %42 ]
%46 = phi i64 [ 0, %27 ], [ %32, %39 ], [ %32, %42 ]
%47 = phi i32 [ 0, %27 ], [ 0, %39 ], [ %43, %42 ]
%48 = tail call i32 @munmap(i32* %45, i64 %46)
%49 = tail call i32 @fclose(i32* %18)
br label %50
50: ; preds = %44, %20, %14
%51 = phi i32 [ 0, %14 ], [ 0, %20 ], [ %47, %44 ]
ret i32 %51
}