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

/* Variables and functions */
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
void test_fopen_file_reading() {
  FILE *f = fopen("/etc/resolv.conf", "r");
  if (!f) {
    errx(1, "Couldn't open /etc/resolv.conf (assumed to exist for testing)");
  }
  char buf[100];
  do {
    size_t size = fread(buf, sizeof(buf), 1, f);
    if (ferror(f)) {
      puts("Error reading stream: BROKEN");
    }
  } while (!feof(f));
  fclose(f);
}


x86 assembly (clang, -O0):
<test_fopen_file_reading>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11ae <test_fopen_file_reading+0x3e>
mov    $0x1,%edi
lea    0xe6a(%rip),%rsi
call   1070 <errx@plt>
jmp    11b3 <test_fopen_file_reading+0x43>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x64,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
mov    %rax,-0x78(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     11ed <test_fopen_file_reading+0x7d>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
jmp    11f2 <test_fopen_file_reading+0x82>
mov    -0x8(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b3 <test_fopen_file_reading+0x43>
mov    -0x8(%rbp),%rdi
call   1060 <fclose@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fopen_file_reading>:
push   %r15
push   %r14
push   %rbx
sub    $0x70,%rsp
lea    0xe80(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a5 <test_fopen_file_reading+0x35>
lea    0xe78(%rip),%rsi
mov    $0x1,%edi
call   1070 <errx@plt>
mov    %rsp,%r14
lea    0xea2(%rip),%r15
jmp    11cc <test_fopen_file_reading+0x5c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    11f8 <test_fopen_file_reading+0x88>
mov    %r14,%rdi
mov    $0x64,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11c0 <test_fopen_file_reading+0x50>
mov    %r15,%rdi
call   1030 <puts@plt>
jmp    11c0 <test_fopen_file_reading+0x50>
mov    %rbx,%rdi
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <fclose@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [62 x i8] c"Couldn't open /etc/resolv.conf (assumed to exist for testing)\00"
@.str.3 = [29 x i8] c"Error reading stream: BROKEN\00"
define void @test_fopen_file_reading() {
%1 = alloca i32*
%2 = alloca [100 x i8]
%3 = alloca i64
%4 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = icmp ne i32* %5, null
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = call i32 @errx(i32 1, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
br label %10
10: ; preds = %20, %9
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%12 = load i32*, i32** %1
%13 = call i64 @fread(i8* %11, i32 100, i32 1, i32* %12)
store i64 %13, i64* %3
%14 = load i32*, i32** %1
%15 = call i64 @ferror(i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %10
br label %20
20: ; preds = %19
%21 = load i32*, i32** %1
%22 = call i32 @feof(i32* %21)
%23 = icmp ne i32 %22, 0
%24 = xor i1 %23, true
br i1 %24, label %10, label %25
25: ; preds = %20
%26 = load i32*, i32** %1
%27 = call i32 @fclose(i32* %26)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [62 x i8] c"Couldn't open /etc/resolv.conf (assumed to exist for testing)\00"
@.str.3 = [29 x i8] c"Error reading stream: BROKEN\00"
define void @test_fopen_file_reading() {
%1 = alloca [100 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
br label %8
8: ; preds = %14, %6
%9 = call i64 @fread(i8* %7, i32 100, i32 1, i32* %2)
%10 = tail call i64 @ferror(i32* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = tail call i32 @feof(i32* %2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %8, label %17
17: ; preds = %14
%18 = tail call i32 @fclose(i32* %2)
ret void
}