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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(void *, size_t, size_t, int /*<<< orphan*/ *);
size_t fread_wrapper(void *__restrict __ptr, size_t __size, size_t __n,
                     FILE *__restrict __s) {
  size_t res = 0;
  while (1) {
    res = fread(__ptr, __size, __n, __s);
    if (res == __n) {
      return res;
    } else if (ferror(__s) && errno == EINTR) {
      errno = 0;
      clearerr(__s);
    } else {
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<fread_wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1184 <fread_wrapper+0x54>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <fread_wrapper+0xb4>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11cd <fread_wrapper+0x9d>
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jne    11cd <fread_wrapper+0x9d>
mov    0x2e28(%rip),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <clearerr@plt>
jmp    11da <fread_wrapper+0xaa>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <fread_wrapper+0xb4>
jmp    11df <fread_wrapper+0xaf>
jmp    1150 <fread_wrapper+0x20>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_wrapper>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <fread@plt>
cmp    %r14,%rax
je     11a2 <fread_wrapper+0x72>
mov    0x2e88(%rip),%r13
mov    0x2e79(%rip),%rbp
nop
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     119f <fread_wrapper+0x6f>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
jne    119f <fread_wrapper+0x6f>
movq   $0x0,0x0(%r13)
mov    %rbx,%rdi
call   1050 <clearerr@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r14,%rax
jne    1160 <fread_wrapper+0x30>
jmp    11a2 <fread_wrapper+0x72>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i64 @fread_wrapper(i8* noalias %0, i64 %1, i64 %2, i32* noalias %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i64 0, i64* %10
br label %11
11: ; preds = %4, %35
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = load i64, i64* %8
%15 = load i32*, i32** %9
%16 = call i64 @fread(i8* %12, i64 %13, i64 %14, i32* %15)
store i64 %16, i64* %10
%17 = load i64, i64* %10
%18 = load i64, i64* %8
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = load i64, i64* %10
store i64 %21, i64* %5
br label %36
22: ; preds = %11
%23 = load i32*, i32** %9
%24 = call i64 @ferror(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
store i64 0, i64* @errno
%31 = load i32*, i32** %9
%32 = call i32 @clearerr(i32* %31)
br label %34
33: ; preds = %26, %22
store i64 0, i64* %5
br label %36
34: ; preds = %30
br label %35
35: ; preds = %34
br label %11
36: ; preds = %33, %20
%37 = load i64, i64* %5
ret i64 %37
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @fread_wrapper(i8* noalias nocapture %0, i64 %1, i64 %2, i32* noalias nocapture %3) {
%5 = tail call i64 @fread(i8* %0, i64 %1, i64 %2, i32* %3)
%6 = icmp eq i64 %5, %2
br i1 %6, label %18, label %7
7: ; preds = %4, %14
%8 = tail call i64 @ferror(i32* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %10
store i64 0, i64* @errno
%15 = tail call i32 @clearerr(i32* %3)
%16 = tail call i64 @fread(i8* %0, i64 %1, i64 %2, i32* %3)
%17 = icmp eq i64 %16, %2
br i1 %17, label %18, label %7
18: ; preds = %14, %10, %7, %4
%19 = phi i64 [ %2, %4 ], [ 0, %7 ], [ 0, %10 ], [ %2, %14 ]
ret i64 %19
}