#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*/ fpos_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fgetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fsetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int myfread(void *buffer, size_t size, size_t count, FILE *stream) {
  size_t i;
  i = fread(buffer, size, count, stream);
  if (count == 1 && size == 1 && i == 1) {
    char ch = *(char *)buffer;
    fpos_t pos;
    fgetpos(stream, &pos);
    while (ch == '\0' && fread(&ch, 1, 1, stream) == 1)
      ;
    fsetpos(stream, &pos);
    if (ch == '\0')
      return 0;
  }
  return i;
}


x86 assembly (clang, -O0):
<myfread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
call   1030 <fread@plt>
cltq
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x20(%rbp)
jne    120d <myfread+0xdd>
cmpq   $0x1,-0x18(%rbp)
jne    120d <myfread+0xdd>
cmpq   $0x1,-0x30(%rbp)
jne    120d <myfread+0xdd>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x31(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1040 <fgetpos@plt>
movsbl -0x31(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x39(%rbp)
jne    11cd <myfread+0x9d>
mov    -0x28(%rbp),%rcx
lea    -0x31(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11dd <myfread+0xad>
jmp    11e2 <myfread+0xb2>
jmp    119e <myfread+0x6e>
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <fsetpos@plt>
movsbl -0x31(%rbp),%eax
cmp    $0x0,%eax
jne    1208 <myfread+0xd8>
movl   $0x0,-0x4(%rbp)
jmp    1214 <myfread+0xe4>
jmp    120d <myfread+0xdd>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myfread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r15
call   1030 <fread@plt>
mov    %eax,%r14d
cmp    $0x1,%rbx
jne    11c7 <myfread+0x97>
cmp    $0x1,%r12
jne    11c7 <myfread+0x97>
cmp    $0x1,%r14d
jne    11c7 <myfread+0x97>
mov    (%r15),%bl
mov    %bl,0xb(%rsp)
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
call   1040 <fgetpos@plt>
test   %bl,%bl
je     1189 <myfread+0x59>
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <fsetpos@plt>
jmp    11c7 <myfread+0x97>
lea    0xb(%rsp),%r15
xchg   %ax,%ax
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
movzbl 0xb(%rsp),%ebx
cmp    $0x1,%eax
jne    11b3 <myfread+0x83>
test   %bl,%bl
je     1190 <myfread+0x60>
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <fsetpos@plt>
test   %bl,%bl
jne    11c7 <myfread+0x97>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @myfread(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i8
%12 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%13 = load i8*, i8** %6
%14 = load i64, i64* %7
%15 = trunc i64 %14 to i32
%16 = load i64, i64* %8
%17 = trunc i64 %16 to i32
%18 = load i32*, i32** %9
%19 = call i32 @fread(i8* %13, i32 %15, i32 %17, i32* %18)
%20 = sext i32 %19 to i64
store i64 %20, i64* %10
%21 = load i64, i64* %8
%22 = icmp eq i64 %21, 1
br i1 %22, label %23, label %53
23: ; preds = %4
%24 = load i64, i64* %7
%25 = icmp eq i64 %24, 1
br i1 %25, label %26, label %53
26: ; preds = %23
%27 = load i64, i64* %10
%28 = icmp eq i64 %27, 1
br i1 %28, label %29, label %53
29: ; preds = %26
%30 = load i8*, i8** %6
%31 = load i8, i8* %30
store i8 %31, i8* %11
%32 = load i32*, i32** %9
%33 = call i32 @fgetpos(i32* %32, i32* %12)
br label %34
34: ; preds = %44, %29
%35 = load i8, i8* %11
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32*, i32** %9
%40 = call i32 @fread(i8* %11, i32 1, i32 1, i32* %39)
%41 = icmp eq i32 %40, 1
br label %42
42: ; preds = %38, %34
%43 = phi i1 [ false, %34 ], [ %41, %38 ]
br i1 %43, label %44, label %45
44: ; preds = %42
br label %34
45: ; preds = %42
%46 = load i32*, i32** %9
%47 = call i32 @fsetpos(i32* %46, i32* %12)
%48 = load i8, i8* %11
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45
store i32 0, i32* %5
br label %56
52: ; preds = %45
br label %53
53: ; preds = %52, %26, %23, %4
%54 = load i64, i64* %10
%55 = trunc i64 %54 to i32
store i32 %55, i32* %5
br label %56
56: ; preds = %53, %51
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
define i32 @myfread(i8* nocapture %0, i64 %1, i64 %2, i32* nocapture %3) {
%5 = alloca i8
%6 = alloca i32
%7 = trunc i64 %1 to i32
%8 = trunc i64 %2 to i32
%9 = tail call i32 @fread(i8* %0, i32 %7, i32 %8, i32* %3)
%10 = icmp eq i64 %2, 1
%11 = icmp eq i64 %1, 1
%12 = and i1 %11, %10
%13 = icmp eq i32 %9, 1
%14 = and i1 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %4
%16 = load i8, i8* %0
store i8 %16, i8* %5
%17 = bitcast i32* %6 to i8*
%18 = call i32 @fgetpos(i32* %3, i32* %6)
%19 = icmp eq i8 %16, 0
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = call i32 @fsetpos(i32* %3, i32* %6)
br label %30
22: ; preds = %15, %22
%23 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %3)
%24 = icmp eq i32 %23, 1
%25 = load i8, i8* %5
%26 = icmp eq i8 %25, 0
%27 = select i1 %24, i1 %26, i1 false
br i1 %27, label %22, label %28
28: ; preds = %22
%29 = call i32 @fsetpos(i32* %3, i32* %6)
br i1 %26, label %31, label %30
30: ; preds = %20, %28, %4
br label %31
31: ; preds = %28, %30
%32 = phi i32 [ %9, %30 ], [ 0, %28 ]
ret i32 %32
}