#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 {
  int st_size;
};
typedef int /*<<< orphan*/ EVP_MD_CTX;
typedef int /*<<< orphan*/ EVP_MD;

/* Variables and functions */
int /*<<< orphan*/ EVP_DigestFinal_ex(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestInit_ex(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ const *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                    int);
int /*<<< orphan*/ *EVP_MD_CTX_create();
int /*<<< orphan*/ EVP_MD_CTX_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_sha1();
int O_NOFOLLOW;
int O_RDONLY;
int TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ *malloc(int);
int open(char *, int);
int /*<<< orphan*/ read(int, int /*<<< orphan*/ *, int);
int get_file_info(char *filename, int *size, uint8_t *hash) {
  int fd;
  struct stat stat_buf;
  const EVP_MD *m;
  EVP_MD_CTX *ctx;
  uint8_t *read_buf;
  fd = open(filename, O_RDONLY | O_NOFOLLOW);
  if (fd < 0)
    return 0;
  if (fstat(fd, &stat_buf) < 0) {
    close(fd);
    return 0;
  }
  *size = stat_buf.st_size;
  if (!(ctx = EVP_MD_CTX_create())) {
    close(fd);
    return 0;
  }
  if (!(m = EVP_sha1())) {
    close(fd);
    EVP_MD_CTX_destroy(ctx);
    return 0;
  }
  if (!EVP_DigestInit_ex(ctx, m, NULL)) {
    close(fd);
    EVP_MD_CTX_destroy(ctx);
    return 0;
  }
  read_buf = malloc(65536);
  while (1) {
    int b = TEMP_FAILURE_RETRY(read(fd, read_buf, 65536));
    if (b < 0) {
      close(fd);
      EVP_MD_CTX_destroy(ctx);
      free(read_buf);
      return 0;
    }
    if (b == 0)
      break;
    EVP_DigestUpdate(ctx, read_buf, b);
  }
  EVP_DigestFinal_ex(ctx, hash, NULL);
  close(fd);
  EVP_MD_CTX_destroy(ctx);
  free(read_buf);
  return 1;
}


x86 assembly (clang, -O0):
<get_file_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1218 <get_file_info+0x48>
movl   $0x0,-0x4(%rbp)
jmp    138c <get_file_info+0x1bc>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10e0 <fstat@plt>
cmp    $0x0,%rax
jge    1242 <get_file_info+0x72>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    138c <get_file_info+0x1bc>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x0,%al
call   10f0 <EVP_MD_CTX_create@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1274 <get_file_info+0xa4>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    138c <get_file_info+0x1bc>
mov    $0x0,%al
call   1050 <EVP_sha1@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12a6 <get_file_info+0xd6>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    138c <get_file_info+0x1bc>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <EVP_DigestInit_ex@plt>
cmp    $0x0,%eax
jne    12dd <get_file_info+0x10d>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    138c <get_file_info+0x1bc>
mov    $0x10000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    $0x10000,%edx
call   1070 <read@plt>
mov    %eax,%edi
call   10d0 <TEMP_FAILURE_RETRY@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1336 <get_file_info+0x166>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    138c <get_file_info+0x1bc>
cmpl   $0x0,-0x44(%rbp)
jne    1345 <get_file_info+0x175>
jmp    135a <get_file_info+0x18a>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1090 <EVP_DigestUpdate@plt>
jmp    12eb <get_file_info+0x11b>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <EVP_DigestFinal_ex@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_info>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2dfb(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   10c0 <open@plt>
xor    %r12d,%r12d
test   %eax,%eax
js     12fa <get_file_info+0x12a>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10e0 <fstat@plt>
test   %rax,%rax
js     129d <get_file_info+0xcd>
mov    (%rsp),%eax
mov    %eax,(%r15)
xor    %r12d,%r12d
xor    %eax,%eax
call   10f0 <EVP_MD_CTX_create@plt>
test   %rax,%rax
je     129d <get_file_info+0xcd>
mov    %rax,%r15
xor    %r12d,%r12d
xor    %eax,%eax
call   1050 <EVP_sha1@plt>
test   %rax,%rax
je     12a6 <get_file_info+0xd6>
xor    %r12d,%r12d
mov    %r15,%rdi
mov    %rax,%rsi
xor    %edx,%edx
call   1080 <EVP_DigestInit_ex@plt>
test   %eax,%eax
je     12a6 <get_file_info+0xd6>
mov    $0x10000,%edi
call   10a0 <malloc@plt>
mov    %rax,%r12
mov    %ebx,%edi
mov    %rax,%rsi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x10000,%edx
call   1070 <read@plt>
mov    %eax,%edi
call   10d0 <TEMP_FAILURE_RETRY@plt>
test   %eax,%eax
js     12b7 <get_file_info+0xe7>
mov    %r15,%rdi
test   %eax,%eax
je     12d3 <get_file_info+0x103>
mov    %r12,%rsi
mov    %eax,%edx
call   1090 <EVP_DigestUpdate@plt>
mov    %ebx,%edi
mov    %r12,%rsi
jmp    1270 <get_file_info+0xa0>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    12fa <get_file_info+0x12a>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15,%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
jmp    12fa <get_file_info+0x12a>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15,%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
jmp    12fa <get_file_info+0x12a>
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <EVP_DigestFinal_ex@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15,%rdi
call   10b0 <EVP_MD_CTX_destroy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_NOFOLLOW = global i32 0
define i32 @get_file_info(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%14 = load i8*, i8** %5
%15 = load i32, i32* @O_RDONLY
%16 = load i32, i32* @O_NOFOLLOW
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 0, i32* %4
br label %91
22: ; preds = %3
%23 = load i32, i32* %8
%24 = call i64 @fstat(i32 %23, %struct.stat* %9)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
store i32 0, i32* %4
br label %91
29: ; preds = %22
%30 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32*, i32** %6
store i32 %31, i32* %32
%33 = call i32* (...) @EVP_MD_CTX_create()
store i32* %33, i32** %11
%34 = icmp ne i32* %33, null
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
store i32 0, i32* %4
br label %91
38: ; preds = %29
%39 = call i32* (...) @EVP_sha1()
store i32* %39, i32** %10
%40 = icmp ne i32* %39, null
br i1 %40, label %46, label %41
41: ; preds = %38
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
%44 = load i32*, i32** %11
%45 = call i32 @EVP_MD_CTX_destroy(i32* %44)
store i32 0, i32* %4
br label %91
46: ; preds = %38
%47 = load i32*, i32** %11
%48 = load i32*, i32** %10
%49 = call i32 @EVP_DigestInit_ex(i32* %47, i32* %48, i32* null)
%50 = icmp ne i32 %49, 0
br i1 %50, label %56, label %51
51: ; preds = %46
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
%54 = load i32*, i32** %11
%55 = call i32 @EVP_MD_CTX_destroy(i32* %54)
store i32 0, i32* %4
br label %91
56: ; preds = %46
%57 = call i32* @malloc(i32 65536)
store i32* %57, i32** %12
br label %58
58: ; preds = %56, %76
%59 = load i32, i32* %8
%60 = load i32*, i32** %12
%61 = call i32 @read(i32 %59, i32* %60, i32 65536)
%62 = call i32 @TEMP_FAILURE_RETRY(i32 %61)
store i32 %62, i32* %13
%63 = load i32, i32* %13
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %58
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
%68 = load i32*, i32** %11
%69 = call i32 @EVP_MD_CTX_destroy(i32* %68)
%70 = load i32*, i32** %12
%71 = call i32 @free(i32* %70)
store i32 0, i32* %4
br label %91
72: ; preds = %58
%73 = load i32, i32* %13
%74 = icmp eq i32 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %72
br label %81
76: ; preds = %72
%77 = load i32*, i32** %11
%78 = load i32*, i32** %12
%79 = load i32, i32* %13
%80 = call i32 @EVP_DigestUpdate(i32* %77, i32* %78, i32 %79)
br label %58
81: ; preds = %75
%82 = load i32*, i32** %11
%83 = load i32*, i32** %7
%84 = call i32 @EVP_DigestFinal_ex(i32* %82, i32* %83, i32* null)
%85 = load i32, i32* %8
%86 = call i32 @close(i32 %85)
%87 = load i32*, i32** %11
%88 = call i32 @EVP_MD_CTX_destroy(i32* %87)
%89 = load i32*, i32** %12
%90 = call i32 @free(i32* %89)
store i32 1, i32* %4
br label %91
91: ; preds = %81, %65, %51, %41, %35, %26, %21
%92 = load i32, i32* %4
ret i32 %92
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_NOFOLLOW = global i32 0
define i32 @get_file_info(i8* nocapture %0, i32* nocapture writeonly %1, i32* %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_NOFOLLOW
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %57, label %11
11: ; preds = %3
%12 = call i64 @fstat(i32 %9, %struct.stat* %4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @close(i32 %9)
br label %57
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %1
%19 = tail call i32* (...) @EVP_MD_CTX_create()
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @close(i32 %9)
br label %57
23: ; preds = %16
%24 = tail call i32* (...) @EVP_sha1()
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i32 @close(i32 %9)
%28 = tail call i32 @EVP_MD_CTX_destroy(i32* %19)
br label %57
29: ; preds = %23
%30 = tail call i32 @EVP_DigestInit_ex(i32* %19, i32* %24, i32* null)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = tail call i32 @close(i32 %9)
%34 = tail call i32 @EVP_MD_CTX_destroy(i32* %19)
br label %57
35: ; preds = %29
%36 = tail call i32* @malloc(i32 65536)
%37 = tail call i32 @read(i32 %9, i32* %36, i32 65536)
%38 = tail call i32 @TEMP_FAILURE_RETRY(i32 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %47, %35
%41 = tail call i32 @close(i32 %9)
%42 = tail call i32 @EVP_MD_CTX_destroy(i32* %19)
%43 = tail call i32 @free(i32* %36)
br label %57
44: ; preds = %35, %47
%45 = phi i32 [ %50, %47 ], [ %38, %35 ]
%46 = icmp eq i32 %45, 0
br i1 %46, label %52, label %47
47: ; preds = %44
%48 = tail call i32 @EVP_DigestUpdate(i32* %19, i32* %36, i32 %45)
%49 = tail call i32 @read(i32 %9, i32* %36, i32 65536)
%50 = tail call i32 @TEMP_FAILURE_RETRY(i32 %49)
%51 = icmp slt i32 %50, 0
br i1 %51, label %40, label %44
52: ; preds = %44
%53 = tail call i32 @EVP_DigestFinal_ex(i32* %19, i32* %2, i32* null)
%54 = tail call i32 @close(i32 %9)
%55 = tail call i32 @EVP_MD_CTX_destroy(i32* %19)
%56 = tail call i32 @free(i32* %36)
br label %57
57: ; preds = %40, %3, %52, %32, %26, %21, %14
%58 = phi i32 [ 0, %14 ], [ 1, %52 ], [ 0, %32 ], [ 0, %26 ], [ 0, %21 ], [ 0, %3 ], [ 0, %40 ]
ret i32 %58
}