#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 EOF;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ isascii(int);
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/ *);
FILE *fopen_read(const char *path) {
  FILE *file = fopen(path, "rt");
  int chr;
  if (file) {
    while (!isascii(chr = fgetc(file)) && chr != EOF)
      ;
    ungetc(chr, file);
  }
  return file;
}


x86 assembly (clang, -O0):
<fopen_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c6 <fopen_read+0x86>
jmp    1170 <fopen_read+0x30>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,%edi
mov    %edi,-0x14(%rbp)
call   1060 <isascii@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x15(%rbp)
jne    11a5 <fopen_read+0x65>
mov    -0x14(%rbp),%eax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11b5 <fopen_read+0x75>
jmp    11ba <fopen_read+0x7a>
jmp    1170 <fopen_read+0x30>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <ungetc@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_read>:
push   %r14
push   %rbx
push   %rax
lea    0xec5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     116e <fopen_read+0x3e>
mov    0x2e99(%rip),%r14
nop
mov    %rbx,%rdi
call   1030 <fgetc@plt>
cmp    $0x80,%eax
jb     1164 <fopen_read+0x34>
cmp    (%r14),%eax
jne    1150 <fopen_read+0x20>
mov    %eax,%edi
mov    %rbx,%rsi
call   1040 <ungetc@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rt\00"
@EOF = global i32 0
define i32* @fopen_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %26
9: ; preds = %1
br label %10
10: ; preds = %21, %9
%11 = load i32*, i32** %3
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %4
%13 = call i32 @isascii(i32 %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = load i32, i32* %4
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br label %19
19: ; preds = %15, %10
%20 = phi i1 [ false, %10 ], [ %18, %15 ]
br i1 %20, label %21, label %22
21: ; preds = %19
br label %10
22: ; preds = %19
%23 = load i32, i32* %4
%24 = load i32*, i32** %3
%25 = call i32 @ungetc(i32 %23, i32* %24)
br label %26
26: ; preds = %22, %1
%27 = load i32*, i32** %3
ret i32* %27
}

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
@EOF = global i32 0
define noalias i32* @fopen_read(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %12, label %4
4: ; preds = %1, %4
%5 = tail call i32 @fgetc(i32* %2)
%6 = icmp ugt i32 %5, 127
%7 = load i32, i32* @EOF
%8 = icmp ne i32 %5, %7
%9 = select i1 %6, i1 %8, i1 false
br i1 %9, label %4, label %10
10: ; preds = %4
%11 = tail call i32 @ungetc(i32 %5, i32* %2)
br label %12
12: ; preds = %10, %1
ret i32* %2
}