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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
unsigned char *SHA1(unsigned char *, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fstat(int, struct stat *);
unsigned char *malloc(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
unsigned int read(int, unsigned char *, int /*<<< orphan*/);
unsigned char *get_filehash(char *filename) {
  int fd;
  unsigned char *buf;
  unsigned char *hash = NULL;
  unsigned int len;
  struct stat s;
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    perror(filename);
    return NULL;
  }
  fstat(fd, &s);
  buf = malloc(s.st_size);
  if (!buf)
    exit(1);
  len = read(fd, buf, s.st_size);
  close(fd);
  hash = SHA1(buf, len, NULL);
  free(buf);
  return hash;
}


x86 assembly (clang, -O0):
<get_filehash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d9 <get_filehash+0x49>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1245 <get_filehash+0xb5>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10b0 <fstat@plt>
mov    -0x30(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1206 <get_filehash+0x76>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <SHA1@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filehash>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     120b <get_filehash+0x7b>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
mov    0x8(%rsp),%r14d
mov    %r14d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1217 <get_filehash+0x87>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %edx,%edx
call   1070 <SHA1@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
call   1090 <perror@plt>
xor    %eax,%eax
jmp    1202 <get_filehash+0x72>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i8* @get_filehash(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %3
store i8* null, i8** %6
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32 @perror(i8* %15)
store i8* null, i8** %2
br label %41
17: ; preds = %1
%18 = load i32, i32* %4
%19 = call i32 @fstat(i32 %18, %struct.stat* %8)
%20 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = icmp ne i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %17
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = load i32, i32* %4
%29 = load i8*, i8** %5
%30 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @read(i32 %28, i8* %29, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %5
%36 = load i32, i32* %7
%37 = call i8* @SHA1(i8* %35, i32 %36, i32* null)
store i8* %37, i8** %6
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
%40 = load i8*, i8** %6
store i8* %40, i8** %2
br label %41
41: ; preds = %27, %14
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i8* @get_filehash(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* %0)
br label %22
9: ; preds = %1
%10 = call i32 @fstat(i32 %5, %struct.stat* %2)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i8* @malloc(i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = tail call i32 @read(i32 %5, i8* %13, i32 %12)
%19 = tail call i32 @close(i32 %5)
%20 = tail call i8* @SHA1(i8* %13, i32 %18, i32* null)
%21 = tail call i32 @free(i8* %13)
br label %22
22: ; preds = %17, %7
%23 = phi i8* [ null, %7 ], [ %20, %17 ]
ret i8* %23
}