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

/* Variables and functions */
scalar_t__ SHA1_Update(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int stream_data(int infd, int outfd, off_t *total_written,
                ssize_t expected_length, SHA_CTX *hash_ctx) {
  char buf[(32 * 1024)];
  *total_written = 0;
  while (expected_length != 0) {
    ssize_t amt_wanted;
    if (expected_length >= 0 && expected_length < (32 * 1024)) {
      amt_wanted = expected_length;
    } else {
      amt_wanted = (32 * 1024);
    }
    ssize_t amt_read = read(infd, buf, amt_wanted);
    if (amt_read == -1) {
      perror("Reading incoming data failed");
      return 0;
    }
    if (amt_read == 0) {
      if (expected_length > 0) {
        fprintf(stderr, "Input was %lld bytes shorter than expected.\n",
                (long long)expected_length);
        return 0;
      } else {
        return 1;
      }
    }
    if (SHA1_Update(hash_ctx, buf, amt_read) == 0) {
      fprintf(stderr, "Hashing failed.\n");
      return 0;
    }
    ssize_t amt_written = 0;
    while (amt_written < amt_read) {
      ssize_t amt_written_now =
          write(outfd, buf + amt_written, amt_read - amt_written);
      if (amt_written_now == -1) {
        perror("Writing to data file failed");
        return 0;
      }
      amt_written += amt_written_now;
      *total_written += amt_written_now;
      if (expected_length >= 0) {
        expected_length -= amt_written_now;
      }
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<stream_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8040,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpl   $0x0,-0x1c(%rbp)
je     132e <stream_data+0x1de>
cmpl   $0x0,-0x1c(%rbp)
jl     11a6 <stream_data+0x56>
cmpl   $0x8000,-0x1c(%rbp)
jge    11a6 <stream_data+0x56>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8034(%rbp)
jmp    11b0 <stream_data+0x60>
movl   $0x8000,-0x8034(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x8030(%rbp),%rsi
mov    -0x8034(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x8038(%rbp)
cmpl   $0xffffffff,-0x8038(%rbp)
jne    11f0 <stream_data+0xa0>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1335 <stream_data+0x1e5>
cmpl   $0x0,-0x8038(%rbp)
jne    123a <stream_data+0xea>
cmpl   $0x0,-0x1c(%rbp)
jle    122e <stream_data+0xde>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
movslq -0x1c(%rbp),%rdx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1335 <stream_data+0x1e5>
movl   $0x1,-0x4(%rbp)
jmp    1335 <stream_data+0x1e5>
mov    -0x28(%rbp),%rdi
lea    -0x8030(%rbp),%rsi
mov    -0x8038(%rbp),%edx
call   1060 <SHA1_Update@plt>
cmp    $0x0,%rax
jne    127d <stream_data+0x12d>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1335 <stream_data+0x1e5>
movl   $0x0,-0x803c(%rbp)
mov    -0x803c(%rbp),%eax
cmp    -0x8038(%rbp),%eax
jge    1329 <stream_data+0x1d9>
mov    -0xc(%rbp),%edi
lea    -0x8030(%rbp),%rsi
movslq -0x803c(%rbp),%rax
add    %rax,%rsi
mov    -0x8038(%rbp),%edx
sub    -0x803c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x8040(%rbp)
cmpl   $0xffffffff,-0x8040(%rbp)
jne    12e9 <stream_data+0x199>
lea    0xd83(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1335 <stream_data+0x1e5>
mov    -0x8040(%rbp),%eax
add    -0x803c(%rbp),%eax
mov    %eax,-0x803c(%rbp)
movslq -0x8040(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
cmpl   $0x0,-0x1c(%rbp)
jl     1324 <stream_data+0x1d4>
mov    -0x8040(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1287 <stream_data+0x137>
jmp    1177 <stream_data+0x27>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x8040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stream_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8018,%rsp
mov    %r8,0x8(%rsp)
mov    %edi,0x4(%rsp)
movq   $0x0,(%rdx)
mov    $0x1,%r14d
test   %ecx,%ecx
je     1293 <stream_data+0x143>
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %esi,%r12d
lea    0x10(%rsp),%r13
xor    %r15d,%r15d
jmp    11ad <stream_data+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
lea    0x10(%rsp),%r13
je     128d <stream_data+0x13d>
cmp    $0x8000,%ebp
mov    $0x8000,%edx
cmovb  %ebp,%edx
mov    0x4(%rsp),%edi
mov    %r13,%rsi
call   1040 <read@plt>
test   %eax,%eax
je     1249 <stream_data+0xf9>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     12a8 <stream_data+0x158>
mov    0x8(%rsp),%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1060 <SHA1_Update@plt>
test   %rax,%rax
je     1271 <stream_data+0x121>
test   %r14d,%r14d
jle    11a0 <stream_data+0x50>
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
movslq %r13d,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    %r14d,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1238 <stream_data+0xe8>
add    %eax,%r13d
movslq %eax,%rcx
add    %rcx,(%rbx)
test   %ebp,%ebp
cmovs  %r15d,%eax
sub    %eax,%ebp
cmp    %r13d,%r14d
jg     1200 <stream_data+0xb0>
jmp    11a0 <stream_data+0x50>
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
xor    %r14d,%r14d
jmp    1293 <stream_data+0x143>
test   %ebp,%ebp
mov    $0x1,%r14d
jle    1293 <stream_data+0x143>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%edx
lea    0xdb8(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1293 <stream_data+0x143>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1293 <stream_data+0x143>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd51(%rip),%rdi
jmp    123f <stream_data+0xef>

LLVM IR (-O0):
@.str = [29 x i8] c"Reading incoming data failed\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"Input was %lld bytes shorter than expected.\0A\00"
@.str.2 = [17 x i8] c"Hashing failed.\0A\00"
@.str.3 = [28 x i8] c"Writing to data file failed\00"
define i32 @stream_data(i32 %0, i32 %1, i64* %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca [32768 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i64* %2, i64** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%17 = load i64*, i64** %9
store i64 0, i64* %17
br label %18
18: ; preds = %95, %5
%19 = load i32, i32* %10
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %96
21: ; preds = %18
%22 = load i32, i32* %10
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 32768
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i32, i32* %10
store i32 %28, i32* %13
br label %30
29: ; preds = %24, %21
store i32 32768, i32* %13
br label %30
30: ; preds = %29, %27
%31 = load i32, i32* %7
%32 = getelementptr inbounds [32768 x i8], [32768 x i8]* %12, i64 0, i64 0
%33 = load i32, i32* %13
%34 = call i32 @read(i32 %31, i8* %32, i32 %33)
store i32 %34, i32* %14
%35 = load i32, i32* %14
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %97
39: ; preds = %30
%40 = load i32, i32* %14
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = load i32, i32* %10
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %48)
store i32 0, i32* %6
br label %97
50: ; preds = %42
store i32 1, i32* %6
br label %97
51: ; preds = %39
%52 = load i32*, i32** %11
%53 = getelementptr inbounds [32768 x i8], [32768 x i8]* %12, i64 0, i64 0
%54 = load i32, i32* %14
%55 = call i64 @SHA1_Update(i32* %52, i8* %53, i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %97
60: ; preds = %51
store i32 0, i32* %15
br label %61
61: ; preds = %94, %60
%62 = load i32, i32* %15
%63 = load i32, i32* %14
%64 = icmp slt i32 %62, %63
br i1 %64, label %65, label %95
65: ; preds = %61
%66 = load i32, i32* %8
%67 = getelementptr inbounds [32768 x i8], [32768 x i8]* %12, i64 0, i64 0
%68 = load i32, i32* %15
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
%71 = load i32, i32* %14
%72 = load i32, i32* %15
%73 = sub nsw i32 %71, %72
%74 = call i32 @write(i32 %66, i8* %70, i32 %73)
store i32 %74, i32* %16
%75 = load i32, i32* %16
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %79
77: ; preds = %65
%78 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %6
br label %97
79: ; preds = %65
%80 = load i32, i32* %16
%81 = load i32, i32* %15
%82 = add nsw i32 %81, %80
store i32 %82, i32* %15
%83 = load i32, i32* %16
%84 = sext i32 %83 to i64
%85 = load i64*, i64** %9
%86 = load i64, i64* %85
%87 = add nsw i64 %86, %84
store i64 %87, i64* %85
%88 = load i32, i32* %10
%89 = icmp sge i32 %88, 0
br i1 %89, label %90, label %94
90: ; preds = %79
%91 = load i32, i32* %16
%92 = load i32, i32* %10
%93 = sub nsw i32 %92, %91
store i32 %93, i32* %10
br label %94
94: ; preds = %90, %79
br label %61
95: ; preds = %61
br label %18
96: ; preds = %18
store i32 1, i32* %6
br label %97
97: ; preds = %96, %77, %57, %50, %45, %37
%98 = load i32, i32* %6
ret i32 %98
}

LLVM IR (-O2):
@.str = [29 x i8] c"Reading incoming data failed\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"Input was %lld bytes shorter than expected.\0A\00"
@.str.2 = [17 x i8] c"Hashing failed.\0A\00"
@.str.3 = [28 x i8] c"Writing to data file failed\00"
define i32 @stream_data(i32 %0, i32 %1, i64* nocapture %2, i32 %3, i32* %4) {
%6 = alloca [32768 x i8]
%7 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
store i64 0, i64* %2
%8 = icmp eq i32 %3, 0
br i1 %8, label %52, label %12
9: ; preds = %43, %28
%10 = phi i32 [ %13, %28 ], [ %50, %43 ]
%11 = icmp eq i32 %10, 0
br i1 %11, label %52, label %12
12: ; preds = %5, %9
%13 = phi i32 [ %10, %9 ], [ %3, %5 ]
%14 = icmp ult i32 %13, 32768
%15 = select i1 %14, i32 %13, i32 32768
%16 = call i32 @read(i32 %0, i8* %7, i32 %15)
switch i32 %16, label %25 [
i32 -1, label %17
i32 0, label %19
]
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %52
19: ; preds = %12
%20 = icmp sgt i32 %13, 0
br i1 %20, label %21, label %52
21: ; preds = %19
%22 = load i32, i32* @stderr
%23 = zext i32 %13 to i64
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %23)
br label %52
25: ; preds = %12
%26 = call i64 @SHA1_Update(i32* %4, i8* %7, i32 %16)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = icmp sgt i32 %16, 0
br i1 %29, label %33, label %9
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %52
33: ; preds = %28, %43
%34 = phi i32 [ %44, %43 ], [ 0, %28 ]
%35 = phi i32 [ %50, %43 ], [ %13, %28 ]
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 %36
%38 = sub nsw i32 %16, %34
%39 = call i32 @write(i32 %1, i8* %37, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %52
43: ; preds = %33
%44 = add nsw i32 %39, %34
%45 = sext i32 %39 to i64
%46 = load i64, i64* %2
%47 = add nsw i64 %46, %45
store i64 %47, i64* %2
%48 = icmp slt i32 %35, 0
%49 = select i1 %48, i32 0, i32 %39
%50 = sub nsw i32 %35, %49
%51 = icmp sgt i32 %16, %44
br i1 %51, label %33, label %9
52: ; preds = %9, %5, %41, %19, %30, %21, %17
%53 = phi i32 [ 0, %41 ], [ 1, %19 ], [ 0, %30 ], [ 0, %21 ], [ 0, %17 ], [ 1, %5 ], [ 1, %9 ]
ret i32 %53
}