#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 fuzzy_state {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fuzzy_digest(struct fuzzy_state *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fuzzy_free(struct fuzzy_state *);
struct fuzzy_state *fuzzy_new();
scalar_t__ fuzzy_update(struct fuzzy_state *, unsigned char *, size_t);
int fuzzy_hash_stream(FILE *handle, char *result) {
  struct fuzzy_state *ctx;
  unsigned char buffer[4096];
  size_t n;
  int ret = -1;
  if (NULL == (ctx = fuzzy_new()))
    return -1;
  for (;;) {
    n = fread(buffer, 1, 4096, handle);
    if (0 == n)
      break;
    if (fuzzy_update(ctx, buffer, n) < 0)
      goto out;
  }
  if (ferror(handle) != 0)
    goto out;
  if (fuzzy_digest(ctx, result, 0) < 0)
    goto out;
  ret = 0;
out:
  fuzzy_free(ctx);
  return ret;
}


x86 assembly (clang, -O0):
<fuzzy_hash_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x102c(%rbp)
ff ff ff
mov    $0x0,%al
call   1070 <fuzzy_new@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11a2 <fuzzy_hash_stream+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <fuzzy_hash_stream+0xf9>
jmp    11a7 <fuzzy_hash_stream+0x47>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1060 <fread@plt>
mov    %rax,-0x1028(%rbp)
xor    %eax,%eax
cmp    -0x1028(%rbp),%rax
jne    11dc <fuzzy_hash_stream+0x7c>
jmp    1207 <fuzzy_hash_stream+0xa7>
mov    -0x20(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1040 <fuzzy_update@plt>
cmp    $0x0,%rax
jge    1202 <fuzzy_hash_stream+0xa2>
jmp    1247 <fuzzy_hash_stream+0xe7>
jmp    11a7 <fuzzy_hash_stream+0x47>
mov    -0x10(%rbp),%rdi
call   1050 <ferror@plt>
cmp    $0x0,%rax
je     121f <fuzzy_hash_stream+0xbf>
jmp    1247 <fuzzy_hash_stream+0xe7>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <fuzzy_digest@plt>
cmp    $0x0,%rax
jge    123d <fuzzy_hash_stream+0xdd>
jmp    1247 <fuzzy_hash_stream+0xe7>
movl   $0x0,-0x102c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fuzzy_free@plt>
mov    -0x102c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fuzzy_hash_stream>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
xor    %eax,%eax
call   1070 <fuzzy_new@plt>
test   %rax,%rax
je     11c4 <fuzzy_hash_stream+0x64>
mov    %rax,%r15
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1060 <fread@plt>
test   %rax,%rax
je     11cb <fuzzy_hash_stream+0x6b>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <fuzzy_update@plt>
test   %rax,%rax
jns    1190 <fuzzy_hash_stream+0x30>
mov    $0xffffffff,%ebx
jmp    11f1 <fuzzy_hash_stream+0x91>
mov    $0xffffffff,%ebx
jmp    11f9 <fuzzy_hash_stream+0x99>
mov    %r12,%rdi
call   1050 <ferror@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11f1 <fuzzy_hash_stream+0x91>
mov    %r15,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1080 <fuzzy_digest@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
mov    %r15,%rdi
call   1030 <fuzzy_free@plt>
mov    %ebx,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.fuzzy_state = type { i32 }
define i32 @fuzzy_hash_stream(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca %struct.fuzzy_state*
%7 = alloca [4096 x i8]
%8 = alloca i64
%9 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 -1, i32* %9
%10 = call %struct.fuzzy_state* (...) @fuzzy_new()
store %struct.fuzzy_state* %10, %struct.fuzzy_state** %6
%11 = icmp eq %struct.fuzzy_state* null, %10
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %45
13: ; preds = %2
br label %14
14: ; preds = %28, %13
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%16 = load i32*, i32** %4
%17 = call i64 @fread(i8* %15, i32 1, i32 4096, i32* %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp eq i64 0, %18
br i1 %19, label %20, label %21
20: ; preds = %14
br label %29
21: ; preds = %14
%22 = load %struct.fuzzy_state*, %struct.fuzzy_state** %6
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%24 = load i64, i64* %8
%25 = call i64 @fuzzy_update(%struct.fuzzy_state* %22, i8* %23, i64 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
br label %41
28: ; preds = %21
br label %14
29: ; preds = %20
%30 = load i32*, i32** %4
%31 = call i64 @ferror(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
br label %41
34: ; preds = %29
%35 = load %struct.fuzzy_state*, %struct.fuzzy_state** %6
%36 = load i8*, i8** %5
%37 = call i64 @fuzzy_digest(%struct.fuzzy_state* %35, i8* %36, i32 0)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
br label %41
40: ; preds = %34
store i32 0, i32* %9
br label %41
41: ; preds = %40, %39, %33, %27
%42 = load %struct.fuzzy_state*, %struct.fuzzy_state** %6
%43 = call i32 @fuzzy_free(%struct.fuzzy_state* %42)
%44 = load i32, i32* %9
store i32 %44, i32* %3
br label %45
45: ; preds = %41, %12
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.fuzzy_state = type { i32 }
define i32 @fuzzy_hash_stream(i32* nocapture %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call %struct.fuzzy_state* (...) @fuzzy_new()
%6 = icmp eq %struct.fuzzy_state* %5, null
br i1 %6, label %23, label %7
7: ; preds = %2, %10
%8 = call i64 @fread(i8* %4, i32 1, i32 4096, i32* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = call i64 @fuzzy_update(%struct.fuzzy_state* %5, i8* %4, i64 %8)
%12 = icmp slt i64 %11, 0
br i1 %12, label %20, label %7
13: ; preds = %7
%14 = call i64 @ferror(i32* %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i64 @fuzzy_digest(%struct.fuzzy_state* %5, i8* %1, i32 0)
%18 = ashr i64 %17, 63
%19 = trunc i64 %18 to i32
br label %20
20: ; preds = %10, %16, %13
%21 = phi i32 [ -1, %13 ], [ %19, %16 ], [ -1, %10 ]
%22 = call i32 @fuzzy_free(%struct.fuzzy_state* %5)
br label %23
23: ; preds = %2, %20
%24 = phi i32 [ %21, %20 ], [ -1, %2 ]
ret i32 %24
}