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

/* Variables and functions */
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
ssize_t seek_by_read(FILE *fd, size_t len) {
  ssize_t cc, total = 0;
  char buf[4096];
  while (len) {
    cc = fread(buf, sizeof(buf[0]), len > 4096 ? 4096 : len, fd);
    total += cc;
    len -= cc;
    if (feof(fd) || ferror(fd))
      break;
  }
  return total;
}


x86 assembly (clang, -O0):
<seek_by_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11fb <seek_by_read+0xcb>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
cmpq   $0x1000,-0x10(%rbp)
jbe    1183 <seek_by_read+0x53>
mov    $0x1000,%eax
mov    %rax,-0x1030(%rbp)
jmp    118e <seek_by_read+0x5e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1028(%rbp),%rdi
mov    -0x1030(%rbp),%rax
mov    %eax,%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
jne    11f1 <seek_by_read+0xc1>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11f6 <seek_by_read+0xc6>
jmp    11fb <seek_by_read+0xcb>
jmp    114b <seek_by_read+0x1b>
mov    -0x20(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seek_by_read>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1000,%rsp
test   %rsi,%rsi
je     11a7 <seek_by_read+0x77>
mov    %rsi,%r13
mov    %rdi,%r15
xor    %r14d,%r14d
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x1000,%r13
mov    $0x1000,%edx
cmovb  %r13d,%edx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
add    %rax,%r14
mov    %r15,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    11aa <seek_by_read+0x7a>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11aa <seek_by_read+0x7a>
sub    %rbx,%r13
jne    1160 <seek_by_read+0x30>
jmp    11aa <seek_by_read+0x7a>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @seek_by_read(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca [4096 x i8]
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 0, i64* %6
br label %8
8: ; preds = %37, %2
%9 = load i64, i64* %4
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %8
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%13 = load i64, i64* %4
%14 = icmp ugt i64 %13, 4096
br i1 %14, label %15, label %16
15: ; preds = %11
br label %18
16: ; preds = %11
%17 = load i64, i64* %4
br label %18
18: ; preds = %16, %15
%19 = phi i64 [ 4096, %15 ], [ %17, %16 ]
%20 = trunc i64 %19 to i32
%21 = load i32*, i32** %3
%22 = call i64 @fread(i8* %12, i32 1, i32 %20, i32* %21)
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = load i64, i64* %6
%25 = add nsw i64 %24, %23
store i64 %25, i64* %6
%26 = load i64, i64* %5
%27 = load i64, i64* %4
%28 = sub i64 %27, %26
store i64 %28, i64* %4
%29 = load i32*, i32** %3
%30 = call i64 @feof(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %18
%33 = load i32*, i32** %3
%34 = call i64 @ferror(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32, %18
br label %38
37: ; preds = %32
br label %8
38: ; preds = %36, %8
%39 = load i64, i64* %6
ret i64 %39
}

LLVM IR (-O2):
define i64 @seek_by_read(i32* nocapture %0, i64 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i64 %1, 0
br i1 %5, label %22, label %6
6: ; preds = %2, %17
%7 = phi i64 [ %14, %17 ], [ %1, %2 ]
%8 = phi i64 [ %13, %17 ], [ 0, %2 ]
%9 = icmp ult i64 %7, 4096
%10 = select i1 %9, i64 %7, i64 4096
%11 = trunc i64 %10 to i32
%12 = call i64 @fread(i8* %4, i32 1, i32 %11, i32* %0)
%13 = add nsw i64 %12, %8
%14 = sub i64 %7, %12
%15 = tail call i64 @feof(i32* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %6
%18 = tail call i64 @ferror(i32* %0)
%19 = icmp eq i64 %18, 0
%20 = icmp ne i64 %14, 0
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %6, label %22
22: ; preds = %6, %17, %2
%23 = phi i64 [ 0, %2 ], [ %13, %17 ], [ %13, %6 ]
ret i64 %23
}