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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, unsigned char *,
                               int /*<<< orphan*/);
int SHA_DIGEST_LENGTH;
int /*<<< orphan*/ b64_ntop(unsigned char *, int, unsigned char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ write(int, unsigned char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  ssize_t len;
  unsigned char *buffer;
  unsigned char digest[SHA_DIGEST_LENGTH];
  SHA_CTX ctx;
  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    fd = 0;
  }
  SHA1_Init(&ctx);
  buffer = malloc(1024 * 1024);
  while ((len = read(fd, buffer, 1024 * 1024)) != 0) {
    SHA1_Update(&ctx, buffer, len);
  }
  SHA1_Final(digest, &ctx);
  if (fd != 0) {
    close(fd);
  }
  len = b64_ntop(digest, SHA_DIGEST_LENGTH, buffer, 1024 * 1024);
  write(1, buffer, len);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120c <main+0x6c>
movl   $0x0,-0x14(%rbp)
lea    -0x34(%rbp),%rdi
call   1090 <SHA1_Init@plt>
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x100000,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1255 <main+0xb5>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    -0x34(%rbp),%rdi
call   1080 <SHA1_Update@plt>
jmp    1223 <main+0x83>
mov    -0x40(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   10c0 <SHA1_Final@plt>
cmpl   $0x0,-0x14(%rbp)
je     1274 <main+0xd4>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%rdi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
mov    $0x100000,%ecx
call   10b0 <b64_ntop@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e2b(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
xor    %r15d,%r15d
cmp    $0xffffffff,%eax
cmovne %eax,%r15d
lea    -0x2c(%rbp),%rdi
call   1090 <SHA1_Init@plt>
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %rax,%rsi
mov    $0x100000,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1241 <main+0xa1>
lea    -0x2c(%rbp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1080 <SHA1_Update@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100000,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    1220 <main+0x80>
lea    -0x2c(%rbp),%rsi
mov    %r14,%rdi
call   10c0 <SHA1_Final@plt>
test   %r15d,%r15d
je     125a <main+0xba>
mov    %r15d,%edi
call   1050 <close@plt>
mov    0x0(%r13),%esi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    $0x100000,%ecx
call   10b0 <b64_ntop@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA_DIGEST_LENGTH = global i32 0
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @SHA_DIGEST_LENGTH
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %9
%15 = alloca i8, i64 %13
store i64 %13, i64* %10
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 0, i32* %6
br label %24
24: ; preds = %23, %2
%25 = call i32 @SHA1_Init(i32* %11)
%26 = call i8* @malloc(i32 1048576)
store i8* %26, i8** %8
br label %27
27: ; preds = %32, %24
%28 = load i32, i32* %6
%29 = load i8*, i8** %8
%30 = call i32 @read(i32 %28, i8* %29, i32 1048576)
store i32 %30, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i8*, i8** %8
%34 = load i32, i32* %7
%35 = call i32 @SHA1_Update(i32* %11, i8* %33, i32 %34)
br label %27
36: ; preds = %27
%37 = call i32 @SHA1_Final(i8* %15, i32* %11)
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40, %36
%44 = load i32, i32* @SHA_DIGEST_LENGTH
%45 = load i8*, i8** %8
%46 = call i32 @b64_ntop(i8* %15, i32 %44, i8* %45, i32 1048576)
store i32 %46, i32* %7
%47 = load i8*, i8** %8
%48 = load i32, i32* %7
%49 = call i32 @write(i32 1, i8* %47, i32 %48)
%50 = load i8*, i8** %8
%51 = call i32 @free(i8* %50)
store i32 0, i32* %3
%52 = load i8*, i8** %9
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = load i32, i32* @SHA_DIGEST_LENGTH
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp eq i32 %11, -1
%13 = select i1 %12, i32 0, i32 %11
%14 = call i32 @SHA1_Init(i32* %3)
%15 = call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%16 = call i32 @read(i32 %13, i8* %15, i32 1048576)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %2, %18
%19 = phi i32 [ %21, %18 ], [ %16, %2 ]
%20 = call i32 @SHA1_Update(i32* %3, i8* %15, i32 %19)
%21 = call i32 @read(i32 %13, i8* %15, i32 1048576)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %18
23: ; preds = %18, %2
%24 = call i32 @SHA1_Final(i8* %6, i32* %3)
%25 = icmp eq i32 %13, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @close(i32 %13)
br label %28
28: ; preds = %26, %23
%29 = load i32, i32* @SHA_DIGEST_LENGTH
%30 = call i32 @b64_ntop(i8* %6, i32 %29, i8* %15, i32 1048576)
%31 = call i32 @write(i32 1, i8* %15, i32 %30)
%32 = call i32 @free(i8* %15)
ret i32 0
}