#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
void readChunk(void) {
  char buf[8];
  FILE *inputFile;
  size_t nread;
  inputFile = fopen("test.txt", "r");
  if (inputFile) {
    while ((nread = fread(buf, 1, sizeof buf, inputFile)) > 0) {
      printf("\nNumber of bytes: ");
      printf("%lu", nread);
      printf("\n");
      fwrite(buf, 1, nread, stdout);
    }
    if (ferror(inputFile)) {
    }
    fclose(inputFile);
  }
}


x86 assembly (clang, -O0):
<readChunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1223 <readChunk+0xc3>
jmp    118f <readChunk+0x2f>
lea    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1040 <fread@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jbe    1202 <readChunk+0xa2>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1080 <fwrite@plt>
jmp    118f <readChunk+0x2f>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     121a <readChunk+0xba>
jmp    121a <readChunk+0xba>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readChunk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1231 <readChunk+0xc1>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     1221 <readChunk+0xb1>
mov    %rax,%rbx
lea    0xe4d(%rip),%r14
lea    0xe59(%rip),%r15
mov    0x2e14(%rip),%rbp
mov    %rsp,%r13
nop
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x0(%rbp),%ecx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1090 <fwrite@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <readChunk+0x60>
mov    %r12,%rdi
call   1040 <ferror@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"\0ANumber of bytes: \00"
@.str.3 = [4 x i8] c"%lu\00"
@.str.4 = [2 x i8] c"\0A\00"
@stdout = global i32 0
define void @readChunk() {
%1 = alloca [8 x i8]
%2 = alloca i32*
%3 = alloca i64
%4 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 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** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %30
7: ; preds = %0
br label %8
8: ; preds = %13, %7
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 0
%10 = load i32*, i32** %2
%11 = call i64 @fread(i8* %9, i32 1, i32 8, i32* %10)
store i64 %11, i64* %3
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %8
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%15 = load i64, i64* %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%18 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 0
%19 = load i64, i64* %3
%20 = load i32, i32* @stdout
%21 = call i32 @fwrite(i8* %18, i32 1, i64 %19, i32 %20)
br label %8
22: ; preds = %8
%23 = load i32*, i32** %2
%24 = call i64 @ferror(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
br label %27
27: ; preds = %26, %22
%28 = load i32*, i32** %2
%29 = call i32 @fclose(i32* %28)
br label %30
30: ; preds = %27, %0
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"\0ANumber of bytes: \00"
@.str.3 = [4 x i8] c"%lu\00"
@stdout = global i32 0
define void @readChunk() {
%1 = alloca [8 x i8]
%2 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %20, label %5
5: ; preds = %0
%6 = call i64 @fread(i8* %2, i32 1, i32 8, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %5, %8
%9 = phi i64 [ %15, %8 ], [ %6, %5 ]
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %9)
%12 = call i32 @putchar(i32 10)
%13 = load i32, i32* @stdout
%14 = call i32 @fwrite(i8* %2, i32 1, i64 %9, i32 %13)
%15 = call i64 @fread(i8* %2, i32 1, i32 8, i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %8
17: ; preds = %8, %5
%18 = call i64 @ferror(i32* %3)
%19 = call i32 @fclose(i32* %3)
br label %20
20: ; preds = %17, %0
ret void
}