#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*/ buf;

/* Variables and functions */
int vc_stream_read(int, char *, int);
unsigned int vc_scan_stream(int handle) {
  int rc;
  char buf[4096];
  unsigned int total = 0;
  do {
    rc = vc_stream_read(handle, buf, sizeof(buf));
    if (rc > 0) {
      total += rc;
    }
  } while ((rc == sizeof(buf)) && (total < (sizeof(buf) * 16)));
  return total;
}


x86 assembly (clang, -O0):
<vc_scan_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x1014(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <vc_stream_read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1158 <vc_scan_stream+0x48>
mov    -0x8(%rbp),%eax
add    -0x1014(%rbp),%eax
mov    %eax,-0x1014(%rbp)
jmp    115d <vc_scan_stream+0x4d>
movslq -0x8(%rbp),%rcx
xor    %eax,%eax
cmp    $0x1000,%rcx
mov    %al,-0x1015(%rbp)
jne    118b <vc_scan_stream+0x7b>
mov    -0x1014(%rbp),%eax
cmp    $0x10000,%rax
setb   %al
mov    %al,-0x1015(%rbp)
mov    -0x1015(%rbp),%al
test   $0x1,%al
jne    1128 <vc_scan_stream+0x18>
mov    -0x1014(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vc_scan_stream>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %edi,%ebx
xor    %eax,%eax
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1030 <vc_stream_read@plt>
mov    %eax,%ecx
test   %eax,%eax
mov    $0x0,%eax
cmovns %ecx,%eax
add    %ebp,%eax
cmp    $0x1000,%ecx
jne    115e <vc_scan_stream+0x4e>
cmp    $0x10000,%eax
jb     1130 <vc_scan_stream+0x20>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @vc_scan_stream(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
br label %6
6: ; preds = %25, %1
%7 = load i32, i32* %2
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%9 = call i32 @vc_stream_read(i32 %7, i8* %8, i32 4096)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = load i32, i32* %3
%14 = load i32, i32* %5
%15 = add i32 %14, %13
store i32 %15, i32* %5
br label %16
16: ; preds = %12, %6
br label %17
17: ; preds = %16
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = icmp eq i64 %19, 4096
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* %5
%23 = zext i32 %22 to i64
%24 = icmp ult i64 %23, 65536
br label %25
25: ; preds = %21, %17
%26 = phi i1 [ false, %17 ], [ %24, %21 ]
br i1 %26, label %6, label %27
27: ; preds = %25
%28 = load i32, i32* %5
ret i32 %28
}

LLVM IR (-O2):
define i32 @vc_scan_stream(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %4, %1
%5 = phi i32 [ 0, %1 ], [ %9, %4 ]
%6 = call i32 @vc_stream_read(i32 %0, i8* %3, i32 4096)
%7 = icmp sgt i32 %6, 0
%8 = select i1 %7, i32 %6, i32 0
%9 = add nuw i32 %8, %5
%10 = icmp eq i32 %6, 4096
%11 = icmp ult i32 %9, 65536
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %4, label %13
13: ; preds = %4
ret i32 %9
}