#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*/ 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*/ *, unsigned char *,
                               unsigned long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
int OS_SHA1_File(char *fname, char *output) {
  SHA_CTX c;
  FILE *fp;
  unsigned char buf[2048 + 2];
  unsigned char md[20];
  int n;
  memset(output, 0, 65);
  buf[2049] = '\0';
  fp = fopen(fname, "r");
  if (!fp)
    return (-1);
  SHA1_Init(&c);
  while ((n = fread(buf, 1, 2048, fp)) > 0) {
    buf[n] = '\0';
    SHA1_Update(&c, buf, (unsigned long)n);
  }
  SHA1_Final(&(md[0]), &c);
  for (n = 0; n < 20; n++) {
    snprintf(output, 3, "%02x", md[n]);
    output += 2;
  }
  fclose(fp);
  return (0);
}


x86 assembly (clang, -O0):
<OS_SHA1_File>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1060 <memset@plt>
movb   $0x0,-0x2f(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d2 <OS_SHA1_File+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <OS_SHA1_File+0x12a>
lea    -0x1c(%rbp),%rdi
call   1080 <SHA1_Init@plt>
lea    -0x830(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x800,%edx
call   1030 <fread@plt>
mov    %eax,-0x854(%rbp)
cmp    $0x0,%eax
jle    122f <OS_SHA1_File+0xaf>
movslq -0x854(%rbp),%rax
movb   $0x0,-0x830(%rbp,%rax,1)
lea    -0x830(%rbp),%rsi
movslq -0x854(%rbp),%rdx
lea    -0x1c(%rbp),%rdi
call   1070 <SHA1_Update@plt>
jmp    11db <OS_SHA1_File+0x5b>
lea    -0x850(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   10a0 <SHA1_Final@plt>
movl   $0x0,-0x854(%rbp)
cmpl   $0x14,-0x854(%rbp)
jge    129a <OS_SHA1_File+0x11a>
mov    -0x18(%rbp),%rdi
movslq -0x854(%rbp),%rax
mov    $0x3,%esi
lea    0xd95(%rip),%rdx
movzbl -0x850(%rbp,%rax,1),%ecx
ff
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x854(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x854(%rbp)
jmp    1249 <OS_SHA1_File+0xc9>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OS_SHA1_File>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1060 <memset@plt>
movb   $0x0,0x821(%rsp)
lea    0xe4e(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1404 <OS_SHA1_File+0x284>
mov    %rax,%r12
lea    0x1c(%rsp),%rdi
call   1080 <SHA1_Init@plt>
lea    0x20(%rsp),%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    122b <OS_SHA1_File+0xab>
lea    0x1c(%rsp),%r15
lea    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%edx
movb   $0x0,0x20(%rsp,%rdx,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <SHA1_Update@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     1200 <OS_SHA1_File+0x80>
mov    %rsp,%rdi
lea    0x1c(%rsp),%rsi
call   10a0 <SHA1_Final@plt>
movzbl (%rsp),%ecx
lea    0xdbf(%rip),%r15
mov    %r14,%rdi
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x2(%r14),%rdi
movzbl 0x1(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x4(%r14),%rdi
movzbl 0x2(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x6(%r14),%rdi
movzbl 0x3(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x8(%r14),%rdi
movzbl 0x4(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0xa(%r14),%rdi
movzbl 0x5(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0xc(%r14),%rdi
movzbl 0x6(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0xe(%r14),%rdi
movzbl 0x7(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x10(%r14),%rdi
movzbl 0x8(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x12(%r14),%rdi
movzbl 0x9(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x14(%r14),%rdi
movzbl 0xa(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x16(%r14),%rdi
movzbl 0xb(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x18(%r14),%rdi
movzbl 0xc(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x1a(%r14),%rdi
movzbl 0xd(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x1c(%r14),%rdi
movzbl 0xe(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x1e(%r14),%rdi
movzbl 0xf(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x20(%r14),%rdi
movzbl 0x10(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x22(%r14),%rdi
movzbl 0x11(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x24(%r14),%rdi
movzbl 0x12(%rsp),%ecx
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
add    $0x26,%r14
movzbl 0x13(%rsp),%ecx
mov    %r14,%rdi
mov    $0x3,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1409 <OS_SHA1_File+0x289>
mov    $0xffffffff,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [5 x i8] c"%02x\00"
define i32 @OS_SHA1_File(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [2050 x i8]
%9 = alloca [20 x i8]
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = call i32 @memset(i8* %11, i32 0, i32 65)
%13 = getelementptr inbounds [2050 x i8], [2050 x i8]* %8, i64 0, i64 2049
store i8 0, i8* %13
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %2
store i32 -1, i32* %3
br label %55
19: ; preds = %2
%20 = call i32 @SHA1_Init(i32* %6)
br label %21
21: ; preds = %26, %19
%22 = getelementptr inbounds [2050 x i8], [2050 x i8]* %8, i64 0, i64 0
%23 = load i32*, i32** %7
%24 = call i32 @fread(i8* %22, i32 1, i32 2048, i32* %23)
store i32 %24, i32* %10
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %21
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [2050 x i8], [2050 x i8]* %8, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [2050 x i8], [2050 x i8]* %8, i64 0, i64 0
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = call i32 @SHA1_Update(i32* %6, i8* %30, i64 %32)
br label %21
34: ; preds = %21
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%36 = call i32 @SHA1_Final(i8* %35, i32* %6)
store i32 0, i32* %10
br label %37
37: ; preds = %49, %34
%38 = load i32, i32* %10
%39 = icmp slt i32 %38, 20
br i1 %39, label %40, label %52
40: ; preds = %37
%41 = load i8*, i8** %5
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = call i32 @snprintf(i8* %41, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %45)
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i64 2
store i8* %48, i8** %5
br label %49
49: ; preds = %40
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %37
52: ; preds = %37
%53 = load i32*, i32** %7
%54 = call i32 @fclose(i32* %53)
store i32 0, i32* %3
br label %55
55: ; preds = %52, %18
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [5 x i8] c"%02x\00"
define i32 @OS_SHA1_File(i8* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = alloca [2050 x i8]
%5 = alloca [20 x i8]
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [2050 x i8], [2050 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%9 = tail call i32 @memset(i8* %1, i32 0, i32 65)
%10 = getelementptr inbounds [2050 x i8], [2050 x i8]* %4, i64 0, i64 2049
store i8 0, i8* %10
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %105, label %13
13: ; preds = %2
%14 = call i32 @SHA1_Init(i32* %3)
%15 = call i32 @fread(i8* %7, i32 1, i32 2048, i32* %11)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %13, %17
%18 = phi i32 [ %22, %17 ], [ %15, %13 ]
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [2050 x i8], [2050 x i8]* %4, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 @SHA1_Update(i32* %3, i8* %7, i64 %19)
%22 = call i32 @fread(i8* %7, i32 1, i32 2048, i32* %11)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %17, label %24
24: ; preds = %17, %13
%25 = call i32 @SHA1_Final(i8* %8, i32* %3)
%26 = load i8, i8* %8
%27 = call i32 @snprintf(i8* %1, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %26)
%28 = getelementptr inbounds i8, i8* %1, i64 2
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 1
%30 = load i8, i8* %29
%31 = call i32 @snprintf(i8* %28, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %30)
%32 = getelementptr inbounds i8, i8* %1, i64 4
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 2
%34 = load i8, i8* %33
%35 = call i32 @snprintf(i8* %32, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %34)
%36 = getelementptr inbounds i8, i8* %1, i64 6
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 3
%38 = load i8, i8* %37
%39 = call i32 @snprintf(i8* %36, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %38)
%40 = getelementptr inbounds i8, i8* %1, i64 8
%41 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 4
%42 = load i8, i8* %41
%43 = call i32 @snprintf(i8* %40, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %42)
%44 = getelementptr inbounds i8, i8* %1, i64 10
%45 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 5
%46 = load i8, i8* %45
%47 = call i32 @snprintf(i8* %44, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %46)
%48 = getelementptr inbounds i8, i8* %1, i64 12
%49 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 6
%50 = load i8, i8* %49
%51 = call i32 @snprintf(i8* %48, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %50)
%52 = getelementptr inbounds i8, i8* %1, i64 14
%53 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 7
%54 = load i8, i8* %53
%55 = call i32 @snprintf(i8* %52, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %54)
%56 = getelementptr inbounds i8, i8* %1, i64 16
%57 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 8
%58 = load i8, i8* %57
%59 = call i32 @snprintf(i8* %56, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %58)
%60 = getelementptr inbounds i8, i8* %1, i64 18
%61 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 9
%62 = load i8, i8* %61
%63 = call i32 @snprintf(i8* %60, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %62)
%64 = getelementptr inbounds i8, i8* %1, i64 20
%65 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 10
%66 = load i8, i8* %65
%67 = call i32 @snprintf(i8* %64, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %66)
%68 = getelementptr inbounds i8, i8* %1, i64 22
%69 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 11
%70 = load i8, i8* %69
%71 = call i32 @snprintf(i8* %68, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %70)
%72 = getelementptr inbounds i8, i8* %1, i64 24
%73 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 12
%74 = load i8, i8* %73
%75 = call i32 @snprintf(i8* %72, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %74)
%76 = getelementptr inbounds i8, i8* %1, i64 26
%77 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 13
%78 = load i8, i8* %77
%79 = call i32 @snprintf(i8* %76, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %78)
%80 = getelementptr inbounds i8, i8* %1, i64 28
%81 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 14
%82 = load i8, i8* %81
%83 = call i32 @snprintf(i8* %80, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %82)
%84 = getelementptr inbounds i8, i8* %1, i64 30
%85 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 15
%86 = load i8, i8* %85
%87 = call i32 @snprintf(i8* %84, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %86)
%88 = getelementptr inbounds i8, i8* %1, i64 32
%89 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 16
%90 = load i8, i8* %89
%91 = call i32 @snprintf(i8* %88, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %90)
%92 = getelementptr inbounds i8, i8* %1, i64 34
%93 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 17
%94 = load i8, i8* %93
%95 = call i32 @snprintf(i8* %92, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %94)
%96 = getelementptr inbounds i8, i8* %1, i64 36
%97 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 18
%98 = load i8, i8* %97
%99 = call i32 @snprintf(i8* %96, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %98)
%100 = getelementptr inbounds i8, i8* %1, i64 38
%101 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 19
%102 = load i8, i8* %101
%103 = call i32 @snprintf(i8* %100, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %102)
%104 = call i32 @fclose(i32* %11)
br label %105
105: ; preds = %2, %24
%106 = phi i32 [ 0, %24 ], [ -1, %2 ]
ret i32 %106
}