#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*/ ffn;
typedef int /*<<< orphan*/ SHA_CTX;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, char *, size_t);
char *dbbdir();
int /*<<< orphan*/ error(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
void shaget(const char *fn, unsigned char *sha) {
  char ffn[1024];
  FILE *fd;
  SHA_CTX c;
  snprintf(ffn, sizeof(ffn), "%s/%s", dbbdir(), fn);
  if (!(fd = fopen(ffn, "rb"))) {
    error(0, "file open failed for '%s'", ffn);
    return;
  }
  SHA1_Init(&c);
  while (!feof(fd)) {
    char buf[8192];
    size_t r = fread(buf, 1, 8192, fd);
    SHA1_Update(&c, buf, r);
  }
  SHA1_Final(sha, &c);
  fclose(fd);
}


x86 assembly (clang, -O0):
<shaget>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x2430(%rbp)
mov    $0x0,%al
call   1060 <dbbdir@plt>
mov    -0x2430(%rbp),%rdi
mov    %rax,%rcx
mov    -0x8(%rbp),%r8
mov    $0x400,%esi
lea    0xe1e(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jne    1225 <shaget+0x85>
lea    -0x410(%rbp),%rdx
xor    %edi,%edi
lea    0xdee(%rip),%rsi
call   10a0 <error@plt>
jmp    12b1 <shaget+0x111>
lea    -0x41c(%rbp),%rdi
call   1090 <SHA1_Init@plt>
mov    -0x418(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1252 <shaget+0xb2>
jmp    1295 <shaget+0xf5>
lea    -0x2420(%rbp),%rdi
mov    -0x418(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %rax,-0x2428(%rbp)
lea    -0x2420(%rbp),%rsi
mov    -0x2428(%rbp),%rdx
lea    -0x41c(%rbp),%rdi
call   1080 <SHA1_Update@plt>
jmp    1231 <shaget+0x91>
mov    -0x10(%rbp),%rdi
lea    -0x41c(%rbp),%rsi
call   10c0 <SHA1_Final@plt>
mov    -0x418(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x2430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shaget>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2418,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <dbbdir@plt>
lea    0xe3e(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rax,%rcx
mov    %rbx,%r8
call   1050 <snprintf@plt>
lea    0xe25(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1266 <shaget+0xc6>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1090 <SHA1_Init@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    124f <shaget+0xaf>
lea    0x410(%rsp),%r15
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1080 <SHA1_Update@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     1220 <shaget+0x80>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
call   10c0 <SHA1_Final@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1279 <shaget+0xd9>
lea    0xd9c(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   10a0 <error@plt>
add    $0x2418,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"file open failed for '%s'\00"
define void @shaget(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [8192 x i8]
%9 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = call i8* (...) @dbbdir()
%12 = load i8*, i8** %3
%13 = call i32 @snprintf(i8* %10, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %6
%16 = icmp ne i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = call i32 @error(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %39
20: ; preds = %2
%21 = call i32 @SHA1_Init(i32* %7)
br label %22
22: ; preds = %27, %20
%23 = load i32*, i32** %6
%24 = call i32 @feof(i32* %23)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br i1 %26, label %27, label %34
27: ; preds = %22
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%29 = load i32*, i32** %6
%30 = call i64 @fread(i8* %28, i32 1, i32 8192, i32* %29)
store i64 %30, i64* %9
%31 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%32 = load i64, i64* %9
%33 = call i32 @SHA1_Update(i32* %7, i8* %31, i64 %32)
br label %22
34: ; preds = %22
%35 = load i8*, i8** %4
%36 = call i32 @SHA1_Final(i8* %35, i32* %7)
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
br label %39
39: ; preds = %34, %17
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"file open failed for '%s'\00"
define void @shaget(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca [8192 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = tail call i8* (...) @dbbdir()
%9 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %0)
%10 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @error(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %28
14: ; preds = %2
%15 = call i32 @SHA1_Init(i32* %4)
%16 = call i32 @feof(i32* %10)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
br label %20
20: ; preds = %18, %20
%21 = call i64 @fread(i8* %19, i32 1, i32 8192, i32* %10)
%22 = call i32 @SHA1_Update(i32* %4, i8* %19, i64 %21)
%23 = call i32 @feof(i32* %10)
%24 = icmp eq i32 %23, 0
br i1 %24, label %20, label %25
25: ; preds = %20, %14
%26 = call i32 @SHA1_Final(i8* %1, i32* %4)
%27 = call i32 @fclose(i32* %10)
br label %28
28: ; preds = %25, %12
ret void
}