#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*/ _IOFBF;
int /*<<< orphan*/ *fopen(char const *, char *);
size_t getpagesize();
char *malloc(size_t);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/,
                           size_t);
FILE *proc_fopen(const char *name) {
  static char *buffer;
  static size_t pagesz;
  FILE *fd = fopen(name, "r");
  if (fd == NULL)
    return NULL;
  if (!buffer) {
    pagesz = getpagesize();
    buffer = malloc(pagesz);
  }
  setvbuf(fd, buffer, _IOFBF, pagesz);
  return fd;
}


x86 assembly (clang, -O0):
<proc_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1178 <proc_fopen+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11cf <proc_fopen+0x8f>
cmpq   $0x0,0x2ec8(%rip)
jne    11a7 <proc_fopen+0x67>
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %rax,0x2ebc(%rip)
mov    0x2eb5(%rip),%rdi
call   1030 <malloc@plt>
mov    %rax,0x2ea1(%rip)
mov    -0x18(%rbp),%rdi
mov    0x2e96(%rip),%rsi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    0x2e8e(%rip),%rcx
call   1040 <setvbuf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_fopen>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a4 <proc_fopen+0x64>
mov    0x2ee9(%rip),%rsi
test   %rsi,%rsi
je     116d <proc_fopen+0x2d>
mov    0x2ee5(%rip),%r14
jmp    1190 <proc_fopen+0x50>
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %rax,%r14
mov    %rax,0x2ed2(%rip)
mov    %rax,%rdi
call   1030 <malloc@plt>
mov    %rax,%rsi
mov    %rax,0x2eb8(%rip)
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rcx
call   1040 <setvbuf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@proc_fopen.buffer = internal global i8* null
@proc_fopen.pagesz = internal global i64 0
@.str = [2 x i8] c"r\00"
@_IOFBF = global i32 0
define i32* @proc_fopen(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i32* null, i32** %2
br label %24
10: ; preds = %1
%11 = load i8*, i8** @proc_fopen.buffer
%12 = icmp ne i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = call i64 (...) @getpagesize()
store i64 %14, i64* @proc_fopen.pagesz
%15 = load i64, i64* @proc_fopen.pagesz
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** @proc_fopen.buffer
br label %17
17: ; preds = %13, %10
%18 = load i32*, i32** %4
%19 = load i8*, i8** @proc_fopen.buffer
%20 = load i32, i32* @_IOFBF
%21 = load i64, i64* @proc_fopen.pagesz
%22 = call i32 @setvbuf(i32* %18, i8* %19, i32 %20, i64 %21)
%23 = load i32*, i32** %4
store i32* %23, i32** %2
br label %24
24: ; preds = %17, %9
%25 = load i32*, i32** %2
ret i32* %25
}

LLVM IR (-O2):
@proc_fopen.buffer = internal unnamed_addr global i8* null
@proc_fopen.pagesz = internal unnamed_addr global i64 0
@.str = [2 x i8] c"r\00"
@_IOFBF = global i32 0
define noalias i32* @proc_fopen(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = load i8*, i8** @proc_fopen.buffer
%6 = icmp eq i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = load i64, i64* @proc_fopen.pagesz
br label %12
9: ; preds = %4
%10 = tail call i64 (...) @getpagesize()
store i64 %10, i64* @proc_fopen.pagesz
%11 = tail call i8* @malloc(i64 %10)
store i8* %11, i8** @proc_fopen.buffer
br label %12
12: ; preds = %7, %9
%13 = phi i64 [ %10, %9 ], [ %8, %7 ]
%14 = phi i8* [ %11, %9 ], [ %5, %7 ]
%15 = load i32, i32* @_IOFBF
%16 = tail call i32 @setvbuf(i32* %2, i8* %14, i32 %15, i64 %13)
br label %17
17: ; preds = %1, %12
ret i32* %2
}