#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int main(void) {
  char c = '%';
  FILE *p0 = NULL;
  FILE *p1 = NULL;
  p0 = fopen("while_fread0", "r");
  p1 = fopen("while_fread1", "w+");
  while (fread(&c, 1, 1, p0)) {
    if (c == '\n') {
      fwrite("\n", 1, 1, p1);
      continue;
    }
    fwrite(&c, 1, 1, p1);
  }
  fclose(p0);
  fclose(p1);
  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    0xe96(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe8e(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x5(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x0,%rax
je     11f1 <main+0xb1>
movsbl -0x5(%rbp),%eax
cmp    $0xa,%eax
jne    11d8 <main+0x98>
mov    -0x18(%rbp),%rcx
lea    0xe58(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
jmp    1191 <main+0x51>
mov    -0x18(%rbp),%rcx
lea    -0x5(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
jmp    1191 <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    0xeab(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
lea    0xea4(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11c7 <main+0x87>
lea    0xf(%rsp),%r15
nopl   0x0(%rax,%rax,1)
movsbl 0xf(%rsp),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11a0 <main+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"while_fread0\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"while_fread1\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [2 x i8] c"\0A\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 ([13 x i8], [13 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 ([13 x i8], [13 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 = %18, %15, %0
%8 = load i32*, i32** %3
%9 = call i64 @fread(i8* %2, i32 1, i32 1, i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %7
%12 = load i8, i8* %2
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 10
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32*, i32** %4
%17 = call i32 @fwrite(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 1, i32* %16)
br label %7
18: ; preds = %11
%19 = load i32*, i32** %4
%20 = call i32 @fwrite(i8* %2, i32 1, i32 1, i32* %19)
br label %7
21: ; preds = %7
%22 = load i32*, i32** %3
%23 = call i32 @fclose(i32* %22)
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"while_fread0\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"while_fread1\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 ([13 x i8], [13 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 ([13 x i8], [13 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 i64 @fread(i8* %1, i32 1, i32 1, i32* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %0, %6
%7 = load i8, i8* %1
%8 = sext i8 %7 to i32
%9 = tail call i32 @fputc(i32 %8, i32* %3)
%10 = call i64 @fread(i8* %1, i32 1, i32 1, i32* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %6
12: ; preds = %6, %0
%13 = tail call i32 @fclose(i32* %2)
%14 = tail call i32 @fclose(i32* %3)
ret i32 0
}