#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__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int main() {
  char c = '%';
  FILE *p1 = NULL;
  FILE *p2 = NULL;
  p1 = fopen("test.txt", "r");
  p2 = fopen("testeof.txt", "w+");
  while (1) {
    fread(&c, 1, 1, p1);
    if (feof(p1)) {
      break;
    }
    fwrite(&c, 1, 1, p2);
  }
  fclose(p1);
  fclose(p2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x25,-0x5(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x5(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11cd <main+0x7d>
jmp    11e6 <main+0x96>
mov    -0x18(%rbp),%rcx
lea    -0x5(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
jmp    11a1 <main+0x51>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x25,0xf(%rsp)
lea    0xe9b(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe90(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    11ef <main+0x9f>
lea    0xf(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0xf(%rsp),%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     11c0 <main+0x70>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"testeof.txt\00"
@.str.3 = [3 x i8] c"w+\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32*
%4 = alloca i32*
store i32 0, i32* %1
store i8 37, i8* %2
store i32* null, i32** %3
store i32* null, i32** %4
%5 = 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* %5, i32** %3
%6 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %6, i32** %4
br label %7
7: ; preds = %0, %14
%8 = load i32*, i32** %3
%9 = call i32 @fread(i8* %2, i32 1, i32 1, i32* %8)
%10 = load i32*, i32** %3
%11 = call i64 @feof(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %7
br label %17
14: ; preds = %7
%15 = load i32*, i32** %4
%16 = call i32 @fwrite(i8* %2, i32 1, i32 1, i32* %15)
br label %7
17: ; preds = %13
%18 = load i32*, i32** %3
%19 = call i32 @fclose(i32* %18)
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"testeof.txt\00"
@.str.3 = [3 x i8] c"w+\00"
define i32 @main() {
%1 = alloca i8
store i8 37, i8* %1
%2 = 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))
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%4 = call i32 @fread(i8* %1, i32 1, i32 1, i32* %2)
%5 = tail call i64 @feof(i32* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %0, %7
%8 = load i8, i8* %1
%9 = sext i8 %8 to i32
%10 = tail call i32 @fputc(i32 %9, i32* %3)
%11 = call i32 @fread(i8* %1, i32 1, i32 1, i32* %2)
%12 = tail call i64 @feof(i32* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %7, label %14
14: ; preds = %7, %0
%15 = tail call i32 @fclose(i32* %2)
%16 = tail call i32 @fclose(i32* %3)
ret i32 0
}