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

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
void cat(FILE *fp) {
  char buffer[4096];
  rewind(fp);
  while (fgets(buffer, sizeof(buffer), fp) != NULL) {
    printf("reading");
    fflush(stdout);
  }
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <rewind@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1196 <cat+0x56>
lea    0xe82(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1158 <cat+0x18>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
call   1040 <rewind@plt>
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11a6 <cat+0x66>
lea    0xe8e(%rip),%r14
mov    0x2e67(%rip),%r12
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <printf@plt>
mov    (%r12),%edi
call   1060 <fflush@plt>
mov    %r15,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1180 <cat+0x40>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"reading\00"
@stdout = global i32 0
define void @cat(i32* %0) {
%2 = alloca i32*
%3 = alloca [4096 x i8]
store i32* %0, i32** %2
%4 = load i32*, i32** %2
%5 = call i32 @rewind(i32* %4)
br label %6
6: ; preds = %11, %1
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%8 = load i32*, i32** %2
%9 = call i32* @fgets(i8* %7, i32 4096, i32* %8)
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stdout
%14 = call i32 @fflush(i32 %13)
br label %6
15: ; preds = %6
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"reading\00"
@stdout = global i32 0
define void @cat(i32* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @rewind(i32* %0)
%5 = call i32* @fgets(i8* %3, i32 4096, i32* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %13, label %7
7: ; preds = %1, %7
%8 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @stdout
%10 = call i32 @fflush(i32 %9)
%11 = call i32* @fgets(i8* %3, i32 4096, i32* %0)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %7
13: ; preds = %7, %1
ret void
}