#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 */
int errno;
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int full_fread(void *, size_t, size_t, int /*<<< orphan*/ *);
int handle_gez(int);
int /*<<< orphan*/ printf(char *, ...);
int fread_test(void *ptr, size_t size, size_t nitems, FILE *stream) {
  int ret, save_errno;
  int passed;
  printf("fread(): ptr=0x%p, size=%ld, nitmes=%ld, stream=0x%p\n", ptr, size,
         nitems, stream);
  fflush(NULL);
  passed = handle_gez(ret = full_fread(ptr, size, nitems, stream));
  save_errno = errno;
  switch (passed) {
  case -1:
    printf("\tFailed Phase 1: returned undefined value! "
           "ret = %d\n",
           ret);
    fflush(NULL);
    break;
  case 0:
    if ((ret == 0) && ferror(stream)) {
      printf("\t\tHit end of file.\n");
      fflush(NULL);
    }
    printf("\t\tRead %d blocks\n", ret);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed Phase 1: returned undefined value! "
           "ret = %d\n",
           ret);
    fflush(NULL);
    break;
  default:
    printf("Internal syscalltester error: passed = %d\n", passed);
    fflush(NULL);
    break;
  }
  errno = save_errno;
  return ret;
}


x86 assembly (clang, -O0):
<fread_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe81(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <full_fread@plt>
mov    %eax,%edi
mov    %edi,-0x24(%rbp)
call   1070 <handle_gez@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0xfffffffe,%eax
je     1275 <fread_test+0x125>
jmp    11d5 <fread_test+0x85>
mov    -0x30(%rbp),%eax
sub    $0xffffffff,%eax
je     11f6 <fread_test+0xa6>
jmp    11e6 <fread_test+0x96>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     1215 <fread_test+0xc5>
jmp    1294 <fread_test+0x144>
mov    -0x24(%rbp),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    12ae <fread_test+0x15e>
cmpl   $0x0,-0x24(%rbp)
jne    1248 <fread_test+0xf8>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
je     1248 <fread_test+0xf8>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x24(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    12ae <fread_test+0x15e>
mov    -0x24(%rbp),%esi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    12ae <fread_test+0x15e>
mov    -0x2c(%rbp),%esi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x28(%rbp),%ecx
mov    0x2d28(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe86(%rip),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <full_fread@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <handle_gez@plt>
mov    0x2e2b(%rip),%r15
mov    (%r15),%ebx
cmp    $0xfffffffe,%eax
je     11c6 <fread_test+0x66>
test   %eax,%eax
je     11d1 <fread_test+0x71>
cmp    $0xffffffff,%eax
jne    1212 <fread_test+0xb2>
lea    0xe69(%rip),%rdi
mov    %ebp,%esi
jmp    121b <fread_test+0xbb>
test   %ebp,%ebp
jne    11f4 <fread_test+0x94>
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %eax,%eax
je     11f4 <fread_test+0x94>
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
lea    0xe70(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
jmp    1222 <fread_test+0xc2>
lea    0xe64(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebx,(%r15)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [54 x i8] c"fread(): ptr=0x%p, size=%ld, nitmes=%ld, stream=0x%p\0A\00"
@errno = global i32 0
@.str.1 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.2 = [20 x i8] c"\09\09Hit end of file.\0A\00"
@.str.3 = [18 x i8] c"\09\09Read %d blocks\0A\00"
@.str.4 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32 @fread_test(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%12 = load i8*, i8** %5
%13 = load i64, i64* %6
%14 = load i64, i64* %7
%15 = load i32*, i32** %8
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %12, i64 %13, i64 %14, i32* %15)
%17 = call i32 @fflush(i32* null)
%18 = load i8*, i8** %5
%19 = load i64, i64* %6
%20 = load i64, i64* %7
%21 = load i32*, i32** %8
%22 = call i32 @full_fread(i8* %18, i64 %19, i64 %20, i32* %21)
store i32 %22, i32* %9
%23 = call i32 @handle_gez(i32 %22)
store i32 %23, i32* %11
%24 = load i32, i32* @errno
store i32 %24, i32* %10
%25 = load i32, i32* %11
switch i32 %25, label %49 [
i32 -1, label %26
i32 0, label %30
i32 -2, label %45
]
26: ; preds = %4
%27 = load i32, i32* %9
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 @fflush(i32* null)
br label %53
30: ; preds = %4
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i32*, i32** %8
%35 = call i32 @ferror(i32* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @fflush(i32* null)
br label %40
40: ; preds = %37, %33, %30
%41 = load i32, i32* %9
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @fflush(i32* null)
br label %53
45: ; preds = %4
%46 = load i32, i32* %9
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %46)
%48 = call i32 @fflush(i32* null)
br label %53
49: ; preds = %4
%50 = load i32, i32* %11
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = call i32 @fflush(i32* null)
br label %53
53: ; preds = %49, %45, %40, %26
%54 = load i32, i32* %10
store i32 %54, i32* @errno
%55 = load i32, i32* %9
ret i32 %55
}

LLVM IR (-O2):
@.str = [54 x i8] c"fread(): ptr=0x%p, size=%ld, nitmes=%ld, stream=0x%p\0A\00"
@errno = global i32 0
@.str.1 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.3 = [18 x i8] c"\09\09Read %d blocks\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
@str = [19 x i8] c"\09Succeeded Phase 1\00"
@str.6 = [19 x i8] c"\09\09Hit end of file.\00"
define i32 @fread_test(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %1, i64 %2, i32* %3)
%6 = tail call i32 @fflush(i32* null)
%7 = tail call i32 @full_fread(i8* %0, i64 %1, i64 %2, i32* %3)
%8 = tail call i32 @handle_gez(i32 %7)
%9 = load i32, i32* @errno
switch i32 %8, label %25 [
i32 -1, label %10
i32 0, label %12
i32 -2, label %23
]
10: ; preds = %4
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %27
12: ; preds = %4
%13 = icmp eq i32 %7, 0
br i1 %13, label %14, label %20
14: ; preds = %12
%15 = tail call i32 @ferror(i32* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%19 = tail call i32 @fflush(i32* null)
br label %20
20: ; preds = %17, %14, %12
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %27
23: ; preds = %4
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %27
25: ; preds = %4
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %8)
br label %27
27: ; preds = %25, %23, %20, %10
%28 = tail call i32 @fflush(i32* null)
store i32 %9, i32* @errno
ret i32 %7
}