#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 /*<<< orphan*/ error(char *, long, char *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
char *ftell(int /*<<< orphan*/ *);
size_t my_fread(void *ptr, long bytes, FILE *f) {
  size_t result;
  if (bytes == 0)
    return 0;
  if (bytes < 0 || bytes != (size_t)bytes ||
      (result = fread(ptr, 1, (size_t)bytes, f)) != bytes) {
    error("my_fread(%ld) at %ld failed", bytes, ftell(f));
  }
  return result;
}


x86 assembly (clang, -O0):
<my_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115c <my_fread+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <my_fread+0x95>
cmpq   $0x0,-0x18(%rbp)
jl     1199 <my_fread+0x69>
mov    -0x18(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1199 <my_fread+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmp    -0x18(%rbp),%rax
je     11bd <my_fread+0x8d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <ftell@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe48(%rip),%rdi
call   1050 <error@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fread>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     115c <my_fread+0x2c>
mov    %rdx,%r14
mov    %rsi,%rbx
js     1160 <my_fread+0x30>
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %rax,%r15
cmp    %rbx,%rax
jne    1160 <my_fread+0x30>
jmp    117d <my_fread+0x4d>
xor    %ebx,%ebx
jmp    117d <my_fread+0x4d>
mov    %r14,%rdi
call   1040 <ftell@plt>
lea    0xe91(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <error@plt>
mov    %r15,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [28 x i8] c"my_fread(%ld) at %ld failed\00"
define i64 @my_fread(i8* %0, i64 %1, i32* %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
%9 = load i64, i64* %6
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
store i64 0, i64* %4
br label %33
12: ; preds = %3
%13 = load i64, i64* %6
%14 = icmp slt i64 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = load i64, i64* %6
%17 = load i64, i64* %6
%18 = icmp ne i64 %16, %17
br i1 %18, label %26, label %19
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%22 = load i32*, i32** %7
%23 = call i64 @fread(i8* %20, i32 1, i64 %21, i32* %22)
store i64 %23, i64* %8
%24 = load i64, i64* %6
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %19, %15, %12
%27 = load i64, i64* %6
%28 = load i32*, i32** %7
%29 = call i8* @ftell(i32* %28)
%30 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %27, i8* %29)
br label %31
31: ; preds = %26, %19
%32 = load i64, i64* %8
store i64 %32, i64* %4
br label %33
33: ; preds = %31, %11
%34 = load i64, i64* %4
ret i64 %34
}

LLVM IR (-O2):
@.str = [28 x i8] c"my_fread(%ld) at %ld failed\00"
define i64 @my_fread(i8* nocapture %0, i64 %1, i32* nocapture %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = icmp slt i64 %1, 0
br i1 %6, label %10, label %7
7: ; preds = %5
%8 = tail call i64 @fread(i8* %0, i32 1, i64 %1, i32* %2)
%9 = icmp eq i64 %8, %1
br i1 %9, label %14, label %10
10: ; preds = %7, %5
%11 = phi i64 [ undef, %5 ], [ %8, %7 ]
%12 = tail call i8* @ftell(i32* %2)
%13 = tail call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %1, i8* %12)
br label %14
14: ; preds = %7, %10, %3
%15 = phi i64 [ 0, %3 ], [ %11, %10 ], [ %1, %7 ]
ret i64 %15
}