#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*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ ms_log(int, char *);
int ms_fread(char *buf, int size, int num, FILE *stream) {
  int read = 0;
  read = fread(buf, size, num, stream);
  if (read <= 0 && size && num) {
    if (ferror(stream))
      ms_log(2, "ms_fread(): Cannot read input file\n");
    else if (!feof(stream))
      ms_log(2, "ms_fread(): Unknown return from fread()\n");
  }
  return read;
}


x86 assembly (clang, -O0):
<ms_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1040 <fread@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11e7 <ms_fread+0xa7>
cmpl   $0x0,-0xc(%rbp)
je     11e7 <ms_fread+0xa7>
cmpl   $0x0,-0x10(%rbp)
je     11e7 <ms_fread+0xa7>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11ba <ms_fread+0x7a>
mov    $0x2,%edi
lea    0xe50(%rip),%rsi
call   1060 <ms_log@plt>
jmp    11e2 <ms_fread+0xa2>
mov    -0x18(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
jne    11dd <ms_fread+0x9d>
mov    $0x2,%edi
lea    0xe4c(%rip),%rsi
call   1060 <ms_log@plt>
jmp    11e2 <ms_fread+0xa2>
jmp    11e7 <ms_fread+0xa7>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ms_fread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %edx,%ebx
mov    %esi,%ebp
call   1040 <fread@plt>
mov    %eax,%r14d
test   %ebx,%ebx
je     118f <ms_fread+0x4f>
test   %ebp,%ebp
je     118f <ms_fread+0x4f>
test   %r14d,%r14d
jg     118f <ms_fread+0x4f>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1183 <ms_fread+0x43>
lea    0xe89(%rip),%rsi
mov    $0x2,%edi
call   1060 <ms_log@plt>
jmp    118f <ms_fread+0x4f>
mov    %r15,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     119d <ms_fread+0x5d>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe80(%rip),%rsi
jmp    1177 <ms_fread+0x37>

LLVM IR (-O0):
@.str = [36 x i8] c"ms_fread(): Cannot read input file\0A\00"
@.str.1 = [41 x i8] c"ms_fread(): Unknown return from fread()\0A\00"
define i32 @ms_fread(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = load i32*, i32** %8
%14 = call i32 @fread(i8* %10, i32 %11, i32 %12, i32* %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %37
17: ; preds = %4
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %20
%24 = load i32*, i32** %8
%25 = call i64 @ferror(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @ms_log(i32 2, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %36
29: ; preds = %23
%30 = load i32*, i32** %8
%31 = call i32 @feof(i32* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = call i32 @ms_log(i32 2, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
br label %36
36: ; preds = %35, %27
br label %37
37: ; preds = %36, %20, %17, %4
%38 = load i32, i32* %9
ret i32 %38
}

LLVM IR (-O2):
@.str = [36 x i8] c"ms_fread(): Cannot read input file\0A\00"
@.str.1 = [41 x i8] c"ms_fread(): Unknown return from fread()\0A\00"
define i32 @ms_fread(i8* nocapture %0, i32 %1, i32 %2, i32* nocapture %3) {
%5 = tail call i32 @fread(i8* %0, i32 %1, i32 %2, i32* %3)
%6 = icmp slt i32 %5, 1
%7 = icmp ne i32 %1, 0
%8 = and i1 %7, %6
%9 = icmp ne i32 %2, 0
%10 = and i1 %9, %8
br i1 %10, label %11, label %20
11: ; preds = %4
%12 = tail call i64 @ferror(i32* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @feof(i32* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14, %11
%18 = phi i8* [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), %14 ]
%19 = tail call i32 @ms_log(i32 2, i8* %18)
br label %20
20: ; preds = %17, %14, %4
ret i32 %5
}