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

/* Variables and functions */
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char buffered_fgetc(char *buffer, uint64_t *pos, uint64_t *read, FILE *f) {
  if (*pos >= 50000000) {
    *pos = 0;
    memset(buffer, 0, 50000000);
    *read = fread(buffer, 1, 50000000, f);
  }
  *pos = *pos + 1;
  return buffer[*pos - 1];
}


x86 assembly (clang, -O0):
<buffered_fgetc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x2faf080,(%rax)
jl     1181 <buffered_fgetc+0x61>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x2faf080,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2faf080,%edx
call   1030 <fread@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffered_fgetc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%eax
cmp    $0x2faf080,%eax
jl     116c <buffered_fgetc+0x4c>
mov    %rcx,%r12
mov    %rdx,%r15
movl   $0x0,(%rbx)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x2faf080,%edx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x2faf080,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %eax,(%r15)
mov    (%rbx),%eax
lea    0x1(%rax),%ecx
mov    %ecx,(%rbx)
cltq
mov    (%r14,%rax,1),%al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define signext i8 @buffered_fgetc(i8* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%9 = load i32*, i32** %6
%10 = load i32, i32* %9
%11 = icmp sge i32 %10, 50000000
br i1 %11, label %12, label %20
12: ; preds = %4
%13 = load i32*, i32** %6
store i32 0, i32* %13
%14 = load i8*, i8** %5
%15 = call i32 @memset(i8* %14, i32 0, i32 50000000)
%16 = load i8*, i8** %5
%17 = load i32*, i32** %8
%18 = call i32 @fread(i8* %16, i32 1, i32 50000000, i32* %17)
%19 = load i32*, i32** %7
store i32 %18, i32* %19
br label %20
20: ; preds = %12, %4
%21 = load i32*, i32** %6
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 1
%24 = load i32*, i32** %6
store i32 %23, i32* %24
%25 = load i8*, i8** %5
%26 = load i32*, i32** %6
%27 = load i32, i32* %26
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %25, i64 %29
%31 = load i8, i8* %30
ret i8 %31
}

LLVM IR (-O2):
define signext i8 @buffered_fgetc(i8* %0, i32* nocapture %1, i32* nocapture writeonly %2, i32* nocapture %3) {
%5 = load i32, i32* %1
%6 = icmp sgt i32 %5, 49999999
br i1 %6, label %7, label %11
7: ; preds = %4
store i32 0, i32* %1
%8 = tail call i32 @memset(i8* %0, i32 0, i32 50000000)
%9 = tail call i32 @fread(i8* %0, i32 1, i32 50000000, i32* %3)
store i32 %9, i32* %2
%10 = load i32, i32* %1
br label %11
11: ; preds = %7, %4
%12 = phi i32 [ %10, %7 ], [ %5, %4 ]
%13 = add nsw i32 %12, 1
store i32 %13, i32* %1
%14 = sext i32 %12 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
ret i8 %16
}