#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__ feof(int /*<<< orphan*/ *);
size_t fread(void *, size_t, int, int /*<<< orphan*/ *);
int stdio_read(void *p, size_t length, void *file) {
  size_t r;
  FILE *fp = file;
  r = fread(p, length, 1, fp);
  if (r == 0 && feof(fp))
    return 0;
  return r == 0 ? -1 : 1;
}


x86 assembly (clang, -O0):
<stdio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1180 <stdio_read+0x60>
mov    -0x30(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%rax
je     1180 <stdio_read+0x60>
movl   $0x0,-0x4(%rbp)
jmp    1198 <stdio_read+0x78>
mov    -0x28(%rbp),%rdx
mov    $0x1,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdio_read>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    $0x1,%ebp
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    1151 <stdio_read+0x31>
mov    %rbx,%rdi
call   1040 <feof@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1151 <stdio_read+0x31>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stdio_read(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %7
%11 = bitcast i8* %10 to i32*
store i32* %11, i32** %9
%12 = load i8*, i8** %5
%13 = load i64, i64* %6
%14 = load i32*, i32** %9
%15 = call i64 @fread(i8* %12, i64 %13, i32 1, i32* %14)
store i64 %15, i64* %8
%16 = load i64, i64* %8
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %3
%19 = load i32*, i32** %9
%20 = call i64 @feof(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 0, i32* %4
br label %28
23: ; preds = %18, %3
%24 = load i64, i64* %8
%25 = icmp eq i64 %24, 0
%26 = zext i1 %25 to i64
%27 = select i1 %25, i32 -1, i32 1
store i32 %27, i32* %4
br label %28
28: ; preds = %23, %22
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
define i32 @stdio_read(i8* nocapture %0, i64 %1, i8* nocapture %2) {
%4 = bitcast i8* %2 to i32*
%5 = tail call i64 @fread(i8* %0, i64 %1, i32 1, i32* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i64 @feof(i32* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %3
%11 = phi i32 [ -1, %7 ], [ 1, %3 ]
br label %12
12: ; preds = %7, %10
%13 = phi i32 [ %11, %10 ], [ 0, %7 ]
ret i32 %13
}