#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 sha512_ctx {
  int dummy;
};
typedef int ssize_t;
typedef int off_t;

/* Variables and functions */
int EINTR;
int ENOMEM;
int ESPIPE;
int errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int pread(int, char *, int, int);
int /*<<< orphan*/ sha512_process_block(char *, int, struct sha512_ctx *);
int /*<<< orphan*/ sha512_process_bytes(char *, size_t, struct sha512_ctx *);
int sha512_file_ctx(int fd, off_t offset, size_t count,
                    struct sha512_ctx *ctx) {
  size_t sum, total = 0;
  char *buffer = malloc(32768 + 72);
  if (!buffer)
    return -ENOMEM;
  while (1) {
    ssize_t n;
    sum = 0;
    while (1) {
      n = pread(fd, buffer + sum, 32768 - sum, offset);
      if (n == -1) {
        if (errno == EINTR) {
          continue;
        } else {
          free(buffer);
          return -errno;
        }
      }
      sum += n;
      offset += n;
      if (sum == 32768)
        break;
      if (n == 0) {
        if (total + sum < count) {
          free(buffer);
          return -ESPIPE;
        } else {
          goto process_partial_block;
        }
      }
    }
    if (total + sum < count) {
      total += sum;
    } else {
      goto process_partial_block;
    }
    sha512_process_block(buffer, 32768, ctx);
  }
process_partial_block:
  sum = count - total;
  if (sum > 0)
    sha512_process_bytes(buffer, sum, ctx);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<sha512_file_ctx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x8048,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    119a <sha512_file_ctx+0x4a>
xor    %eax,%eax
mov    0x2e48(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ec <sha512_file_ctx+0x19c>
jmp    119f <sha512_file_ctx+0x4f>
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    $0x8000,%eax
sub    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0xc(%rbp),%ecx
call   1070 <pread@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    120b <sha512_file_ctx+0xbb>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%eax
jne    11ef <sha512_file_ctx+0x9f>
jmp    11a7 <sha512_file_ctx+0x57>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    0x2dcf(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ec <sha512_file_ctx+0x19c>
movslq -0x3c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x3c(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpq   $0x8000,-0x28(%rbp)
jne    1233 <sha512_file_ctx+0xe3>
jmp    1275 <sha512_file_ctx+0x125>
cmpl   $0x0,-0x3c(%rbp)
jne    1270 <sha512_file_ctx+0x120>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    126b <sha512_file_ctx+0x11b>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    0x2d87(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ec <sha512_file_ctx+0x19c>
jmp    12b4 <sha512_file_ctx+0x164>
jmp    11a7 <sha512_file_ctx+0x57>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1298 <sha512_file_ctx+0x148>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    129d <sha512_file_ctx+0x14d>
jmp    12b4 <sha512_file_ctx+0x164>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x8000,%esi
call   1040 <sha512_process_block@plt>
jmp    119f <sha512_file_ctx+0x4f>
mov    -0x18(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    12dc <sha512_file_ctx+0x18c>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <sha512_process_bytes@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha512_file_ctx>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebp
mov    %edi,%r13d
mov    $0x8048,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1247 <sha512_file_ctx+0xf7>
mov    %rax,%rbx
mov    %r15,0x18(%rsp)
mov    %r14,0x10(%rsp)
xor    %eax,%eax
mov    %rax,(%rsp)
mov    0x2e32(%rip),%r12
mov    %rbx,0x8(%rsp)
xor    %ecx,%ecx
lea    (%rbx,%rcx,1),%r14
mov    $0x8000,%r15d
mov    %rcx,0x20(%rsp)
sub    %ecx,%r15d
nop
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r15d,%edx
mov    %ebp,%ecx
call   1070 <pread@plt>
cmp    $0xffffffff,%eax
jne    11e0 <sha512_file_ctx+0x90>
mov    0x2e04(%rip),%rax
mov    (%rax),%ebx
cmp    (%r12),%ebx
je     11b0 <sha512_file_ctx+0x60>
jmp    1239 <sha512_file_ctx+0xe9>
cs nopw 0x0(%rax,%rax,1)
movslq %eax,%rcx
mov    0x20(%rsp),%rdx
add    %rcx,%rdx
add    %eax,%ebp
cmp    $0x8000,%rdx
je     1204 <sha512_file_ctx+0xb4>
mov    %rdx,%rcx
test   %eax,%eax
mov    0x8(%rsp),%rbx
jne    119d <sha512_file_ctx+0x4d>
jmp    1263 <sha512_file_ctx+0x113>
mov    (%rsp),%rax
lea    0x8000(%rax),%r14
mov    0x18(%rsp),%rsi
cmp    %rsi,%r14
mov    0x8(%rsp),%rbx
jae    1285 <sha512_file_ctx+0x135>
mov    %rbx,%rdi
mov    $0x8000,%esi
mov    0x10(%rsp),%rdx
call   1040 <sha512_process_block@plt>
mov    %r14,(%rsp)
jmp    119b <sha512_file_ctx+0x4b>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
neg    %ebx
jmp    1252 <sha512_file_ctx+0x102>
mov    0x2d8a(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rax
add    %rax,%rcx
mov    0x18(%rsp),%rsi
cmp    %rsi,%rcx
jae    1285 <sha512_file_ctx+0x135>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2d65(%rip),%rax
jmp    124e <sha512_file_ctx+0xfe>
sub    %rax,%rsi
je     1297 <sha512_file_ctx+0x147>
mov    %rbx,%rdi
mov    0x10(%rsp),%rdx
call   1050 <sha512_process_bytes@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1252 <sha512_file_ctx+0x102>

LLVM IR (-O0):
%struct.sha512_ctx = type { i32 }
@ENOMEM = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@ESPIPE = global i32 0
define i32 @sha512_file_ctx(i32 %0, i32 %1, i64 %2, %struct.sha512_ctx* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca %struct.sha512_ctx*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store %struct.sha512_ctx* %3, %struct.sha512_ctx** %9
store i64 0, i64* %11
%14 = call i8* @malloc(i32 32840)
store i8* %14, i8** %12
%15 = load i8*, i8** %12
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %4
%18 = load i32, i32* @ENOMEM
%19 = sub nsw i32 0, %18
store i32 %19, i32* %5
br label %100
20: ; preds = %4
br label %21
21: ; preds = %20, %82
store i64 0, i64* %10
br label %22
22: ; preds = %21, %38, %70
%23 = load i32, i32* %6
%24 = load i8*, i8** %12
%25 = load i64, i64* %10
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i64, i64* %10
%28 = sub i64 32768, %27
%29 = trunc i64 %28 to i32
%30 = load i32, i32* %7
%31 = call i32 @pread(i32 %23, i8* %26, i32 %29, i32 %30)
store i32 %31, i32* %13
%32 = load i32, i32* %13
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %44
34: ; preds = %22
%35 = load i32, i32* @errno
%36 = load i32, i32* @EINTR
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34
br label %22
39: ; preds = %34
%40 = load i8*, i8** %12
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* @errno
%43 = sub nsw i32 0, %42
store i32 %43, i32* %5
br label %100
44: ; preds = %22
%45 = load i32, i32* %13
%46 = sext i32 %45 to i64
%47 = load i64, i64* %10
%48 = add i64 %47, %46
store i64 %48, i64* %10
%49 = load i32, i32* %13
%50 = load i32, i32* %7
%51 = add nsw i32 %50, %49
store i32 %51, i32* %7
%52 = load i64, i64* %10
%53 = icmp eq i64 %52, 32768
br i1 %53, label %54, label %55
54: ; preds = %44
br label %71
55: ; preds = %44
%56 = load i32, i32* %13
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %70
58: ; preds = %55
%59 = load i64, i64* %11
%60 = load i64, i64* %10
%61 = add i64 %59, %60
%62 = load i64, i64* %8
%63 = icmp ult i64 %61, %62
br i1 %63, label %64, label %69
64: ; preds = %58
%65 = load i8*, i8** %12
%66 = call i32 @free(i8* %65)
%67 = load i32, i32* @ESPIPE
%68 = sub nsw i32 0, %67
store i32 %68, i32* %5
br label %100
69: ; preds = %58
br label %86
70: ; preds = %55
br label %22
71: ; preds = %54
%72 = load i64, i64* %11
%73 = load i64, i64* %10
%74 = add i64 %72, %73
%75 = load i64, i64* %8
%76 = icmp ult i64 %74, %75
br i1 %76, label %77, label %81
77: ; preds = %71
%78 = load i64, i64* %10
%79 = load i64, i64* %11
%80 = add i64 %79, %78
store i64 %80, i64* %11
br label %82
81: ; preds = %71
br label %86
82: ; preds = %77
%83 = load i8*, i8** %12
%84 = load %struct.sha512_ctx*, %struct.sha512_ctx** %9
%85 = call i32 @sha512_process_block(i8* %83, i32 32768, %struct.sha512_ctx* %84)
br label %21
86: ; preds = %81, %69
%87 = load i64, i64* %8
%88 = load i64, i64* %11
%89 = sub i64 %87, %88
store i64 %89, i64* %10
%90 = load i64, i64* %10
%91 = icmp ugt i64 %90, 0
br i1 %91, label %92, label %97
92: ; preds = %86
%93 = load i8*, i8** %12
%94 = load i64, i64* %10
%95 = load %struct.sha512_ctx*, %struct.sha512_ctx** %9
%96 = call i32 @sha512_process_bytes(i8* %93, i64 %94, %struct.sha512_ctx* %95)
br label %97
97: ; preds = %92, %86
%98 = load i8*, i8** %12
%99 = call i32 @free(i8* %98)
store i32 0, i32* %5
br label %100
100: ; preds = %97, %64, %39, %17
%101 = load i32, i32* %5
ret i32 %101
}

LLVM IR (-O2):
%struct.sha512_ctx = type { i32 }
@ENOMEM = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@ESPIPE = global i32 0
define i32 @sha512_file_ctx(i32 %0, i32 %1, i64 %2, %struct.sha512_ctx* %3) {
%5 = tail call dereferenceable_or_null(32840) i8* @malloc(i32 32840)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @ENOMEM
%9 = sub nsw i32 0, %8
br label %55
10: ; preds = %4, %46
%11 = phi i64 [ %44, %46 ], [ 0, %4 ]
%12 = phi i32 [ %32, %46 ], [ %1, %4 ]
br label %13
13: ; preds = %34, %10
%14 = phi i64 [ %31, %34 ], [ 0, %10 ]
%15 = phi i32 [ %32, %34 ], [ %12, %10 ]
%16 = getelementptr inbounds i8, i8* %5, i64 %14
%17 = trunc i64 %14 to i32
%18 = sub i32 32768, %17
br label %19
19: ; preds = %13, %22
%20 = tail call i32 @pread(i32 %0, i8* %16, i32 %18, i32 %15)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = load i32, i32* @EINTR
%25 = icmp eq i32 %23, %24
br i1 %25, label %19, label %26
26: ; preds = %22
%27 = tail call i32 @free(i8* %5)
%28 = sub nsw i32 0, %23
br label %55
29: ; preds = %19
%30 = sext i32 %20 to i64
%31 = add i64 %14, %30
%32 = add nsw i32 %20, %15
%33 = icmp eq i64 %31, 32768
br i1 %33, label %43, label %34
34: ; preds = %29
%35 = icmp eq i32 %20, 0
br i1 %35, label %36, label %13
36: ; preds = %34
%37 = add i64 %31, %11
%38 = icmp ult i64 %37, %2
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = tail call i32 @free(i8* %5)
%41 = load i32, i32* @ESPIPE
%42 = sub nsw i32 0, %41
br label %55
43: ; preds = %29
%44 = add i64 %11, 32768
%45 = icmp ult i64 %44, %2
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = tail call i32 @sha512_process_block(i8* %5, i32 32768, %struct.sha512_ctx* %3)
br label %10
48: ; preds = %43, %36
%49 = sub i64 %2, %11
%50 = icmp eq i64 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %48
%52 = tail call i32 @sha512_process_bytes(i8* %5, i64 %49, %struct.sha512_ctx* %3)
br label %53
53: ; preds = %51, %48
%54 = tail call i32 @free(i8* %5)
br label %55
55: ; preds = %39, %26, %53, %7
%56 = phi i32 [ 0, %53 ], [ %9, %7 ], [ %42, %39 ], [ %28, %26 ]
ret i32 %56
}