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

/* Variables and functions */
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, int);
size_t fread_all(const void *buf, size_t size, FILE *stream, int readlen) {
  size_t total = 0;
  off_t bytesleft = size;
  off_t n;
  int toread;
  while (total < size) {
    if (bytesleft > readlen) {
      toread = readlen;
    } else {
      toread = bytesleft;
    }
    if ((n = fread((void *)buf + total, 1, toread, stream)) == -1) {
      printf("dident manage to fread all the data as %s:%d.\n",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/bowlofstew/enterprise-search/refs/"
             "heads/master/src/common/io.c",
             78);
      return 0;
    }
    total += n;
    bytesleft -= n;
  }
  return total;
}


x86 assembly (clang, -O0):
<fread_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d7 <fread_all+0xb7>
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    116b <fread_all+0x4b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1171 <fread_all+0x51>
mov    -0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    11bb <fread_all+0x9b>
lea    0xe63(%rip),%rdi
lea    0xe8b(%rip),%rsi
mov    $0x4e,%edx
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11df <fread_all+0xbf>
movslq -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
jmp    1146 <fread_all+0x26>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     1196 <fread_all+0x76>
mov    %ecx,%r13d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
mov    %r15d,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %r13d,%ebp
mov    %ebp,%edx
cmovg  %r13d,%edx
lea    (%r12,%rbx,1),%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0xffffffff,%eax
je     117e <fread_all+0x5e>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
cmp    %r15,%rbx
jb     1150 <fread_all+0x30>
jmp    1198 <fread_all+0x78>
lea    0xe7b(%rip),%rdi
lea    0xea3(%rip),%rsi
mov    $0x4e,%edx
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c"dident manage to fread all the data as %s:%d.\0A\00"
@.str.1 = [153 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/bowlofstew/enterprise-search/refs/heads/master/src/common/io.c\00"
define i64 @fread_all(i8* %0, i64 %1, i32* %2, i32 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i64 0, i64* %10
%14 = load i64, i64* %7
%15 = trunc i64 %14 to i32
store i32 %15, i32* %11
br label %16
16: ; preds = %38, %4
%17 = load i64, i64* %10
%18 = load i64, i64* %7
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %46
20: ; preds = %16
%21 = load i32, i32* %11
%22 = load i32, i32* %9
%23 = icmp sgt i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i32, i32* %9
store i32 %25, i32* %13
br label %28
26: ; preds = %20
%27 = load i32, i32* %11
store i32 %27, i32* %13
br label %28
28: ; preds = %26, %24
%29 = load i8*, i8** %6
%30 = load i64, i64* %10
%31 = getelementptr i8, i8* %29, i64 %30
%32 = load i32, i32* %13
%33 = load i32*, i32** %8
%34 = call i32 @fread(i8* %31, i32 1, i32 %32, i32* %33)
store i32 %34, i32* %12
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([153 x i8], [153 x i8]* @.str.1, i64 0, i64 0), i32 78)
store i64 0, i64* %5
br label %48
38: ; preds = %28
%39 = load i32, i32* %12
%40 = sext i32 %39 to i64
%41 = load i64, i64* %10
%42 = add i64 %41, %40
store i64 %42, i64* %10
%43 = load i32, i32* %12
%44 = load i32, i32* %11
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %11
br label %16
46: ; preds = %16
%47 = load i64, i64* %10
store i64 %47, i64* %5
br label %48
48: ; preds = %46, %36
%49 = load i64, i64* %5
ret i64 %49
}

LLVM IR (-O2):
@.str = [47 x i8] c"dident manage to fread all the data as %s:%d.\0A\00"
@.str.1 = [153 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/bowlofstew/enterprise-search/refs/heads/master/src/common/io.c\00"
define i64 @fread_all(i8* nocapture %0, i64 %1, i32* nocapture %2, i32 %3) {
%5 = icmp eq i64 %1, 0
br i1 %5, label %23, label %6
6: ; preds = %4
%7 = trunc i64 %1 to i32
br label %8
8: ; preds = %6, %18
%9 = phi i32 [ %21, %18 ], [ %7, %6 ]
%10 = phi i64 [ %20, %18 ], [ 0, %6 ]
%11 = icmp sgt i32 %9, %3
%12 = select i1 %11, i32 %3, i32 %9
%13 = getelementptr i8, i8* %0, i64 %10
%14 = tail call i32 @fread(i8* %13, i32 1, i32 %12, i32* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([153 x i8], [153 x i8]* @.str.1, i64 0, i64 0), i32 78)
br label %23
18: ; preds = %8
%19 = sext i32 %14 to i64
%20 = add i64 %10, %19
%21 = sub nsw i32 %9, %14
%22 = icmp ult i64 %20, %1
br i1 %22, label %8, label %23
23: ; preds = %18, %4, %16
%24 = phi i64 [ 0, %16 ], [ 0, %4 ], [ %20, %18 ]
ret i64 %24
}